Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle the conditionally existing builtins edge case #121

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions beniget/beniget.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,15 @@ def _str(self, nodes):
# this should probably be assigned to the filename give to DefUseChains instead.
Builtins["__file__"] = __file__

# 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()

def collect_future_imports(node):
Expand Down
11 changes: 11 additions & 0 deletions tests/test_chains.py
Original file line number Diff line number Diff line change
Expand Up @@ -1548,6 +1548,17 @@ class Point:
'x -> (<MatchClass> -> (), x -> (<Call> -> ()))',
'y -> (<MatchClass> -> (), y -> (<Call> -> ()))'])

def test_WindowsError_builtin_name(self):
# 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):
ast = _ast
Expand Down