Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add _Py_AddToGlobalDict().
  • Loading branch information
ericsnowcurrently committed Mar 22, 2023
commit eb42aa1a83df5e29dc73dad442ae7306f92cca9d
2 changes: 2 additions & 0 deletions Include/internal/pycore_pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ extern void _PyThreadState_ClearDetached(PyThreadState *);
extern PyThreadState * _Py_AcquireGlobalObjectsState(PyInterpreterState *);
extern void _Py_ReleaseGlobalObjectsState(PyThreadState *);

extern PyObject * _Py_AddToGlobalDict(PyObject *, PyObject *, PyObject *);


static inline void
_PyThreadState_UpdateTracingState(PyThreadState *tstate)
Expand Down
30 changes: 30 additions & 0 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,36 @@ _Py_ReleaseGlobalObjectsState(PyThreadState *oldts)
}
}

PyObject *
_Py_AddToGlobalDict(PyObject *dict, PyObject *key, PyObject *value)
{
assert(dict != NULL);

/* Swap to the main interpreter, if necessary. */
PyInterpreterState *interp = _PyInterpreterState_GET();
PyThreadState *oldts = _Py_AcquireGlobalObjectsState(interp);

/* This might trigger a resize, which is why we must "acquire"
the global object state. */
PyObject *actual = PyDict_SetDefault(dict, key, value);
if (actual == NULL) {
/* Raising an exception from one interpreter in another
is problematic, so we clear it and let the caller deal
with the returned NULL. */
assert(PyErr_ExceptionMatches(PyExc_MemoryError));
PyErr_Clear();
}

/* Swap back, it it wasn't in the main interpreter already. */
if (oldts != NULL) {
_Py_ReleaseGlobalObjectsState(oldts);
}

// XXX Immortalize the key and value.

return actual;
}


/*************************************/
/* the per-interpreter runtime state */
Expand Down