gh-103092: Isolate `_pickle` module (#102982)

Co-authored-by: Mohamed Koubaa <koubaa.m@gmail.com>
Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
This commit is contained in:
Erlend E. Aasland 2023-04-04 12:08:54 +02:00 committed by GitHub
parent 810d365b5e
commit c00dcf0e38
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 724 additions and 690 deletions

View File

@ -293,6 +293,34 @@ if has_c_implementation:
pass
pickler_class = CustomCPicklerClass
@support.cpython_only
class HeapTypesTests(unittest.TestCase):
def setUp(self):
pickler = _pickle.Pickler(io.BytesIO())
unpickler = _pickle.Unpickler(io.BytesIO())
self._types = (
_pickle.Pickler,
_pickle.Unpickler,
type(pickler.memo),
type(unpickler.memo),
# We cannot test the _pickle.Pdata;
# there's no way to get to it.
)
def test_have_gc(self):
import gc
for tp in self._types:
with self.subTest(tp=tp):
self.assertTrue(gc.is_tracked(tp))
def test_immutable(self):
for tp in self._types:
with self.subTest(tp=tp):
with self.assertRaisesRegex(TypeError, "immutable"):
tp.foo = "bar"
@support.cpython_only
class SizeofTests(unittest.TestCase):
check_sizeof = support.check_sizeof

View File

@ -0,0 +1 @@
Adapt :mod:`!_pickle` to :pep:`687`. Patch by Mohamed Koubaa and Erlend Aasland.

File diff suppressed because it is too large Load Diff

View File

@ -38,7 +38,42 @@ PyDoc_STRVAR(_pickle_Pickler_dump__doc__,
"Write a pickled representation of the given object to the open file.");
#define _PICKLE_PICKLER_DUMP_METHODDEF \
{"dump", (PyCFunction)_pickle_Pickler_dump, METH_O, _pickle_Pickler_dump__doc__},
{"dump", _PyCFunction_CAST(_pickle_Pickler_dump), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _pickle_Pickler_dump__doc__},
static PyObject *
_pickle_Pickler_dump_impl(PicklerObject *self, PyTypeObject *cls,
PyObject *obj);
static PyObject *
_pickle_Pickler_dump(PicklerObject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
{
PyObject *return_value = NULL;
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
# define KWTUPLE (PyObject *)&_Py_SINGLETON(tuple_empty)
#else
# define KWTUPLE NULL
#endif
static const char * const _keywords[] = {"", NULL};
static _PyArg_Parser _parser = {
.keywords = _keywords,
.fname = "dump",
.kwtuple = KWTUPLE,
};
#undef KWTUPLE
PyObject *argsbuf[1];
PyObject *obj;
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
if (!args) {
goto exit;
}
obj = args[0];
return_value = _pickle_Pickler_dump_impl(self, cls, obj);
exit:
return return_value;
}
PyDoc_STRVAR(_pickle_Pickler___sizeof____doc__,
"__sizeof__($self, /)\n"
@ -242,15 +277,19 @@ PyDoc_STRVAR(_pickle_Unpickler_load__doc__,
"specified therein.");
#define _PICKLE_UNPICKLER_LOAD_METHODDEF \
{"load", (PyCFunction)_pickle_Unpickler_load, METH_NOARGS, _pickle_Unpickler_load__doc__},
{"load", _PyCFunction_CAST(_pickle_Unpickler_load), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _pickle_Unpickler_load__doc__},
static PyObject *
_pickle_Unpickler_load_impl(UnpicklerObject *self);
_pickle_Unpickler_load_impl(UnpicklerObject *self, PyTypeObject *cls);
static PyObject *
_pickle_Unpickler_load(UnpicklerObject *self, PyObject *Py_UNUSED(ignored))
_pickle_Unpickler_load(UnpicklerObject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
{
return _pickle_Unpickler_load_impl(self);
if (nargs) {
PyErr_SetString(PyExc_TypeError, "load() takes no arguments");
return NULL;
}
return _pickle_Unpickler_load_impl(self, cls);
}
PyDoc_STRVAR(_pickle_Unpickler_find_class__doc__,
@ -267,26 +306,41 @@ PyDoc_STRVAR(_pickle_Unpickler_find_class__doc__,
"needed. Both arguments passed are str objects.");
#define _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF \
{"find_class", _PyCFunction_CAST(_pickle_Unpickler_find_class), METH_FASTCALL, _pickle_Unpickler_find_class__doc__},
{"find_class", _PyCFunction_CAST(_pickle_Unpickler_find_class), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _pickle_Unpickler_find_class__doc__},
static PyObject *
_pickle_Unpickler_find_class_impl(UnpicklerObject *self,
_pickle_Unpickler_find_class_impl(UnpicklerObject *self, PyTypeObject *cls,
PyObject *module_name,
PyObject *global_name);
static PyObject *
_pickle_Unpickler_find_class(UnpicklerObject *self, PyObject *const *args, Py_ssize_t nargs)
_pickle_Unpickler_find_class(UnpicklerObject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
{
PyObject *return_value = NULL;
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
# define KWTUPLE (PyObject *)&_Py_SINGLETON(tuple_empty)
#else
# define KWTUPLE NULL
#endif
static const char * const _keywords[] = {"", "", NULL};
static _PyArg_Parser _parser = {
.keywords = _keywords,
.fname = "find_class",
.kwtuple = KWTUPLE,
};
#undef KWTUPLE
PyObject *argsbuf[2];
PyObject *module_name;
PyObject *global_name;
if (!_PyArg_CheckPositional("find_class", nargs, 2, 2)) {
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
if (!args) {
goto exit;
}
module_name = args[0];
global_name = args[1];
return_value = _pickle_Unpickler_find_class_impl(self, module_name, global_name);
return_value = _pickle_Unpickler_find_class_impl(self, cls, module_name, global_name);
exit:
return return_value;
@ -980,4 +1034,4 @@ skip_optional_kwonly:
exit:
return return_value;
}
/*[clinic end generated code: output=730dc26938561313 input=a9049054013a1b77]*/
/*[clinic end generated code: output=a0e04b85e7bae626 input=a9049054013a1b77]*/

View File

@ -390,11 +390,6 @@ Modules/_decimal/_decimal.c - PyDecContextManager_Type -
Modules/_decimal/_decimal.c - PyDecContext_Type -
Modules/_decimal/_decimal.c - PyDecSignalDictMixin_Type -
Modules/_decimal/_decimal.c - PyDec_Type -
Modules/_pickle.c - Pdata_Type -
Modules/_pickle.c - PicklerMemoProxyType -
Modules/_pickle.c - Pickler_Type -
Modules/_pickle.c - UnpicklerMemoProxyType -
Modules/_pickle.c - Unpickler_Type -
Modules/ossaudiodev.c - OSSAudioType -
Modules/ossaudiodev.c - OSSMixerType -
Modules/socketmodule.c - sock_type -

Can't render this file because it has a wrong number of fields in line 4.