Skip to content

Expose C level compiler flag constants to Python code #68656

Open

Description

@ncoghlan
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']

    Linked PRs

    Activity

    1. ncoghlan commented on Jun 19, 2015

      @ncoghlan
      ContributorAuthor

      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?

    2. larryhastings commented on Jun 19, 2015

      @larryhastings
      Contributor

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

    3. ncoghlan commented on Jun 19, 2015

      @ncoghlan
      ContributorAuthor

      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.

    4. 1st1 commented on Jun 19, 2015

      @1st1
      Member

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

    5. ncoghlan commented on Jun 20, 2015

      @ncoghlan
      ContributorAuthor

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

    6. changed the title [-]Expose compiler flag constants as code object attributes[/-] [+]Expose C level compiler flag constants to Python code[/+] on Jun 20, 2015
    7. 1st1 commented on Jun 20, 2015

      @1st1
      Member

      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.

    8. ncoghlan commented on Jun 21, 2015

      @ncoghlan
      ContributorAuthor

      Making _opcode a builtin module rather than an extension module makes more sense to me than adding more random stuff to the sys module.

    9. larryhastings commented on Jul 3, 2015

      @larryhastings
      Contributor

      opcode.patch is okay for 3.5.

    10. transferred this issue fromon Apr 10, 2022
    11. added 2 commits that reference this issue on Oct 15, 2026
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      interpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions