diff --git a/hy/_compat.py b/hy/_compat.py index a2ab7a5..0da0b09 100644 --- a/hy/_compat.py +++ b/hy/_compat.py @@ -17,11 +17,6 @@ PY38 = sys.version_info >= (3, 8) # It is always true on Pythons >= 3.3, which use USC-4 on all systems. UCS4 = sys.maxunicode == 0x10FFFF -str_type = str if PY3 else unicode # NOQA -bytes_type = bytes if PY3 else str # NOQA -long_type = int if PY3 else long # NOQA -string_types = str if PY3 else basestring # NOQA - # # Inspired by the same-named `six` functions. # diff --git a/hy/compiler.py b/hy/compiler.py index 9a20daf..b1d6e2d 100755 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -14,8 +14,7 @@ from hy.errors import (HyCompileError, HyTypeError, HyLanguageError, from hy.lex import mangle, unmangle, hy_parse, parse_one_thing, LexException -from hy._compat import (string_types, str_type, bytes_type, long_type, PY3, - PY36, PY38, reraise) +from hy._compat import (PY3, PY36, PY38, reraise) from hy.macros import require, load_macros, macroexpand, tag_macroexpand import hy.core @@ -512,7 +511,7 @@ class HyASTCompiler(object): compiled_value = self.compile(value) ret += compiled_value - arg = str_type(expr)[1:] + arg = str(expr)[1:] keywords.append(asty.keyword( expr, arg=ast_str(arg), value=compiled_value.force_expr)) @@ -1358,7 +1357,7 @@ class HyASTCompiler(object): def compile_maths_expression(self, expr, root, args): if len(args) == 0: # Return the identity element for this operator. - return asty.Num(expr, n=long_type( + return asty.Num(expr, n=( {"+": 0, "|": 0, "*": 1}[root])) if len(args) == 1: @@ -1763,7 +1762,7 @@ class HyASTCompiler(object): @builds_model(HyInteger, HyFloat, HyComplex) def compile_numeric_literal(self, x): - f = {HyInteger: long_type, + f = {HyInteger: int, HyFloat: float, HyComplex: complex}[type(x)] return asty.Num(x, n=f(x)) @@ -1810,9 +1809,9 @@ class HyASTCompiler(object): def compile_string(self, string): if type(string) is HyString and string.is_format: # This is a format string (a.k.a. an f-string). - return self._format_string(string, str_type(string)) + return self._format_string(string, str(string)) node = asty.Bytes if PY3 and type(string) is HyBytes else asty.Str - f = bytes_type if type(string) is HyBytes else str_type + f = bytes if type(string) is HyBytes else str return node(string, s=f(string)) def _format_string(self, string, rest, allow_recursion=True): @@ -1859,7 +1858,7 @@ class HyASTCompiler(object): try: model, item = parse_one_thing(item) except (ValueError, LexException) as e: - raise self._syntax_error(string, "f-string: " + str_type(e)) + raise self._syntax_error(string, "f-string: " + str(e)) # Look for a conversion character. item = item.lstrip() @@ -1933,7 +1932,7 @@ def get_compiler_module(module=None, compiler=None, calling_frame=False): module = getattr(compiler, 'module', None) or module - if isinstance(module, string_types): + if isinstance(module, str): if module.startswith('<') and module.endswith('>'): module = types.ModuleType(module) else: @@ -2098,7 +2097,7 @@ def hy_compile(tree, module, root=ast.Module, get_expr=False, """ module = get_compiler_module(module, compiler, False) - if isinstance(module, string_types): + if isinstance(module, str): if module.startswith('<') and module.endswith('>'): module = types.ModuleType(module) else: diff --git a/hy/completer.py b/hy/completer.py index 38cf8eb..0f65ae6 100644 --- a/hy/completer.py +++ b/hy/completer.py @@ -9,7 +9,7 @@ import sys import hy.macros import hy.compiler -from hy._compat import builtins, string_types +from hy._compat import builtins docomplete = True @@ -78,7 +78,7 @@ class Completer(object): matches = [] for p in self.path: for k in p.keys(): - if isinstance(k, string_types): + if isinstance(k, str): k = k.replace("_", "-") if k.startswith(text): matches.append(k) @@ -89,7 +89,7 @@ class Completer(object): matches = [] for p in self.tag_path: for k in p.keys(): - if isinstance(k, string_types): + if isinstance(k, str): if k.startswith(text): matches.append("#{}".format(k)) return matches diff --git a/hy/contrib/hy_repr.hy b/hy/contrib/hy_repr.hy index c99a45a..3baaebb 100644 --- a/hy/contrib/hy_repr.hy +++ b/hy/contrib/hy_repr.hy @@ -7,7 +7,7 @@ re datetime collections - [hy._compat [PY3 PY36 str-type bytes-type long-type]] + [hy._compat [PY36]] [hy.models [HyObject HyExpression HySymbol HyKeyword HyInteger HyFloat HyComplex HyList HyDict HySet HyString HyBytes]]) (try @@ -84,10 +84,10 @@ (+ "(" (-cat x) ")")))) (hy-repr-register [HySymbol HyKeyword] str) -(hy-repr-register [str-type bytes-type] (fn [x] +(hy-repr-register [str bytes] (fn [x] (setv r (.lstrip (-base-repr x) "ub")) (+ - (if (instance? bytes-type x) "b" "") + (if (instance? bytes x) "b" "") (if (.startswith "\"" r) ; If Python's built-in repr produced a double-quoted string, use ; that. @@ -96,10 +96,6 @@ ; convert it. (+ "\"" (.replace (cut r 1 -1) "\"" "\\\"") "\""))))) (hy-repr-register bool str) -(if (not PY3) (hy-repr-register int (fn [x] - (.format "(int {})" (-base-repr x))))) -(if (not PY3) (hy-repr-register long_type (fn [x] - (.rstrip (-base-repr x) "L")))) (hy-repr-register float (fn [x] (if (isnan x) "NaN" @@ -131,7 +127,7 @@ (-repr-time-innards x)))) (defn -repr-time-innards [x] (.rstrip (+ " " (.join " " (filter identity [ - (if x.microsecond (str-type x.microsecond)) + (if x.microsecond (str x.microsecond)) (if (not (none? x.tzinfo)) (+ ":tzinfo " (hy-repr x.tzinfo))) (if (and PY36 (!= x.fold 0)) (+ ":fold " (hy-repr x.fold)))]))))) (defn -strftime-0 [x fmt] diff --git a/hy/core/language.hy b/hy/core/language.hy index 53af7fb..687bd5c 100644 --- a/hy/core/language.hy +++ b/hy/core/language.hy @@ -11,7 +11,6 @@ (import [fractions [Fraction :as fraction]]) (import operator) ; shadow not available yet (import sys) -(import [hy._compat [long-type]]) ; long for python2, int for python3 (import [hy.models [HySymbol HyKeyword]]) (import [hy.lex [tokenize mangle unmangle read read-str]]) (import [hy.lex.exceptions [LexException PrematureEndOfInput]]) @@ -254,11 +253,11 @@ Return series of accumulated sums (or other binary function results)." (defn integer [x] "Return Hy kind of integer for `x`." - (long-type x)) + (int x)) (defn integer? [x] "Check if `x` is an integer." - (isinstance x (, int long-type))) + (isinstance x int)) (defn integer-char? [x] "Check if char `x` parses as an integer." diff --git a/hy/lex/__init__.py b/hy/lex/__init__.py index d133f5f..b29709c 100644 --- a/hy/lex/__init__.py +++ b/hy/lex/__init__.py @@ -8,7 +8,7 @@ import re import sys import unicodedata -from hy._compat import str_type, isidentifier, UCS4 +from hy._compat import isidentifier, UCS4 from hy.lex.exceptions import PrematureEndOfInput, LexException # NOQA from hy.models import HyExpression, HySymbol @@ -116,7 +116,7 @@ def mangle(s): assert s - s = str_type(s) + s = str(s) s = s.replace("-", "_") s2 = s.lstrip('_') leading_underscores = '_' * (len(s) - len(s2)) @@ -147,7 +147,7 @@ def unmangle(s): form. This may not round-trip, because different Hy symbol names can mangle to the same Python identifier.""" - s = str_type(s) + s = str(s) s2 = s.lstrip('_') leading_underscores = len(s) - len(s2) @@ -203,4 +203,4 @@ def read(from_file=sys.stdin, eof=""): def read_str(input): - return read(StringIO(str_type(input))) + return read(StringIO(str(input))) diff --git a/hy/lex/parser.py b/hy/lex/parser.py index 6a1acfb..1d5ce24 100755 --- a/hy/lex/parser.py +++ b/hy/lex/parser.py @@ -9,7 +9,6 @@ from functools import wraps from rply import ParserGenerator -from hy._compat import str_type from hy.models import (HyBytes, HyComplex, HyDict, HyExpression, HyFloat, HyInteger, HyKeyword, HyList, HySet, HyString, HySymbol) from .lexer import lexer @@ -214,7 +213,7 @@ def t_string(state, p): raise LexException.from_lexer("Can't convert {} to a HyString".format(p[0].value), state, p[0]) return (HyString(s, is_format = is_format) - if isinstance(s, str_type) + if isinstance(s, str) else HyBytes(s)) diff --git a/hy/macros.py b/hy/macros.py index e2cec31..97d0976 100644 --- a/hy/macros.py +++ b/hy/macros.py @@ -9,7 +9,7 @@ import traceback from contextlib import contextmanager -from hy._compat import PY3, string_types, reraise, rename_function +from hy._compat import PY3, reraise, rename_function from hy.models import replace_hy_obj, HyExpression, HySymbol, wrap_value from hy.lex import mangle from hy.errors import (HyLanguageError, HyMacroExpansionError, HyTypeError, @@ -156,7 +156,7 @@ def require(source_module, target_module, assignments, prefix=""): parent_frame = inspect.stack()[1][0] target_namespace = parent_frame.f_globals target_module = target_namespace.get('__name__', None) - elif isinstance(target_module, string_types): + elif isinstance(target_module, str): target_module = importlib.import_module(target_module) target_namespace = target_module.__dict__ elif inspect.ismodule(target_module): diff --git a/hy/models.py b/hy/models.py index 458d615..fbced02 100644 --- a/hy/models.py +++ b/hy/models.py @@ -6,7 +6,6 @@ from __future__ import unicode_literals from contextlib import contextmanager from math import isnan, isinf from hy import _initialize_env_var -from hy._compat import PY3, str_type, bytes_type, long_type, string_types from hy.errors import HyWrapperError from fractions import Fraction from clint.textui import colored @@ -88,7 +87,7 @@ def repr_indent(obj): return repr(obj).replace("\n", "\n ") -class HyString(HyObject, str_type): +class HyString(HyObject, str): """ Generic Hy String object. Helpful to store string literals from Hy scripts. It's either a ``str`` or a ``unicode``, depending on the @@ -100,20 +99,20 @@ class HyString(HyObject, str_type): value.brackets = brackets return value -_wrappers[str_type] = HyString +_wrappers[str] = HyString -class HyBytes(HyObject, bytes_type): +class HyBytes(HyObject, bytes): """ Generic Hy Bytes object. It's either a ``bytes`` or a ``str``, depending on the Python version. """ pass -_wrappers[bytes_type] = HyBytes +_wrappers[bytes] = HyBytes -class HySymbol(HyObject, str_type): +class HySymbol(HyObject, str): """ Hy Symbol. Basically a string. """ @@ -170,42 +169,39 @@ def strip_digit_separators(number): # Don't strip a _ or , if it's the first character, as _42 and # ,42 aren't valid numbers return (number[0] + number[1:].replace("_", "").replace(",", "") - if isinstance(number, string_types) and len(number) > 1 + if isinstance(number, str) and len(number) > 1 else number) -class HyInteger(HyObject, long_type): +class HyInteger(HyObject, int): """ Internal representation of a Hy Integer. May raise a ValueError as if - int(foo) was called, given HyInteger(foo). On python 2.x long will - be used instead + int(foo) was called, given HyInteger(foo). """ def __new__(cls, number, *args, **kwargs): - if isinstance(number, string_types): + if isinstance(number, str): number = strip_digit_separators(number) bases = {"0x": 16, "0o": 8, "0b": 2} for leader, base in bases.items(): if number.startswith(leader): # We've got a string, known leader, set base. - number = long_type(number, base=base) + number = int(number, base=base) break else: # We've got a string, no known leader; base 10. - number = long_type(number, base=10) + number = int(number, base=10) else: # We've got a non-string; convert straight. - number = long_type(number) + number = int(number) return super(HyInteger, cls).__new__(cls, number) _wrappers[int] = HyInteger -if not PY3: # do not add long on python3 - _wrappers[long_type] = HyInteger def check_inf_nan_cap(arg, value): - if isinstance(arg, string_types): + if isinstance(arg, str): if isinf(value) and "i" in arg.lower() and "Inf" not in arg: raise ValueError('Inf must be capitalized as "Inf"') if isnan(value) and "NaN" not in arg: @@ -233,7 +229,7 @@ class HyComplex(HyObject, complex): """ def __new__(cls, real, imag=0, *args, **kwargs): - if isinstance(real, string_types): + if isinstance(real, str): value = super(HyComplex, cls).__new__( cls, strip_digit_separators(real) ) diff --git a/tests/test_models.py b/tests/test_models.py index fd1b615..b9e6a90 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -5,14 +5,13 @@ import copy import hy from clint.textui.colored import clean -from hy._compat import long_type, str_type from hy.models import (wrap_value, replace_hy_obj, HyString, HyInteger, HyList, HyDict, HySet, HyExpression, HyComplex, HyFloat, pretty) -def test_wrap_long_type(): +def test_wrap_int(): """ Test conversion of integers.""" - wrapped = wrap_value(long_type(0)) + wrapped = wrap_value(0) assert type(wrapped) == HyInteger @@ -26,27 +25,27 @@ def test_wrap_tuple(): def test_wrap_nested_expr(): """ Test conversion of HyExpressions with embedded non-HyObjects.""" - wrapped = wrap_value(HyExpression([long_type(0)])) + wrapped = wrap_value(HyExpression([0])) assert type(wrapped) == HyExpression assert type(wrapped[0]) == HyInteger assert wrapped == HyExpression([HyInteger(0)]) -def test_replace_long_type(): +def test_replace_int(): """ Test replacing integers.""" - replaced = replace_hy_obj(long_type(0), HyInteger(13)) + replaced = replace_hy_obj(0, HyInteger(13)) assert replaced == HyInteger(0) def test_replace_string_type(): """Test replacing python string""" - replaced = replace_hy_obj(str_type("foo"), HyString("bar")) + replaced = replace_hy_obj("foo", HyString("bar")) assert replaced == HyString("foo") def test_replace_tuple(): """ Test replacing tuples.""" - replaced = replace_hy_obj((long_type(0), ), HyInteger(13)) + replaced = replace_hy_obj((0, ), HyInteger(13)) assert type(replaced) == HyList assert type(replaced[0]) == HyInteger assert replaced == HyList([HyInteger(0)])