Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1 | |
| 2 | :mod:`codeop` --- Compile Python code |
| 3 | ===================================== |
| 4 | |
| 5 | .. module:: codeop |
| 6 | :synopsis: Compile (possibly incomplete) Python code. |
| 7 | .. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il> |
| 8 | .. sectionauthor:: Michael Hudson <mwh@python.net> |
| 9 | |
| 10 | |
| 11 | .. % LaTeXed from excellent doc-string. |
| 12 | |
| 13 | The :mod:`codeop` module provides utilities upon which the Python |
| 14 | read-eval-print loop can be emulated, as is done in the :mod:`code` module. As |
| 15 | a result, you probably don't want to use the module directly; if you want to |
| 16 | include such a loop in your program you probably want to use the :mod:`code` |
| 17 | module instead. |
| 18 | |
| 19 | There are two parts to this job: |
| 20 | |
| 21 | #. Being able to tell if a line of input completes a Python statement: in |
| 22 | short, telling whether to print '``>>>``' or '``...``' next. |
| 23 | |
| 24 | #. Remembering which future statements the user has entered, so subsequent |
| 25 | input can be compiled with these in effect. |
| 26 | |
| 27 | The :mod:`codeop` module provides a way of doing each of these things, and a way |
| 28 | of doing them both. |
| 29 | |
| 30 | To do just the former: |
| 31 | |
| 32 | |
| 33 | .. function:: compile_command(source[, filename[, symbol]]) |
| 34 | |
| 35 | Tries to compile *source*, which should be a string of Python code and return a |
| 36 | code object if *source* is valid Python code. In that case, the filename |
| 37 | attribute of the code object will be *filename*, which defaults to |
| 38 | ``'<input>'``. Returns ``None`` if *source* is *not* valid Python code, but is a |
| 39 | prefix of valid Python code. |
| 40 | |
| 41 | If there is a problem with *source*, an exception will be raised. |
| 42 | :exc:`SyntaxError` is raised if there is invalid Python syntax, and |
| 43 | :exc:`OverflowError` or :exc:`ValueError` if there is an invalid literal. |
| 44 | |
| 45 | The *symbol* argument determines whether *source* is compiled as a statement |
| 46 | (``'single'``, the default) or as an expression (``'eval'``). Any other value |
| 47 | will cause :exc:`ValueError` to be raised. |
| 48 | |
Georg Brandl | bf863b1 | 2007-08-15 19:06:04 +0000 | [diff] [blame^] | 49 | .. warning:: |
| 50 | |
| 51 | It is possible (but not likely) that the parser stops parsing with a |
| 52 | successful outcome before reaching the end of the source; in this case, |
| 53 | trailing symbols may be ignored instead of causing an error. For example, |
| 54 | a backslash followed by two newlines may be followed by arbitrary garbage. |
| 55 | This will be fixed once the API for the parser is better. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 56 | |
| 57 | |
| 58 | .. class:: Compile() |
| 59 | |
| 60 | Instances of this class have :meth:`__call__` methods identical in signature to |
| 61 | the built-in function :func:`compile`, but with the difference that if the |
| 62 | instance compiles program text containing a :mod:`__future__` statement, the |
| 63 | instance 'remembers' and compiles all subsequent program texts with the |
| 64 | statement in force. |
| 65 | |
| 66 | |
| 67 | .. class:: CommandCompiler() |
| 68 | |
| 69 | Instances of this class have :meth:`__call__` methods identical in signature to |
| 70 | :func:`compile_command`; the difference is that if the instance compiles program |
| 71 | text containing a ``__future__`` statement, the instance 'remembers' and |
| 72 | compiles all subsequent program texts with the statement in force. |
| 73 | |
| 74 | A note on version compatibility: the :class:`Compile` and |
| 75 | :class:`CommandCompiler` are new in Python 2.2. If you want to enable the |
| 76 | future-tracking features of 2.2 but also retain compatibility with 2.1 and |
| 77 | earlier versions of Python you can either write :: |
| 78 | |
| 79 | try: |
| 80 | from codeop import CommandCompiler |
| 81 | compile_command = CommandCompiler() |
| 82 | del CommandCompiler |
| 83 | except ImportError: |
| 84 | from codeop import compile_command |
| 85 | |
| 86 | which is a low-impact change, but introduces possibly unwanted global state into |
| 87 | your program, or you can write:: |
| 88 | |
| 89 | try: |
| 90 | from codeop import CommandCompiler |
| 91 | except ImportError: |
| 92 | def CommandCompiler(): |
| 93 | from codeop import compile_command |
| 94 | return compile_command |
| 95 | |
| 96 | and then call ``CommandCompiler`` every time you need a fresh compiler object. |
| 97 | |