Repository navigation
[WIP] bpo-13224 Change str for classes, functions and modules #3608
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Change str(x) to return only the qualname for classes, functions and modules |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -221,6 +221,17 @@ meth_repr(PyCFunctionObject *m) | |
| m->m_self); | ||
| } | ||
|
|
||
| static PyObject * | ||
| meth_str(PyCFunctionObject *m) | ||
| { | ||
| if (m->m_self == NULL || PyModule_Check(m->m_self)) | ||
| return PyUnicode_FromFormat("%s", m->m_ml->ml_name); | ||
| return PyUnicode_FromFormat("<built-in method %s of %s object at %p>", | ||
| m->m_ml->ml_name, | ||
| m->m_self->ob_type->tp_name, | ||
| m->m_self); | ||
| } | ||
|
|
||
| static PyObject * | ||
| meth_richcompare(PyObject *self, PyObject *other, int op) | ||
| { | ||
|
|
@@ -284,11 +295,11 @@ PyTypeObject PyCFunction_Type = { | |
| 0, /* tp_as_mapping */ | ||
| (hashfunc)meth_hash, /* tp_hash */ | ||
| PyCFunction_Call, /* tp_call */ | ||
| 0, /* tp_str */ | ||
| (reprfunc)meth_str, /* tp_str */ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just use
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The point of the change is to have a
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh well, it is Guido who wanted a unified behaviour for modules, classes and functions (since they are things created by syntax (import*, class and def) with a * I think a module’s |
||
| PyObject_GenericGetAttr, /* tp_getattro */ | ||
| 0, /* tp_setattro */ | ||
| 0, /* tp_as_buffer */ | ||
| Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ | ||
| Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ | ||
| 0, /* tp_doc */ | ||
| (traverseproc)meth_traverse, /* tp_traverse */ | ||
| 0, /* tp_clear */ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -701,6 +701,27 @@ module_getattro(PyModuleObject *m, PyObject *name) | |
| return NULL; | ||
| } | ||
|
|
||
| static PyObject * | ||
| module_str(PyModuleObject *m) | ||
| { | ||
| PyObject *name, *str; | ||
|
|
||
| name = PyModule_GetNameObject((PyObject *)m); | ||
| if (name == NULL) { | ||
| /* XXX PyUnicode_FromStringAndSize is documented as part of | ||
| the legacy API */ | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Address this
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Really? I never heart that
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the comment. I don’t have the original Mercurial clone on this laptop, so I can’t look for my original commit. This code is removed in my latest changes (not pushed yet). |
||
| PyErr_Clear(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't silence all errors. Only |
||
| name = PyUnicode_FromStringAndSize("?", 1); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't be better to fall back to |
||
| if (name == NULL) /* */ | ||
| return NULL; | ||
| } | ||
|
|
||
| str = PyUnicode_FromFormat("%U", name); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the purpose of this line? |
||
|
|
||
| Py_DECREF(name); | ||
| return str; | ||
| } | ||
|
|
||
| static int | ||
| module_traverse(PyModuleObject *m, visitproc visit, void *arg) | ||
| { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -906,6 +906,21 @@ type_repr(PyTypeObject *type) | |
| return rtn; | ||
| } | ||
|
|
||
| static PyObject * | ||
| type_str(PyTypeObject *type) | ||
| { | ||
| PyObject *name, *rtn; | ||
|
|
||
| name = type_qualname(type, NULL); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't be better to include a module name for getting rid of ambiguity? I would just return
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The original discussion on the ticket mentioned only the type qualname, without its module. Looking at Lib/typing.py and other files (for example the error message in functools), I think maybe including the module name too would be useful. Let’s see comments after I get a smaller patch that passes the test suite. |
||
| if (name == NULL) | ||
| return NULL; | ||
|
|
||
| rtn = PyUnicode_FromFormat("%U", name); | ||
|
|
||
| Py_DECREF(name); | ||
| return rtn; | ||
| } | ||
|
|
||
| static PyObject * | ||
| type_call(PyTypeObject *type, PyObject *args, PyObject *kwds) | ||
| { | ||
|
|
@@ -3458,7 +3473,7 @@ PyTypeObject PyType_Type = { | |
| 0, /* tp_as_mapping */ | ||
| 0, /* tp_hash */ | ||
| (ternaryfunc)type_call, /* tp_call */ | ||
| 0, /* tp_str */ | ||
| (reprfunc)type_str, /* tp_str */ | ||
| (getattrofunc)type_getattro, /* tp_getattro */ | ||
| (setattrofunc)type_setattro, /* tp_setattro */ | ||
| 0, /* tp_as_buffer */ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We want
classmethod test.test_descrtut.C 1There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Turns out we don't: qualname is defined to not include the module name, so C is good here.
Alternative: change the code of the foo method to print
repr(cls)instead ofcls.