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