Skip to content
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

Open
ncoghlan opened this issue Jun 19, 2015 · 8 comments
Open

Expose C level compiler flag constants to Python code #68656

ncoghlan opened this issue Jun 19, 2015 · 8 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement

Comments

@ncoghlan
Copy link
Contributor

BPO 24468
Nosy @gvanrossum, @ncoghlan, @larryhastings, @1st1
Dependencies
  • bpo-24400: Awaitable ABC incompatible with functools.singledispatch
  • Files
  • code.patch
  • opcode.patch
  • 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 = None
    closed_at = None
    created_at = <Date 2015-06-19.04:47:33.778>
    labels = ['type-feature']
    title = 'Expose C level compiler flag constants to Python code'
    updated_at = <Date 2015-07-03.00:56:21.016>
    user = 'https://github.com/ncoghlan'

    bugs.python.org fields:

    activity = <Date 2015-07-03.00:56:21.016>
    actor = 'larry'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = []
    creation = <Date 2015-06-19.04:47:33.778>
    creator = 'ncoghlan'
    dependencies = ['24400']
    files = ['39741', '39752']
    hgrepos = []
    issue_num = 24468
    keywords = ['patch']
    message_count = 8.0
    messages = ['245487', '245488', '245489', '245510', '245541', '245573', '245580', '246115']
    nosy_count = 5.0
    nosy_names = ['gvanrossum', 'ncoghlan', 'larry', 'yselivanov', 'superluser']
    pr_nums = []
    priority = 'normal'
    resolution = None
    stage = 'needs patch'
    status = 'open'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue24468'
    versions = ['Python 3.5', 'Python 3.6']

    @ncoghlan
    Copy link
    Contributor Author

    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?

    @ncoghlan ncoghlan added the type-feature A feature request or enhancement label Jun 19, 2015
    @larryhastings
    Copy link
    Contributor

    Probably, though I want to see a sample implementation before I agree to anything.

    @ncoghlan
    Copy link
    Contributor Author

    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
    Copy link
    Member

    1st1 commented Jun 19, 2015

    Nick, Larry, please take a look at the attached patch.

    @ncoghlan
    Copy link
    Contributor Author

    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).

    @ncoghlan ncoghlan changed the title Expose compiler flag constants as code object attributes Expose C level compiler flag constants to Python code Jun 20, 2015
    @1st1
    Copy link
    Member

    1st1 commented 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
    Copy link
    Contributor Author

    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
    Copy link
    Contributor

    opcode.patch is okay for 3.5.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @iritkatriel iritkatriel added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Nov 27, 2023
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants