Skip to content

Commit 0fb7dc5

Browse files
committed
ModuleBuiltins: move implementation of __repr__ to _frozen_importlib (delegate to importlib._bootstrap._module_repr)
1 parent a3efc7c commit 0fb7dc5

File tree

3 files changed

+11
-37
lines changed

3 files changed

+11
-37
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/module/ModuleBuiltins.java

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,10 @@
4848
import static com.oracle.graal.python.nodes.SpecialMethodNames.__GETATTRIBUTE__;
4949
import static com.oracle.graal.python.nodes.SpecialMethodNames.__GETATTR__;
5050
import static com.oracle.graal.python.nodes.SpecialMethodNames.__INIT__;
51-
import static com.oracle.graal.python.nodes.SpecialMethodNames.__REPR__;
5251
import static com.oracle.graal.python.runtime.exception.PythonErrorType.AttributeError;
5352

5453
import java.util.List;
5554

56-
import com.oracle.graal.python.PythonLanguage;
5755
import com.oracle.graal.python.builtins.Builtin;
5856
import com.oracle.graal.python.builtins.CoreFunctions;
5957
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
@@ -68,26 +66,17 @@
6866
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
6967
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
7068
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
71-
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
7269
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
7370
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
7471
import com.oracle.graal.python.nodes.util.CannotCastException;
7572
import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
76-
import com.oracle.graal.python.runtime.PythonContext;
7773
import com.oracle.graal.python.runtime.exception.PException;
7874
import com.oracle.truffle.api.dsl.Cached;
79-
import com.oracle.truffle.api.dsl.CachedContext;
8075
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
8176
import com.oracle.truffle.api.dsl.NodeFactory;
8277
import com.oracle.truffle.api.dsl.Specialization;
8378
import com.oracle.truffle.api.dsl.TypeSystemReference;
8479
import com.oracle.truffle.api.frame.VirtualFrame;
85-
import com.oracle.truffle.api.interop.ArityException;
86-
import com.oracle.truffle.api.interop.InteropLibrary;
87-
import com.oracle.truffle.api.interop.UnknownIdentifierException;
88-
import com.oracle.truffle.api.interop.UnsupportedMessageException;
89-
import com.oracle.truffle.api.interop.UnsupportedTypeException;
90-
import com.oracle.truffle.api.library.CachedLibrary;
9180
import com.oracle.truffle.api.profiles.ConditionProfile;
9281

9382
@CoreFunctions(extendClasses = PythonBuiltinClassType.PythonModule)
@@ -98,31 +87,6 @@ protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFa
9887
return ModuleBuiltinsFactory.getFactories();
9988
}
10089

101-
@Builtin(name = __REPR__, minNumOfPositionalArgs = 1, declaresExplicitSelf = true)
102-
@GenerateNodeFactory
103-
public abstract static class ModuleReprNode extends PythonUnaryBuiltinNode {
104-
@Specialization
105-
public Object repr(PythonModule self,
106-
@CachedLibrary(limit = "1") InteropLibrary lib,
107-
@CachedContext(PythonLanguage.class) PythonContext context) {
108-
// PyObject_CallMethod(interp->importlib, "_module_repr", "O", m);
109-
PythonModule builtins = context.getCore().getBuiltins();
110-
try {
111-
Object __import__ = lib.readMember(builtins, "__import__");
112-
Object module_repr = importFrom(__import__, "importlib._bootstrap", "_module_repr", lib);
113-
return lib.execute(module_repr, self);
114-
} catch (UnknownIdentifierException | UnsupportedMessageException | UnsupportedTypeException | ArityException e) {
115-
return self.toString();
116-
}
117-
}
118-
119-
private Object importFrom(Object importBuiltinFunction, String moduleName, String from, InteropLibrary lib)
120-
throws UnsupportedTypeException, ArityException, UnsupportedMessageException, UnknownIdentifierException {
121-
Object _bootstrap = lib.execute(importBuiltinFunction, moduleName, PNone.NONE, PNone.NONE, factory().createList(new Object[]{from}));
122-
return lib.readMember(_bootstrap, from);
123-
}
124-
}
125-
12690
@Builtin(name = __INIT__, minNumOfPositionalArgs = 2, declaresExplicitSelf = true, parameterNames = {"self", "name", "doc"})
12791
@GenerateNodeFactory
12892
@TypeSystemReference(PythonArithmeticTypes.class)

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/str/StringUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0

graalpython/lib-graalpython/_frozen_importlib.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,13 @@ def load(suffix=""):
5757
# Insert our meta finder for caching
5858
_imp.CachedImportFinder.ModuleSpec = importlib.ModuleSpec
5959
sys.meta_path.insert(0, _imp.CachedImportFinder)
60+
61+
ModuleType = type(sys)
62+
63+
64+
def _repr(module):
65+
from importlib import _bootstrap
66+
return _bootstrap._module_repr(module)
67+
68+
69+
ModuleType.__repr__ = _repr

0 commit comments

Comments
 (0)