New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expose C level compiler flag constants to Python code #68656
Comments
As part of the PEP-492 implementation, Yury has needed to hardcode compile flag contants in various places, with adjacent comments explaining what the magic numbers mean. It occurred to me that there's a way we could make those constants readily available to any code manipulating code objects: expose them as read-only attributes via the code object type. Does this seem like a reasonable idea? If yes, would it be reasonable to classify it as part of the PEP-492 implementation process and include it during the 3.5 beta cycle? |
Probably, though I want to see a sample implementation before I agree to anything. |
In my last set of review comments on bpo-24400 I suggested changing the Python level attributes for coroutine objects to cr_frame, cr_code, and cr_running. It's possible that may provide a different way to eliminate some of the current compiler flag checks. |
Nick, Larry, please take a look at the attached patch. |
Bringing a design discussion back from the code review, since I didn't explain the problem to be solved very well, and instead jumped straight to recommending a specific solution. Currently, dis has a dictionary mapping from hardcoded compiler flag values (in decimal, no less!) to flag names. The inspect module then iterates over this dictionary at import time to publish the CO_* constants as module level attributes in inspect. Neither dis.COMPILER_FLAG_NAMES nor the inspect.CO_* constants are documented, and the *C* level flag values aren't published to the Python layer anywhere (hence the hardcoding of the magic numbers in dis). For the kind of code that needs to interrogate the flags in the bpo-24400 patch, inspect and dis are also not appropriate things to adopt as dependencies. However, I like Larry's suggestion of adopting the _opcode module as our standard vector for passing this information up from the C layer to the Python layer better than my original idea of using code objects themselves. The opcode values themselves could potentially also be exposed that way (they're currently duplicated by hand in Lib/opcode.py). |
Larry, Nick, Attached patch exposes CO* constants in the '_opcode' module. There is one slight complication though: importing '_opcode' in 'types' (required for bpo-24400) breaks Python compilation, as 'types' module is used by many tools at the point where '_opcode' module isn't yet compiled. We should either make '_opcode' compiled earlier or move constants to the 'sys' module. |
Making _opcode a builtin module rather than an extension module makes more sense to me than adding more random stuff to the sys module. |
opcode.patch is okay for 3.5. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: