Ronald Oussoren via Python-Dev schrieb am 03.02.22 um 14:46:
On 2 Feb 2022, at 23:41, Eric Snow wrote:
* a little less convenient: adding a global string requires modifying
a separate file from the one where you actually want to use the string
* strings can get "orphaned" (I'm planning on checking in CI)
* some strings may never get used for any given ./python invocation
(not that big a difference though)

The first two cons can probably be fixed by adding some indirection, with some
markers at the place of use and a script that uses those to generate the
C definitions.

Although my gut feeling is that adding a the CI check you mention is good
enough and adding the tooling for generating code isn’t worth the additional
complexity.

It's what we do in Cython, and it works really well there. It's very straight forward, you just write something like

    PYUNICODE("some text here")
    PYIDENT("somename")

in your C code and Cython creates a deduplicated global string table from them and replaces the string constants with the corresponding global variables. (We have two different names because an identifier in Py2 is 'str', not 'unicode'.)

Now, the thing with CPython is that the C sources where the replacement would take place are VCS controlled. And a script that replaces the identifiers would have to somehow make sure that the new references do not get renamed, which would lead to non-local changes when strings are added.

What you could try is to number the identifiers, i.e. use a macro like

    _Py_STR(123, "some text here")

where you manually add a new identifier as

    _Py_STR("some text here")

and the number is filled in automatically by a script that finds all of them, deduplicates, and adds new identifiers at the end, adding 1 to the maximum number that it finds. That makes sure that identifiers that already have an ID number will not be touched, deleted strings disappear automatically, and non-local changes are prevented.

Defining the _Py_STR() macro as

   #define _Py_STR(id, text)  (_Py_global_string_table[id])

or

   #define _Py_STR(id, text)  (_Py_global_string_table##id)

would also give you a compile error if someone forgets to run the script.

Stefan

_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/LD3JM2NQ5ZUZDK63RH4IVZPCZ7HC4X3G/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to