Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
py_compile Namespace Reference

Functions

def wr_long
 
def compile
 

Variables

tuple MAGIC = imp.get_magic()
 
list __all__ = ["compile"]
 

Detailed Description

Routine to "compile" a .py file to a .pyc (or .pyo) file.

This module has intimate knowledge of the format of .pyc files.

Function Documentation

def py_compile.compile (   file,
  cfile = None,
  dfile = None 
)
Byte-compile one Python source file to Python bytecode.

Arguments:

file:  source filename
cfile: target filename; defaults to source with 'c' or 'o' appended
       ('c' normally, 'o' in optimizing mode, giving .pyc or .pyo)
dfile: purported filename; defaults to source (this is the filename
       that will show up in error messages)

Note that it isn't necessary to byte-compile Python modules for
execution efficiency -- Python itself byte-compiles a module when
it is loaded, and if it can, writes out the bytecode to the
corresponding .pyc (or .pyo) file.

However, if a Python installation is shared between users, it is a
good idea to byte-compile all modules upon installation, since
other users may not be able to write in the source directories,
and thus they won't be able to write the .pyc/.pyo file, and then
they would be byte-compiling every module each time it is loaded.
This can slow down program start-up considerably.

See compileall.py for a script/module that uses this module to
byte-compile all installed files (or all files in selected
directories).

Definition at line 18 of file py_compile.py.

References traceback.format_exception_only(), aifc.open(), and wr_long().

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, marshal, __builtin__
48  f = open(file)
49  try:
50  timestamp = long(os.fstat(f.fileno())[8])
51  except AttributeError:
52  timestamp = long(os.stat(file)[8])
53  codestring = f.read()
54  # If parsing from a string, line breaks are \n (see parsetok.c:tok_nextc)
55  # Replace will return original string if pattern is not found, so
56  # we don't need to check whether it is found first.
57  codestring = codestring.replace("\r\n","\n")
58  codestring = codestring.replace("\r","\n")
59  f.close()
60  if codestring and codestring[-1] != '\n':
61  codestring = codestring + '\n'
62  try:
63  codeobject = __builtin__.compile(codestring, dfile or file, 'exec')
64  except SyntaxError, detail:
65  import traceback, sys
66  lines = traceback.format_exception_only(SyntaxError, detail)
67  for line in lines:
68  sys.stderr.write(line.replace('File "<string>"',
69  'File "%s"' % (dfile or file)))
70  return
71  if not cfile:
72  cfile = file + (__debug__ and 'c' or 'o')
73  fc = open(cfile, 'wb')
74  fc.write('\0\0\0\0')
75  wr_long(fc, timestamp)
76  marshal.dump(codeobject, fc)
77  fc.flush()
78  fc.seek(0, 0)
79  fc.write(MAGIC)
80  fc.close()
81  if os.name == 'mac':
82  import macfs
83  macfs.FSSpec(cfile).SetCreatorType('Pyth', 'PYC ')
def py_compile.wr_long (   f,
  x 
)
Internal; write a 32-bit int to a file in little-endian order.

Definition at line 11 of file py_compile.py.

11 
12 def wr_long(f, x):
13  """Internal; write a 32-bit int to a file in little-endian order."""
14  f.write(chr( x & 0xff))
15  f.write(chr((x >> 8) & 0xff))
16  f.write(chr((x >> 16) & 0xff))
17  f.write(chr((x >> 24) & 0xff))

Variable Documentation

list __all__ = ["compile"]

Definition at line 9 of file py_compile.py.

tuple MAGIC = imp.get_magic()

Definition at line 7 of file py_compile.py.