/Doc/library/codeop.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 93 lines · 67 code · 26 blank · 0 comment · 0 complexity · bc4c06ff93c7e7a50768a6fe19ac9bcc MD5 · raw file

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