Skip to content

Commit c030c60

Browse files
committed
Store internal module attributes in a field of PythonModule instead of HiddenAttr
1 parent e26fa1e commit c030c60

File tree

13 files changed

+127
-199
lines changed

13 files changed

+127
-199
lines changed

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

Lines changed: 24 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242

4343
import static com.oracle.graal.python.nodes.BuiltinNames.J_READLINE;
4444
import static com.oracle.graal.python.nodes.BuiltinNames.T_READLINE;
45-
import static com.oracle.graal.python.nodes.HiddenAttr.DATA;
4645
import static com.oracle.graal.python.util.PythonUtils.toTruffleStringUncached;
4746

4847
import java.io.BufferedReader;
@@ -62,7 +61,6 @@
6261
import com.oracle.graal.python.builtins.objects.module.PythonModule;
6362
import com.oracle.graal.python.builtins.objects.str.PString;
6463
import com.oracle.graal.python.nodes.ErrorMessages;
65-
import com.oracle.graal.python.nodes.HiddenAttr;
6664
import com.oracle.graal.python.nodes.PRaiseNode;
6765
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
6866
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
@@ -100,17 +98,15 @@ private static final class LocalData {
10098
@Override
10199
public void postInitialize(Python3Core core) {
102100
super.postInitialize(core);
103-
HiddenAttr.WriteNode.executeUncached(core.lookupBuiltinModule(T_READLINE), DATA, new LocalData());
101+
core.lookupBuiltinModule(T_READLINE).setInternalAttributes(new LocalData());
104102
}
105103

106104
@Builtin(name = "get_completer", minNumOfPositionalArgs = 1, declaresExplicitSelf = true)
107105
@GenerateNodeFactory
108106
abstract static class GetCompleterNode extends PythonUnaryBuiltinNode {
109107
@Specialization
110-
static Object getCompleter(PythonModule self,
111-
@Bind("this") Node inliningTarget,
112-
@Cached HiddenAttr.ReadNode readNode) {
113-
LocalData data = (LocalData) readNode.execute(inliningTarget, self, DATA, null);
108+
static Object getCompleter(PythonModule self) {
109+
LocalData data = self.getInternalAttributes();
114110
if (data.completer != null) {
115111
return data.completer;
116112
} else {
@@ -123,10 +119,8 @@ static Object getCompleter(PythonModule self,
123119
@GenerateNodeFactory
124120
abstract static class SetCompleterNode extends PythonBinaryBuiltinNode {
125121
@Specialization
126-
PNone setCompleter(PythonModule self, Object callable,
127-
@Bind("this") Node inliningTarget,
128-
@Cached HiddenAttr.ReadNode readNode) {
129-
LocalData data = (LocalData) readNode.execute(inliningTarget, self, DATA, null);
122+
PNone setCompleter(PythonModule self, Object callable) {
123+
LocalData data = self.getInternalAttributes();
130124
data.completer = callable;
131125
return PNone.NONE;
132126
}
@@ -140,7 +134,7 @@ abstract static class ParseAndBindNode extends PythonBinaryBuiltinNode {
140134
PNone setCompleter(PythonModule self, TruffleString tspec) {
141135
String spec = tspec.toJavaStringUncached();
142136
if (spec.startsWith("tab:")) {
143-
LocalData data = (LocalData) HiddenAttr.ReadNode.executeUncached(self, DATA, null);
137+
LocalData data = self.getInternalAttributes();
144138
data.bindings.put("tab", spec.split(":")[1].trim());
145139
return PNone.NONE;
146140
} else {
@@ -165,7 +159,7 @@ abstract static class GetHistoryLengthNode extends PythonUnaryBuiltinNode {
165159
@Specialization
166160
@TruffleBoundary
167161
int setCompleter(PythonModule self) {
168-
LocalData data = (LocalData) HiddenAttr.ReadNode.executeUncached(self, DATA, null);
162+
LocalData data = self.getInternalAttributes();
169163
return data.history.size();
170164
}
171165
}
@@ -176,7 +170,7 @@ abstract static class SetHistoryLengthNode extends PythonBinaryBuiltinNode {
176170
@Specialization
177171
@TruffleBoundary
178172
TruffleString setCompleter(PythonModule self, int index) {
179-
LocalData data = (LocalData) HiddenAttr.ReadNode.executeUncached(self, DATA, null);
173+
LocalData data = self.getInternalAttributes();
180174
try {
181175
return data.history.get(index);
182176
} catch (IndexOutOfBoundsException e) {
@@ -198,7 +192,7 @@ TruffleString setCompleter(PythonModule self, int index, PString string,
198192
@Specialization
199193
@TruffleBoundary
200194
TruffleString setCompleter(PythonModule self, int index, TruffleString string) {
201-
LocalData data = (LocalData) HiddenAttr.ReadNode.executeUncached(self, DATA, null);
195+
LocalData data = self.getInternalAttributes();
202196
try {
203197
return data.history.set(index, string);
204198
} catch (IndexOutOfBoundsException e) {
@@ -213,7 +207,7 @@ abstract static class DeleteItemNode extends PythonBinaryBuiltinNode {
213207
@Specialization
214208
@TruffleBoundary
215209
TruffleString setCompleter(PythonModule self, int index) {
216-
LocalData data = (LocalData) HiddenAttr.ReadNode.executeUncached(self, DATA, null);
210+
LocalData data = self.getInternalAttributes();
217211
try {
218212
return data.history.remove(index);
219213
} catch (IndexOutOfBoundsException e) {
@@ -235,7 +229,7 @@ static PNone addHistory(PythonModule self, PString item,
235229
@Specialization
236230
@TruffleBoundary
237231
static PNone addHistory(PythonModule self, TruffleString item) {
238-
LocalData data = (LocalData) HiddenAttr.ReadNode.executeUncached(self, DATA, null);
232+
LocalData data = self.getInternalAttributes();
239233
data.history.add(item);
240234
return PNone.NONE;
241235
}
@@ -255,7 +249,7 @@ PNone setCompleter(PythonModule self, PString path,
255249
@TruffleBoundary
256250
@SuppressWarnings("try")
257251
PNone setCompleter(PythonModule self, TruffleString path) {
258-
LocalData data = (LocalData) HiddenAttr.ReadNode.executeUncached(self, DATA, null);
252+
LocalData data = self.getInternalAttributes();
259253
try (GilNode.UncachedRelease gil = GilNode.uncachedRelease()) {
260254
BufferedReader reader = getContext().getEnv().getPublicTruffleFile(path.toJavaStringUncached()).newBufferedReader();
261255
String line;
@@ -283,7 +277,7 @@ PNone setCompleter(PythonModule self, PString path,
283277
@Specialization
284278
@TruffleBoundary
285279
PNone setCompleter(PythonModule self, TruffleString path) {
286-
LocalData data = (LocalData) HiddenAttr.ReadNode.executeUncached(self, DATA, null);
280+
LocalData data = self.getInternalAttributes();
287281
try {
288282
BufferedWriter writer = getContext().getEnv().getPublicTruffleFile(path.toJavaStringUncached()).newBufferedWriter(StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
289283
for (TruffleString l : data.history) {
@@ -304,7 +298,7 @@ abstract static class ClearNode extends PythonUnaryBuiltinNode {
304298
@Specialization
305299
@TruffleBoundary
306300
static PNone setCompleter(PythonModule self) {
307-
LocalData data = (LocalData) HiddenAttr.ReadNode.executeUncached(self, DATA, null);
301+
LocalData data = self.getInternalAttributes();
308302
data.history.clear();
309303
return PNone.NONE;
310304
}
@@ -314,7 +308,7 @@ static PNone setCompleter(PythonModule self) {
314308
@GenerateNodeFactory
315309
abstract static class InsertTextNode extends PythonUnaryBuiltinNode {
316310
@Specialization
317-
PNone setCompleter(@SuppressWarnings("unused") Object text) {
311+
static PNone setCompleter(@SuppressWarnings("unused") Object text) {
318312
return PNone.NONE;
319313
}
320314
}
@@ -323,7 +317,7 @@ PNone setCompleter(@SuppressWarnings("unused") Object text) {
323317
@GenerateNodeFactory
324318
abstract static class RedisplayNode extends PythonBuiltinNode {
325319
@Specialization
326-
PNone setCompleter() {
320+
static PNone setCompleter() {
327321
return PNone.NONE;
328322
}
329323
}
@@ -332,10 +326,8 @@ PNone setCompleter() {
332326
@GenerateNodeFactory
333327
abstract static class GetAutoHistoryNode extends PythonUnaryBuiltinNode {
334328
@Specialization
335-
boolean setCompleter(PythonModule self,
336-
@Bind("this") Node inliningTarget,
337-
@Cached HiddenAttr.ReadNode readNode) {
338-
LocalData data = (LocalData) readNode.execute(inliningTarget, self, DATA, null);
329+
static boolean setCompleter(PythonModule self) {
330+
LocalData data = self.getInternalAttributes();
339331
return data.autoHistory;
340332
}
341333
}
@@ -344,10 +336,8 @@ boolean setCompleter(PythonModule self,
344336
@GenerateNodeFactory
345337
abstract static class SetAutoHistoryNode extends PythonBinaryBuiltinNode {
346338
@Specialization
347-
PNone setCompleter(PythonModule self, boolean enabled,
348-
@Bind("this") Node inliningTarget,
349-
@Cached HiddenAttr.ReadNode readNode) {
350-
LocalData data = (LocalData) readNode.execute(inliningTarget, self, DATA, null);
339+
static PNone setCompleter(PythonModule self, boolean enabled) {
340+
LocalData data = self.getInternalAttributes();
351341
data.autoHistory = enabled;
352342
return PNone.NONE;
353343
}
@@ -357,10 +347,8 @@ PNone setCompleter(PythonModule self, boolean enabled,
357347
@GenerateNodeFactory
358348
abstract static class SetCompleterDelimsNode extends PythonBinaryBuiltinNode {
359349
@Specialization
360-
PNone setCompleterDelims(PythonModule self, TruffleString completerDelims,
361-
@Bind("this") Node inliningTarget,
362-
@Cached HiddenAttr.ReadNode readNode) {
363-
LocalData data = (LocalData) readNode.execute(inliningTarget, self, DATA, null);
350+
static PNone setCompleterDelims(PythonModule self, TruffleString completerDelims) {
351+
LocalData data = self.getInternalAttributes();
364352
data.completerDelims = completerDelims;
365353
return PNone.NONE;
366354
}
@@ -370,10 +358,8 @@ PNone setCompleterDelims(PythonModule self, TruffleString completerDelims,
370358
@GenerateNodeFactory
371359
abstract static class GetCompleterDelimsNode extends PythonBuiltinNode {
372360
@Specialization
373-
Object getCompleterDelims(PythonModule self,
374-
@Bind("this") Node inliningTarget,
375-
@Cached HiddenAttr.ReadNode readNode) {
376-
LocalData data = (LocalData) readNode.execute(inliningTarget, self, DATA, null);
361+
static Object getCompleterDelims(PythonModule self) {
362+
LocalData data = self.getInternalAttributes();
377363
return (data.completerDelims != null) ? data.completerDelims : PNone.NONE;
378364
}
379365
}

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

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@
4141
package com.oracle.graal.python.builtins.modules;
4242

4343
import static com.oracle.graal.python.nodes.BuiltinNames.T__SIGNAL;
44-
import static com.oracle.graal.python.nodes.HiddenAttr.CURRENT_ALARM;
45-
import static com.oracle.graal.python.nodes.HiddenAttr.SIGNAL_MODULE_DATA;
4644
import static com.oracle.graal.python.util.PythonUtils.tsLiteral;
4745
import static com.oracle.graal.python.util.TimeUtils.SEC_TO_US;
4846

@@ -72,7 +70,6 @@
7270
import com.oracle.graal.python.lib.PyTimeFromObjectNode;
7371
import com.oracle.graal.python.lib.PyTimeFromObjectNode.RoundType;
7472
import com.oracle.graal.python.nodes.ErrorMessages;
75-
import com.oracle.graal.python.nodes.HiddenAttr;
7673
import com.oracle.graal.python.nodes.PConstructAndRaiseNode;
7774
import com.oracle.graal.python.nodes.PRaiseNode;
7875
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
@@ -138,7 +135,7 @@ public void postInitialize(Python3Core core) {
138135

139136
PythonModule signalModule = core.lookupBuiltinModule(T__SIGNAL);
140137
ModuleData moduleData = new ModuleData();
141-
HiddenAttr.WriteNode.executeUncached(signalModule, SIGNAL_MODULE_DATA, moduleData);
138+
signalModule.setInternalAttributes(moduleData);
142139

143140
core.getContext().registerAsyncAction(() -> {
144141
SignalTriggerAction poll = moduleData.signalQueue.poll();
@@ -163,8 +160,8 @@ public void postInitialize(Python3Core core) {
163160
}
164161

165162
public static void resetSignalHandlers(PythonModule mod) {
166-
Object obj = HiddenAttr.ReadNode.executeUncached(mod, SIGNAL_MODULE_DATA, null);
167-
if (obj instanceof ModuleData data) {
163+
ModuleData data = mod.getInternalAttributes();
164+
if (data != null) {
168165
for (Map.Entry<Integer, SignalHandler> entry : data.defaultSignalHandlers.entrySet()) {
169166
Signals.setSignalHandler(entry.getKey(), entry.getValue());
170167
}
@@ -230,8 +227,9 @@ abstract static class AlarmNode extends PythonBinaryClinicBuiltinNode {
230227
@TruffleBoundary
231228
int alarm(PythonModule module, int seconds) {
232229
int remaining = 0;
233-
Object currentAlarmObj = HiddenAttr.ReadNode.executeUncached(module, CURRENT_ALARM, null);
234-
if (currentAlarmObj instanceof Signals.Alarm currentAlarm) {
230+
ModuleData data = module.getInternalAttributes();
231+
Signals.Alarm currentAlarm = data.currentAlarm;
232+
if (currentAlarm != null) {
235233
if (currentAlarm.isRunning()) {
236234
remaining = currentAlarm.getRemainingSeconds();
237235
if (remaining < 0) {
@@ -242,7 +240,7 @@ int alarm(PythonModule module, int seconds) {
242240
}
243241
if (seconds > 0) {
244242
Signals.Alarm newAlarm = new Signals.Alarm(seconds);
245-
HiddenAttr.WriteNode.executeUncached(module, CURRENT_ALARM, newAlarm);
243+
data.currentAlarm = newAlarm;
246244
newAlarm.start();
247245
}
248246
return remaining;
@@ -279,7 +277,7 @@ abstract static class GetSignalNode extends PythonBinaryClinicBuiltinNode {
279277
@Specialization
280278
@TruffleBoundary
281279
static Object getsignal(PythonModule mod, int signum) {
282-
ModuleData data = (ModuleData) HiddenAttr.ReadNode.executeUncached(mod, SIGNAL_MODULE_DATA, null);
280+
ModuleData data = mod.getInternalAttributes();
283281
return handlerToPython(Signals.getCurrentSignalHandler(signum), signum, data);
284282
}
285283

@@ -307,7 +305,7 @@ abstract static class SignalNode extends PythonTernaryBuiltinNode {
307305
@TruffleBoundary
308306
static Object signalHandler(PythonModule self, Object signal, Object handler,
309307
@Bind("this") Node inliningTarget) {
310-
ModuleData data = getModuleDataUncached(self);
308+
ModuleData data = self.getInternalAttributes();
311309
int signum = PyNumberAsSizeNode.executeExactUncached(signal);
312310
if (PyCallableCheckNode.executeUncached(handler)) {
313311
return signal(inliningTarget, signum, handler, data);
@@ -423,11 +421,10 @@ protected ArgumentClinicProvider getArgumentClinic() {
423421
@Specialization
424422
Object doIt(VirtualFrame frame, PythonModule self, int which, Object seconds, Object interval,
425423
@Bind("this") Node inliningTarget,
426-
@Cached HiddenAttr.ReadNode readModuleDataNode,
427424
@Cached PyTimeFromObjectNode timeFromObjectNode,
428425
@Cached PConstructAndRaiseNode.Lazy constructAndRaiseNode,
429426
@Cached PythonObjectFactory factory) {
430-
ModuleData moduleData = getModuleData(self, inliningTarget, readModuleDataNode);
427+
ModuleData moduleData = self.getInternalAttributes();
431428
long usDelay = toMicroseconds(frame, inliningTarget, seconds, timeFromObjectNode);
432429
long usInterval = toMicroseconds(frame, inliningTarget, interval, timeFromObjectNode);
433430
if (which != ITIMER_REAL) {
@@ -493,10 +490,9 @@ protected ArgumentClinicProvider getArgumentClinic() {
493490
@Specialization
494491
static Object doIt(VirtualFrame frame, PythonModule self, int which,
495492
@Bind("this") Node inliningTarget,
496-
@Cached HiddenAttr.ReadNode readModuleDataNode,
497493
@Cached PConstructAndRaiseNode.Lazy constructAndRaiseNode,
498494
@Cached PythonObjectFactory factory) {
499-
ModuleData moduleData = getModuleData(self, inliningTarget, readModuleDataNode);
495+
ModuleData moduleData = self.getInternalAttributes();
500496
if (which != ITIMER_REAL) {
501497
throw constructAndRaiseNode.get(inliningTarget).raiseOSError(frame, OSErrorEnum.EINVAL);
502498
}
@@ -522,24 +518,6 @@ static long getOldDelay(ModuleData moduleData) {
522518
}
523519
}
524520

525-
private static ModuleData getModuleDataUncached(PythonModule mod) {
526-
Object obj = HiddenAttr.ReadNode.executeUncached(mod, SIGNAL_MODULE_DATA, null);
527-
if (obj instanceof ModuleData) {
528-
return (ModuleData) obj;
529-
} else {
530-
throw new IllegalStateException("the signal module was not initialized properly!");
531-
}
532-
}
533-
534-
private static ModuleData getModuleData(PythonModule mod, Node inliningTarget, HiddenAttr.ReadNode readNode) {
535-
Object obj = readNode.execute(inliningTarget, mod, SIGNAL_MODULE_DATA, null);
536-
if (obj instanceof ModuleData) {
537-
return (ModuleData) obj;
538-
} else {
539-
throw new IllegalStateException("the signal module was not initialized properly!");
540-
}
541-
}
542-
543521
private static class ModuleData {
544522
final ConcurrentHashMap<Integer, Object> signalHandlers = new ConcurrentHashMap<>();
545523
final ConcurrentHashMap<Integer, SignalHandler> defaultSignalHandlers = new ConcurrentHashMap<>();
@@ -548,6 +526,7 @@ private static class ModuleData {
548526
ScheduledExecutorService itimerService;
549527
ScheduledFuture<?> itimerFuture;
550528
long itimerInterval;
529+
Signals.Alarm currentAlarm;
551530
}
552531

553532
}

0 commit comments

Comments
 (0)