Skip to content

Commit

Permalink
Optimize.
Browse files Browse the repository at this point in the history
  • Loading branch information
serhiy-storchaka committed Nov 29, 2023
1 parent 944d540 commit 90cf9b3
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions Objects/classobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,23 +135,31 @@ 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;
}
PyObject *name = PyObject_GetAttr((PyObject *)Py_TYPE(funcself),
&_Py_ID(__name__));
if (name == NULL) {
Py_DECREF(funcname);
return NULL;
}
PyObject *mangled = _Py_Mangle(name, funcname);
Py_DECREF(name);
Py_DECREF(funcname);
if (mangled == 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) == '_' &&
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, mangled);
"N(ON)", _PyEval_GetBuiltin(&_Py_ID(getattr)), funcself, funcname);
}

static PyMethodDef method_methods[] = {
Expand Down

0 comments on commit 90cf9b3

Please sign in to comment.