Skip to content

Commit

Permalink
Improve recognition of _io module during bootstrapping on PyPy (
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielNoord authored and Pierre-Sassoulas committed May 2, 2022
1 parent 2e383f3 commit 2b19658
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 20 deletions.
25 changes: 9 additions & 16 deletions astroid/raw_building.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,6 @@
TYPE_ELLIPSIS = type(...)


def _io_discrepancy(member):
# _io module names itself `io`: http://bugs.python.org/issue18602
member_self = getattr(member, "__self__", None)
return (
member_self
and inspect.ismodule(member_self)
and member_self.__name__ == "_io"
and member.__module__ == "io"
)


def _attach_local_node(parent, node, name):
node.name = name # needed by add_local_node
parent.add_local_node(node)
Expand Down Expand Up @@ -343,9 +332,7 @@ def object_build(self, node, obj):
if inspect.isfunction(member):
_build_from_function(node, name, member, self._module)
elif inspect.isbuiltin(member):
if not _io_discrepancy(member) and self.imported_member(
node, member, name
):
if self.imported_member(node, member, name):
continue
object_build_methoddescriptor(node, member, name)
elif inspect.isclass(member):
Expand Down Expand Up @@ -383,7 +370,7 @@ def object_build(self, node, obj):
attach_dummy_node(node, name, member)
return None

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

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

real_name = {"gtk": "gtk_gtk"}.get(modname, modname)

if real_name != self._module.__name__:
# check if it sounds valid and then add an import node, else use a
Expand Down
5 changes: 1 addition & 4 deletions tests/unittest_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from astroid.arguments import CallSite
from astroid.bases import BoundMethod, Instance, UnboundMethod
from astroid.builder import AstroidBuilder, extract_node, parse
from astroid.const import IS_PYPY, PY38_PLUS, PY39_PLUS
from astroid.const import PY38_PLUS, PY39_PLUS
from astroid.context import InferenceContext
from astroid.exceptions import (
AstroidTypeError,
Expand Down Expand Up @@ -817,9 +817,6 @@ def test_builtin_open(self) -> None:
self.assertIsInstance(inferred[0], nodes.FunctionDef)
self.assertEqual(inferred[0].name, "open")

if IS_PYPY:
test_builtin_open = unittest.expectedFailure(test_builtin_open)

def test_callfunc_context_func(self) -> None:
code = """
def mirror(arg=None):
Expand Down

0 comments on commit 2b19658

Please sign in to comment.