Skip to content

Commit ed778a2

Browse files
committed
[GR-34524] Use PythonObjectSlowPathFactory as Python3Core.factory and share it everywhere.
PullRequest: graalpython/1999
2 parents cb2ed0b + 08d281a commit ed778a2

24 files changed

+153
-121
lines changed

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/builtins/modules/ConversionNodeTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
import com.oracle.graal.python.runtime.PythonContext;
5757
import com.oracle.graal.python.runtime.PythonContext.PythonThreadState;
5858
import com.oracle.graal.python.runtime.exception.PException;
59-
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
6059
import com.oracle.truffle.api.RootCallTarget;
6160
import com.oracle.truffle.api.frame.VirtualFrame;
6261

@@ -97,7 +96,7 @@ public boolean isPythonInternal() {
9796
}.getCallTarget();
9897
try {
9998
Object[] arguments = PArguments.create(1);
100-
PArguments.setGlobals(arguments, PythonObjectFactory.getUncached().createDict());
99+
PArguments.setGlobals(arguments, pythonContext.getCore().factory().createDict());
101100
PArguments.setException(arguments, PException.NO_EXCEPTION);
102101
PArguments.setArgument(arguments, 0, arg);
103102
PythonThreadState threadState = pythonContext.getThreadState(language);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/Python3Core.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import java.util.regex.Matcher;
4545
import java.util.regex.Pattern;
4646

47+
import com.oracle.graal.python.runtime.object.PythonObjectSlowPathFactory;
4748
import org.graalvm.nativeimage.ImageInfo;
4849

4950
import com.oracle.graal.python.PythonLanguage;
@@ -264,7 +265,6 @@
264265
import com.oracle.graal.python.runtime.exception.PException;
265266
import com.oracle.graal.python.runtime.formatting.ErrorMessageFormatter;
266267
import com.oracle.graal.python.runtime.interop.PythonMapScope;
267-
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
268268
import com.oracle.graal.python.util.PythonUtils;
269269
import com.oracle.graal.python.util.Supplier;
270270
import com.oracle.truffle.api.CallTarget;
@@ -613,7 +613,7 @@ private static PythonBuiltins[] initializeBuiltins(boolean nativeAccessAllowed)
613613
*/
614614
private volatile boolean initialized;
615615

616-
private final PythonObjectFactory objectFactory = PythonObjectFactory.getUncached();
616+
private PythonObjectSlowPathFactory objectFactory;
617617

618618
public Python3Core(PythonParser parser, boolean isNativeSupportAllowed) {
619619
this.parser = parser;
@@ -650,6 +650,7 @@ public boolean isInitialized() {
650650
*/
651651
public void initialize(PythonContext context) {
652652
singletonContext = context;
653+
objectFactory = new PythonObjectSlowPathFactory(context.getAllocationReporter());
653654
initializeJavaCore();
654655
initializePython3Core(context.getCoreHomeOrFail());
655656
assert SpecialMethodSlot.checkSlotOverrides(this);
@@ -817,9 +818,9 @@ private void initializeTypes() {
817818
}
818819
}
819820
// now initialize well-known objects
820-
pyTrue = PythonObjectFactory.getUncached().createInt(PythonBuiltinClassType.Boolean, BigInteger.ONE);
821-
pyFalse = PythonObjectFactory.getUncached().createInt(PythonBuiltinClassType.Boolean, BigInteger.ZERO);
822-
pyNaN = PythonObjectFactory.getUncached().createFloat(Double.NaN);
821+
pyTrue = factory().createInt(PythonBuiltinClassType.Boolean, BigInteger.ONE);
822+
pyFalse = factory().createInt(PythonBuiltinClassType.Boolean, BigInteger.ZERO);
823+
pyNaN = factory().createFloat(Double.NaN);
823824
}
824825

825826
private void populateBuiltins() {
@@ -932,7 +933,7 @@ private void loadFile(String s, String prefix) {
932933
GenericInvokeNode.getUncached().execute(callTarget, PArguments.withGlobals(mod));
933934
}
934935

935-
public PythonObjectFactory factory() {
936+
public PythonObjectSlowPathFactory factory() {
936937
return objectFactory;
937938
}
938939

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/GraalHPyDebugModuleBuiltins.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ private static Object[] getOpenDebugHandles(GraalHPyDebugContext debugContext, i
145145
int n = openHandles.size();
146146
openHandles.sort(Comparator.comparingLong(debugContext::getDebugHandleInfo));
147147
Object[] result = new Object[n];
148-
PythonObjectFactory factory = PythonObjectFactory.getUncached();
148+
PythonObjectFactory factory = debugContext.getContext().getCore().factory();
149149
for (int i = 0; i < n; i++) {
150150
result[i] = factory.createDebugHandle(openHandles.get(i));
151151
}
@@ -168,7 +168,7 @@ private static Object[] getClosedDebugHandles(GraalHPyDebugContext debugContext)
168168
Queue<GraalHPyHandle> openHandles = debugContext.getClosedHandles();
169169
int n = openHandles.size();
170170
Object[] result = new Object[n];
171-
PythonObjectFactory factory = PythonObjectFactory.getUncached();
171+
PythonObjectFactory factory = debugContext.getContext().getCore().factory();
172172
int i = 0;
173173
for (GraalHPyHandle handle : openHandles) {
174174
result[i++] = factory.createDebugHandle(handle);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/SSLModuleBuiltins.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ public void postInitialize(Python3Core core) {
202202
super.postInitialize(core);
203203
loadDefaults();
204204
PythonModule module = core.lookupBuiltinModule("_ssl");
205-
PythonObjectFactory factory = PythonObjectFactory.getUncached();
205+
PythonObjectFactory factory = core.factory();
206206
module.setAttribute("OPENSSL_VERSION_NUMBER", 0);
207207
PTuple versionInfo = factory.createTuple(new int[]{0, 0, 0, 0, 0});
208208
module.setAttribute("OPENSSL_VERSION_INFO", versionInfo);
@@ -379,7 +379,7 @@ private Object decode(TruffleFile file) throws PException {
379379
if (!(cert instanceof X509Certificate)) {
380380
throw raise(SSLError, "Error decoding PEM-encoded file: unexpected type " + cert.getClass().getName());
381381
}
382-
return CertUtils.decodeCertificate(this, (X509Certificate) l.get(0));
382+
return CertUtils.decodeCertificate(this, getContext().getCore().factory(), (X509Certificate) l.get(0));
383383
} catch (IOException ex) {
384384
throw raise(SSLError, "Can't open file: " + ex.toString());
385385
} catch (CertificateException | CRLException ex) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/WarningsModuleBuiltins.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
import com.oracle.graal.python.runtime.exception.PException;
9999
import com.oracle.graal.python.runtime.formatting.ErrorMessageFormatter;
100100
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
101+
import com.oracle.graal.python.runtime.object.PythonObjectSlowPathFactory;
101102
import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage;
102103
import com.oracle.truffle.api.Assumption;
103104
import com.oracle.truffle.api.CompilerDirectives;
@@ -143,28 +144,28 @@ public void initialize(Python3Core core) {
143144
Object defaultaction = "default";
144145
builtinConstants.put("_defaultaction", defaultaction);
145146
builtinConstants.put(DEFAULTACTION, defaultaction);
146-
Object onceregistry = PythonObjectFactory.getUncached().createDict();
147+
Object onceregistry = core.factory().createDict();
147148
builtinConstants.put("_onceregistry", onceregistry);
148149
builtinConstants.put(ONCEREGISTRY, onceregistry);
149-
Object filters = initFilters();
150+
Object filters = initFilters(core.factory());
150151
builtinConstants.put("filters", filters);
151152
builtinConstants.put(FILTERS, filters);
152153
builtinConstants.put(FILTERS_VERSION, 0L);
153154
super.initialize(core);
154155
}
155156

156-
private static PTuple createFilter(PythonBuiltinClassType cat, String id, Object mod) {
157-
return PythonObjectFactory.getUncached().createTuple(new Object[]{id, PNone.NONE, cat, mod, 0});
157+
private static PTuple createFilter(PythonObjectSlowPathFactory factory, PythonBuiltinClassType cat, String id, Object mod) {
158+
return factory.createTuple(new Object[]{id, PNone.NONE, cat, mod, 0});
158159
}
159160

160161
// init_filters
161-
private static PList initFilters() {
162-
return PythonObjectFactory.getUncached().createList(new Object[]{
163-
createFilter(PythonBuiltinClassType.DeprecationWarning, "default", "__main__"),
164-
createFilter(PythonBuiltinClassType.DeprecationWarning, "ignore", PNone.NONE),
165-
createFilter(PythonBuiltinClassType.PendingDeprecationWarning, "ignore", PNone.NONE),
166-
createFilter(PythonBuiltinClassType.ImportWarning, "ignore", PNone.NONE),
167-
createFilter(PythonBuiltinClassType.ResourceWarning, "ignore", PNone.NONE)});
162+
private static PList initFilters(PythonObjectSlowPathFactory factory) {
163+
return factory.createList(new Object[]{
164+
createFilter(factory, PythonBuiltinClassType.DeprecationWarning, "default", "__main__"),
165+
createFilter(factory, PythonBuiltinClassType.DeprecationWarning, "ignore", PNone.NONE),
166+
createFilter(factory, PythonBuiltinClassType.PendingDeprecationWarning, "ignore", PNone.NONE),
167+
createFilter(factory, PythonBuiltinClassType.ImportWarning, "ignore", PNone.NONE),
168+
createFilter(factory, PythonBuiltinClassType.ResourceWarning, "ignore", PNone.NONE)});
168169
}
169170

170171
static final class WarningsModuleNode extends Node implements IndirectCallNode {
@@ -630,8 +631,7 @@ private static String normalizeModule(String filename) {
630631
}
631632

632633
@TruffleBoundary
633-
private static boolean updateRegistry(PythonModule _warnings, PDict registry, Object text, Object category, boolean addZero) {
634-
PythonObjectFactory factory = PythonObjectFactory.getUncached();
634+
private static boolean updateRegistry(PythonObjectSlowPathFactory factory, PythonModule _warnings, PDict registry, Object text, Object category, boolean addZero) {
635635
PTuple altKey;
636636
if (addZero) {
637637
altKey = factory.createTuple(new Object[]{text, category, 0});
@@ -797,13 +797,13 @@ private static void warnExplicitPart2(PythonContext context, Node node, PythonMo
797797
if (PString.equals("once", action)) {
798798
if (registry == null) {
799799
PDict currentRegistry = getOnceRegistry(node, context, warnings);
800-
alreadyWarned = updateRegistry(warnings, currentRegistry, text, category, false);
800+
alreadyWarned = updateRegistry(context.getCore().factory(), warnings, currentRegistry, text, category, false);
801801
} else {
802-
alreadyWarned = updateRegistry(warnings, registry, text, category, false);
802+
alreadyWarned = updateRegistry(context.getCore().factory(), warnings, registry, text, category, false);
803803
}
804804
} else if (PString.equals("module", action)) {
805805
if (registry != null) {
806-
alreadyWarned = updateRegistry(warnings, registry, text, category, false);
806+
alreadyWarned = updateRegistry(context.getCore().factory(), warnings, registry, text, category, false);
807807
}
808808
} else if (!PString.equals("default", action)) {
809809
PRaiseNode.raiseUncached(node, PythonBuiltinClassType.RuntimeError, "Unrecognized action (%s) in warnings.filters:\n %s", action,

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/ast/AstModuleBuiltins.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
import com.oracle.graal.python.builtins.objects.type.PythonBuiltinClass;
5454
import com.oracle.graal.python.builtins.objects.type.PythonClass;
5555
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
56-
import com.oracle.graal.python.runtime.object.PythonObjectSlowPathFactory;
56+
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
5757
import com.oracle.truffle.api.dsl.NodeFactory;
5858

5959
@CoreFunctions(defineModule = "_ast")
@@ -80,7 +80,7 @@ public void postInitialize(Python3Core core) {
8080
* writable class fields.
8181
*/
8282
PythonLanguage language = core.getLanguage();
83-
PythonObjectSlowPathFactory factory = new PythonObjectSlowPathFactory(core.getContext().getAllocationReporter());
83+
PythonObjectFactory factory = core.factory();
8484
PythonClass mod_type = makeType(language, factory, astModule, "mod", astType);
8585
PythonClass Module_type = makeType(language, factory, astModule, "Module", mod_type);
8686
PythonClass Interactive_type = makeType(language, factory, astModule, "Interactive", mod_type);
@@ -196,7 +196,7 @@ public void postInitialize(Python3Core core) {
196196
PythonClass TypeIgnore_type = makeType(language, factory, astModule, "TypeIgnore", type_ignore_type);
197197
}
198198

199-
private static PythonClass makeType(PythonLanguage language, PythonObjectSlowPathFactory factory, PythonModule astModule, String name, PythonAbstractClass base) {
199+
private static PythonClass makeType(PythonLanguage language, PythonObjectFactory factory, PythonModule astModule, String name, PythonAbstractClass base) {
200200
PythonClass newType = factory.createPythonClassAndFixupSlots(language, PythonBuiltinClassType.PythonClass, name, new PythonAbstractClass[]{base});
201201
astModule.setAttribute(name, newType);
202202
return newType;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/ctypes/CtypesModuleBuiltins.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFa
205205
@Override
206206
public void initialize(Python3Core core) {
207207
super.initialize(core);
208-
builtinConstants.put("_pointer_type_cache", PythonObjectFactory.getUncached().createDict());
208+
builtinConstants.put("_pointer_type_cache", core.factory().createDict());
209209
builtinConstants.put("FUNCFLAG_CDECL", FUNCFLAG_CDECL);
210210
builtinConstants.put("FUNCFLAG_USE_ERRNO", FUNCFLAG_USE_ERRNO);
211211
builtinConstants.put("FUNCFLAG_USE_LASTERROR", FUNCFLAG_USE_LASTERROR);
@@ -218,7 +218,7 @@ public void initialize(Python3Core core) {
218218
@Override
219219
public void postInitialize(Python3Core core) {
220220
super.postInitialize(core);
221-
PythonObjectFactory factory = PythonObjectFactory.getUncached();
221+
PythonObjectFactory factory = core.factory();
222222
PythonModule ctypesModule = core.lookupBuiltinModule("_ctypes");
223223
ctypesModule.setAttribute("_string_at_addr", factory.createNativeVoidPtr(StringAtFunction.create()));
224224
ctypesModule.setAttribute("_cast_addr", factory.createNativeVoidPtr(CastFunction.create()));

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/ctypes/LazyPyCArrayTypeBuiltins.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@
7777
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
7878
import com.oracle.graal.python.nodes.object.GetClassNode;
7979
import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
80-
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
80+
import com.oracle.graal.python.runtime.PythonContext;
81+
import com.oracle.graal.python.runtime.object.PythonObjectSlowPathFactory;
8182
import com.oracle.truffle.api.CompilerDirectives;
8283
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
8384
import com.oracle.truffle.api.RootCallTarget;
@@ -124,7 +125,7 @@ private static void createGetSet(PythonLanguage language, Object type, NodeFacto
124125
l -> new BuiltinFunctionRootNode(l, builtin, factory, true),
125126
factory.getNodeClass(),
126127
builtin.name());
127-
PythonObjectFactory f = PythonObjectFactory.getUncached();
128+
PythonObjectSlowPathFactory f = PythonContext.get(null).getCore().factory();
128129
int flags = PBuiltinFunction.getFlags(builtin, rawCallTarget);
129130
PBuiltinFunction getter = f.createBuiltinFunction(name, type, 1, flags, rawCallTarget);
130131
GetSetDescriptor callable = f.createGetSetDescriptor(getter, getter, name, type, false);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/ctypes/LazyPyCSimpleTypeBuiltins.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
import com.oracle.graal.python.nodes.function.BuiltinFunctionRootNode;
7777
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
7878
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
79-
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
79+
import com.oracle.graal.python.runtime.object.PythonObjectSlowPathFactory;
8080
import com.oracle.graal.python.util.PythonUtils;
8181
import com.oracle.truffle.api.CompilerDirectives;
8282
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
@@ -96,37 +96,36 @@ protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFa
9696
}
9797

9898
@TruffleBoundary
99-
protected static void addCVoidPFromParam(PythonLanguage language, Object type) {
99+
protected static void addCVoidPFromParam(PythonObjectSlowPathFactory factory, PythonLanguage language, Object type) {
100100
NodeFactory<CVoidPFromParamNode> rawFactory = CVoidPFromParamNodeFactory.getInstance();
101101
Builtin rawNodeBuiltin = CVoidPFromParamNode.class.getAnnotation(Builtin.class);
102-
addClassMethod(language, type, rawFactory, rawNodeBuiltin);
102+
addClassMethod(factory, language, type, rawFactory, rawNodeBuiltin);
103103
}
104104

105105
@TruffleBoundary
106-
protected static void addCCharPFromParam(PythonLanguage language, Object type) {
106+
protected static void addCCharPFromParam(PythonObjectSlowPathFactory factory, PythonLanguage language, Object type) {
107107
NodeFactory<CCharPFromParamNode> rawFactory = CCharPFromParamNodeFactory.getInstance();
108108
Builtin rawNodeBuiltin = CCharPFromParamNode.class.getAnnotation(Builtin.class);
109-
addClassMethod(language, type, rawFactory, rawNodeBuiltin);
109+
addClassMethod(factory, language, type, rawFactory, rawNodeBuiltin);
110110
}
111111

112112
@TruffleBoundary
113-
protected static void addCWCharPFromParam(PythonLanguage language, Object type) {
113+
protected static void addCWCharPFromParam(PythonObjectSlowPathFactory factory, PythonLanguage language, Object type) {
114114
NodeFactory<CWCharPFromParamNode> rawFactory = CWCharPFromParamNodeFactory.getInstance();
115115
Builtin rawNodeBuiltin = CWCharPFromParamNode.class.getAnnotation(Builtin.class);
116-
addClassMethod(language, type, rawFactory, rawNodeBuiltin);
116+
addClassMethod(factory, language, type, rawFactory, rawNodeBuiltin);
117117
}
118118

119119
@TruffleBoundary
120-
private static void addClassMethod(PythonLanguage language, Object type, NodeFactory<? extends PythonBuiltinBaseNode> factory, Builtin builtin) {
120+
private static void addClassMethod(PythonObjectSlowPathFactory pythonObjectFactory, PythonLanguage language, Object type, NodeFactory<? extends PythonBuiltinBaseNode> factory, Builtin builtin) {
121121
String name = builtin.name();
122122
Object builtinDoc = PNone.NONE;
123123
RootCallTarget callTarget = language.createCachedCallTarget(
124124
l -> new BuiltinFunctionRootNode(l, builtin, factory, true),
125125
factory.getNodeClass(),
126126
builtin.name());
127127
int flags = PBuiltinFunction.getFlags(builtin, callTarget);
128-
PythonObjectFactory f = PythonObjectFactory.getUncached();
129-
PBuiltinFunction function = f.createBuiltinFunction(builtin.name(), type, 1, flags, callTarget);
128+
PBuiltinFunction function = pythonObjectFactory.createBuiltinFunction(builtin.name(), type, 1, flags, callTarget);
130129
function.setAttribute(__DOC__, builtinDoc);
131130
WriteAttributeToObjectNode.getUncached(true).execute(type, name, function);
132131
}

0 commit comments

Comments
 (0)