diff options
author | Eric Snow <ericsnowcurrently@gmail.com> | 2025-04-28 17:23:46 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-04-28 17:23:46 -0600 |
commit | bdd23c0bb95faa37130fdf7af82f0fdddb41bae0 (patch) | |
tree | 975bc2dfb21c40e12844bb22d97da5beacc07b89 /Python/crossinterp.c | |
parent | 68a737691b0fd591de00f4811bb23a5c280fe859 (diff) | |
download | cpython-bdd23c0bb95faa37130fdf7af82f0fdddb41bae0.tar.gz cpython-bdd23c0bb95faa37130fdf7af82f0fdddb41bae0.zip |
gh-132775: Add _PyMarshal_GetXIData() (gh-133108)
Note that the bulk of this change is tests.
Diffstat (limited to 'Python/crossinterp.c')
-rw-r--r-- | Python/crossinterp.c | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/Python/crossinterp.c b/Python/crossinterp.c index 662c9c72b15..753d784a503 100644 --- a/Python/crossinterp.c +++ b/Python/crossinterp.c @@ -2,6 +2,7 @@ /* API for managing interactions between isolated interpreters */ #include "Python.h" +#include "marshal.h" // PyMarshal_WriteObjectToString() #include "pycore_ceval.h" // _Py_simple_func #include "pycore_crossinterp.h" // _PyXIData_t #include "pycore_initconfig.h" // _PyStatus_OK() @@ -286,6 +287,48 @@ _PyObject_GetXIData(PyThreadState *tstate, } +/* marshal wrapper */ + +PyObject * +_PyMarshal_ReadObjectFromXIData(_PyXIData_t *xidata) +{ + PyThreadState *tstate = _PyThreadState_GET(); + _PyBytes_data_t *shared = (_PyBytes_data_t *)xidata->data; + PyObject *obj = PyMarshal_ReadObjectFromString(shared->bytes, shared->len); + if (obj == NULL) { + PyObject *cause = _PyErr_GetRaisedException(tstate); + assert(cause != NULL); + _set_xid_lookup_failure( + tstate, NULL, "object could not be unmarshalled", cause); + Py_DECREF(cause); + return NULL; + } + return obj; +} + +int +_PyMarshal_GetXIData(PyThreadState *tstate, PyObject *obj, _PyXIData_t *xidata) +{ + PyObject *bytes = PyMarshal_WriteObjectToString(obj, Py_MARSHAL_VERSION); + if (bytes == NULL) { + PyObject *cause = _PyErr_GetRaisedException(tstate); + assert(cause != NULL); + _set_xid_lookup_failure( + tstate, NULL, "object could not be marshalled", cause); + Py_DECREF(cause); + return -1; + } + size_t size = sizeof(_PyBytes_data_t); + _PyBytes_data_t *shared = _PyBytes_GetXIDataWrapped( + tstate, bytes, size, _PyMarshal_ReadObjectFromXIData, xidata); + Py_DECREF(bytes); + if (shared == NULL) { + return -1; + } + return 0; +} + + /* using cross-interpreter data */ PyObject * |