Remove if-python2
and its uses
This commit is contained in:
parent
7ba1407257
commit
6af6a2945a
@ -19,9 +19,7 @@
|
||||
`(do
|
||||
(import cProfile pstats)
|
||||
|
||||
(if-python2
|
||||
(import [StringIO [StringIO]])
|
||||
(import [io [StringIO]]))
|
||||
(import [io [StringIO]])
|
||||
|
||||
(setv ~g!hy-pr (.Profile cProfile))
|
||||
(.enable ~g!hy-pr)
|
||||
|
@ -77,10 +77,3 @@
|
||||
(if (not (isinstance lambda-list hy.HyList))
|
||||
(macro-error name "defn/a takes a parameter list as second argument"))
|
||||
`(setv ~name (fn/a ~lambda-list ~@body)))
|
||||
|
||||
(defmacro if-python2 [python2-form python3-form]
|
||||
"If running on python2, execute python2-form, else, execute python3-form"
|
||||
(import sys)
|
||||
(if (< (get sys.version_info 0) 3)
|
||||
python2-form
|
||||
python3-form))
|
||||
|
@ -11,6 +11,7 @@
|
||||
(import [fractions [Fraction :as fraction]])
|
||||
(import operator) ; shadow not available yet
|
||||
(import sys)
|
||||
(import [collections.abc :as cabc])
|
||||
(import [hy.models [HySymbol HyKeyword]])
|
||||
(import [hy.lex [tokenize mangle unmangle read read-str]])
|
||||
(import [hy.lex.exceptions [LexException PrematureEndOfInput]])
|
||||
@ -20,10 +21,6 @@
|
||||
|
||||
(require [hy.core.bootstrap [*]])
|
||||
|
||||
(if-python2
|
||||
(import [collections :as cabc])
|
||||
(import [collections.abc :as cabc]))
|
||||
|
||||
(defn butlast [coll]
|
||||
"Return an iterator of all but the last item in `coll`."
|
||||
(drop-last 1 coll))
|
||||
@ -85,47 +82,12 @@ If the second argument `codegen` is true, generate python code instead."
|
||||
(yield val)
|
||||
(.add seen val)))))
|
||||
|
||||
(if-python2
|
||||
(setv
|
||||
remove itertools.ifilterfalse
|
||||
zip-longest itertools.izip_longest
|
||||
;; not builtin in Python3
|
||||
reduce reduce
|
||||
;; hy is more like Python3
|
||||
filter itertools.ifilter
|
||||
input raw_input
|
||||
map itertools.imap
|
||||
range xrange
|
||||
zip itertools.izip)
|
||||
(setv
|
||||
remove itertools.filterfalse
|
||||
zip-longest itertools.zip_longest
|
||||
;; was builtin in Python2
|
||||
reduce functools.reduce
|
||||
;; Someone can import these directly from `hy.core.language`;
|
||||
;; we'll make some duplicates.
|
||||
filter filter
|
||||
input input
|
||||
map map
|
||||
range range
|
||||
zip zip))
|
||||
|
||||
(if-python2
|
||||
(defn exec [$code &optional $globals $locals]
|
||||
"Execute Python code.
|
||||
|
||||
The parameter names contain weird characters to discourage calling this
|
||||
function with keyword arguments, which isn't supported by Python 3's `exec`."
|
||||
(if
|
||||
(none? $globals) (do
|
||||
(setv frame (._getframe sys (int 1)))
|
||||
(try
|
||||
(setv $globals frame.f_globals $locals frame.f_locals)
|
||||
(finally (del frame))))
|
||||
(none? $locals)
|
||||
(setv $locals $globals))
|
||||
(exec* $code $globals $locals))
|
||||
(setv exec exec))
|
||||
accumulate itertools.accumulate)
|
||||
|
||||
;; infinite iterators
|
||||
(setv
|
||||
@ -151,18 +113,6 @@ function with keyword arguments, which isn't supported by Python 3's `exec`."
|
||||
permutations itertools.permutations
|
||||
product itertools.product)
|
||||
|
||||
;; also from itertools, but not in Python2, and without func option until 3.3
|
||||
(defn accumulate [iterable &optional [func operator.add]]
|
||||
"Accumulate `func` on `iterable`.
|
||||
|
||||
Return series of accumulated sums (or other binary function results)."
|
||||
(setv it (iter iterable)
|
||||
total (next it))
|
||||
(yield total)
|
||||
(for [element it]
|
||||
(setv total (func total element))
|
||||
(yield total)))
|
||||
|
||||
(defn drop [count coll]
|
||||
"Drop `count` elements from `coll` and yield back the rest."
|
||||
(islice coll count None))
|
||||
@ -389,15 +339,11 @@ with overlap."
|
||||
|
||||
(defn string [x]
|
||||
"Cast `x` as the current python version's string implementation."
|
||||
(if-python2
|
||||
(unicode x)
|
||||
(str x)))
|
||||
(str x))
|
||||
|
||||
(defn string? [x]
|
||||
"Check if `x` is a string."
|
||||
(if-python2
|
||||
(isinstance x (, str unicode))
|
||||
(isinstance x str)))
|
||||
(isinstance x str))
|
||||
|
||||
(defn take [count coll]
|
||||
"Take `count` elements from `coll`."
|
||||
@ -456,11 +402,11 @@ Even objects with the __name__ magic will work."
|
||||
(setv EXPORTS
|
||||
'[*map accumulate butlast calling-module calling-module-name chain coll?
|
||||
combinations comp complement compress constantly count cycle dec distinct
|
||||
disassemble drop drop-last drop-while empty? eval even? every? exec first
|
||||
filter flatten float? fraction gensym group-by identity inc input instance?
|
||||
disassemble drop drop-last drop-while empty? eval even? every? first
|
||||
flatten float? fraction gensym group-by identity inc instance?
|
||||
integer integer? integer-char? interleave interpose islice iterable?
|
||||
iterate iterator? juxt keyword keyword? last list? macroexpand
|
||||
macroexpand-1 mangle map merge-with multicombinations name neg? none? nth
|
||||
numeric? odd? partition permutations pos? product range read read-str
|
||||
macroexpand-1 mangle merge-with multicombinations name neg? none? nth
|
||||
numeric? odd? partition permutations pos? product read read-str
|
||||
remove repeat repeatedly rest reduce second some string string? symbol?
|
||||
take take-nth take-while tuple? unmangle xor tee zero? zip zip-longest])
|
||||
take take-nth take-while tuple? unmangle xor tee zero? zip-longest])
|
||||
|
@ -1579,9 +1579,7 @@ macros()
|
||||
|
||||
(defn test-read []
|
||||
"NATIVE: test that read takes something for stdin and reads"
|
||||
(if-python2
|
||||
(import [StringIO [StringIO]])
|
||||
(import [io [StringIO]]))
|
||||
(import [io [StringIO]])
|
||||
(import [hy.models [HyExpression]])
|
||||
|
||||
(setv stdin-buffer (StringIO "(+ 2 2)\n(- 2 2)"))
|
||||
|
@ -140,11 +140,6 @@
|
||||
(assert initialized)
|
||||
(assert (test-initialized))
|
||||
|
||||
(defn test-if-python2 []
|
||||
(import sys)
|
||||
(assert (= (get sys.version_info 0)
|
||||
(if-python2 2 3))))
|
||||
|
||||
(defn test-gensym-in-macros []
|
||||
(import ast)
|
||||
(import [astor.code-gen [to-source]])
|
||||
|
Loading…
Reference in New Issue
Block a user