Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

DO-NOT-MERGE: bpo-34595: Add %t format to PyUnicode_FromFormatV()#9122

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
vstinner wants to merge1 commit intopython:masterfromvstinner:unicode_format_t
Closed

DO-NOT-MERGE: bpo-34595: Add %t format to PyUnicode_FromFormatV()#9122

vstinner wants to merge1 commit intopython:masterfromvstinner:unicode_format_t

Conversation

vstinner
Copy link
Member

@vstinnervstinner commentedSep 9, 2018
edited by bedevere-bot
Loading

  • The %T format of PyUnicode_FromFormatV() now returns the fully
    qualified name of an object type (ex: "module.namespace.typename").
  • Add %t format to PyUnicode_FromFormatV(), and so to
    PyUnicode_FromFormat() and PyErr_Format(), to format the "short
    name" of an object type: equivalent to "%s" with
    _PyType_Name(Py_TYPE(obj)).
  • Replace %T format with %t format in unicodeobject.c.

https://bugs.python.org/issue34595



PyObject *
_PyType_FullName(PyTypeObject *type)

Choose a reason for hiding this comment

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

In case of non-heap types you can just returnPyUnicode_FromString(type->tp_name).

In case of heap types the code can be a tiny bit simpler if inline_PyType_Module() and_PyType_QualName() because you can get rid of increfs/decrefs and NULL checks.

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

In case of non-heap types you can just return PyUnicode_FromString(type->tp_name).

Done. This change removes "builtins." when formatting builtins type. So %T of a string becomes "str" (instead of "builtins.str"). IMHO it's the expected behaviour.

In case of heap types the code can be a tiny bit simpler if inline _PyType_Module() and _PyType_QualName() because you can get rid of increfs/decrefs and NULL checks.

I don't think that performance matters here. I prefer to reuse the same code, to make sure that type.qualname, type.module and %T behave the same for heap types.

static PyObject *
type_name(PyTypeObject *type, void *context)
_PyType_QualName(PyTypeObject *type)

Choose a reason for hiding this comment

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

Whytype_qualname() andtype_module() have been renamed to_PyType_QualName() and_PyType_Module()?

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

Hum. I moved code in a weird way. I fixed that.

@@ -536,6 +540,9 @@ APIs:
.. [1] For integer specifiers (d, u, ld, li, lu, lld, lli, llu, zd, zi,
zu, i, x): the 0-conversion flag has effect even when a precision is given.

.. [2] The object type fully qualified name is equivalent to:
``f"{type(obj).__module__}.{type(obj).__qualname__}"``.

Choose a reason for hiding this comment

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

Except that the module name is omitted for types in the builtins module (and for non-heap extension types that don't specify the module, but this can be considered as a bug).

@@ -768,7 +768,7 @@ ensure_unicode(PyObject *obj)
{
if (!PyUnicode_Check(obj)) {
PyErr_Format(PyExc_TypeError,
"must be str, not %T", obj);
"must be str, not %t", obj);

Choose a reason for hiding this comment

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

What was used instead of %t/%T before?

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

Python 3.7 code:

        PyErr_Format(PyExc_TypeError,                     "must be str, not %.100s",                     Py_TYPE(obj)->tp_name);

Choose a reason for hiding this comment

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

Thus it was closer to %T. In error messages it is better to use fully qualified names.

* The %T format of PyUnicode_FromFormatV() now returns the fully  qualified name of an object type (ex: "module.namespace.typename").* Add %t format to PyUnicode_FromFormatV(), and so to  PyUnicode_FromFormat() and PyErr_Format(), to format the "short  name" of an object type: equivalent to "%s" with  _PyType_Name(Py_TYPE(obj)).* Replace %T format with %t format in unicodeobject.c.* Update existing NEWS entry
@vstinner
Copy link
MemberAuthor

I rebased my changed and made requested changes.

@vstinner
Copy link
MemberAuthor

I propose to replace Py_TYPE(obj)->tp_name with %t in C to mimick Python code which uses type(obj).name or obj.class.name.

If we want to use %T in C code, I suggest to also update the related Python code, especially for "C accelerators" modules like _asyncio/_pickle. I suggest to only start to use %T on a case by base basis.

@vstinner
Copy link
MemberAuthor

#Linux-PR_20180911.05 failed: the CI is broken, it failed on apt-get install.

@vstinnervstinner changed the titlebpo-34595: Add %t format to PyUnicode_FromFormatV()DO-NOT-MERGE: bpo-34595: Add %t format to PyUnicode_FromFormatV()Sep 11, 2018
@vstinner
Copy link
MemberAuthor

I changed the status of this PR to "DO-NOT-MERGE", since Petr Viktorin asked me to open a discussion on python-dev:

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

@serhiy-storchakaserhiy-storchakaserhiy-storchaka left review comments

Assignees
No one assigned
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

4 participants
@vstinner@serhiy-storchaka@the-knights-who-say-ni@bedevere-bot

[8]ページ先頭

©2009-2025 Movatter.jp