Skip to content

Commit b5ccf7d

Browse files
committed
Avoid using PythonObjectFactory.getUncached() where possible
1 parent df31acd commit b5ccf7d

20 files changed

+107
-89
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public boolean isPythonInternal() {
9797
}.getCallTarget();
9898
try {
9999
Object[] arguments = PArguments.create(1);
100-
PArguments.setGlobals(arguments, PythonObjectFactory.getUncached().createDict());
100+
PArguments.setGlobals(arguments, pythonContext.getCore().factory().createDict());
101101
PArguments.setException(arguments, PException.NO_EXCEPTION);
102102
PArguments.setArgument(arguments, 0, arg);
103103
PythonThreadState threadState = pythonContext.getThreadState(language);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -827,9 +827,9 @@ private void initializeTypes() {
827827
}
828828
}
829829
// now initialize well-known objects
830-
pyTrue = PythonObjectFactory.getUncached().createInt(PythonBuiltinClassType.Boolean, BigInteger.ONE);
831-
pyFalse = PythonObjectFactory.getUncached().createInt(PythonBuiltinClassType.Boolean, BigInteger.ZERO);
832-
pyNaN = PythonObjectFactory.getUncached().createFloat(Double.NaN);
830+
pyTrue = factory().createInt(PythonBuiltinClassType.Boolean, BigInteger.ONE);
831+
pyFalse = factory().createInt(PythonBuiltinClassType.Boolean, BigInteger.ZERO);
832+
pyNaN = factory().createFloat(Double.NaN);
833833
}
834834

835835
private void populateBuiltins() {

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
@@ -97,6 +97,7 @@
9797
import com.oracle.graal.python.runtime.exception.PException;
9898
import com.oracle.graal.python.runtime.formatting.ErrorMessageFormatter;
9999
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
100+
import com.oracle.graal.python.runtime.object.PythonObjectSlowPathFactory;
100101
import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage;
101102
import com.oracle.truffle.api.Assumption;
102103
import com.oracle.truffle.api.CompilerDirectives;
@@ -142,28 +143,28 @@ public void initialize(Python3Core core) {
142143
Object defaultaction = "default";
143144
builtinConstants.put("_defaultaction", defaultaction);
144145
builtinConstants.put(DEFAULTACTION, defaultaction);
145-
Object onceregistry = PythonObjectFactory.getUncached().createDict();
146+
Object onceregistry = core.factory().createDict();
146147
builtinConstants.put("_onceregistry", onceregistry);
147148
builtinConstants.put(ONCEREGISTRY, onceregistry);
148-
Object filters = initFilters();
149+
Object filters = initFilters(core.factory());
149150
builtinConstants.put("filters", filters);
150151
builtinConstants.put(FILTERS, filters);
151152
builtinConstants.put(FILTERS_VERSION, 0L);
152153
super.initialize(core);
153154
}
154155

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

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

169170
static final class WarningsModuleNode extends Node implements IndirectCallNode {
@@ -624,8 +625,7 @@ private static String normalizeModule(String filename) {
624625
}
625626

626627
@TruffleBoundary
627-
private static boolean updateRegistry(PythonModule _warnings, PDict registry, Object text, Object category, boolean addZero) {
628-
PythonObjectFactory factory = PythonObjectFactory.getUncached();
628+
private static boolean updateRegistry(PythonObjectSlowPathFactory factory, PythonModule _warnings, PDict registry, Object text, Object category, boolean addZero) {
629629
PTuple altKey;
630630
if (addZero) {
631631
altKey = factory.createTuple(new Object[]{text, category, 0});
@@ -791,13 +791,13 @@ private static void warnExplicitPart2(PythonContext context, Node node, PythonMo
791791
if (PString.equals("once", action)) {
792792
if (registry == null) {
793793
PDict currentRegistry = getOnceRegistry(node, context, warnings);
794-
alreadyWarned = updateRegistry(warnings, currentRegistry, text, category, false);
794+
alreadyWarned = updateRegistry(context.getCore().factory(), warnings, currentRegistry, text, category, false);
795795
} else {
796-
alreadyWarned = updateRegistry(warnings, registry, text, category, false);
796+
alreadyWarned = updateRegistry(context.getCore().factory(), warnings, registry, text, category, false);
797797
}
798798
} else if (PString.equals("module", action)) {
799799
if (registry != null) {
800-
alreadyWarned = updateRegistry(warnings, registry, text, category, false);
800+
alreadyWarned = updateRegistry(context.getCore().factory(), warnings, registry, text, category, false);
801801
}
802802
} else if (!PString.equals("default", action)) {
803803
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/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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@
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.PythonContext;
8081
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
82+
import com.oracle.graal.python.runtime.object.PythonObjectSlowPathFactory;
8183
import com.oracle.truffle.api.CompilerDirectives;
8284
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
8385
import com.oracle.truffle.api.RootCallTarget;
@@ -124,7 +126,7 @@ private static void createGetSet(PythonLanguage language, Object type, NodeFacto
124126
l -> new BuiltinFunctionRootNode(l, builtin, factory, true),
125127
factory.getNodeClass(),
126128
builtin.name());
127-
PythonObjectFactory f = PythonObjectFactory.getUncached();
129+
PythonObjectSlowPathFactory f = PythonContext.get(null).getCore().factory();
128130
int flags = PBuiltinFunction.getFlags(builtin, rawCallTarget);
129131
PBuiltinFunction getter = f.createBuiltinFunction(name, type, 1, flags, rawCallTarget);
130132
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 & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
7878
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
7979
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
80+
import com.oracle.graal.python.runtime.object.PythonObjectSlowPathFactory;
8081
import com.oracle.graal.python.util.PythonUtils;
8182
import com.oracle.truffle.api.CompilerDirectives;
8283
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
@@ -96,37 +97,36 @@ protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFa
9697
}
9798

9899
@TruffleBoundary
99-
protected static void addCVoidPFromParam(PythonLanguage language, Object type) {
100+
protected static void addCVoidPFromParam(PythonObjectSlowPathFactory factory, PythonLanguage language, Object type) {
100101
NodeFactory<CVoidPFromParamNode> rawFactory = CVoidPFromParamNodeFactory.getInstance();
101102
Builtin rawNodeBuiltin = CVoidPFromParamNode.class.getAnnotation(Builtin.class);
102-
addClassMethod(language, type, rawFactory, rawNodeBuiltin);
103+
addClassMethod(factory, language, type, rawFactory, rawNodeBuiltin);
103104
}
104105

105106
@TruffleBoundary
106-
protected static void addCCharPFromParam(PythonLanguage language, Object type) {
107+
protected static void addCCharPFromParam(PythonObjectSlowPathFactory factory, PythonLanguage language, Object type) {
107108
NodeFactory<CCharPFromParamNode> rawFactory = CCharPFromParamNodeFactory.getInstance();
108109
Builtin rawNodeBuiltin = CCharPFromParamNode.class.getAnnotation(Builtin.class);
109-
addClassMethod(language, type, rawFactory, rawNodeBuiltin);
110+
addClassMethod(factory, language, type, rawFactory, rawNodeBuiltin);
110111
}
111112

112113
@TruffleBoundary
113-
protected static void addCWCharPFromParam(PythonLanguage language, Object type) {
114+
protected static void addCWCharPFromParam(PythonObjectSlowPathFactory factory, PythonLanguage language, Object type) {
114115
NodeFactory<CWCharPFromParamNode> rawFactory = CWCharPFromParamNodeFactory.getInstance();
115116
Builtin rawNodeBuiltin = CWCharPFromParamNode.class.getAnnotation(Builtin.class);
116-
addClassMethod(language, type, rawFactory, rawNodeBuiltin);
117+
addClassMethod(factory, language, type, rawFactory, rawNodeBuiltin);
117118
}
118119

119120
@TruffleBoundary
120-
private static void addClassMethod(PythonLanguage language, Object type, NodeFactory<? extends PythonBuiltinBaseNode> factory, Builtin builtin) {
121+
private static void addClassMethod(PythonObjectSlowPathFactory pythonObjectFactory, PythonLanguage language, Object type, NodeFactory<? extends PythonBuiltinBaseNode> factory, Builtin builtin) {
121122
String name = builtin.name();
122123
Object builtinDoc = PNone.NONE;
123124
RootCallTarget callTarget = language.createCachedCallTarget(
124125
l -> new BuiltinFunctionRootNode(l, builtin, factory, true),
125126
factory.getNodeClass(),
126127
builtin.name());
127128
int flags = PBuiltinFunction.getFlags(builtin, callTarget);
128-
PythonObjectFactory f = PythonObjectFactory.getUncached();
129-
PBuiltinFunction function = f.createBuiltinFunction(builtin.name(), type, 1, flags, callTarget);
129+
PBuiltinFunction function = pythonObjectFactory.createBuiltinFunction(builtin.name(), type, 1, flags, callTarget);
130130
function.setAttribute(__DOC__, builtinDoc);
131131
WriteAttributeToObjectNode.getUncached(true).execute(type, name, function);
132132
}

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161

6262
import com.oracle.graal.python.builtins.Builtin;
6363
import com.oracle.graal.python.builtins.CoreFunctions;
64+
import com.oracle.graal.python.builtins.Python3Core;
6465
import com.oracle.graal.python.builtins.PythonBuiltins;
6566
import com.oracle.graal.python.builtins.modules.BuiltinConstructors.TypeNode;
6667
import com.oracle.graal.python.builtins.modules.BuiltinFunctions.IsInstanceNode;
@@ -89,6 +90,7 @@
8990
import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
9091
import com.oracle.graal.python.runtime.exception.PException;
9192
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
93+
import com.oracle.graal.python.runtime.object.PythonObjectSlowPathFactory;
9294
import com.oracle.graal.python.util.PythonUtils;
9395
import com.oracle.truffle.api.dsl.Cached;
9496
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
@@ -185,18 +187,20 @@ Object PyCSimpleType_new(VirtualFrame frame, Object type, Object[] args, PKeywor
185187
* Install from_param class methods in ctypes base classes. Overrides the
186188
* PyCSimpleType_from_param generic method.
187189
*/
188-
if (getBaseClassNode.execute(result) == getCore().lookupType(SimpleCData)) {
190+
Python3Core core = getCore();
191+
PythonObjectSlowPathFactory factory = core.factory();
192+
if (getBaseClassNode.execute(result) == core.lookupType(SimpleCData)) {
189193
switch (proto_str) {
190194
case "z": /* c_char_p */
191-
LazyPyCSimpleTypeBuiltins.addCCharPFromParam(getLanguage(), result);
195+
LazyPyCSimpleTypeBuiltins.addCCharPFromParam(factory, getLanguage(), result);
192196
stgdict.flags |= TYPEFLAG_ISPOINTER;
193197
break;
194198
case "Z": /* c_wchar_p */
195-
LazyPyCSimpleTypeBuiltins.addCWCharPFromParam(getLanguage(), result);
199+
LazyPyCSimpleTypeBuiltins.addCWCharPFromParam(factory, getLanguage(), result);
196200
stgdict.flags |= TYPEFLAG_ISPOINTER;
197201
break;
198202
case "P": /* c_void_p */
199-
LazyPyCSimpleTypeBuiltins.addCVoidPFromParam(getLanguage(), result);
203+
LazyPyCSimpleTypeBuiltins.addCVoidPFromParam(factory, getLanguage(), result);
200204
stgdict.flags |= TYPEFLAG_ISPOINTER;
201205
break;
202206
case "s":

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/ExternalFunctionNodes.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,16 +1583,6 @@ protected void postprocessCArguments(VirtualFrame frame, Object[] cArguments) {
15831583
public Signature getSignature() {
15841584
return SIGNATURE;
15851585
}
1586-
1587-
@TruffleBoundary
1588-
public static PBuiltinFunction createFunction(PythonLanguage lang, Object owner, String propertyName, Object target, Object closure) {
1589-
RootCallTarget rootCallTarget = PExternalFunctionWrapper.getOrCreateCallTarget(PExternalFunctionWrapper.GETTER, lang, propertyName, true);
1590-
if (rootCallTarget == null) {
1591-
throw CompilerDirectives.shouldNotReachHere("Calling non-native get descriptor functions is not support");
1592-
}
1593-
PythonObjectFactory factory = PythonObjectFactory.getUncached();
1594-
return factory.createGetSetBuiltinFunction(propertyName, owner, PythonUtils.EMPTY_OBJECT_ARRAY, ExternalFunctionNodes.createKwDefaults(target, closure), rootCallTarget);
1595-
}
15961586
}
15971587

15981588
/**

0 commit comments

Comments
 (0)