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 — gh-89653: PEP 670: Convert unicodeobject.h macros to functions by vstinner · Pull Request #91696 · python/cpython

Conversation

@vstinner

@vstinner vstinner commented Apr 19, 2022

Copy link
Copy Markdown
Member

Convert unicodeobject.h macros to static inline functions:

  • Reorder functions to declare functions before their first usage.
  • PyUnicode_READ_CHAR() and PyUnicode_MAX_CHAR_VALUE() now only call
    PyUnicode_KIND() once.
  • Simplify PyUnicode_GET_SIZE().
  • PyUnicode_READ_CHAR() now uses PyUnicode_1BYTE_DATA(),
    PyUnicode_2BYTE_DATA() and PyUnicode_4BYTE_DATA().
  • Remove redundant PyUnicode_Check() assertions.

Static inline functions are wrapped into macros which casts pointer
types (PyObject*, void*) to prevent introducing new compiler warnings
when passing const pointers (ex: PyUnicode_WRITE).

PyUnicode_KIND() return type is "unsigned int" rather than "enum
PyUnicode_Kind" to prevent introducing new compiler warnings.

Sorry, something went wrong.

Uh oh!

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

All reactions
@vstinner
gh-89653: PEP 670: Convert unicodeobject.h macros to functions
c3e480a
Convert unicodeobject.h macros to static inline functions:

* Reorder functions to declare functions before their first usage.
* PyUnicode_READ_CHAR() and PyUnicode_MAX_CHAR_VALUE() now only call
  PyUnicode_KIND() once.
* Simplify PyUnicode_GET_SIZE().
* PyUnicode_READ_CHAR() now uses PyUnicode_1BYTE_DATA(),
  PyUnicode_2BYTE_DATA() and PyUnicode_4BYTE_DATA().
* Remove redundant PyUnicode_Check() assertions.

Static inline functions are wrapped into macros which casts pointer
types (PyObject*, void*) to prevent introducing new compiler warnings
when passing const pointers (ex: PyUnicode_WRITE).

PyUnicode_KIND() return type is "unsigned int" rather than "enum
PyUnicode_Kind" to prevent introducing new compiler warnings.
@vstinner vstinner added the skip news label Apr 19, 2022
@bedevere-bot bedevere-bot added the awaiting core review label Apr 19, 2022
@vstinner

vstinner commented Apr 19, 2022

Copy link
Copy Markdown
Member Author

Changes since my #31221 POC:

  • Add macros to prevent emitting new compiler warnings: so it's not needed to modify C code using these functions to fix const vs non-const => PEP 670 requires that
  • PyUnicode_KIND() return type is now unsigned int, not enum Uncode_Kind
  • The following functions return type is now unsigned int instead of int: PyUnicode_IS_COMPACT(), PyUnicode_CHECK_INTERNED(), PyUnicode_IS_READY(), PyUnicode_IS_ASCII(). Previously, the macros just exposed structure members of type unsigned int.
  • Don't deprecate PyUnicode_GET_SIZE(), PyUnicode_GET_DATA_SIZE(), PyUnicode_AS_UNICODE(), PyUnicode_AS_DATA()
  • Don't add new assertions to ease review.
  • Don't convert _PyUnicodeWriter_Prepare() and _PyUnicodeWriter_PrepareKind() macros to functions to keep this PR as small as possible.

I will re-do some on these changes (like deprecate functions) as separated PRs once this PR is merged.

All reactions

Sorry, something went wrong.

Uh oh!

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

@vstinner

vstinner commented Apr 19, 2022

Copy link
Copy Markdown
Member Author

Ah, and I also updated comments :-)

@erlend-aasland @gpshead: Would yo mind to review this PR?

All reactions

Sorry, something went wrong.

Uh oh!

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

@vstinner

vstinner commented Apr 19, 2022

Copy link
Copy Markdown
Member Author

@erlend-aasland: Tell me if you prefer that I split this PR into smaller PRs.

All reactions

Sorry, something went wrong.

Uh oh!

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

@vstinner

vstinner commented Apr 19, 2022

Copy link
Copy Markdown
Member Author

The following functions return type is now unsigned int instead of int: PyUnicode_IS_COMPACT(), PyUnicode_CHECK_INTERNED(), PyUnicode_IS_READY(), PyUnicode_IS_ASCII(). Previously, the macros just exposed structure members of type unsigned int.

Oh. It seems like it's wrong. The following code emits a new compiler warning: comparison of integer expressions of different signedness: ‘unsigned int’ and ‘int’ [-Wsign-compare].

    int skind = PyUnicode_KIND(self);
    ...
    int rkind = skind;
    ...
    assert(PyUnicode_KIND(u) == rkind);
All reactions

Sorry, something went wrong.

Uh oh!

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

@erlend-aasland

erlend-aasland commented Apr 19, 2022

Copy link
Copy Markdown
Contributor

@erlend-aasland: Tell me if you prefer that I split this PR into smaller PRs.

I would prefer that, yes :)

All reactions

Sorry, something went wrong.

Uh oh!

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

@vstinner

vstinner commented Apr 19, 2022

Copy link
Copy Markdown
Member Author

I would prefer that, yes :)

Here is a shorter PR: #91705

erlend-aasland reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

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

@vstinner vstinner mentioned this pull request Apr 20, 2022
[C API] Avoid "const PyObject*" type #91768
Closed
@vstinner

vstinner commented Apr 21, 2022

Copy link
Copy Markdown
Member Author

I splitted this PR into smaller PRs.

erlend-aasland reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

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

@vstinner vstinner closed this Apr 21, 2022
@vstinner
vstinner deleted the unicode_static_inline2 branch April 21, 2022 21:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

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

Assignees

No one assigned

Labels

awaiting core review skip news

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.

3 participants

@vstinner @erlend-aasland @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.