Skip to content

Commit

Permalink
Merge pull request hylang#2447 from Kodiologist/nondeprecated
Browse files Browse the repository at this point in the history
Replace all uses of features that are deprecated in Python 3.12
  • Loading branch information
Kodiologist authored May 30, 2023
2 parents a259e6b + c9541ee commit 961e943
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 42 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion hy/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
14 changes: 7 additions & 7 deletions hy/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.Num(x, n=f(x))
return asty.Constant(x, value =
{Integer: int, Float: float, Complex: complex}[type(x)](x))

@builds_model(Symbol)
def compile_symbol(self, symbol):
Expand Down Expand Up @@ -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 =
(bytes if type(string) is Bytes else str)(string))

@builds_model(FComponent)
def compile_fcomponent(self, fcomponent):
Expand Down Expand Up @@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion hy/core/result_macros.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "/":
Expand Down
9 changes: 6 additions & 3 deletions hy/errors.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import os
import pkgutil
import re
import sys
import traceback
import importlib.util
from contextlib import contextmanager
from functools import reduce

from hy import _initialize_env_var
from hy._compat import PYPY
Expand Down Expand Up @@ -197,7 +196,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

Expand Down
39 changes: 14 additions & 25 deletions hy/macros.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import importlib
import inspect
import os
import pkgutil
import re
import sys
import traceback
Expand Down Expand Up @@ -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):
Expand Down
8 changes: 4 additions & 4 deletions tests/compilers/test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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():
Expand All @@ -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"
Expand Down

0 comments on commit 961e943

Please sign in to comment.