GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — DO-NOT-MERGE: bpo-34595: Add %t format to PyUnicode_FromFormatV() by vstinner · Pull Request #9122 · python/cpython

Conversation

@vstinner

@vstinner vstinner commented Sep 9, 2018 •
edited by bedevere-bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member
  • 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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@vstinner
vstinner requested a review from serhiy-storchaka September 9, 2018 07:43
@the-knights-who-say-ni the-knights-who-say-ni added the CLA signed label Sep 9, 2018
@bedevere-bot bedevere-bot added the awaiting merge label Sep 9, 2018
serhiy-storchaka
serhiy-storchaka reviewed Sep 9, 2018
View reviewed changes
Comment thread Objects/typeobject.c Outdated

@serhiy-storchaka serhiy-storchaka Sep 9, 2018

Copy link
Copy Markdown
Member

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.

In case of non-heap types you can just return PyUnicode_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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@vstinner vstinner Sep 11, 2018

Copy link
Copy Markdown
Member Author

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.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Objects/typeobject.c Outdated

@serhiy-storchaka serhiy-storchaka Sep 9, 2018

Copy link
Copy Markdown
Member

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.

Why type_qualname() and type_module() have been renamed to _PyType_QualName() and _PyType_Module()?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@vstinner vstinner Sep 11, 2018

Copy link
Copy Markdown
Member Author

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.

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Doc/c-api/unicode.rst Outdated

@serhiy-storchaka serhiy-storchaka Sep 9, 2018

Copy link
Copy Markdown
Member

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.

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Objects/unicodeobject.c Outdated

@serhiy-storchaka serhiy-storchaka Sep 9, 2018

Copy link
Copy Markdown
Member

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.

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@vstinner vstinner Sep 11, 2018

Copy link
Copy Markdown
Member Author

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.

Python 3.7 code:

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@serhiy-storchaka serhiy-storchaka Sep 11, 2018

Copy link
Copy Markdown
Member

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.

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@vstinner
bpo-34595: Add %t format to PyUnicode_FromFormatV()
71450a6
* 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

vstinner commented Sep 11, 2018

Copy link
Copy Markdown
Member Author

I rebased my changed and made requested changes.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Sep 11, 2018

Copy link
Copy Markdown
Member Author

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Sep 11, 2018

Copy link
Copy Markdown
Member Author

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner vstinner added the DO-NOT-MERGE label Sep 11, 2018
@vstinner vstinner changed the title bpo-34595: Add %t format to PyUnicode_FromFormatV() DO-NOT-MERGE: bpo-34595: Add %t format to PyUnicode_FromFormatV() Sep 11, 2018
@vstinner

vstinner commented Sep 11, 2018

Copy link
Copy Markdown
Member Author

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

  • https://mail.python.org/pipermail/python-dev/2018-September/155150.html
  • https://bugs.python.org/issue34595#msg325079
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jamadden jamadden mentioned this pull request Sep 13, 2018
Type name varies in the repr() depending on the presence of the C extension zopefoundation/persistent#92
Closed
@vstinner vstinner closed this Sep 19, 2018
@vstinner
vstinner deleted the unicode_format_t branch September 19, 2018 22:48
@vstinner vstinner mentioned this pull request Nov 29, 2023
PEP 737: gh-111696: Add %T format to PyUnicode_FromFormat() #111703
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@serhiy-storchaka serhiy-storchaka serhiy-storchaka left review comments

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

awaiting merge DO-NOT-MERGE

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@vstinner @serhiy-storchaka @the-knights-who-say-ni @bedevere-bot
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.