GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-125286: Share the Main Refchain With Legacy Interpreters by ericsnowcurrently · Pull Request #125709 · python/cpython

gh-125286: Share the Main Refchain With Legacy Interpreters #125709

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
ericsnowcurrently merged 16 commits into python:main from ericsnowcurrently:legacy-shared-refchain
Oct 23, 2024

Uh oh!

There was an error while loading. Please reload this page.

Merged

gh-125286: Share the Main Refchain With Legacy Interpreters #125709

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
53cfb82
Share the main refchain with legacy interpreters.
ericsnowcurrently Oct 18, 2024
b2bf609
Add a has_own_refchain() helper function.
ericsnowcurrently Oct 18, 2024
203c112
Drop _Py_NormalizeImmortalReference().
ericsnowcurrently Oct 21, 2024
ffe2633
Drop has_own_refchain() calls where not needed.
ericsnowcurrently Oct 21, 2024
8d52258
Call _PyObject_InitState() *after* computing the feature flags.
ericsnowcurrently Oct 21, 2024
216dde2
Revert "Call _PyObject_InitState() *after* computing the feature flags."
ericsnowcurrently Oct 21, 2024
fbc4e2d
Revert "Drop has_own_refchain() calls where not needed."
ericsnowcurrently Oct 21, 2024
cb06847
has_own_refchain() -> maybe_fix_refchain()
ericsnowcurrently Oct 21, 2024
7aa33a9
Handle late runtime fini correctly.
ericsnowcurrently Oct 21, 2024
3421a19
Pass the interpreter config to init_interpreter().
ericsnowcurrently Oct 22, 2024
b276454
Revert "Pass the interpreter config to init_interpreter()."
ericsnowcurrently Oct 22, 2024
7ab086c
Move the _PyObject_InitState() call out of init_interpreter().
ericsnowcurrently Oct 22, 2024
9d539f6
Add docs and a What's New entry
encukou Oct 22, 2024
d80fe6a
Fix typos.
ericsnowcurrently Oct 22, 2024
1360d86
Do not try to destroy the refchain if it is NULL.
ericsnowcurrently Oct 22, 2024
090b528
Docs: Link to the new sys.getobjects() docs
encukou Oct 23, 2024
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view
Prev Previous commit
Next Next commit
has_own_refchain() -> maybe_fix_refchain()
  • Loading branch information

Uh oh!

There was an error while loading. Please reload this page.

@ericsnowcurrently
ericsnowcurrently committed Oct 21, 2024
commit cb0684719ae0efbfb6969ded9ca03f2ce372c430

There are no files selected for viewing

72 changes: 54 additions & 18 deletions 72 Objects/object.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -209,25 +209,69 @@ refchain_fini(PyInterpreterState *interp)
if (has_own_refchain(interp)) {
_Py_hashtable_destroy(REFCHAIN(interp));
}
// This extra branch can be removed once we start setting
// interp->feature_flags *before* refchain_init() gets called.
else if (REFCHAIN(interp) != REFCHAIN(_PyInterpreterState_Main())) {
_Py_hashtable_destroy(REFCHAIN(interp));
}
REFCHAIN(interp) = NULL;
}

/* Currently refchain_init() is called during interpreter initialization,
via _PyObject_InitState(), before interp->feature_flags is set,
so an interpreter may have its own refchain even though it shouldn't.
Until that gets fixed, we patch it all up wherever apprpriate.
maybe_fix_refchain() and move_refchain_item() can be dropped
once we sort out the init order problem. */

static int
copy_refchain_item(_Py_hashtable_t *ht,
const void *key, const void *value,
void *user_data)
{
if (value != REFCHAIN_VALUE) {
assert(value == NULL);
return 0;
}
_Py_hashtable_t *new_chain = (_Py_hashtable_t *)user_data;
if (_Py_hashtable_set(new_chain, key, REFCHAIN_VALUE) < 0) {
Py_FatalError("_Py_hashtable_set() memory allocation failed");
}
return 0;
}

static void
maybe_fix_refchain(PyInterpreterState *interp)
{
if (has_own_refchain(interp)) {
// It's okay or we haven't set the feature flags correctly yet.
return;
}

_Py_hashtable_t *cur_chain = REFCHAIN(interp);
_Py_hashtable_t *main_chain = REFCHAIN(_PyInterpreterState_Main());
if (cur_chain == main_chain) {
// It was already fixed.
return;
}
REFCHAIN(interp) = main_chain;

(void)_Py_hashtable_foreach(cur_chain, copy_refchain_item, main_chain);
_Py_hashtable_destroy(cur_chain);
}

bool
_PyRefchain_IsTraced(PyInterpreterState *interp, PyObject *obj)
{
if (!has_own_refchain(interp)) {
interp = _PyInterpreterState_Main();
}
maybe_fix_refchain(interp);
return (_Py_hashtable_get(REFCHAIN(interp), obj) == REFCHAIN_VALUE);
}


static void
_PyRefchain_Trace(PyInterpreterState *interp, PyObject *obj)
{
if (!has_own_refchain(interp)) {
interp = _PyInterpreterState_Main();
}
maybe_fix_refchain(interp);
if (_Py_hashtable_set(REFCHAIN(interp), obj, REFCHAIN_VALUE) < 0) {
// Use a fatal error because _Py_NewReference() cannot report
// the error to the caller.
Expand All @@ -239,9 +283,7 @@ _PyRefchain_Trace(PyInterpreterState *interp, PyObject *obj)
static void
_PyRefchain_Remove(PyInterpreterState *interp, PyObject *obj)
{
if (!has_own_refchain(interp)) {
interp = _PyInterpreterState_Main();
}
maybe_fix_refchain(interp);
void *value = _Py_hashtable_steal(REFCHAIN(interp), obj);
#ifndef NDEBUG
assert(value == REFCHAIN_VALUE);
Expand Down Expand Up @@ -2587,9 +2629,7 @@ _Py_PrintReferences(PyInterpreterState *interp, FILE *fp)
interp = _PyInterpreterState_Main();
}
fprintf(fp, "Remaining objects:\n");
if (!has_own_refchain(interp)) {
interp = _PyInterpreterState_Main();
}
maybe_fix_refchain(interp);
_Py_hashtable_foreach(REFCHAIN(interp), _Py_PrintReference, fp);
}

Expand Down Expand Up @@ -2618,9 +2658,7 @@ void
_Py_PrintReferenceAddresses(PyInterpreterState *interp, FILE *fp)
{
fprintf(fp, "Remaining object addresses:\n");
if (!has_own_refchain(interp)) {
interp = _PyInterpreterState_Main();
}
maybe_fix_refchain(interp);
_Py_hashtable_foreach(REFCHAIN(interp), _Py_PrintReferenceAddress, fp);
}

Expand Down Expand Up @@ -2700,9 +2738,7 @@ _Py_GetObjects(PyObject *self, PyObject *args)
.limit = limit,
};
PyInterpreterState *interp = _PyInterpreterState_GET();
if (!has_own_refchain(interp)) {
interp = _PyInterpreterState_Main();
}
maybe_fix_refchain(interp);
int res = _Py_hashtable_foreach(REFCHAIN(interp), _Py_GetObject, &data);
if (res == _PY_GETOBJECTS_ERROR) {
Py_DECREF(list);
Expand Down
3 changes: 3 additions & 0 deletions 3 Python/pystate.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,9 @@ init_interpreter(PyInterpreterState *interp,
assert(next != NULL || (interp == runtime->interpreters.main));
interp->next = next;

// This relies on interp->feature_flags being set already,
// but currently we don't set that by this point.
// That's something to fix.
PyStatus status = _PyObject_InitState(interp);
if (_PyStatus_EXCEPTION(status)) {
return status;
Expand Down
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.