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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.2)
  3.  
  4. '''Routine to "compile" a .py file to a .pyc (or .pyo) file.
  5.  
  6. This module has intimate knowledge of the format of .pyc files.
  7. '''
  8. import imp
  9. MAGIC = imp.get_magic()
  10. __all__ = [
  11.     'compile']
  12.  
  13. def wr_long(f, x):
  14.     '''Internal; write a 32-bit int to a file in little-endian order.'''
  15.     f.write(chr(x & 255))
  16.     f.write(chr(x >> 8 & 255))
  17.     f.write(chr(x >> 16 & 255))
  18.     f.write(chr(x >> 24 & 255))
  19.  
  20.  
  21. def compile(file, cfile = None, dfile = None):
  22.     """Byte-compile one Python source file to Python bytecode.
  23.  
  24.     Arguments:
  25.  
  26.     file:  source filename
  27.     cfile: target filename; defaults to source with 'c' or 'o' appended
  28.            ('c' normally, 'o' in optimizing mode, giving .pyc or .pyo)
  29.     dfile: purported filename; defaults to source (this is the filename
  30.            that will show up in error messages)
  31.  
  32.     Note that it isn't necessary to byte-compile Python modules for
  33.     execution efficiency -- Python itself byte-compiles a module when
  34.     it is loaded, and if it can, writes out the bytecode to the
  35.     corresponding .pyc (or .pyo) file.
  36.  
  37.     However, if a Python installation is shared between users, it is a
  38.     good idea to byte-compile all modules upon installation, since
  39.     other users may not be able to write in the source directories,
  40.     and thus they won't be able to write the .pyc/.pyo file, and then
  41.     they would be byte-compiling every module each time it is loaded.
  42.     This can slow down program start-up considerably.
  43.  
  44.     See compileall.py for a script/module that uses this module to
  45.     byte-compile all installed files (or all files in selected
  46.     directories).
  47.  
  48.     """
  49.     import os
  50.     import marshal
  51.     import __builtin__
  52.     f = open(file)
  53.     
  54.     try:
  55.         timestamp = long(os.fstat(f.fileno())[8])
  56.     except AttributeError:
  57.         timestamp = long(os.stat(file)[8])
  58.  
  59.     codestring = f.read()
  60.     codestring = codestring.replace('\r\n', '\n')
  61.     codestring = codestring.replace('\r', '\n')
  62.     f.close()
  63.     if codestring and codestring[-1] != '\n':
  64.         codestring = codestring + '\n'
  65.     
  66.     
  67.     try:
  68.         if not dfile:
  69.             pass
  70.         codeobject = __builtin__.compile(codestring, file, 'exec')
  71.     except SyntaxError:
  72.         detail = None
  73.         import traceback
  74.         import sys
  75.         lines = traceback.format_exception_only(SyntaxError, detail)
  76.         for line in lines:
  77.             if not dfile:
  78.                 pass
  79.             sys.stderr.write(line.replace('File "<string>"', 'File "%s"' % file))
  80.         
  81.         return None
  82.  
  83.     if not cfile:
  84.         if not __debug__ and 'c':
  85.             pass
  86.         cfile = file + 'o'
  87.     
  88.     fc = open(cfile, 'wb')
  89.     fc.write('\x00\x00\x00\x00')
  90.     wr_long(fc, timestamp)
  91.     marshal.dump(codeobject, fc)
  92.     fc.flush()
  93.     fc.seek(0, 0)
  94.     fc.write(MAGIC)
  95.     fc.close()
  96.     if os.name == 'mac':
  97.         import macfs
  98.         macfs.FSSpec(cfile).SetCreatorType('Pyth', 'PYC ')
  99.     
  100.  
  101.