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

Add a method to importlib.abc.SourceLoader for converting source to a code object #59832

Closed
brettcannon opened this issue Aug 12, 2012 · 11 comments
Assignees
Labels
stdlib Python modules in the Lib dir

Comments

@brettcannon
Copy link
Member

BPO 15627
Nosy @brettcannon, @merwok, @asvetlov, @meadori, @Julian, @ericsnowcurrently, @1st1

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 = 'https://github.com/brettcannon'
closed_at = <Date 2012-11-18.15:23:10.323>
created_at = <Date 2012-08-12.00:44:36.137>
labels = ['library']
title = 'Add a method to importlib.abc.SourceLoader for converting source to a code object'
updated_at = <Date 2013-03-31.18:45:58.034>
user = 'https://github.com/brettcannon'

bugs.python.org fields:

activity = <Date 2013-03-31.18:45:58.034>
actor = 'eric.araujo'
assignee = 'brett.cannon'
closed = True
closed_date = <Date 2012-11-18.15:23:10.323>
closer = 'brett.cannon'
components = ['Library (Lib)']
creation = <Date 2012-08-12.00:44:36.137>
creator = 'brett.cannon'
dependencies = []
files = []
hgrepos = []
issue_num = 15627
keywords = []
message_count = 11.0
messages = ['168015', '175635', '175658', '175760', '175876', '176521', '176554', '176970', '185589', '185596', '185641']
nosy_count = 9.0
nosy_names = ['brett.cannon', 'eric.araujo', 'Arfrever', 'asvetlov', 'meador.inge', 'Julian', 'python-dev', 'eric.snow', 'yselivanov']
pr_nums = []
priority = 'low'
resolution = 'fixed'
stage = 'resolved'
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue15627'
versions = ['Python 3.4']

@brettcannon
Copy link
Member Author

A classmethod called importlib.abc.SourceLoader.source_to_code(source_bytes, path) would return a code object. By default it would be equivalent to compile(source_bytes, source_path, 'exec', dont_inherit=True), but others could override this to e.g. convert the source to an AST, optimize the AST, and then call compile() to get the desired code object.

@brettcannon brettcannon added the stdlib Python modules in the Lib dir label Aug 12, 2012
@brettcannon brettcannon self-assigned this Aug 13, 2012
@brettcannon
Copy link
Member Author

Might name this compile_source() instead.

@ericsnowcurrently
Copy link
Member

Though I haven't been able to get to this, I do have a demo of sorts that I've been using elsewhere:

https://bitbucket.org/ericsnowcurrently/pylt/src/default/pylt/_importing.py?at=default#cl-225

@brettcannon
Copy link
Member Author

We might want an keyword-only 'optimize' argument that maps to the compile() function's 'optimize' argument.

@python-dev
Copy link
Mannequin

python-dev mannequin commented Nov 18, 2012

New changeset e30bcce5c634 by Brett Cannon in branch 'default':
Issue bpo-15627: Add the compile_source() method to
http://hg.python.org/cpython/rev/e30bcce5c634

@ericsnowcurrently
Copy link
Member

Sorry, just now digging out of a massive backlog here. The originally proposed name of "source_to_code()" might be more appropriate than compile_source(). The relationship to compilation is not compulsory and the name/docstring/docs might imply that. If you're not opposed, Brett, I can make the change.

Regardless, glad to see this in. :)

@brettcannon
Copy link
Member Author

It might not be compulsory, but how else are you going to get a code object? Then again, get_code() kind of codifies the use of the word "code" for methods that return a code object.

So if you want to do the work to change the name then go ahead.

@python-dev
Copy link
Mannequin

python-dev mannequin commented Dec 5, 2012

New changeset 7caab48f171e by Eric Snow in branch 'default':
Issue bpo-15627: This is simply an update to the name of a new method recently added
http://hg.python.org/cpython/rev/7caab48f171e

@merwok
Copy link
Member

merwok commented Mar 30, 2013

The first commit does something fishy with tests: http://hg.python.org/cpython/rev/e30bcce5c634#l3.1

@brettcannon
Copy link
Member Author

It cleans up the run_unittest() call. You can check the file and see that the "missing" classes are actually not in the module anymore.

@merwok
Copy link
Member

merwok commented Mar 31, 2013

Ah, okay. I can’t wait for the time when unittest.main() is used everywhere and we delete run_unittest :)

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir
Projects
None yet
Development

No branches or pull requests

3 participants