home *** CD-ROM | disk | FTP | other *** search
/ Chip 2003 January / Chip_2003-01_cd2.bin / convert / eJayMp3Pro / mp3pro_demo.exe / CODEOP.PYC (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2000-06-06  |  3.0 KB  |  105 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 1.5)
  3.  
  4. '''Utility to compile possibly incomplete Python source code.'''
  5. import sys
  6. import string
  7. import traceback
  8.  
  9. def compile_command(source, filename = '<input>', symbol = 'single'):
  10.     '''Compile a command and determine whether it is incomplete.
  11.  
  12.     Arguments:
  13.  
  14.     source -- the source string; may contain \\n characters
  15.     filename -- optional filename from which source was read; default "<input>"
  16.     symbol -- optional grammar start symbol; "single" (default) or "eval"
  17.  
  18.     Return value / exceptions raised:
  19.  
  20.     - Return a code object if the command is complete and valid
  21.     - Return None if the command is incomplete
  22.     - Raise SyntaxError or OverflowError if the command is a syntax error
  23.       (OverflowError if the error is in a numeric constant)
  24.  
  25.     Approach:
  26.  
  27.     First, check if the source consists entirely of blank lines and
  28.     comments; if so, replace it with \'pass\', because the built-in
  29.     parser doesn\'t always do the right thing for these.
  30.  
  31.     Compile three times: as is, with \\n, and with \\n\\n appended.  If
  32.     it compiles as is, it\'s complete.  If it compiles with one \\n
  33.     appended, we expect more.  If it doesn\'t compile either way, we
  34.     compare the error we get when compiling with \\n or \\n\\n appended.
  35.     If the errors are the same, the code is broken.  But if the errors
  36.     are different, we expect more.  Not intuitive; not even guaranteed
  37.     to hold in future releases; but this matches the compiler\'s
  38.     behavior from Python 1.4 through 1.5.2, at least.
  39.  
  40.     Caveat:
  41.  
  42.     It is possible (but not likely) that the parser stops parsing
  43.     with a successful outcome before reaching the end of the source;
  44.     in this case, trailing symbols may be ignored instead of causing an
  45.     error.  For example, a backslash followed by two newlines may be
  46.     followed by arbitrary garbage.  This will be fixed once the API
  47.     for the parser is better.
  48.  
  49.     '''
  50.     for line in string.split(source, '\n'):
  51.         line = string.strip(line)
  52.     else:
  53.         source = 'pass'
  54.     
  55.     try:
  56.         code = compile(source, filename, symbol)
  57.     except SyntaxError:
  58.         err = err1 = err2 = err = err1 = err2 = None
  59.         err = err1 = err2 = err = err1 = err2 = None
  60.     except:
  61.         code = code1 = code2 = None
  62.  
  63.     
  64.     try:
  65.         code1 = compile(source + '\n', filename, symbol)
  66.     except SyntaxError:
  67.         err = err1 = err2 = err = err1 = err2 = None
  68.         err = err1 = err2 = err = err1 = err2 = None
  69.     except:
  70.         code = code1 = code2 = None
  71.  
  72.     
  73.     try:
  74.         code2 = compile(source + '\n\n', filename, symbol)
  75.     except SyntaxError:
  76.         err = err1 = err2 = err = err1 = err2 = None
  77.         err = err1 = err2 = err = err1 = err2 = None
  78.     except:
  79.         code = code1 = code2 = None
  80.  
  81.     
  82.     try:
  83.         e1 = err1.__dict__
  84.     except AttributeError:
  85.         None if line and line[0] != '#' else string.split(source, '\n') if code else err = err1 = err2 = err = err1 = err2 = None
  86.         None if line and line[0] != '#' else string.split(source, '\n') if code else err = err1 = err2 = err = err1 = err2 = None
  87.         e1 = err1
  88.     except:
  89.         None if line and line[0] != '#' else string.split(source, '\n') if code else err = err1 = err2 = err = err1 = err2 = None
  90.  
  91.     
  92.     try:
  93.         e2 = err2.__dict__
  94.     except AttributeError:
  95.         None if line and line[0] != '#' else string.split(source, '\n') if code else err = err1 = err2 = err = err1 = err2 = None
  96.         None if line and line[0] != '#' else string.split(source, '\n') if code else err = err1 = err2 = err = err1 = err2 = None
  97.         e2 = err2
  98.     except:
  99.         None if line and line[0] != '#' else string.split(source, '\n') if code else err = err1 = err2 = err = err1 = err2 = None
  100.  
  101.     if not code1 and e1 == e2:
  102.         raise SyntaxError, err1
  103.     
  104.  
  105.