-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
gh-76785: Move the Cross-Interpreter Code to Its Own File #111502
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.
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 8 commits into
python:main
from
ericsnowcurrently:crossinterp-utils-refactor
Oct 30, 2023
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
25eb51a
Factor out interp_utils.*.
ericsnowcurrently 7870af1
interp_utils -> crossinterp
ericsnowcurrently 8450753
Add pycore_crossinterp.h.
ericsnowcurrently 2f788d8
Move everything to the internal C-API for now.
ericsnowcurrently f77c5c9
Group the API and code appropriately.
ericsnowcurrently e85f29e
Fix a smelly symbol.
ericsnowcurrently cc3c844
Add a missing include.
ericsnowcurrently 953c39e
Fix the _freeze_module build on Windows.
ericsnowcurrently File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
#ifndef Py_INTERNAL_CROSSINTERP_H | ||
#define Py_INTERNAL_CROSSINTERP_H | ||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
#ifndef Py_BUILD_CORE | ||
# error "this header requires Py_BUILD_CORE define" | ||
#endif | ||
|
||
|
||
/***************************/ | ||
/* cross-interpreter calls */ | ||
/***************************/ | ||
|
||
typedef int (*_Py_simple_func)(void *); | ||
extern int _Py_CallInInterpreter( | ||
PyInterpreterState *interp, | ||
_Py_simple_func func, | ||
void *arg); | ||
extern int _Py_CallInInterpreterAndRawFree( | ||
PyInterpreterState *interp, | ||
_Py_simple_func func, | ||
void *arg); | ||
|
||
|
||
/**************************/ | ||
/* cross-interpreter data */ | ||
/**************************/ | ||
|
||
typedef struct _xid _PyCrossInterpreterData; | ||
typedef PyObject *(*xid_newobjectfunc)(_PyCrossInterpreterData *); | ||
typedef void (*xid_freefunc)(void *); | ||
|
||
// _PyCrossInterpreterData is similar to Py_buffer as an effectively | ||
// opaque struct that holds data outside the object machinery. This | ||
// is necessary to pass safely between interpreters in the same process. | ||
struct _xid { | ||
// data is the cross-interpreter-safe derivation of a Python object | ||
// (see _PyObject_GetCrossInterpreterData). It will be NULL if the | ||
// new_object func (below) encodes the data. | ||
void *data; | ||
// obj is the Python object from which the data was derived. This | ||
// is non-NULL only if the data remains bound to the object in some | ||
// way, such that the object must be "released" (via a decref) when | ||
// the data is released. In that case the code that sets the field, | ||
// likely a registered "crossinterpdatafunc", is responsible for | ||
// ensuring it owns the reference (i.e. incref). | ||
PyObject *obj; | ||
// interp is the ID of the owning interpreter of the original | ||
// object. It corresponds to the active interpreter when | ||
// _PyObject_GetCrossInterpreterData() was called. This should only | ||
// be set by the cross-interpreter machinery. | ||
// | ||
// We use the ID rather than the PyInterpreterState to avoid issues | ||
// with deleted interpreters. Note that IDs are never re-used, so | ||
// each one will always correspond to a specific interpreter | ||
// (whether still alive or not). | ||
int64_t interpid; | ||
// new_object is a function that returns a new object in the current | ||
// interpreter given the data. The resulting object (a new | ||
// reference) will be equivalent to the original object. This field | ||
// is required. | ||
xid_newobjectfunc new_object; | ||
// free is called when the data is released. If it is NULL then | ||
// nothing will be done to free the data. For some types this is | ||
// okay (e.g. bytes) and for those types this field should be set | ||
// to NULL. However, for most the data was allocated just for | ||
// cross-interpreter use, so it must be freed when | ||
// _PyCrossInterpreterData_Release is called or the memory will | ||
// leak. In that case, at the very least this field should be set | ||
// to PyMem_RawFree (the default if not explicitly set to NULL). | ||
// The call will happen with the original interpreter activated. | ||
xid_freefunc free; | ||
}; | ||
|
||
PyAPI_FUNC(_PyCrossInterpreterData *) _PyCrossInterpreterData_New(void); | ||
PyAPI_FUNC(void) _PyCrossInterpreterData_Free(_PyCrossInterpreterData *data); | ||
|
||
|
||
/* defining cross-interpreter data */ | ||
|
||
PyAPI_FUNC(void) _PyCrossInterpreterData_Init( | ||
_PyCrossInterpreterData *data, | ||
PyInterpreterState *interp, void *shared, PyObject *obj, | ||
xid_newobjectfunc new_object); | ||
PyAPI_FUNC(int) _PyCrossInterpreterData_InitWithSize( | ||
_PyCrossInterpreterData *, | ||
PyInterpreterState *interp, const size_t, PyObject *, | ||
xid_newobjectfunc); | ||
PyAPI_FUNC(void) _PyCrossInterpreterData_Clear( | ||
PyInterpreterState *, _PyCrossInterpreterData *); | ||
|
||
|
||
/* using cross-interpreter data */ | ||
|
||
PyAPI_FUNC(int) _PyObject_CheckCrossInterpreterData(PyObject *); | ||
PyAPI_FUNC(int) _PyObject_GetCrossInterpreterData(PyObject *, _PyCrossInterpreterData *); | ||
PyAPI_FUNC(PyObject *) _PyCrossInterpreterData_NewObject(_PyCrossInterpreterData *); | ||
PyAPI_FUNC(int) _PyCrossInterpreterData_Release(_PyCrossInterpreterData *); | ||
PyAPI_FUNC(int) _PyCrossInterpreterData_ReleaseAndRawFree(_PyCrossInterpreterData *); | ||
|
||
|
||
/* cross-interpreter data registry */ | ||
|
||
// For now we use a global registry of shareable classes. An | ||
// alternative would be to add a tp_* slot for a class's | ||
// crossinterpdatafunc. It would be simpler and more efficient. | ||
|
||
typedef int (*crossinterpdatafunc)(PyThreadState *tstate, PyObject *, | ||
_PyCrossInterpreterData *); | ||
|
||
struct _xidregitem; | ||
|
||
struct _xidregitem { | ||
struct _xidregitem *prev; | ||
struct _xidregitem *next; | ||
/* This can be a dangling pointer, but only if weakref is set. */ | ||
PyTypeObject *cls; | ||
/* This is NULL for builtin types. */ | ||
PyObject *weakref; | ||
size_t refcount; | ||
crossinterpdatafunc getdata; | ||
}; | ||
|
||
struct _xidregistry { | ||
PyThread_type_lock mutex; | ||
struct _xidregitem *head; | ||
}; | ||
|
||
PyAPI_FUNC(int) _PyCrossInterpreterData_RegisterClass(PyTypeObject *, crossinterpdatafunc); | ||
PyAPI_FUNC(int) _PyCrossInterpreterData_UnregisterClass(PyTypeObject *); | ||
PyAPI_FUNC(crossinterpdatafunc) _PyCrossInterpreterData_Lookup(PyObject *); | ||
|
||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
#endif /* !Py_INTERNAL_CROSSINTERP_H */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you don't need one of these functions in shared libraries (stdlib extensions), you can replace PyAPI_FUNC() with extern.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They're basically all used in _xxsubinterpretersmodule.c and _xxinterpchannelsmodule.c.