Skip to content

[WIP] bpo-13224 Change str for classes, functions and modules#3608

Closed
merwok wants to merge 4 commits intopython:masterfrom
merwok:change-function-str-13224
Closed

[WIP] bpo-13224 Change str for classes, functions and modules#3608
merwok wants to merge 4 commits intopython:masterfrom
merwok:change-function-str-13224

Conversation

@merwok
Copy link
Copy Markdown
Member

@merwok merwok commented Sep 16, 2017

Comment thread Lib/test/test_descrtut.py Outdated

>>> print(defaultdict) # show our type
<class 'test.test_descrtut.defaultdict'>
defaultdict
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think these tests looked better with the previous outputs. Revert changes and change inputs instead (>>> defaultdict should show repr unless doctest doesn’t emulate the REPL displayhook)

Comment thread Lib/test/test_descrtut.py

>>> C.foo(1)
classmethod <class 'test.test_descrtut.C'> 1
classmethod C 1
Copy link
Copy Markdown
Member Author

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 1

Copy link
Copy Markdown
Member Author

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 of cls.

Comment thread Objects/moduleobject.c
name = PyModule_GetNameObject((PyObject *)m);
if (name == NULL) {
/* XXX PyUnicode_FromStringAndSize is documented as part of
the legacy API */
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Address this

Comment thread Objects/methodobject.c
(hashfunc)meth_hash, /* tp_hash */
PyCFunction_Call, /* tp_call */
0, /* tp_str */
(reprfunc)meth_str, /* tp_str */
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.

Just use meth_repr.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The point of the change is to have a __str__ that’s different than __repr__ :) But see the ticket, I think I will scale back this PR to classes only.

Copy link
Copy Markdown
Member Author

@merwok merwok Sep 19, 2017

Choose a reason for hiding this comment

The 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 __name__ attribute).

* I think a module’s __name__ depends on its loader, not on the name used in the import statement (import as doesn’t change __name__)

Comment thread Objects/moduleobject.c
name = PyModule_GetNameObject((PyObject *)m);
if (name == NULL) {
/* XXX PyUnicode_FromStringAndSize is documented as part of
the legacy API */
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.

Really? I never heart that PyUnicode_FromStringAndSize is a legacy API. This is the main way of creating strings in C. Could you point the place in the documentation where it declared legacy?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The 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).

Comment thread Objects/moduleobject.c
if (name == NULL) {
/* XXX PyUnicode_FromStringAndSize is documented as part of
the legacy API */
PyErr_Clear();
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.

Don't silence all errors. Only AttributeError.

Comment thread Objects/moduleobject.c
/* XXX PyUnicode_FromStringAndSize is documented as part of
the legacy API */
PyErr_Clear();
name = PyUnicode_FromStringAndSize("?", 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.

Wouldn't be better to fall back to repr()?

Comment thread Objects/moduleobject.c
return NULL;
}

str = PyUnicode_FromFormat("%U", name);
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.

What is the purpose of this line?

Comment thread Objects/typeobject.c
type_str(PyTypeObject *type)
{
PyObject *name, *rtn;

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.

Wouldn't be better to include a module name for getting rid of ambiguity?

I would just return PyUnicode_FromString(type->tp_name).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The 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.

@merwok merwok force-pushed the change-function-str-13224 branch from 684c931 to eb99067 Compare September 19, 2017 03:14
@merwok merwok force-pushed the change-function-str-13224 branch from eb99067 to c3c84ad Compare September 19, 2017 03:35
Copy link
Copy Markdown
Contributor

@ncoghlan ncoghlan left a comment

Choose a reason for hiding this comment

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

I think the number of additional changes needed in the test suite and the standard library highlights the compatibility restoration busy-work risks of actually making this change:

  • while test_structures and test_xmlrpc show the kinds of error messages that the change is aimed at improving (and I think it does indeed improve them), it still requires the kind of test case change that is going to be awkward from projects spanning multiple distinct versions of CPython
  • most of the other cases were for code that actually wanted the current formatting, and needed to be updated to explicitly request repr() (either by calling it directly, or by switching to !r or %r in a formatting string)

@merwok
Copy link
Copy Markdown
Member Author

merwok commented Sep 19, 2017

Rejected per Nick’s and Guido’s opinions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants