cpython icon indicating copy to clipboard operation
cpython copied to clipboard

gh-112068: C API: Add support of nullable arguments in PyArg_Parse (suffix)

Open serhiy-storchaka opened this issue 1 year ago • 2 comments

This is a variant of #121187, but with suffix instead of prefix. i? instead of ?i.

  • Issue: gh-112068

serhiy-storchaka avatar Jul 03 '24 05:07 serhiy-storchaka

You essentially need to add that macro after every flag that is parsed instead of handling it as if it were a regular character.

In case of multicharacter format unit you need to reorganize the code and add new code at all right places. This is actually simpler than I thought, because the same macros can be used in all cases (except (...)?), but this is still more complex than the prefix implementation, and more error-prone.

For (...)?, when it encounters an opening (, it needs to search the closing ) and look if there is a following ?. Note that parentheses can be nested, so this is not so trivial. If the closing )? is found and the argument is None, it should scan the format string again and skip PyArg_Parse() arguments corresponded to nested format units. The cost of additional scanning is played every time, even if the new feature is not used. Well, this can be optimized for common cases, but at the cost of more complicated code.

serhiy-storchaka avatar Jul 03 '24 15:07 serhiy-storchaka

I managed to implement (...)? without affecting performance. But the code is still more complex. I need to check it many more times.

serhiy-storchaka avatar Jul 04 '24 08:07 serhiy-storchaka

Added the documentation. It is now ready for review.

serhiy-storchaka avatar Jan 08 '25 17:01 serhiy-storchaka