Remove if-python2 and its uses

This commit is contained in:
Kodi Arfer 2019-05-20 17:09:24 -04:00
parent 7ba1407257
commit 6af6a2945a
5 changed files with 16 additions and 86 deletions

View File

@ -19,9 +19,7 @@
`(do `(do
(import cProfile pstats) (import cProfile pstats)
(if-python2 (import [io [StringIO]])
(import [StringIO [StringIO]])
(import [io [StringIO]]))
(setv ~g!hy-pr (.Profile cProfile)) (setv ~g!hy-pr (.Profile cProfile))
(.enable ~g!hy-pr) (.enable ~g!hy-pr)

View File

@ -77,10 +77,3 @@
(if (not (isinstance lambda-list hy.HyList)) (if (not (isinstance lambda-list hy.HyList))
(macro-error name "defn/a takes a parameter list as second argument")) (macro-error name "defn/a takes a parameter list as second argument"))
`(setv ~name (fn/a ~lambda-list ~@body))) `(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))

View File

@ -11,6 +11,7 @@
(import [fractions [Fraction :as fraction]]) (import [fractions [Fraction :as fraction]])
(import operator) ; shadow not available yet (import operator) ; shadow not available yet
(import sys) (import sys)
(import [collections.abc :as cabc])
(import [hy.models [HySymbol HyKeyword]]) (import [hy.models [HySymbol HyKeyword]])
(import [hy.lex [tokenize mangle unmangle read read-str]]) (import [hy.lex [tokenize mangle unmangle read read-str]])
(import [hy.lex.exceptions [LexException PrematureEndOfInput]]) (import [hy.lex.exceptions [LexException PrematureEndOfInput]])
@ -20,10 +21,6 @@
(require [hy.core.bootstrap [*]]) (require [hy.core.bootstrap [*]])
(if-python2
(import [collections :as cabc])
(import [collections.abc :as cabc]))
(defn butlast [coll] (defn butlast [coll]
"Return an iterator of all but the last item in `coll`." "Return an iterator of all but the last item in `coll`."
(drop-last 1 coll)) (drop-last 1 coll))
@ -85,47 +82,12 @@ If the second argument `codegen` is true, generate python code instead."
(yield val) (yield val)
(.add seen val))))) (.add seen val)))))
(if-python2 (setv
(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 remove itertools.filterfalse
zip-longest itertools.zip_longest zip-longest itertools.zip_longest
;; was builtin in Python2 ;; was builtin in Python2
reduce functools.reduce reduce functools.reduce
;; Someone can import these directly from `hy.core.language`; accumulate itertools.accumulate)
;; 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))
;; infinite iterators ;; infinite iterators
(setv (setv
@ -151,18 +113,6 @@ function with keyword arguments, which isn't supported by Python 3's `exec`."
permutations itertools.permutations permutations itertools.permutations
product itertools.product) 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] (defn drop [count coll]
"Drop `count` elements from `coll` and yield back the rest." "Drop `count` elements from `coll` and yield back the rest."
(islice coll count None)) (islice coll count None))
@ -389,15 +339,11 @@ with overlap."
(defn string [x] (defn string [x]
"Cast `x` as the current python version's string implementation." "Cast `x` as the current python version's string implementation."
(if-python2 (str x))
(unicode x)
(str x)))
(defn string? [x] (defn string? [x]
"Check if `x` is a string." "Check if `x` is a string."
(if-python2 (isinstance x str))
(isinstance x (, str unicode))
(isinstance x str)))
(defn take [count coll] (defn take [count coll]
"Take `count` elements from `coll`." "Take `count` elements from `coll`."
@ -456,11 +402,11 @@ Even objects with the __name__ magic will work."
(setv EXPORTS (setv EXPORTS
'[*map accumulate butlast calling-module calling-module-name chain coll? '[*map accumulate butlast calling-module calling-module-name chain coll?
combinations comp complement compress constantly count cycle dec distinct combinations comp complement compress constantly count cycle dec distinct
disassemble drop drop-last drop-while empty? eval even? every? exec first disassemble drop drop-last drop-while empty? eval even? every? first
filter flatten float? fraction gensym group-by identity inc input instance? flatten float? fraction gensym group-by identity inc instance?
integer integer? integer-char? interleave interpose islice iterable? integer integer? integer-char? interleave interpose islice iterable?
iterate iterator? juxt keyword keyword? last list? macroexpand iterate iterator? juxt keyword keyword? last list? macroexpand
macroexpand-1 mangle map merge-with multicombinations name neg? none? nth macroexpand-1 mangle merge-with multicombinations name neg? none? nth
numeric? odd? partition permutations pos? product range read read-str numeric? odd? partition permutations pos? product read read-str
remove repeat repeatedly rest reduce second some string string? symbol? 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])

View File

@ -1579,9 +1579,7 @@ macros()
(defn test-read [] (defn test-read []
"NATIVE: test that read takes something for stdin and reads" "NATIVE: test that read takes something for stdin and reads"
(if-python2 (import [io [StringIO]])
(import [StringIO [StringIO]])
(import [io [StringIO]]))
(import [hy.models [HyExpression]]) (import [hy.models [HyExpression]])
(setv stdin-buffer (StringIO "(+ 2 2)\n(- 2 2)")) (setv stdin-buffer (StringIO "(+ 2 2)\n(- 2 2)"))

View File

@ -140,11 +140,6 @@
(assert initialized) (assert initialized)
(assert (test-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 [] (defn test-gensym-in-macros []
(import ast) (import ast)
(import [astor.code-gen [to-source]]) (import [astor.code-gen [to-source]])