New submission from Brett Cannon:

With issue #15031 providing a way to take a bytecode file and get a code object 
out of it, there should probably be a comparable method that takes a code 
object and a path stats dict and returns the bytes to write out to a file.

This could also be a slight performance win for subclasses of SourceLoader 
which can read bytecode files but not write them (e.g. zip files where the cost 
of modifying a zip file is too high). That way the method could raise 
NotImplementedError (or return None) to signify it doesn't want anything 
written out by SourceLoader.get_code() and thus skip the entire step of 
preparing the bytecode file bytes instead of what happens now where set_data() 
raises NotImplementedError after the whole set of bytes is constructed.

More importantly, it would clean up py_compile once issue #15627 is in with an 
'optimize' flag as py_compile.compile() would essentially become ``source = 
loader.get_source(...); code = loader.compile_source(source, optimize=...); 
pyc_bytes = loader.create_bytecode_file(code, loader.path_stats(...))``. 
Otherwise get_code() will need to grow an 'optimize' flag and that doesn't seem 

components: Library (Lib)
messages: 175762
nosy: brett.cannon
priority: normal
severity: normal
stage: test needed
status: open
title: Method on importlib.SourceLoader for creating bytecode file 
type: enhancement
versions: Python 3.4

Python tracker <>
Python-bugs-list mailing list

Reply via email to