GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — bpo-45490: Convert unicodeobject.h macros to static inline functions by vstinner · Pull Request #31221 · python/cpython Conversation
|
|
@erlend-aasland: All in one PR to convert (almost) all macros of Include/cpython/unicodeobject.h. I created a single PR to show what can be done with PEP 670, but IMO it will be better to split this large PR into smaller PRs to ease review, and apply (minor) API changes / cleanup in following PRs (not do everything at once). |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Sounds good. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
IMO, this is a great improvement when it comes to readability/maintainability. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| Return NULL if malloc fails and an empty string if invalid characters | ||
| are found. */ | ||
| static char * | ||
| numeric_as_ascii(const PyObject *u, int strip_ws, int ignore_underscores) |
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Do we really need to remove const? Ditto for the rest of the PR.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
If the "u" string is not ready, PyUnicode_READY() will modify it. It's not a read-only operation.
In Python 3.12, PyUnicode_WCHAR_KIND will be removed: https://www.python.org/dev/peps/pep-0623/
In the meanwhile, I prefer to not stop lying: we do modify the object :-)
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
not a whole review, just dropping some notes.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| return _PyASCIIObject_CAST(op)->length; | ||
| } | ||
|
|
||
| /* In the access macros below, "kind" may be evaluated more than once. |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
presumably update comments like these to just mention that they used to be macros with these caveats in previous python versions.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Oh thanks, I didn't look at comments at all. I laser focused on macros code and make sure that I don't change the code :-)
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| if (kind == PyUnicode_1BYTE_KIND) { | ||
| return PyUnicode_1BYTE_DATA(unicode)[index]; | ||
| } | ||
| else if (PyUnicode_KIND(unicode) == PyUnicode_2BYTE_KIND) { |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
as a function this no longer needs to be called twice. (and the comment above becomes less true)
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| /* Use only if you know it's a string */ | ||
| #define PyUnicode_CHECK_INTERNED(op) \ | ||
| (((PyASCIIObject *)(op))->state.interned) | ||
| static inline int PyUnicode_CHECK_INTERNED(PyObject *op) { |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
to avoid the cast going away, consider doing what Py_INCREF did & add indirection through a macro for the cast:
#define PyUnicode_CHECK_INTERNED(op) \
_PyUnicode_CHECK_INTERNED(_PyASCIIObject_CAST(op))
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
The SC asked to not add such macro :-) You're right that without such macro, there is a risk of introducing new compiler warnings.
If possible I would prefer to keep PyObject* for functions in unicodeobject.c, since it's the type used for arguments in existing functions and the type returned by functions creating strings like PyUnicode_New(), PyUnicode_FromString(), etc.
Maybe for this specific header file, we can avoid casts.
For me, PyASCIIObject is an implementation detail which should be hidden. If possible, it should even be moved to the internal C API, but that's a way larger topic which may require a PEP ;-)
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
The
Changing |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Macros not casting their arguments:
Macro casting its argument to
Macros using a cast in their implementation:
The majority of macros use PyUnicode_READ() and PyUnicode_WRITE() expect (kind, data, index) and (kind, data, index, value) arguments: no Python object. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
This PR was an example. I updated PEP 670 from the discussion on this PR. If PEP 670 is accepted, I will rewrite this PR with smaller changes to ease the review. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.