Skip to content

Commit

Permalink
Generate cache for builtins
Browse files Browse the repository at this point in the history
- Create a set with all attributes available in
the most recent python (3.12.2) builtins.

- Use this cache in Symtab.py in declare_builtin functions.

Signed-off-by: Alexandra Pereira <alesilva241@gmail.com>
  • Loading branch information
alexandrasp committed Feb 29, 2024
1 parent fcd37a9 commit 036f4e4
Showing 1 changed file with 161 additions and 4 deletions.
165 changes: 161 additions & 4 deletions Cython/Compiler/Symtab.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import copy
import operator

import builtins

from ..Utils import try_finally_contextmanager
from .Errors import warning, error, InternalError
from .StringEncoding import EncodedString
Expand All @@ -31,6 +29,165 @@
'_Bool', '_Complex'', _Imaginary', 'inline', 'restrict',
}

# Builtins python 3.12.2

builtins_attrs = set(['ArithmeticError',
'AssertionError',
'AttributeError',
'BaseException',
'BaseExceptionGroup',
'BlockingIOError',
'BrokenPipeError',
'BufferError',
'BytesWarning',
'ChildProcessError',
'ConnectionAbortedError',
'ConnectionError',
'ConnectionRefusedError',
'ConnectionResetError',
'DeprecationWarning',
'EOFError',
'Ellipsis',
'EncodingWarning',
'EnvironmentError',
'Exception',
'ExceptionGroup',
'False',
'FileExistsError',
'FileNotFoundError',
'FloatingPointError',
'FutureWarning',
'GeneratorExit',
'IOError',
'ImportError',
'ImportWarning',
'IndentationError',
'IndexError',
'InterruptedError',
'IsADirectoryError',
'KeyError',
'KeyboardInterrupt',
'LookupError',
'MemoryError',
'ModuleNotFoundError',
'NameError',
'None',
'NotADirectoryError',
'NotImplemented',
'NotImplementedError',
'OSError',
'OverflowError',
'PendingDeprecationWarning',
'PermissionError',
'ProcessLookupError',
'RecursionError',
'ReferenceError',
'ResourceWarning',
'RuntimeError',
'RuntimeWarning',
'StopAsyncIteration',
'StopIteration',
'SyntaxError',
'SyntaxWarning',
'SystemError',
'SystemExit',
'TabError',
'TimeoutError',
'True',
'TypeError',
'UnboundLocalError',
'UnicodeDecodeError',
'UnicodeEncodeError',
'UnicodeError',
'UnicodeTranslateError',
'UnicodeWarning',
'UserWarning',
'ValueError',
'Warning',
'ZeroDivisionError',
'__build_class__',
'__debug__',
'__doc__',
'__import__',
'__loader__',
'__name__',
'__package__',
'__spec__',
'abs',
'aiter',
'all',
'anext',
'any',
'ascii',
'bin',
'bool',
'breakpoint',
'bytearray',
'bytes',
'callable',
'chr',
'classmethod',
'compile',
'complex',
'copyright',
'credits',
'delattr',
'dict',
'dir',
'divmod',
'enumerate',
'eval',
'exec',
'exit',
'filter',
'float',
'format',
'frozenset',
'getattr',
'globals',
'hasattr',
'hash',
'help',
'hex',
'id',
'input',
'int',
'isinstance',
'issubclass',
'iter',
'len',
'license',
'list',
'locals',
'map',
'max',
'memoryview',
'min',
'next',
'object',
'oct',
'open',
'ord',
'pow',
'print',
'property',
'quit',
'range',
'repr',
'reversed',
'round',
'set',
'setattr',
'slice',
'sorted',
'staticmethod',
'str',
'sum',
'super',
'tuple',
'type',
'vars',
'zip'])

def c_safe_identifier(cname):
# There are some C limitations on struct entry names.
Expand Down Expand Up @@ -1190,7 +1347,7 @@ def lookup(self, name, language_level=None, str_is_str=None):
return Scope.lookup(self, name)

def declare_builtin(self, name, pos):
if not hasattr(builtins, name):
if not name in builtins_attrs:
if self.outer_scope is not None:
return self.outer_scope.declare_builtin(name, pos)
else:
Expand Down Expand Up @@ -1358,7 +1515,7 @@ def declare_tuple_type(self, pos, components):
return entry

def declare_builtin(self, name, pos):
if not hasattr(builtins, name) \
if not name in builtins_attrs \
and name not in Code.non_portable_builtins_map \
and name not in Code.uncachable_builtins:
if self.has_import_star:
Expand Down

0 comments on commit 036f4e4

Please sign in to comment.