Unverified Kaydet (Commit) 6d2cd903 authored tarafından Eric Snow's avatar Eric Snow Kaydeden (comit) GitHub

bpo-32604: Improve subinterpreter tests. (#6914)

Add more tests for subinterpreters. This patch also fixes a few small defects in the channel implementation.
üst 55e53c30
...@@ -1308,6 +1308,10 @@ _PyCrossInterpreterData_Register_Class(PyTypeObject *cls, ...@@ -1308,6 +1308,10 @@ _PyCrossInterpreterData_Register_Class(PyTypeObject *cls,
return res; return res;
} }
/* Cross-interpreter objects are looked up by exact match on the class.
We can reassess this policy when we move from a global registry to a
tp_* slot. */
crossinterpdatafunc crossinterpdatafunc
_PyCrossInterpreterData_Lookup(PyObject *obj) _PyCrossInterpreterData_Lookup(PyObject *obj)
{ {
...@@ -1332,19 +1336,79 @@ _PyCrossInterpreterData_Lookup(PyObject *obj) ...@@ -1332,19 +1336,79 @@ _PyCrossInterpreterData_Lookup(PyObject *obj)
/* cross-interpreter data for builtin types */ /* cross-interpreter data for builtin types */
struct _shared_bytes_data {
char *bytes;
Py_ssize_t len;
};
static PyObject * static PyObject *
_new_bytes_object(_PyCrossInterpreterData *data) _new_bytes_object(_PyCrossInterpreterData *data)
{ {
return PyBytes_FromString((char *)(data->data)); struct _shared_bytes_data *shared = (struct _shared_bytes_data *)(data->data);
return PyBytes_FromStringAndSize(shared->bytes, shared->len);
} }
static int static int
_bytes_shared(PyObject *obj, _PyCrossInterpreterData *data) _bytes_shared(PyObject *obj, _PyCrossInterpreterData *data)
{ {
data->data = (void *)(PyBytes_AS_STRING(obj)); struct _shared_bytes_data *shared = PyMem_NEW(struct _shared_bytes_data, 1);
if (PyBytes_AsStringAndSize(obj, &shared->bytes, &shared->len) < 0) {
return -1;
}
data->data = (void *)shared;
data->obj = obj; // Will be "released" (decref'ed) when data released. data->obj = obj; // Will be "released" (decref'ed) when data released.
data->new_object = _new_bytes_object; data->new_object = _new_bytes_object;
data->free = NULL; // Do not free the data (it belongs to the object). data->free = PyMem_Free;
return 0;
}
struct _shared_str_data {
int kind;
const void *buffer;
Py_ssize_t len;
};
static PyObject *
_new_str_object(_PyCrossInterpreterData *data)
{
struct _shared_str_data *shared = (struct _shared_str_data *)(data->data);
return PyUnicode_FromKindAndData(shared->kind, shared->buffer, shared->len);
}
static int
_str_shared(PyObject *obj, _PyCrossInterpreterData *data)
{
struct _shared_str_data *shared = PyMem_NEW(struct _shared_str_data, 1);
shared->kind = PyUnicode_KIND(obj);
shared->buffer = PyUnicode_DATA(obj);
shared->len = PyUnicode_GET_LENGTH(obj) - 1;
data->data = (void *)shared;
data->obj = obj; // Will be "released" (decref'ed) when data released.
data->new_object = _new_str_object;
data->free = PyMem_Free;
return 0;
}
static PyObject *
_new_long_object(_PyCrossInterpreterData *data)
{
return PyLong_FromLongLong((int64_t)(data->data));
}
static int
_long_shared(PyObject *obj, _PyCrossInterpreterData *data)
{
int64_t value = PyLong_AsLongLong(obj);
if (value == -1 && PyErr_Occurred()) {
if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
PyErr_SetString(PyExc_OverflowError, "try sending as bytes");
}
return -1;
}
data->data = (void *)value;
data->obj = NULL;
data->new_object = _new_long_object;
data->free = NULL;
return 0; return 0;
} }
...@@ -1374,10 +1438,20 @@ _register_builtins_for_crossinterpreter_data(void) ...@@ -1374,10 +1438,20 @@ _register_builtins_for_crossinterpreter_data(void)
Py_FatalError("could not register None for cross-interpreter sharing"); Py_FatalError("could not register None for cross-interpreter sharing");
} }
// int
if (_register_xidata(&PyLong_Type, _long_shared) != 0) {
Py_FatalError("could not register int for cross-interpreter sharing");
}
// bytes // bytes
if (_register_xidata(&PyBytes_Type, _bytes_shared) != 0) { if (_register_xidata(&PyBytes_Type, _bytes_shared) != 0) {
Py_FatalError("could not register bytes for cross-interpreter sharing"); Py_FatalError("could not register bytes for cross-interpreter sharing");
} }
// str
if (_register_xidata(&PyUnicode_Type, _str_shared) != 0) {
Py_FatalError("could not register str for cross-interpreter sharing");
}
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment