bpo-36876: Avoid static locals.#13372
Conversation
702d6cb to
f9aca0c
Compare
May 17, 2019 00:57
asvetlov
left a comment
There was a problem hiding this comment.
asyncio part looks good
Sorry, something went wrong.
vstinner
left a comment
There was a problem hiding this comment.
move all locally scoped _Py_IDENTIFIER() to global scope
I dislike this change. I prefer to keep "local" static variables in functions. I don't see how moving them as globals is solving any issue, apart your practical issue of detecting them with your tool.
I would prefer to see an API to support static variable per interpreter.
I would suggest to develop an hash table similar to thread TLS: each static variable would have a global unique identifier, and its value would stored in an hash table in the PyInterpreterState. The problem is how to initialize the global unique identifier in a safe way (thread safety / atomicity). But that could be implemented using "local static" variables.
Sorry, something went wrong.
|
When you're done making the requested changes, leave the comment: |
Sorry, something went wrong.
I forgot to mention that "local static variables" would be easier to initialize properly, since they cannot be accessed before the function is called: before Python is initialized. The case of global static variables is worse: we cannot statically initialize them, we need something to dynamically initialize them. Maybe at the first access? About the global unique identifier, one solution would be to use the memory address of the variable: it should be unique. I don't see why a compiler would merge two variables. Draft pseudo API: Problem: we cannot pre-allocate the storage of "var" value into the interpreter "local storage" hash table, so set_interpreter_specific() can fail with a memory allocation failure :-( |
Sorry, something went wrong.
|
@ericsnowcurrently Please re-generate the ast module: |
Sorry, something went wrong.
skrah
left a comment
There was a problem hiding this comment.
As Victor has also requested, I'd remove all comments that mention that variables are process local.
Sorry, something went wrong.
|
When you're done making the requested changes, leave the comment: |
Sorry, something went wrong.
gpshead
left a comment
There was a problem hiding this comment.
overall i'm a little concerned about how fragile this is. it won't be obvious in all code why something static lives outside of the only place it is used. it feels like we need a way to prevent future C code authors from making this mistake.
Sorry, something went wrong.
|
Thanks for all the feedback, everyone! I'm going to re-think this as you've all made good points. :) I may re-open the PR if it later makes sense to re-purpose it, but I'll probably instead tackle the statics in groups by |
Sorry, something went wrong.
This patch touches a lot of files but does only two very specific things:
_Py_IDENTIFIER()to global scopeMoving these to global scope makes it easier to identify problematic usage via
Tools/c-globals/check-c-globals.py. It also has the side effect of dropping a number of duplicate_Py_IDENTIFIER(), thus saving a little bit of space. :)The overall objective is to eliminate any global state that isn't truly process-global (and to ensure the remaining global state is thread-safe). This is only one of the early steps.
https://bugs.python.org/issue36876