Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Lib/test/pickletester.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,15 @@ def __reduce__(self):
# Shouldn't support the recursion itself
return K, (self.value,)


class L:
def __init__(self):
self.attr = self.__foo

def __foo(self):
return 42


import __main__
__main__.C = C
C.__module__ = "__main__"
Expand Down Expand Up @@ -2478,6 +2487,13 @@ def test_many_puts_and_gets(self):
loaded = self.loads(dumped)
self.assert_is_copy(obj, loaded)

def test_mangled_private_methods_roundtrip(self):
obj = L()
for proto in protocols:
with self.subTest(proto=proto):
loaded = self.loads(self.dumps(obj, proto))
self.assertEqual(loaded.attr(), 42)

def test_attribute_name_interning(self):
# Test that attribute names of pickled objects are interned when
# unpickling.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The :mod:`pickle` module now properly handles name-mangled private methods.
21 changes: 21 additions & 0 deletions Objects/classobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "pycore_object.h"
#include "pycore_pyerrors.h"
#include "pycore_pystate.h" // _PyThreadState_GET()
#include "pycore_symtable.h" // _Py_Mangle()


#include "clinic/classobject.c.h"
Expand Down Expand Up @@ -134,9 +135,29 @@ method___reduce___impl(PyMethodObject *self)
PyObject *funcself = PyMethod_GET_SELF(self);
PyObject *func = PyMethod_GET_FUNCTION(self);
PyObject *funcname = PyObject_GetAttr(func, &_Py_ID(__name__));
Py_ssize_t len;
if (funcname == NULL) {
return NULL;
}
if (PyUnicode_Check(funcname) &&
(len = PyUnicode_GET_LENGTH(funcname)) > 2 &&
PyUnicode_READ_CHAR(funcname, 0) == '_' &&
PyUnicode_READ_CHAR(funcname, 1) == '_' &&
!(PyUnicode_READ_CHAR(funcname, len-1) == '_' &&
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would also fail to trigger on methods named ___ and ____ (3 and 4 underscores). it seems rather pedantic to care, but perhaps add a len > 4 check before checking for trailing _s and cover def ____(self): in the regression test?

PyUnicode_READ_CHAR(funcname, len-2) == '_'))
{
PyObject *name = PyObject_GetAttr((PyObject *)Py_TYPE(funcself),
&_Py_ID(__name__));
if (name == NULL) {
Py_DECREF(funcname);
return NULL;
}
Py_SETREF(funcname, _Py_Mangle(name, funcname));
Py_DECREF(name);
if (funcname == NULL) {
return NULL;
}
}
return Py_BuildValue(
"N(ON)", _PyEval_GetBuiltin(&_Py_ID(getattr)), funcself, funcname);
}
Expand Down