Skip to content

Commit 3b192d3

Browse files
Detect builtins module on PyPy 3.9 (#1567)
1 parent 70d6751 commit 3b192d3

File tree

4 files changed

+22
-16
lines changed

4 files changed

+22
-16
lines changed

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ jobs:
282282
strategy:
283283
fail-fast: false
284284
matrix:
285-
python-version: ["pypy-3.7", "pypy-3.8"]
285+
python-version: ["pypy-3.7", "pypy-3.8", "pypy-3.9"]
286286
steps:
287287
- name: Check out code from GitHub
288288
uses: actions/[email protected]

ChangeLog

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Release date: TBA
88

99
* ``astroid`` now requires Python 3.7.2 to run.
1010

11+
* Fix detection of builtins on ``PyPy`` 3.9.
12+
1113
* Fix ``re`` brain on Python ``3.11``. The flags now come from ``re._compile``.
1214

1315
* Build ``nodes.Module`` for frozen modules which have location information in their

astroid/interpreter/_import/spec.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -105,22 +105,22 @@ def find_module(
105105
) -> ModuleSpec | None:
106106
if submodule_path is not None:
107107
submodule_path = list(submodule_path)
108+
elif modname in sys.builtin_module_names:
109+
return ModuleSpec(
110+
name=modname,
111+
location=None,
112+
type=ModuleType.C_BUILTIN,
113+
)
108114
else:
109115
try:
110116
spec = importlib.util.find_spec(modname)
111-
if spec:
112-
if spec.loader is importlib.machinery.BuiltinImporter:
113-
return ModuleSpec(
114-
name=modname,
115-
location=None,
116-
type=ModuleType.C_BUILTIN,
117-
)
118-
if spec.loader is importlib.machinery.FrozenImporter:
119-
return ModuleSpec(
120-
name=modname,
121-
location=getattr(spec.loader_state, "filename", None),
122-
type=ModuleType.PY_FROZEN,
123-
)
117+
if spec and spec.loader is importlib.machinery.FrozenImporter:
118+
# No need for BuiltinImporter; builtins handled above
119+
return ModuleSpec(
120+
name=modname,
121+
location=getattr(spec.loader_state, "filename", None),
122+
type=ModuleType.PY_FROZEN,
123+
)
124124
except ValueError:
125125
pass
126126
submodule_path = sys.path

astroid/raw_building.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from collections.abc import Iterable
1818

1919
from astroid import bases, nodes
20+
from astroid.const import IS_PYPY
2021
from astroid.manager import AstroidManager
2122
from astroid.nodes import node_classes
2223

@@ -321,6 +322,9 @@ def object_build(self, node, obj):
321322
return self._done[obj]
322323
self._done[obj] = node
323324
for name in dir(obj):
325+
# inspect.ismethod() and inspect.isbuiltin() in PyPy return
326+
# the opposite of what they do in CPython for __class_getitem__.
327+
pypy__class_getitem__ = IS_PYPY and name == "__class_getitem__"
324328
try:
325329
with warnings.catch_warnings():
326330
warnings.simplefilter("error")
@@ -329,11 +333,11 @@ def object_build(self, node, obj):
329333
# damned ExtensionClass.Base, I know you're there !
330334
attach_dummy_node(node, name)
331335
continue
332-
if inspect.ismethod(member):
336+
if inspect.ismethod(member) and not pypy__class_getitem__:
333337
member = member.__func__
334338
if inspect.isfunction(member):
335339
_build_from_function(node, name, member, self._module)
336-
elif inspect.isbuiltin(member):
340+
elif inspect.isbuiltin(member) or pypy__class_getitem__:
337341
if self.imported_member(node, member, name):
338342
continue
339343
object_build_methoddescriptor(node, member, name)

0 commit comments

Comments
 (0)