You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Expose C level compiler flag constants to Python code #68656
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:
assignee=Noneclosed_at=Nonecreated_at=<Date2015-06-19.04:47:33.778>labels= ['type-feature']
title='Expose C level compiler flag constants to Python code'updated_at=<Date2015-07-03.00:56:21.016>user='https://github.com/ncoghlan'
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?
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.
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).
changed the title [-]Expose compiler flag constants as code object attributes[/-][+]Expose C level compiler flag constants to Python code[/+]on Jun 20, 2015
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.
Activity
ncoghlan commentedon Jun 19, 2015
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?
larryhastings commentedon Jun 19, 2015
Probably, though I want to see a sample implementation before I agree to anything.
ncoghlan commentedon Jun 19, 2015
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.
1st1 commentedon Jun 19, 2015
Nick, Larry, please take a look at the attached patch.
ncoghlan commentedon Jun 20, 2015
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).
[-]Expose compiler flag constants as code object attributes[/-][+]Expose C level compiler flag constants to Python code[/+]1st1 commentedon Jun 20, 2015
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.
ncoghlan commentedon Jun 21, 2015
Making _opcode a builtin module rather than an extension module makes more sense to me than adding more random stuff to the sys module.
larryhastings commentedon Jul 3, 2015
opcode.patch is okay for 3.5.
pythongh-68656: Pass CO_* constants for inspect from C; deprecate CO_…
pythongh-68656: Pass CO_* constants for inspect from C; deprecate CO_…