Renamed stdin -> from-file and removed apply from tests + docs
This commit is contained in:
parent
f7675c829e
commit
c8adf9b726
@ -867,11 +867,12 @@ Return an iterator of `x`, `fn(x)`, `fn(fn(x))`.
|
||||
read
|
||||
----
|
||||
|
||||
Usage: ``(read [stdin eof])``
|
||||
Usage: ``(read [from-file eof])``
|
||||
|
||||
Reads the given form and parses it to hy. Takes optional argument
|
||||
for a different stdin object or eof byte. Throws `EOFError` when
|
||||
stdin is empty.
|
||||
Reads the next hy expression from `from-file` (defaults to `sys.stdin`), and
|
||||
can take a single byte as EOF (defaults to an empty string).
|
||||
Raises an `EOFError` if `from-file` ends before a complete expression can be
|
||||
parsed.
|
||||
|
||||
.. code-block:: hy
|
||||
=> (read)
|
||||
@ -882,9 +883,9 @@ stdin is empty.
|
||||
4
|
||||
=> (import io)
|
||||
=> (def buffer (io.StringIO "(+ 2 2)\n(- 2 1)"))
|
||||
=> (eval (apply read [] {"stdin" buffer}))
|
||||
=> (eval (apply read [] {"from_file" buffer}))
|
||||
4
|
||||
=> (eval (apply read [] {"stdin" buffer}))
|
||||
=> (eval (apply read [] {"from_file" buffer}))
|
||||
1
|
||||
|
||||
|
||||
|
@ -328,13 +328,13 @@
|
||||
(_numeric_check n)
|
||||
(= n 0))
|
||||
|
||||
(defn read [&optional [stdin sys.stdin]
|
||||
(defn read [&optional [from-file sys.stdin]
|
||||
[eof ""]]
|
||||
"Read from input and returns a tokenized string.
|
||||
Can take a given input buffer to read from"
|
||||
(def buff "")
|
||||
(while true
|
||||
(def inn (str (.read stdin 1)))
|
||||
(def inn (str (.read from-file 1)))
|
||||
(if (= inn eof)
|
||||
(throw (EOFError "Reached end of file" )))
|
||||
(setv buff (+ buff inn))
|
||||
|
@ -1037,21 +1037,20 @@
|
||||
(import [hy.models.expression [HyExpression]])
|
||||
|
||||
(def stdin-buffer (StringIO "(+ 2 2)\n(- 2 2)"))
|
||||
(assert (= (eval (apply read [] {"stdin" stdin-buffer})) 4))
|
||||
(assert (isinstance (apply read [] {"stdin" stdin-buffer}) HyExpression))
|
||||
(assert (= (eval (read stdin-buffer)) 4))
|
||||
(assert (isinstance (read stdin-buffer) HyExpression))
|
||||
|
||||
"Multiline test"
|
||||
(def stdin-buffer (StringIO "(\n+\n41\n1\n)\n(-\n2\n1\n)"))
|
||||
(assert (= (eval (apply read [] {"stdin" stdin-buffer})) 42))
|
||||
(assert (= (eval (apply read [] {"stdin" stdin-buffer})) 1))
|
||||
(assert (= (eval (read stdin-buffer)) 42))
|
||||
(assert (= (eval (read stdin-buffer)) 1))
|
||||
|
||||
"EOF test"
|
||||
(def stdin-buffer (StringIO "(+ 2 2)"))
|
||||
(apply read [] {"stdin" stdin-buffer})
|
||||
(read stdin-buffer)
|
||||
(try
|
||||
(apply read [] {"stdin" stdin-buffer})
|
||||
(read stdin-buffer)
|
||||
(catch [e Exception]
|
||||
(print e)
|
||||
(assert (isinstance e EOFError)))))
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user