Skip to content

Commit 69f82a4

Browse files
committed
Move timezone initialization in time module to postInitialize
Fixes #412
1 parent cb3450c commit 69f82a4

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

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

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ public final class TimeModuleBuiltins extends PythonBuiltins {
149149
"offset from UTC in seconds"
150150
});
151151

152+
public static final TruffleString T_TZNAME = tsLiteral("tzname");
153+
public static final TruffleString T_DAYLIGHT = tsLiteral("daylight");
154+
public static final TruffleString T_TIMEZONE = tsLiteral("timezone");
155+
public static final TruffleString T_ALTZONE = tsLiteral("altzone");
156+
152157
@Override
153158
protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFactories() {
154159
return TimeModuleBuiltinsFactory.getFactories();
@@ -157,30 +162,36 @@ protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFa
157162
@Override
158163
public void initialize(Python3Core core) {
159164
super.initialize(core);
165+
StructSequence.initType(core, STRUCT_TIME_DESC);
166+
addBuiltinConstant("_STRUCT_TM_ITEMS", 11);
167+
}
168+
169+
@Override
170+
public void postInitialize(Python3Core core) {
171+
super.postInitialize(core);
160172
// Should we read TZ env variable?
161173
ZoneId defaultZoneId = core.getContext().getEnv().getTimeZone();
162174
ModuleState moduleState = new ModuleState();
163175
moduleState.currentZoneId = defaultZoneId;
164176
moduleState.timeSlept = 0;
165-
core.lookupBuiltinModule(T_TIME).setModuleState(moduleState);
177+
PythonModule timeModule = core.lookupBuiltinModule(T_TIME);
178+
timeModule.setModuleState(moduleState);
166179

167180
TimeZone defaultTimeZone = TimeZone.getTimeZone(defaultZoneId);
168181
TruffleString noDaylightSavingZone = toTruffleStringUncached(defaultTimeZone.getDisplayName(false, TimeZone.SHORT));
169182
TruffleString daylightSavingZone = toTruffleStringUncached(defaultTimeZone.getDisplayName(true, TimeZone.SHORT));
170183

171184
boolean hasDaylightSaving = !noDaylightSavingZone.equalsUncached(daylightSavingZone, TS_ENCODING);
172185
if (hasDaylightSaving) {
173-
addBuiltinConstant("tzname", core.factory().createTuple(new Object[]{noDaylightSavingZone, daylightSavingZone}));
186+
timeModule.setAttribute(T_TZNAME, core.factory().createTuple(new Object[]{noDaylightSavingZone, daylightSavingZone}));
174187
} else {
175-
addBuiltinConstant("tzname", core.factory().createTuple(new Object[]{noDaylightSavingZone}));
188+
timeModule.setAttribute(T_TZNAME, core.factory().createTuple(new Object[]{noDaylightSavingZone}));
176189
}
177190

178-
addBuiltinConstant("daylight", PInt.intValue(hasDaylightSaving));
191+
timeModule.setAttribute(T_DAYLIGHT, PInt.intValue(hasDaylightSaving));
179192
int rawOffsetSeconds = defaultTimeZone.getRawOffset() / -1000;
180-
addBuiltinConstant("timezone", rawOffsetSeconds);
181-
addBuiltinConstant("altzone", rawOffsetSeconds - 3600);
182-
addBuiltinConstant("_STRUCT_TM_ITEMS", 11);
183-
StructSequence.initType(core, STRUCT_TIME_DESC);
193+
timeModule.setAttribute(T_TIMEZONE, rawOffsetSeconds);
194+
timeModule.setAttribute(T_ALTZONE, rawOffsetSeconds - 3600);
184195
}
185196

186197
@TruffleBoundary

0 commit comments

Comments
 (0)