From 47ea0a1ed5f50601cc3430eb4c3b8f8e049c6433 Mon Sep 17 00:00:00 2001 From: Kodi Arfer Date: Sat, 27 May 2023 09:46:32 -0400 Subject: [PATCH 1/6] Don't use deprecated AST elements --- hy/_compat.py | 2 +- hy/compiler.py | 10 +++++----- hy/core/result_macros.py | 2 +- tests/compilers/test_ast.py | 8 ++++---- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/hy/_compat.py b/hy/_compat.py index a752bfec7..4b83686fa 100644 --- a/hy/_compat.py +++ b/hy/_compat.py @@ -27,7 +27,7 @@ def rewriting_unparse(ast_obj): ast_obj = copy.deepcopy(ast_obj) for node in ast.walk(ast_obj): - if type(node) in (ast.Constant, ast.Str): + if type(node) is ast.Constant: # Don't touch string literals. continue for field in node._fields: diff --git a/hy/compiler.py b/hy/compiler.py index efc6913b6..862e4427c 100755 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -580,7 +580,7 @@ def compile_expression(self, expr, *, allow_annotation_expression=False): @builds_model(Integer, Float, Complex) def compile_numeric_literal(self, x): f = {Integer: int, Float: float, Complex: complex}[type(x)] - return asty.Num(x, n=f(x)) + return asty.Constant(x, value=f(x)) @builds_model(Symbol) def compile_symbol(self, symbol): @@ -612,16 +612,15 @@ def compile_keyword(self, obj): attr="Keyword", ctx=ast.Load(), ), - args=[asty.Str(obj, s=obj.name)], + args=[asty.Constant(obj, value=obj.name)], keywords=[], ) return ret @builds_model(String, Bytes) def compile_string(self, string): - node = asty.Bytes if type(string) is Bytes else asty.Str f = bytes if type(string) is Bytes else str - return node(string, s=f(string)) + return asty.Constant(string, value=f(string)) @builds_model(FComponent) def compile_fcomponent(self, fcomponent): @@ -856,7 +855,8 @@ def hy_compile( if ( result.stmts and isinstance(result.stmts[0], ast.Expr) - and isinstance(result.stmts[0].value, ast.Str) + and isinstance(result.stmts[0].value, ast.Constant) + and isinstance(result.stmts[0].value.value, str) ): body += [result.stmts.pop(0)] diff --git a/hy/core/result_macros.py b/hy/core/result_macros.py index 61e929826..eb296ffd1 100644 --- a/hy/core/result_macros.py +++ b/hy/core/result_macros.py @@ -354,7 +354,7 @@ def compile_chained_comparison(compiler, expr, root, arg1, args): def compile_maths_expression(compiler, expr, root, args): if len(args) == 0: # Return the identity element for this operator. - return asty.Num(expr, n=({"+": 0, "|": 0, "*": 1}[root])) + return asty.Constant(expr, value=({"+": 0, "|": 0, "*": 1}[root])) if len(args) == 1: if root == "/": diff --git a/tests/compilers/test_ast.py b/tests/compilers/test_ast.py index a44607a8e..6cb4c4213 100644 --- a/tests/compilers/test_ast.py +++ b/tests/compilers/test_ast.py @@ -40,7 +40,7 @@ def cant_compile(expr): def s(x): - return can_compile('"module docstring" ' + x).body[-1].value.s + return can_compile('"module docstring" ' + x).body[-1].value.value def test_ast_bad_type(): @@ -374,7 +374,7 @@ def test_lambda_list_keywords_kwonly(): for i, kwonlyarg_name in enumerate(("a", "b")): assert kwonlyarg_name == code.body[0].args.kwonlyargs[i].arg assert code.body[0].args.kw_defaults[0] is None - assert code.body[0].args.kw_defaults[1].n == 2 + assert code.body[0].args.kw_defaults[1].value == 2 def test_lambda_list_keywords_mixed(): @@ -399,8 +399,8 @@ def _compile_string(s): ) # We put hy_s in a list so it isn't interpreted as a docstring. - # code == ast.Module(body=[ast.Expr(value=ast.List(elts=[ast.Str(s=xxx)]))]) - return code.body[0].value.elts[0].s + # code == ast.Module(body=[ast.Expr(value=ast.List(elts=[ast.Constant(value=xxx)]))]) + return code.body[0].value.elts[0].value assert _compile_string("test") == "test" assert _compile_string("\u03b1\u03b2") == "\u03b1\u03b2" From 2f6c2b63e804a1e3cccda6eb8d9f4c49b971762e Mon Sep 17 00:00:00 2001 From: Kodi Arfer Date: Sat, 27 May 2023 09:51:45 -0400 Subject: [PATCH 2/6] Inline some single-use variables --- hy/compiler.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/hy/compiler.py b/hy/compiler.py index 862e4427c..20882092b 100755 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -579,8 +579,8 @@ def compile_expression(self, expr, *, allow_annotation_expression=False): @builds_model(Integer, Float, Complex) def compile_numeric_literal(self, x): - f = {Integer: int, Float: float, Complex: complex}[type(x)] - return asty.Constant(x, value=f(x)) + return asty.Constant(x, value = + {Integer: int, Float: float, Complex: complex}[type(x)](x)) @builds_model(Symbol) def compile_symbol(self, symbol): @@ -619,8 +619,8 @@ def compile_keyword(self, obj): @builds_model(String, Bytes) def compile_string(self, string): - f = bytes if type(string) is Bytes else str - return asty.Constant(string, value=f(string)) + return asty.Constant(string, value = + (bytes if type(string) is Bytes else str)(string)) @builds_model(FComponent) def compile_fcomponent(self, fcomponent): From 96a085d335fd79309db7c0911a06530a3daaf157 Mon Sep 17 00:00:00 2001 From: Kodi Arfer Date: Sat, 27 May 2023 17:41:05 -0400 Subject: [PATCH 3/6] Don't use the deprecated `pkgutil.get_loader` --- hy/errors.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/hy/errors.py b/hy/errors.py index e59a3f746..3f46a9648 100644 --- a/hy/errors.py +++ b/hy/errors.py @@ -1,8 +1,8 @@ import os -import pkgutil import re import sys import traceback +import importlib.util from contextlib import contextmanager from functools import reduce @@ -197,7 +197,11 @@ class HyWrapperError(HyError, TypeError): def _module_filter_name(module_name): try: - compiler_loader = pkgutil.get_loader(module_name) + spec = importlib.util.find_spec(module_name) + if not spec: + return None + + compiler_loader = spec.loader if not compiler_loader: return None From ebef72bd9d885cf3a5e895e001e66ea829773ced Mon Sep 17 00:00:00 2001 From: Kodi Arfer Date: Sat, 27 May 2023 17:41:29 -0400 Subject: [PATCH 4/6] Rewrite `hy.macros._same_modules` To avoid use of the deprecated `pkgutil.get_loader`, and to be more concise. --- hy/macros.py | 39 ++++++++++++++------------------------- 1 file changed, 14 insertions(+), 25 deletions(-) diff --git a/hy/macros.py b/hy/macros.py index a124cee0e..3f159a21b 100644 --- a/hy/macros.py +++ b/hy/macros.py @@ -2,7 +2,6 @@ import importlib import inspect import os -import pkgutil import re import sys import traceback @@ -110,33 +109,23 @@ def _same_modules(source_module, target_module): if not (source_module or target_module): return False - if target_module == source_module: + if target_module is source_module: return True - def _get_filename(module): - filename = None - try: - if not inspect.ismodule(module): - loader = pkgutil.get_loader(module) - if isinstance(loader, importlib.machinery.SourceFileLoader): - filename = loader.get_filename() - else: - filename = inspect.getfile(module) - except (TypeError, ImportError): - pass - - return filename - - source_filename = _get_filename(source_module) - target_filename = _get_filename(target_module) + def get_filename(module): + if inspect.ismodule(module): + return inspect.getfile(module) + elif ( + (spec := importlib.util.find_spec(module)) and + isinstance(spec.loader, importlib.machinery.SourceFileLoader)): + return spec.loader.get_filename() - return ( - source_filename - and target_filename - and os.path.exists(source_filename) - and os.path.exists(target_filename) - and os.path.samefile(source_filename, target_filename) - ) + try: + return os.path.samefile( + get_filename(source_module), + get_filename(target_module)) + except (ValueError, TypeError, ImportError, FileNotFoundError): + return False def derive_target_module(target_module, parent_frame): From 969b80a719fc9b498b7c5e3f48e62a6483216eee Mon Sep 17 00:00:00 2001 From: Kodi Arfer Date: Sat, 27 May 2023 09:57:51 -0400 Subject: [PATCH 5/6] Shorten the name of Mac OS tests on GitHub --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 92f4c4480..4e6045594 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -17,7 +17,7 @@ jobs: - name-prefix: 'win-' os: windows-latest python: 3.11 - - name-prefix: 'macos-' + - name-prefix: 'mac-' os: macos-latest python: 3.11 From c9541eed8f3699fb9b2487494a47d63a227090e0 Mon Sep 17 00:00:00 2001 From: Kodi Arfer Date: Sat, 27 May 2023 17:41:55 -0400 Subject: [PATCH 6/6] Remove an unused import --- hy/errors.py | 1 - 1 file changed, 1 deletion(-) diff --git a/hy/errors.py b/hy/errors.py index 3f46a9648..440f0dc30 100644 --- a/hy/errors.py +++ b/hy/errors.py @@ -4,7 +4,6 @@ import traceback import importlib.util from contextlib import contextmanager -from functools import reduce from hy import _initialize_env_var from hy._compat import PYPY