Skip to content

Commit 8fda697

Browse files
authored
Improve recognition of _io module during bootstrapping on PyPy (#1529)
1 parent 519ae7a commit 8fda697

File tree

2 files changed

+10
-20
lines changed

2 files changed

+10
-20
lines changed

astroid/raw_building.py

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,6 @@
2626
TYPE_ELLIPSIS = type(...)
2727

2828

29-
def _io_discrepancy(member):
30-
# _io module names itself `io`: http://bugs.python.org/issue18602
31-
member_self = getattr(member, "__self__", None)
32-
return (
33-
member_self
34-
and inspect.ismodule(member_self)
35-
and member_self.__name__ == "_io"
36-
and member.__module__ == "io"
37-
)
38-
39-
4029
def _attach_local_node(parent, node, name):
4130
node.name = name # needed by add_local_node
4231
parent.add_local_node(node)
@@ -343,9 +332,7 @@ def object_build(self, node, obj):
343332
if inspect.isfunction(member):
344333
_build_from_function(node, name, member, self._module)
345334
elif inspect.isbuiltin(member):
346-
if not _io_discrepancy(member) and self.imported_member(
347-
node, member, name
348-
):
335+
if self.imported_member(node, member, name):
349336
continue
350337
object_build_methoddescriptor(node, member, name)
351338
elif inspect.isclass(member):
@@ -383,7 +370,7 @@ def object_build(self, node, obj):
383370
attach_dummy_node(node, name, member)
384371
return None
385372

386-
def imported_member(self, node, member, name):
373+
def imported_member(self, node, member, name: str) -> bool:
387374
"""verify this is not an imported class or handle it"""
388375
# /!\ some classes like ExtensionClass doesn't have a __module__
389376
# attribute ! Also, this may trigger an exception on badly built module
@@ -402,7 +389,13 @@ def imported_member(self, node, member, name):
402389
attach_dummy_node(node, name, member)
403390
return True
404391

405-
real_name = {"gtk": "gtk_gtk", "_io": "io"}.get(modname, modname)
392+
# On PyPy during bootstrapping we infer _io while _module is
393+
# builtins. In CPython _io names itself io, see http://bugs.python.org/issue18602
394+
# Therefore, this basically checks whether we are not in PyPy.
395+
if modname == "_io" and not self._module.__name__ == "builtins":
396+
return False
397+
398+
real_name = {"gtk": "gtk_gtk"}.get(modname, modname)
406399

407400
if real_name != self._module.__name__:
408401
# check if it sounds valid and then add an import node, else use a

tests/unittest_inference.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from astroid.arguments import CallSite
2121
from astroid.bases import BoundMethod, Instance, UnboundMethod
2222
from astroid.builder import AstroidBuilder, extract_node, parse
23-
from astroid.const import IS_PYPY, PY38_PLUS, PY39_PLUS
23+
from astroid.const import PY38_PLUS, PY39_PLUS
2424
from astroid.context import InferenceContext
2525
from astroid.exceptions import (
2626
AstroidTypeError,
@@ -817,9 +817,6 @@ def test_builtin_open(self) -> None:
817817
self.assertIsInstance(inferred[0], nodes.FunctionDef)
818818
self.assertEqual(inferred[0].name, "open")
819819

820-
if IS_PYPY:
821-
test_builtin_open = unittest.expectedFailure(test_builtin_open)
822-
823820
def test_callfunc_context_func(self) -> None:
824821
code = """
825822
def mirror(arg=None):

0 commit comments

Comments
 (0)