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

PEP 737: PyUnicode_FromFormat(): Add %T format to format the type name of an object #111696

Closed
@vstinner

Description

@vstinner

It's common to format a type name by accessingPyTypeObject.tp_name member. Example:

PyErr_Format(PyExc_TypeError,             "__format__ must return a str, not %.200s",             Py_TYPE(result)->tp_name);

Problems:

  • PyTypeObject.tp_name (type.__name__) is less helpful thanPyHeapTypeObject.ht_qualname (type.__qualname__). I would prefer to display thequalified type name.
  • PyTypeObject.tp_name is a UTF-8 encoded string, it requires todecode the UTF-8 string at each call.
  • I would like toremovePyTypeObject members (tp_name) from the public C API: issueC API: Investigate how the PyTypeObject members can be removed from the public C API #105970.
  • By the way, in the early days of Python,PyString_FromFormat() used a buffer with a fixed size, so the output string should be truncated to avoid overflow. But nowadays,PyUnicode_FromFormat() allocates a buffer on the heap and is no longer limited to 200 characters. Truncated a type name can miss important information in the error message. I would prefer tonot truncate the type name.

I propose adding a%T format to PyUnicode_FromUnicode() to format the qualified name of an object type. For example, the example would become:

PyErr_Format(PyExc_TypeError,             "__format__ must return a str, not %T", result);

In 2018, I already added%T format to PyUnicode_FromFormat(): issueGH-78776. See relatedpython-dev discussion. The change was reverted.

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp