Skip to content

Commit 7ab086c

Browse files
Move the _PyObject_InitState() call out of init_interpreter().
1 parent b276454 commit 7ab086c

File tree

3 files changed

+16
-61
lines changed

3 files changed

+16
-61
lines changed

Objects/object.c

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -210,69 +210,19 @@ refchain_fini(PyInterpreterState *interp)
210210
if (has_own_refchain(interp)) {
211211
_Py_hashtable_destroy(REFCHAIN(interp));
212212
}
213-
// This extra branch can be removed once we start setting
214-
// interp->feature_flags *before* refchain_init() gets called.
215-
else if (REFCHAIN(interp) != REFCHAIN(_PyInterpreterState_Main())) {
216-
_Py_hashtable_destroy(REFCHAIN(interp));
217-
}
218213
REFCHAIN(interp) = NULL;
219214
}
220215

221-
/* Currently refchain_init() is called during interpreter initialization,
222-
via _PyObject_InitState(), before interp->feature_flags is set,
223-
so an interpreter may have its own refchain even though it shouldn't.
224-
Until that gets fixed, we patch it all up wherever apprpriate.
225-
maybe_fix_refchain() and move_refchain_item() can be dropped
226-
once we sort out the init order problem. */
227-
228-
static int
229-
copy_refchain_item(_Py_hashtable_t *ht,
230-
const void *key, const void *value,
231-
void *user_data)
232-
{
233-
if (value != REFCHAIN_VALUE) {
234-
assert(value == NULL);
235-
return 0;
236-
}
237-
_Py_hashtable_t *new_chain = (_Py_hashtable_t *)user_data;
238-
if (_Py_hashtable_set(new_chain, key, REFCHAIN_VALUE) < 0) {
239-
Py_FatalError("_Py_hashtable_set() memory allocation failed");
240-
}
241-
return 0;
242-
}
243-
244-
static void
245-
maybe_fix_refchain(PyInterpreterState *interp)
246-
{
247-
if (has_own_refchain(interp)) {
248-
// It's okay or we haven't set the feature flags correctly yet.
249-
return;
250-
}
251-
252-
_Py_hashtable_t *cur_chain = REFCHAIN(interp);
253-
_Py_hashtable_t *main_chain = REFCHAIN(_PyInterpreterState_Main());
254-
if (cur_chain == main_chain) {
255-
// It was already fixed.
256-
return;
257-
}
258-
REFCHAIN(interp) = main_chain;
259-
260-
(void)_Py_hashtable_foreach(cur_chain, copy_refchain_item, main_chain);
261-
_Py_hashtable_destroy(cur_chain);
262-
}
263-
264216
bool
265217
_PyRefchain_IsTraced(PyInterpreterState *interp, PyObject *obj)
266218
{
267-
maybe_fix_refchain(interp);
268219
return (_Py_hashtable_get(REFCHAIN(interp), obj) == REFCHAIN_VALUE);
269220
}
270221

271222

272223
static void
273224
_PyRefchain_Trace(PyInterpreterState *interp, PyObject *obj)
274225
{
275-
maybe_fix_refchain(interp);
276226
if (_Py_hashtable_set(REFCHAIN(interp), obj, REFCHAIN_VALUE) < 0) {
277227
// Use a fatal error because _Py_NewReference() cannot report
278228
// the error to the caller.
@@ -284,7 +234,6 @@ _PyRefchain_Trace(PyInterpreterState *interp, PyObject *obj)
284234
static void
285235
_PyRefchain_Remove(PyInterpreterState *interp, PyObject *obj)
286236
{
287-
maybe_fix_refchain(interp);
288237
void *value = _Py_hashtable_steal(REFCHAIN(interp), obj);
289238
#ifndef NDEBUG
290239
assert(value == REFCHAIN_VALUE);
@@ -2630,7 +2579,6 @@ _Py_PrintReferences(PyInterpreterState *interp, FILE *fp)
26302579
interp = _PyInterpreterState_Main();
26312580
}
26322581
fprintf(fp, "Remaining objects:\n");
2633-
maybe_fix_refchain(interp);
26342582
_Py_hashtable_foreach(REFCHAIN(interp), _Py_PrintReference, fp);
26352583
}
26362584

@@ -2659,7 +2607,6 @@ void
26592607
_Py_PrintReferenceAddresses(PyInterpreterState *interp, FILE *fp)
26602608
{
26612609
fprintf(fp, "Remaining object addresses:\n");
2662-
maybe_fix_refchain(interp);
26632610
_Py_hashtable_foreach(REFCHAIN(interp), _Py_PrintReferenceAddress, fp);
26642611
}
26652612

@@ -2739,7 +2686,6 @@ _Py_GetObjects(PyObject *self, PyObject *args)
27392686
.limit = limit,
27402687
};
27412688
PyInterpreterState *interp = _PyInterpreterState_GET();
2742-
maybe_fix_refchain(interp);
27432689
int res = _Py_hashtable_foreach(REFCHAIN(interp), _Py_GetObject, &data);
27442690
if (res == _PY_GETOBJECTS_ERROR) {
27452691
Py_DECREF(list);

Python/pylifecycle.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,13 @@ pycore_create_interpreter(_PyRuntimeState *runtime,
674674
return status;
675675
}
676676

677+
// This could be done in init_interpreter() (in pystate.c) if it
678+
// didn't depend on interp->feature_flags being set already.
679+
status = _PyObject_InitState(interp);
680+
if (_PyStatus_EXCEPTION(status)) {
681+
return status;
682+
}
683+
677684
// initialize the interp->obmalloc state. This must be done after
678685
// the settings are loaded (so that feature_flags are set) but before
679686
// any calls are made to obmalloc functions.
@@ -2297,6 +2304,13 @@ new_interpreter(PyThreadState **tstate_p,
22972304
goto error;
22982305
}
22992306

2307+
// This could be done in init_interpreter() (in pystate.c) if it
2308+
// didn't depend on interp->feature_flags being set already.
2309+
status = _PyObject_InitState(interp);
2310+
if (_PyStatus_EXCEPTION(status)) {
2311+
return status;
2312+
}
2313+
23002314
// initialize the interp->obmalloc state. This must be done after
23012315
// the settings are loaded (so that feature_flags are set) but before
23022316
// any calls are made to obmalloc functions.

Python/pystate.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -629,13 +629,8 @@ init_interpreter(PyInterpreterState *interp,
629629
assert(next != NULL || (interp == runtime->interpreters.main));
630630
interp->next = next;
631631

632-
// This relies on interp->feature_flags being set already,
633-
// but currently we don't set that by this point.
634-
// That's something to fix.
635-
PyStatus status = _PyObject_InitState(interp);
636-
if (_PyStatus_EXCEPTION(status)) {
637-
return status;
638-
}
632+
// We would call _PyObject_InitState() at this point
633+
// if interp->feature_flags were alredy set.
639634

640635
_PyEval_InitState(interp);
641636
_PyGC_InitState(&interp->gc);

0 commit comments

Comments
 (0)