Movatterモバイル変換


[0]ホーム

URL:


Up one LevelPython Library ReferenceContentsModule IndexIndex

27.2codeop -- Compile Python code

Thecodeop module provides utilities upon which the Pythonread-eval-print loop can be emulated, as is done in thecode module. As a result, you probably don't want to usethe module directly; if you want to include such a loop in yourprogram you probably want to use thecode module instead.

There are two parts to this job:

  1. Being able to tell if a line of input completes a Python statement: in short, telling whether to print `>>> ' or `... ' next.
  2. Remembering which future statements the user has entered, so subsequent input can be compiled with these in effect.

Thecodeop module provides a way of doing each of thesethings, and a way of doing them both.

To do just the former:

compile_command(source[, filename[, symbol]])
Tries to compilesource, which should be a string of Pythoncode and return a code object ifsource is validPython code. In that case, the filename attribute of the code objectwill befilename, which defaults to'<input>'.ReturnsNone ifsource isnot valid Pythoncode, but is a prefix of valid Python code.

If there is a problem withsource, an exception will be raised.SyntaxError is raised if there is invalid Python syntax,andOverflowError orValueError if there is aninvalid literal.

Thesymbol argument determines whethersource is compiledas a statement ('single', the default) or as an expression('eval'). Any other value will causeValueError to be raised.

Caveat:It is possible (but not likely) that the parser stops parsingwith a successful outcome before reaching the end of the source;in this case, trailing symbols may be ignored instead of causing anerror. For example, a backslash followed by two newlines may befollowed by arbitrary garbage. This will be fixed once the APIfor the parser is better.

class Compile()
Instances of this class have__call__() methods identical insignature to the built-in functioncompile(), but with thedifference that if the instance compiles program text containing a__future__ statement, the instance 'remembers' and compilesall subsequent program texts with the statement in force.

class CommandCompiler()
Instances of this class have__call__() methods identical insignature tocompile_command(); the difference is that ifthe instance compiles program text containing a__future__statement, the instance 'remembers' and compiles all subsequentprogram texts with the statement in force.

A note on version compatibility: theCompile andCommandCompiler are new in Python 2.2. If you want to enablethe future-tracking features of 2.2 but also retain compatibility with2.1 and earlier versions of Python you can either write

try:    from codeop import CommandCompiler    compile_command = CommandCompiler()    del CommandCompilerexcept ImportError:    from codeop import compile_command

which is a low-impact change, but introduces possibly unwanted globalstate into your program, or you can write:

try:    from codeop import CommandCompilerexcept ImportError:    def CommandCompiler():        from codeop import compile_command        return compile_command

and then callCommandCompiler every time you need a freshcompiler object.


Up one LevelPython Library ReferenceContentsModule IndexIndex

Release 2.5.2, documentation updated on 21st February, 2008.
SeeAbout this document... for information on suggesting changes.
[8]ページ先頭

©2009-2025 Movatter.jp