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:
>>> ' or `... ' next.Thecodeop module provides a way of doing each of thesethings, and a way of doing them both.
To do just the former:
| source[, filename[, symbol]]) |
'<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.
| ) |
| ) |
__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.