home *** CD-ROM | disk | FTP | other *** search
/ PC Extra 07 & 08 / pca1507.iso / Software / psp8 / Data1.cab / codeop.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2003-04-22  |  7.1 KB  |  191 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.2)
  3.  
  4. """Utilities to compile possibly incomplete Python source code.
  5.  
  6. This module provides two interfaces, broadly similar to the builtin
  7. function compile(), that take progam text, a filename and a 'mode'
  8. and:
  9.  
  10. - Return a code object if the command is complete and valid
  11. - Return None if the command is incomplete
  12. - Raise SyntaxError, ValueError or OverflowError if the command is a
  13.   syntax error (OverflowError and ValueError can be produced by
  14.   malformed literals).
  15.  
  16. Approach:
  17.  
  18. First, check if the source consists entirely of blank lines and
  19. comments; if so, replace it with 'pass', because the built-in
  20. parser doesn't always do the right thing for these.
  21.  
  22. Compile three times: as is, with \\n, and with \\n\\n appended.  If it
  23. compiles as is, it's complete.  If it compiles with one \\n appended,
  24. we expect more.  If it doesn't compile either way, we compare the
  25. error we get when compiling with \\n or \\n\\n appended.  If the errors
  26. are the same, the code is broken.  But if the errors are different, we
  27. expect more.  Not intuitive; not even guaranteed to hold in future
  28. releases; but this matches the compiler's behavior from Python 1.4
  29. through 2.2, at least.
  30.  
  31. Caveat:
  32.  
  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
  35. case, trailing symbols may be ignored instead of causing an error.
  36. For example, a backslash followed by two newlines may be followed by
  37. arbitrary garbage.  This will be fixed once the API for the parser is
  38. better.
  39.  
  40. The two interfaces are:
  41.  
  42. compile_command(source, filename, symbol):
  43.  
  44.     Compiles a single command in the manner described above.
  45.  
  46. CommandCompiler():
  47.  
  48.     Instances of this class have __call__ methods identical in
  49.     signature to compile_command; the difference is that if the
  50.     instance compiles program text containing a __future__ statement,
  51.     the instance 'remembers' and compiles all subsequent program texts
  52.     with the statement in force.
  53.  
  54. The module also provides another class:
  55.  
  56. Compile():
  57.  
  58.     Instances of this class act like the built-in function compile,
  59.     but with 'memory' in the sense described above.
  60. """
  61. import __future__
  62. _features = [ getattr(__future__, fname) for fname in __future__.all_feature_names ]
  63. __all__ = [
  64.     'compile_command',
  65.     'Compile',
  66.     'CommandCompiler']
  67.  
  68. def _maybe_compile(compiler, source, filename, symbol):
  69.     for line in source.split('\n'):
  70.         line = line.strip()
  71.         if line and line[0] != '#':
  72.             break
  73.         
  74.     else:
  75.         source = 'pass'
  76.     err = err1 = err2 = None
  77.     code = code1 = code2 = None
  78.     
  79.     try:
  80.         code = compiler(source, filename, symbol)
  81.     except SyntaxError:
  82.         err = None
  83.  
  84.     
  85.     try:
  86.         code1 = compiler(source + '\n', filename, symbol)
  87.     except SyntaxError:
  88.         err1 = None
  89.  
  90.     
  91.     try:
  92.         code2 = compiler(source + '\n\n', filename, symbol)
  93.     except SyntaxError:
  94.         err2 = None
  95.  
  96.     if code:
  97.         return code
  98.     
  99.     
  100.     try:
  101.         e1 = err1.__dict__
  102.     except AttributeError:
  103.         e1 = err1
  104.  
  105.     
  106.     try:
  107.         e2 = err2.__dict__
  108.     except AttributeError:
  109.         e2 = err2
  110.  
  111.     if not code1 and e1 == e2:
  112.         raise SyntaxError, err1
  113.     
  114.  
  115.  
  116. def compile_command(source, filename = '<input>', symbol = 'single'):
  117.     '''Compile a command and determine whether it is incomplete.
  118.  
  119.     Arguments:
  120.  
  121.     source -- the source string; may contain \\n characters
  122.     filename -- optional filename from which source was read; default
  123.                 "<input>"
  124.     symbol -- optional grammar start symbol; "single" (default) or "eval"
  125.  
  126.     Return value / exceptions raised:
  127.  
  128.     - Return a code object if the command is complete and valid
  129.     - Return None if the command is incomplete
  130.     - Raise SyntaxError, ValueError or OverflowError if the command is a
  131.       syntax error (OverflowError and ValueError can be produced by
  132.       malformed literals).
  133.     '''
  134.     return _maybe_compile(compile, source, filename, symbol)
  135.  
  136.  
  137. class Compile:
  138.     '''Instances of this class behave much like the built-in compile
  139.     function, but if one is used to compile text containing a future
  140.     statement, it "remembers" and compiles all subsequent program texts
  141.     with the statement in force.'''
  142.     
  143.     def __init__(self):
  144.         self.flags = 0
  145.  
  146.     
  147.     def __call__(self, source, filename, symbol):
  148.         codeob = compile(source, filename, symbol, self.flags, 1)
  149.         for feature in _features:
  150.             if codeob.co_flags & feature.compiler_flag:
  151.                 self.flags |= feature.compiler_flag
  152.             
  153.         
  154.         return codeob
  155.  
  156.  
  157.  
  158. class CommandCompiler:
  159.     """Instances of this class have __call__ methods identical in
  160.     signature to compile_command; the difference is that if the
  161.     instance compiles program text containing a __future__ statement,
  162.     the instance 'remembers' and compiles all subsequent program texts
  163.     with the statement in force."""
  164.     
  165.     def __init__(self):
  166.         self.compiler = Compile()
  167.  
  168.     
  169.     def __call__(self, source, filename = '<input>', symbol = 'single'):
  170.         '''Compile a command and determine whether it is incomplete.
  171.  
  172.         Arguments:
  173.  
  174.         source -- the source string; may contain \\n characters
  175.         filename -- optional filename from which source was read;
  176.                     default "<input>"
  177.         symbol -- optional grammar start symbol; "single" (default) or
  178.                   "eval"
  179.  
  180.         Return value / exceptions raised:
  181.  
  182.         - Return a code object if the command is complete and valid
  183.         - Return None if the command is incomplete
  184.         - Raise SyntaxError, ValueError or OverflowError if the command is a
  185.           syntax error (OverflowError and ValueError can be produced by
  186.           malformed literals).
  187.         '''
  188.         return _maybe_compile(self.compiler, source, filename, symbol)
  189.  
  190.  
  191.