From 26e0e1f18a94c12de96d4d29232650ad030d0c94 Mon Sep 17 00:00:00 2001 From: tristanlatr Date: Tue, 28 Jan 2025 13:16:24 -0500 Subject: [PATCH 1/3] Fix 119 --- beniget/beniget.py | 4 ++++ tests/test_chains.py | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/beniget/beniget.py b/beniget/beniget.py index e7714d3..208ad50 100644 --- a/beniget/beniget.py +++ b/beniget/beniget.py @@ -202,6 +202,10 @@ def _str(self, nodes): # this should probably be assigned to the filename give to DefUseChains instead. Builtins["__file__"] = __file__ +# The WindowsError is the sole occurence of a conditionally existing builtin. +# So we handle it by special-casing it. +Builtins.setdefault('WindowsError', OSError) + DeclarationStep, DefinitionStep = object(), object() def collect_future_imports(node): diff --git a/tests/test_chains.py b/tests/test_chains.py index 396f66d..0ddeb16 100644 --- a/tests/test_chains.py +++ b/tests/test_chains.py @@ -1548,6 +1548,11 @@ class Point: 'x -> ( -> (), x -> ( -> ()))', 'y -> ( -> (), y -> ( -> ()))']) + def test_WindowsError_builtin_name(self): + # Test for issue https://github.com/serge-sans-paille/beniget/issues/119 + code = 'try: 1/0\nexcept WindowsError as e: raise' + self.check_message(code, []) + class TestDefUseChainsStdlib(TestDefUseChains): ast = _ast From 332e76b16a52a11a501a89c34a06419d43d6765f Mon Sep 17 00:00:00 2001 From: tristanlatr Date: Tue, 28 Jan 2025 18:30:12 -0500 Subject: [PATCH 2/3] Handle builtins introduced after Python 3.6. --- beniget/beniget.py | 11 ++++++++--- tests/test_chains.py | 8 +++++++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/beniget/beniget.py b/beniget/beniget.py index 208ad50..5482bf0 100644 --- a/beniget/beniget.py +++ b/beniget/beniget.py @@ -202,9 +202,14 @@ def _str(self, nodes): # this should probably be assigned to the filename give to DefUseChains instead. Builtins["__file__"] = __file__ -# The WindowsError is the sole occurence of a conditionally existing builtin. -# So we handle it by special-casing it. -Builtins.setdefault('WindowsError', OSError) +# Cope with conditionally existing builtins by special-casing them. +Builtins.setdefault('WindowsError', OSError) # never defined under Linux +Builtins.setdefault('anext', next) # added in Python 3.10 +Builtins.setdefault('aiter', iter) # added in Python 3.10 +Builtins.setdefault('EncodingWarning', Warning) # added in Python 3.10 +Builtins.setdefault('PythonFinalizationError', RuntimeError) # added in Python 3.13 +# beniget doesn't run Python 3.5 and below, so we don't need to +# account for names introduced before Python 3.6 DeclarationStep, DefinitionStep = object(), object() diff --git a/tests/test_chains.py b/tests/test_chains.py index 0ddeb16..078d5b6 100644 --- a/tests/test_chains.py +++ b/tests/test_chains.py @@ -1549,9 +1549,15 @@ class Point: 'y -> ( -> (), y -> ( -> ()))']) def test_WindowsError_builtin_name(self): - # Test for issue https://github.com/serge-sans-paille/beniget/issues/119 + # Tests for issue https://github.com/serge-sans-paille/beniget/issues/119 code = 'try: 1/0\nexcept WindowsError as e: raise' self.check_message(code, []) + + def test_newer_Python_version_builtin_name(self): + # Tests for issue https://github.com/serge-sans-paille/beniget/issues/119 + code = ('try: 1/0\nexcept PythonFinalizationError, EncodingWarning as e: raise\n' + 'a,b = anext(), aiter()') + self.check_message(code, []) class TestDefUseChainsStdlib(TestDefUseChains): From ab08f547093f0fd1eeee3e1f0211178eda76d22d Mon Sep 17 00:00:00 2001 From: tristanlatr Date: Tue, 28 Jan 2025 18:31:40 -0500 Subject: [PATCH 3/3] Fix SyntaxError --- tests/test_chains.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_chains.py b/tests/test_chains.py index 078d5b6..544e661 100644 --- a/tests/test_chains.py +++ b/tests/test_chains.py @@ -1555,7 +1555,7 @@ def test_WindowsError_builtin_name(self): def test_newer_Python_version_builtin_name(self): # Tests for issue https://github.com/serge-sans-paille/beniget/issues/119 - code = ('try: 1/0\nexcept PythonFinalizationError, EncodingWarning as e: raise\n' + code = ('try: 1/0\nexcept (PythonFinalizationError, EncodingWarning) as e: raise\n' 'a,b = anext(), aiter()') self.check_message(code, [])