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

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