gh-77188: Add support for pickling private methods and nested classes (GH-21480)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
Zackery Spytz 2026-02-05 11:50:51 -08:00 committed by GitHub
parent 4644fed819
commit 01a1dd283b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 174 additions and 1 deletions

View file

@ -3183,6 +3183,27 @@ _Py_MaybeMangle(PyObject *privateobj, PySTEntryObject *ste, PyObject *name)
return _Py_Mangle(privateobj, name);
}
int
_Py_IsPrivateName(PyObject *ident)
{
if (!PyUnicode_Check(ident)) {
return 0;
}
Py_ssize_t nlen = PyUnicode_GET_LENGTH(ident);
if (nlen < 3 ||
PyUnicode_READ_CHAR(ident, 0) != '_' ||
PyUnicode_READ_CHAR(ident, 1) != '_')
{
return 0;
}
if (PyUnicode_READ_CHAR(ident, nlen-1) == '_' &&
PyUnicode_READ_CHAR(ident, nlen-2) == '_')
{
return 0; /* Don't mangle __whatever__ */
}
return 1;
}
PyObject *
_Py_Mangle(PyObject *privateobj, PyObject *ident)
{