Skip to content

bpo-23892: Introduce sys.implementation.opt_levels #9826

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

Closed
wants to merge 2 commits into from

Conversation

csabella
Copy link
Contributor

@csabella csabella commented Oct 12, 2018

@brettcannon brettcannon removed their request for review March 27, 2019 18:29
@csabella csabella requested review from ericsnowcurrently and pfmoore and removed request for ericsnowcurrently May 14, 2019 11:56
@@ -814,6 +814,11 @@ always available.
``cache_tag`` is set to ``None``, it indicates that module caching should
be disabled.

*opt_levels* is a tuple containing the available compiler-supported
optimization levels. See :pep:`488` for more information.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that mentioning the possible values, and the relation to the "optimization" parameter of importlib.util.cache_from_source(), is perhaps warranted here (in addition to linking to PEP-488).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

The relationship between sys.flags.optimize and that "optimization" parameter (as well as the "optimize" parameter of compile()) reflects a gap in the solution presented by PEP 488. That needs to be resolved.

@taleinat
Copy link
Contributor

Looks solid overall.

@brettcannon brettcannon self-requested a review June 11, 2019 23:52
Copy link
Member

@ericsnowcurrently ericsnowcurrently left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I noted on the BPO issue, I think there are problems with the naive approach I suggested. We should discuss more there.

Also, I'd expect changes to Lib/importlib/_bootstrap_external.py...

Also, didn't zipimport get rewritten recently?

*opt_levels* is a tuple containing the available compiler-supported
optimization levels. See :pep:`488` for more information.

.. versionadded:: 3.8
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3.9 now :)

@@ -736,6 +736,7 @@ def test_implementation(self):
self.assertTrue(hasattr(sys.implementation, 'version'))
self.assertTrue(hasattr(sys.implementation, 'hexversion'))
self.assertTrue(hasattr(sys.implementation, 'cache_tag'))
self.assertTrue(hasattr(sys.implementation, 'opt_levels'))

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, we should probably also make sure that no other required attrs exist. (We could parameterize this part at the same time.)

required = [attr for attr in dir(sys.implementation) if not attr.startswith('_')]
expected = ['name', 'version', 'hexversion', 'cache_tag', 'opt_levels']
self.assertCountEqual(required, expected)
# Make sure (best effort) that sys.implementation.__dir__() is correct.
for attr in expected:
    self.assertTrue(hasattr(sys.implementation, attr))

We should also make sure that sys.implementation.opt_levels is a tuple of only non-negative ints and alphanumeric strings.

self.assertIs(type(sys.implementation.opt_levels), tuple, sys.implementation.opt_levels)
for opt in sys.implementation.opt_levels:
    if type(opt) is int:
        self.assertTrue(opt.isalnum(), opt)
    else:
        self.assertIs(type(opt), int, opt)
        self.assertGreaterEqual(opt, 0)

@@ -2034,63 +2034,44 @@ def _compile(file, optimize=-1):
return False
return True

def _check_cache(pycache, file):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I think even if this ends up not being in sys.implementation, these changes in zipfile still might be worth adding to simplify the code.

fname = arcname = file_py
# Compile py into PEP 3147 pyc file.
if _compile(file_py):
fname = pycache_opt[sys.flags.optimize]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this catch the IndexError and fall back fname = arcname = file_py?

FWIW, this code implies that sys.flags.optimize is guaranteed to be suitable as a tuple index (but such a guarantee does not exist).

Also, PEP 488 opens that door for dealing with sets of optimizations (either as a container of strings/ints or as a hash of them). This code would fall apart then.

We can discuss this more on the BPO issue.

# file name in the archive.
fname = pycache
arcname = file_pyc
break
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This "break" implies that the order of sys.implementation.opt_levels is extremely important. In a world where we deal only in sets of optimizations represented as increasing interger "levels" thats fine, but PEP 488 suggests that's not necessarily where we're headed. We should discuss this more on the BPO issue. :)

If we end up sticking with opt levels as currently used then the importance of order should be made clear in the docs.

@brettcannon brettcannon removed their request for review June 28, 2019 00:27
@csabella csabella closed this Jun 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants