py/binary: Reorganize and refactor definitions for typecode letters. #18590
+198
−165
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.
Summary
This PR collects together all of the macro constants associated with the "typecode" value used across several different modules to encode information about the type identity of an associated pointer or memory buffer. These values were previously scattered across the codebase, often leading to confusion about the C API, resulting in e.g. defective memoryview objects with no typecode except for the read-write flag.
With this change:
MP_TYPECODE_C(custom_typedefed_int_t)would automatically pick the appropriate typecode letter at compile time thanks to the semantics of_Generic.'q'means, but it's still a lot easier to spot if you instead write it like:Testing
I've run the test suite across unix and rp2 on my own hardware. All extant tests pass; though as a pure refactor, this change can't be checked directly.
Some of the changes touch port-specific code for devices I don't have access to; hopefully CI will catch anything aggressively defective, but this ought to be tested on those other devices.
Trade-offs and Alternatives
Using
_Genericlimits compatibility to C11 and above. If that's a problem, potentially separate macros for each type could be declared instead --- but that's only marginally useful than single-letter typecodes.Potentially, it would be better to use the stdint names for the typecodes, i.e.
uint8_tinstead ofunsigned char; and in several of the places I've refactored there might be a more semantic typename to use as the argument --- the actual typename of the associated buffer, or perhaps even atypeofexpression involving that buffer.