| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`codeop` --- Compile Python code | 
 | 2 | ===================================== | 
 | 3 |  | 
 | 4 | .. module:: codeop | 
 | 5 |    :synopsis: Compile (possibly incomplete) Python code. | 
 | 6 | .. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il> | 
 | 7 | .. sectionauthor:: Michael Hudson <mwh@python.net> | 
 | 8 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 9 | The :mod:`codeop` module provides utilities upon which the Python | 
 | 10 | read-eval-print loop can be emulated, as is done in the :mod:`code` module.  As | 
 | 11 | a result, you probably don't want to use the module directly; if you want to | 
 | 12 | include such a loop in your program you probably want to use the :mod:`code` | 
 | 13 | module instead. | 
 | 14 |  | 
 | 15 | There are two parts to this job: | 
 | 16 |  | 
 | 17 | #. Being able to tell if a line of input completes a Python  statement: in | 
 | 18 |    short, telling whether to print '``>>>``' or '``...``' next. | 
 | 19 |  | 
 | 20 | #. Remembering which future statements the user has entered, so  subsequent | 
 | 21 |    input can be compiled with these in effect. | 
 | 22 |  | 
 | 23 | The :mod:`codeop` module provides a way of doing each of these things, and a way | 
 | 24 | of doing them both. | 
 | 25 |  | 
 | 26 | To do just the former: | 
 | 27 |  | 
| Georg Brandl | c2a4f4f | 2009-04-10 09:03:43 +0000 | [diff] [blame] | 28 | .. function:: compile_command(source, filename="<input>", symbol="single") | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 29 |  | 
 | 30 |    Tries to compile *source*, which should be a string of Python code and return a | 
 | 31 |    code object if *source* is valid Python code. In that case, the filename | 
 | 32 |    attribute of the code object will be *filename*, which defaults to | 
 | 33 |    ``'<input>'``. Returns ``None`` if *source* is *not* valid Python code, but is a | 
 | 34 |    prefix of valid Python code. | 
 | 35 |  | 
 | 36 |    If there is a problem with *source*, an exception will be raised. | 
 | 37 |    :exc:`SyntaxError` is raised if there is invalid Python syntax, and | 
 | 38 |    :exc:`OverflowError` or :exc:`ValueError` if there is an invalid literal. | 
 | 39 |  | 
 | 40 |    The *symbol* argument determines whether *source* is compiled as a statement | 
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 41 |    (``'single'``, the default) or as an :term:`expression` (``'eval'``).  Any | 
 | 42 |    other value will cause :exc:`ValueError` to  be raised. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 43 |  | 
| Georg Brandl | e720c0a | 2009-04-27 16:20:50 +0000 | [diff] [blame] | 44 |    .. note:: | 
| Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 45 |  | 
| Guido van Rossum | da27fd2 | 2007-08-17 00:24:54 +0000 | [diff] [blame] | 46 |       It is possible (but not likely) that the parser stops parsing with a | 
 | 47 |       successful outcome before reaching the end of the source; in this case, | 
 | 48 |       trailing symbols may be ignored instead of causing an error.  For example, | 
 | 49 |       a backslash followed by two newlines may be followed by arbitrary garbage. | 
 | 50 |       This will be fixed once the API for the parser is better. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 51 |  | 
 | 52 |  | 
 | 53 | .. class:: Compile() | 
 | 54 |  | 
 | 55 |    Instances of this class have :meth:`__call__` methods identical in signature to | 
 | 56 |    the built-in function :func:`compile`, but with the difference that if the | 
 | 57 |    instance compiles program text containing a :mod:`__future__` statement, the | 
 | 58 |    instance 'remembers' and compiles all subsequent program texts with the | 
 | 59 |    statement in force. | 
 | 60 |  | 
 | 61 |  | 
 | 62 | .. class:: CommandCompiler() | 
 | 63 |  | 
 | 64 |    Instances of this class have :meth:`__call__` methods identical in signature to | 
 | 65 |    :func:`compile_command`; the difference is that if the instance compiles program | 
 | 66 |    text containing a ``__future__`` statement, the instance 'remembers' and | 
 | 67 |    compiles all subsequent program texts with the statement in force. |