AppPkg/Applications/Python: Add Python 2.7.2 sources since the release of Python 2.7.3 made them unavailable from the python.org web site.

These files are a subset of the python-2.7.2.tgz distribution from python.org.  Changed files from PyMod-2.7.2 have been copied into the corresponding directories of this tree, replacing the original files in the distribution.

Signed-off-by: daryl.mcdaniel@intel.com


git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@13197 6f19259b-4bc3-4df7-8a09-765794883524
This commit is contained in:
darylm503
2012-04-16 22:12:42 +00:00
parent cbc6b5e545
commit 4710c53dca
2106 changed files with 871583 additions and 0 deletions

View File

@@ -0,0 +1,41 @@
This directory contains a number of Python programs that are useful
while building or extending Python.
bgen Generate complete extension modules from a
description. Still under development!
WARNING: bgen has been removed in 3.0.
compiler Tools used to maintain the compiler package in the
standard library.
faqwiz FAQ Wizard.
See http://www.python.org/cgi-bin/faqw.py
for a live example.
freeze Create a stand-alone executable from a Python program.
gdb Python code to be run inside gdb, to make it easier to
debug Python itself (by David Malcolm).
i18n Tools for internationalization. pygettext.py
parses Python source code and generates .pot files,
and msgfmt.py generates a binary message catalog
from a catalog in text format.
scripts A number of useful single-file programs, e.g. tabnanny.py
by Tim Peters, which checks for inconsistent mixing of
tabs and spaces, and 2to3, which converts Python 2 code
to Python 3 code.
unicode Tools used to generate unicode database files for
Python 2.0 (by Fredrik Lundh).
versioncheck A tool to automate checking whether you have the latest
version of a package (by Jack Jansen).
webchecker A link checker for web sites.
world Script to take a list of Internet addresses and print
out where in the world those addresses originate from,
based on the top-level domain country code found in
the address.

View File

@@ -0,0 +1,9 @@
BGEN -- Automatic Generation of Extension Modules
=================================================
This directory contains BGEN -- a package that helps in generating
complete source code for Python extension module. For examples of its
use, see the Mac Python source distribution (available separately
from the Python ftp archives). Note that BGEN is not Mac specific!
WARNING: bgen has been removed in 3.0.

View File

@@ -0,0 +1,12 @@
"Export everything in the various bgen submodules."
from bgenType import *
from bgenVariable import *
from bgenBuffer import *
from bgenStackBuffer import *
from bgenHeapBuffer import *
from bgenStringBuffer import *
from bgenOutput import *
from bgenGenerator import *
from bgenModule import *
from bgenObjectDefinition import *

View File

@@ -0,0 +1,301 @@
"""Buffers are character arrays that may contain null bytes.
There are a number of variants depending on:
- how the buffer is allocated (for output buffers), and
- whether and how the size is passed into and/or out of the called function.
"""
from bgenType import Type, InputOnlyMixIn, OutputOnlyMixIn, InputOnlyType, OutputOnlyType
from bgenOutput import *
# Map common types to their format characters
type2format = {
'long': 'l',
'int': 'i',
'short': 'h',
'char': 'b',
'unsigned long': 'l',
'unsigned int': 'i',
'unsigned short': 'h',
'unsigned char': 'b',
}
# ----- PART 1: Fixed character buffers -----
class FixedInputOutputBufferType(InputOnlyType):
"""Fixed buffer -- passed as (inbuffer, outbuffer)."""
def __init__(self, size, datatype = 'char', sizetype = 'int', sizeformat = None):
self.typeName = "Buffer"
self.size = str(size)
self.datatype = datatype
self.sizetype = sizetype
self.sizeformat = sizeformat or type2format[sizetype]
self.label_needed = 0
def getArgDeclarations(self, name, reference=False, constmode=False, outmode=False):
if reference:
raise RuntimeError, "Cannot pass buffer types by reference"
return (self.getBufferDeclarations(name, constmode, outmode) +
self.getSizeDeclarations(name, outmode))
def getBufferDeclarations(self, name, constmode=False, outmode=False):
return self.getInputBufferDeclarations(name, constmode) + \
self.getOutputBufferDeclarations(name, constmode, outmode)
def getInputBufferDeclarations(self, name, constmode=False):
if constmode:
const = "const "
else:
const = ""
return ["%s%s *%s__in__" % (const, self.datatype, name)]
def getOutputBufferDeclarations(self, name, constmode=False, outmode=False):
if constmode:
raise RuntimeError, "Cannot use const output buffer"
if outmode:
out = "*"
else:
out = ""
return ["%s%s %s__out__[%s]" % (self.datatype, out, name, self.size)]
def getSizeDeclarations(self, name, outmode=False):
if outmode:
out = "*"
else:
out = ""
return ["%s%s %s__len__" %(self.sizetype, out, name)]
def getAuxDeclarations(self, name):
return ["int %s__in_len__" %(name)]
def getargsFormat(self):
return "s#"
def getargsArgs(self, name):
return "&%s__in__, &%s__in_len__" % (name, name)
def getargsCheck(self, name):
Output("if (%s__in_len__ != %s)", name, self.size)
OutLbrace()
Output('PyErr_SetString(PyExc_TypeError, "buffer length should be %s");',
self.size)
Output("goto %s__error__;", name)
self.label_needed = 1
OutRbrace()
self.transferSize(name)
def transferSize(self, name):
Output("%s__len__ = %s__in_len__;", name, name)
def passOutput(self, name):
return "%s__in__, %s__out__" % (name, name)
def mkvalueFormat(self):
return "s#"
def mkvalueArgs(self, name):
return "%s__out__, (int)%s" % (name, self.size)
def cleanup(self, name):
if self.label_needed:
DedentLevel()
Output(" %s__error__: ;", name)
IndentLevel()
class FixedCombinedInputOutputBufferType(FixedInputOutputBufferType):
"""Like fixed buffer -- but same parameter is input and output."""
def passOutput(self, name):
return "(%s *)memcpy(%s__out__, %s__in__, %s)" % \
(self.datatype, name, name, self.size)
class InputOnlyBufferMixIn(InputOnlyMixIn):
def getOutputBufferDeclarations(self, name, constmode=False, outmode=False):
return []
class OutputOnlyBufferMixIn(OutputOnlyMixIn):
def getInputBufferDeclarations(self, name, constmode=False):
return []
class OptionalInputBufferMixIn:
"""Add to input buffers if the buffer may be omitted: pass None in Python
and the C code will get a NULL pointer and zero size"""
def getargsFormat(self):
return "z#"
class FixedInputBufferType(InputOnlyBufferMixIn, FixedInputOutputBufferType):
"""Fixed size input buffer -- passed without size information.
Instantiate with the size as parameter.
"""
def passInput(self, name):
return "%s__in__" % name
class OptionalFixedInputBufferType(OptionalInputBufferMixIn, FixedInputBufferType):
pass
class FixedOutputBufferType(OutputOnlyBufferMixIn, FixedInputOutputBufferType):
"""Fixed size output buffer -- passed without size information.
Instantiate with the size as parameter.
"""
def passOutput(self, name):
return "%s__out__" % name
class VarInputBufferType(FixedInputBufferType):
"""Variable size input buffer -- passed as (buffer, size).
Instantiate without size parameter.
"""
def __init__(self, datatype = 'char', sizetype = 'int', sizeformat = None):
FixedInputBufferType.__init__(self, "0", datatype, sizetype, sizeformat)
def getargsCheck(self, name):
Output("%s__len__ = %s__in_len__;", name, name)
def passInput(self, name):
return "%s__in__, %s__len__" % (name, name)
class ReverseInputBufferMixin:
""" Mixin for input buffers that are passed as (size, buffer) """
def passInput(self, name):
return "%s__len__, %s__in__" % (name, name)
class OptionalVarInputBufferType(OptionalInputBufferMixIn, VarInputBufferType):
pass
# ----- PART 2: Structure buffers -----
class StructInputOutputBufferType(FixedInputOutputBufferType):
"""Structure buffer -- passed as a structure pointer.
Instantiate with the struct type as parameter.
"""
def __init__(self, type):
FixedInputOutputBufferType.__init__(self, "sizeof(%s)" % type)
self.typeName = self.type = type
def getInputBufferDeclarations(self, name, constmode=False):
if constmode:
const = "const "
else:
const = ""
return ["%s%s *%s__in__" % (const, self.type, name)]
def getSizeDeclarations(self, name, outmode=False):
return []
def getAuxDeclarations(self, name):
return ["int %s__in_len__" % (name)]
def getOutputBufferDeclarations(self, name, constmode=False, outmode=False):
if constmode:
raise RuntimeError, "Cannot use const output buffer"
if outmode:
out = "*"
else:
out = ""
return ["%s%s %s__out__" % (self.type, out, name)]
def getargsArgs(self, name):
return "(char **)&%s__in__, &%s__in_len__" % (name, name)
def transferSize(self, name):
pass
def passInput(self, name):
return "%s__in__" % name
def passOutput(self, name):
return "%s__in__, &%s__out__" % (name, name)
def mkvalueArgs(self, name):
return "(char *)&%s__out__, (int)%s" % (name, self.size)
class StructCombinedInputOutputBufferType(StructInputOutputBufferType):
"""Like structure buffer -- but same parameter is input and output."""
def passOutput(self, name):
return "(%s *)memcpy((char *)%s__out__, (char *)%s__in__, %s)" % \
(self.type, name, name, self.size)
class StructInputBufferType(InputOnlyBufferMixIn, StructInputOutputBufferType):
"""Fixed size input buffer -- passed as a pointer to a structure.
Instantiate with the struct type as parameter.
"""
class StructByValueBufferType(StructInputBufferType):
"""Fixed size input buffer -- passed as a structure BY VALUE.
Instantiate with the struct type as parameter.
"""
def passInput(self, name):
return "*%s__in__" % name
class StructOutputBufferType(OutputOnlyBufferMixIn, StructInputOutputBufferType):
"""Fixed size output buffer -- passed as a pointer to a structure.
Instantiate with the struct type as parameter.
"""
def getSizeDeclarations(self, name, outmode=False):
return []
def getAuxDeclarations(self, name):
return []
def passOutput(self, name):
return "&%s__out__" % name
class ArrayOutputBufferType(OutputOnlyBufferMixIn, StructInputOutputBufferType):
"""Fixed size output buffer -- declared as a typedef, passed as an array.
Instantiate with the struct type as parameter.
"""
def getSizeDeclarations(self, name, outmode=False):
return []
def getAuxDeclarations(self, name):
return []
def passOutput(self, name):
return "%s__out__" % name

View File

@@ -0,0 +1,302 @@
from bgenOutput import *
from bgenType import *
from bgenVariable import *
Error = "bgenGenerator.Error"
DEBUG=0
# Strings to specify argument transfer modes in generator calls
IN = "in"
OUT = "out"
INOUT = IN_OUT = "in-out"
class BaseFunctionGenerator:
def __init__(self, name, condition=None, callname=None, modifiers=None):
if DEBUG: print "<--", name
self.name = name
if callname:
self.callname = callname
else:
self.callname = name
self.prefix = name
self.objecttype = "PyObject" # Type of _self argument to function
self.condition = condition
self.modifiers = modifiers
def setprefix(self, prefix):
self.prefix = prefix
def checkgenerate(self):
return True
def generate(self):
if not self.checkgenerate():
return
if DEBUG: print "-->", self.name
if self.condition:
Output()
Output(self.condition)
self.functionheader()
self.functionbody()
self.functiontrailer()
if self.condition:
Output("#endif")
def functionheader(self):
Output()
Output("static PyObject *%s_%s(%s *_self, PyObject *_args)",
self.prefix, self.name, self.objecttype)
OutLbrace()
Output("PyObject *_res = NULL;")
def functionbody(self):
Output("/* XXX To be provided */")
def functiontrailer(self):
OutRbrace()
def reference(self, name = None):
if not self.checkgenerate():
return
if name is None:
name = self.name
docstring = self.docstring()
if self.condition:
Output()
Output(self.condition)
Output("{\"%s\", (PyCFunction)%s_%s, 1,", name, self.prefix, self.name)
Output(" PyDoc_STR(%s)},", stringify(docstring))
if self.condition:
Output("#endif")
def docstring(self):
return None
def __cmp__(self, other):
if not hasattr(other, 'name'):
return cmp(id(self), id(other))
return cmp(self.name, other.name)
_stringify_map = {'\n': '\\n', '\t': '\\t', '\r': '\\r', '\b': '\\b',
'\e': '\\e', '\a': '\\a', '\f': '\\f', '"': '\\"'}
def stringify(str):
if str is None: return "NULL"
res = '"'
map = _stringify_map
for c in str:
if map.has_key(c): res = res + map[c]
elif ' ' <= c <= '~': res = res + c
else: res = res + '\\%03o' % ord(c)
res = res + '"'
return res
class ManualGenerator(BaseFunctionGenerator):
def __init__(self, name, body, condition=None):
BaseFunctionGenerator.__init__(self, name, condition=condition)
self.body = body
def functionbody(self):
Output("%s", self.body)
def setselftype(self, selftype, itselftype):
self.objecttype = selftype
self.itselftype = itselftype
class FunctionGenerator(BaseFunctionGenerator):
def __init__(self, returntype, name, *argumentList, **conditionlist):
BaseFunctionGenerator.__init__(self, name, **conditionlist)
self.returntype = returntype
self.argumentList = []
self.setreturnvar()
self.parseArgumentList(argumentList)
self.prefix = "XXX" # Will be changed by setprefix() call
self.itselftype = None # Type of _self->ob_itself, if defined
def setreturnvar(self):
if self.returntype:
self.rv = self.makereturnvar()
self.argumentList.append(self.rv)
else:
self.rv = None
def makereturnvar(self):
return Variable(self.returntype, "_rv", OutMode)
def setselftype(self, selftype, itselftype):
self.objecttype = selftype
self.itselftype = itselftype
def parseArgumentList(self, argumentList):
iarg = 0
for type, name, mode in argumentList:
iarg = iarg + 1
if name is None: name = "_arg%d" % iarg
arg = Variable(type, name, mode)
self.argumentList.append(arg)
def docstring(self):
input = []
output = []
for arg in self.argumentList:
if arg.flags == ErrorMode or arg.flags == SelfMode:
continue
if arg.type is None:
str = 'void'
else:
if hasattr(arg.type, 'typeName'):
typeName = arg.type.typeName
if typeName is None: # Suppressed type
continue
else:
typeName = "?"
print "Nameless type", arg.type
str = typeName + ' ' + arg.name
if arg.mode in (InMode, InOutMode):
input.append(str)
if arg.mode in (InOutMode, OutMode):
output.append(str)
if not input:
instr = "()"
else:
instr = "(%s)" % ", ".join(input)
if not output or output == ["void"]:
outstr = "None"
else:
outstr = "(%s)" % ", ".join(output)
return instr + " -> " + outstr
def functionbody(self):
self.declarations()
self.precheck()
self.getargs()
self.callit()
self.checkit()
self.returnvalue()
def declarations(self):
for arg in self.argumentList:
arg.declare()
def getargs(self):
sep = ",\n" + ' '*len("if (!PyArg_ParseTuple(")
fmt, lst = self.getargsFormatArgs(sep)
Output("if (!PyArg_ParseTuple(_args, \"%s\"%s))", fmt, lst)
IndentLevel()
Output("return NULL;")
DedentLevel()
for arg in self.argumentList:
if arg.flags == SelfMode:
continue
if arg.mode in (InMode, InOutMode):
arg.getargsCheck()
def getargsFormatArgs(self, sep):
fmt = ""
lst = ""
for arg in self.argumentList:
if arg.flags == SelfMode:
continue
if arg.mode in (InMode, InOutMode):
arg.getargsPreCheck()
fmt = fmt + arg.getargsFormat()
args = arg.getargsArgs()
if args:
lst = lst + sep + args
return fmt, lst
def precheck(self):
pass
def beginallowthreads(self):
pass
def endallowthreads(self):
pass
def callit(self):
args = ""
s = "%s%s(" % (self.getrvforcallit(), self.callname)
sep = ",\n" + ' '*len(s)
for arg in self.argumentList:
if arg is self.rv:
continue
s = arg.passArgument()
if args: s = sep + s
args = args + s
self.beginallowthreads()
Output("%s%s(%s);",
self.getrvforcallit(), self.callname, args)
self.endallowthreads()
def getrvforcallit(self):
if self.rv:
return "%s = " % self.rv.name
else:
return ""
def checkit(self):
for arg in self.argumentList:
arg.errorCheck()
def returnvalue(self):
sep = ",\n" + ' '*len("return Py_BuildValue(")
fmt, lst = self.mkvalueFormatArgs(sep)
if fmt == "":
Output("Py_INCREF(Py_None);")
Output("_res = Py_None;");
else:
Output("_res = Py_BuildValue(\"%s\"%s);", fmt, lst)
tmp = self.argumentList[:]
tmp.reverse()
for arg in tmp:
if not arg: continue
arg.cleanup()
Output("return _res;")
def mkvalueFormatArgs(self, sep):
fmt = ""
lst = ""
for arg in self.argumentList:
if not arg: continue
if arg.flags == ErrorMode: continue
if arg.mode in (OutMode, InOutMode):
arg.mkvaluePreCheck()
fmt = fmt + arg.mkvalueFormat()
lst = lst + sep + arg.mkvalueArgs()
return fmt, lst
class MethodGenerator(FunctionGenerator):
def parseArgumentList(self, args):
a0, args = args[0], args[1:]
t0, n0, m0 = a0
if m0 != InMode:
raise ValueError, "method's 'self' must be 'InMode'"
self.itself = Variable(t0, "_self->ob_itself", SelfMode)
self.argumentList.append(self.itself)
FunctionGenerator.parseArgumentList(self, args)
def _test():
void = None
eggs = FunctionGenerator(void, "eggs",
(stringptr, 'cmd', InMode),
(int, 'x', InMode),
(double, 'y', InOutMode),
(int, 'status', ErrorMode),
)
eggs.setprefix("spam")
print "/* START */"
eggs.generate()
if __name__ == "__main__":
_test()

View File

@@ -0,0 +1,40 @@
from bgenOutput import *
class GeneratorGroup:
def __init__(self, prefix):
self.prefix = prefix
self.generators = []
def add(self, g, dupcheck=0):
if dupcheck:
if g in self.generators:
print 'DUP', g.name
return
g.setprefix(self.prefix)
self.generators.append(g)
def generate(self):
for g in self.generators:
g.generate()
Output()
Output("static PyMethodDef %s_methods[] = {", self.prefix)
IndentLevel()
for g in self.generators:
g.reference()
Output("{NULL, NULL, 0}")
DedentLevel()
Output("};")
def _test():
void = None
from bgenGenerator import FunctionGenerator
group = GeneratorGroup("spam")
eggs = FunctionGenerator(void, "eggs")
group.add(eggs)
print "/* START */"
group.generate()
if __name__ == "__main__":
_test()

View File

@@ -0,0 +1,145 @@
# Buffers allocated on the heap
from bgenOutput import *
from bgenType import OutputOnlyMixIn
from bgenBuffer import FixedInputOutputBufferType
class HeapInputOutputBufferType(FixedInputOutputBufferType):
"""Input-output buffer allocated on the heap -- passed as (inbuffer, outbuffer, size).
Instantiate without parameters.
Call from Python with input buffer.
"""
def __init__(self, datatype = 'char', sizetype = 'int', sizeformat = None):
FixedInputOutputBufferType.__init__(self, "0", datatype, sizetype, sizeformat)
def getOutputBufferDeclarations(self, name, constmode=False, outmode=False):
if constmode:
raise RuntimeError, "Cannot use const output buffer"
if outmode:
out = "*"
else:
out = ""
return ["%s%s *%s__out__" % (self.datatype, out, name)]
def getargsCheck(self, name):
Output("if ((%s__out__ = malloc(%s__in_len__)) == NULL)", name, name)
OutLbrace()
Output('PyErr_NoMemory();')
Output("goto %s__error__;", name)
self.label_needed = 1
OutRbrace()
Output("%s__len__ = %s__in_len__;", name, name)
def passOutput(self, name):
return "%s__in__, %s__out__, (%s)%s__len__" % \
(name, name, self.sizetype, name)
def mkvalueArgs(self, name):
return "%s__out__, (int)%s__len__" % (name, name)
def cleanup(self, name):
Output("free(%s__out__);", name)
FixedInputOutputBufferType.cleanup(self, name)
class VarHeapInputOutputBufferType(HeapInputOutputBufferType):
"""same as base class, but passed as (inbuffer, outbuffer, &size)"""
def passOutput(self, name):
return "%s__in__, %s__out__, &%s__len__" % (name, name, name)
class HeapCombinedInputOutputBufferType(HeapInputOutputBufferType):
"""same as base class, but passed as (inoutbuffer, size)"""
def passOutput(self, name):
return "(%s *)memcpy(%s__out__, %s__in__, %s__len__)" % \
(self.datatype, name, name, name)
class VarHeapCombinedInputOutputBufferType(HeapInputOutputBufferType):
"""same as base class, but passed as (inoutbuffer, &size)"""
def passOutput(self, name):
return "(%s *)memcpy(%s__out__, %s__in__, &%s__len__)" % \
(self.datatype, name, name, name)
class HeapOutputBufferType(OutputOnlyMixIn, HeapInputOutputBufferType):
"""Output buffer allocated on the heap -- passed as (buffer, size).
Instantiate without parameters.
Call from Python with buffer size.
"""
def getInputBufferDeclarations(self, name, constmode=False):
return []
def getargsFormat(self):
return "i"
def getargsArgs(self, name):
return "&%s__in_len__" % name
def passOutput(self, name):
return "%s__out__, %s__len__" % (name, name)
class VarHeapOutputBufferType(HeapOutputBufferType):
"""Output buffer allocated on the heap -- passed as (buffer, &size).
Instantiate without parameters.
Call from Python with buffer size.
"""
def passOutput(self, name):
return "%s__out__, &%s__len__" % (name, name)
class VarVarHeapOutputBufferType(VarHeapOutputBufferType):
"""Output buffer allocated on the heap -- passed as (buffer, size, &size).
Instantiate without parameters.
Call from Python with buffer size.
"""
def passOutput(self, name):
return "%s__out__, %s__len__, &%s__len__" % (name, name, name)
class MallocHeapOutputBufferType(HeapOutputBufferType):
"""Output buffer allocated by the called function -- passed as (&buffer, &size).
Instantiate without parameters.
Call from Python without parameters.
"""
def getargsCheck(self, name):
Output("%s__out__ = NULL;", name)
def getAuxDeclarations(self, name):
return []
def passOutput(self, name):
return "&%s__out__, &%s__len__" % (name, name)
def getargsFormat(self):
return ""
def getargsArgs(self, name):
return None
def mkvalueFormat(self):
return "z#"
def cleanup(self, name):
Output("if( %s__out__ ) free(%s__out__);", name, name)

View File

@@ -0,0 +1,94 @@
from bgenOutput import *
from bgenGeneratorGroup import GeneratorGroup
class Module(GeneratorGroup):
def __init__(self, name, prefix = None,
includestuff = None,
finalstuff = None,
initstuff = None,
variablestuff = None,
longname = None):
GeneratorGroup.__init__(self, prefix or name)
self.name = name
if longname:
self.longname = longname
else:
self.longname = name
self.includestuff = includestuff
self.initstuff = initstuff
self.finalstuff = finalstuff
self.variablestuff = variablestuff
self.typeobjects = []
def addobject(self, od):
self.generators.append(od)
self.typeobjects.append(od)
od.setmodulename(self.longname)
def generate(self):
OutHeader1("Module " + self.name)
Output("#include \"Python.h\"")
Output()
if self.includestuff:
Output()
Output("%s", self.includestuff)
self.declareModuleVariables()
GeneratorGroup.generate(self)
if self.finalstuff:
Output()
Output("%s", self.finalstuff)
Output()
Output("void init%s(void)", self.name)
OutLbrace()
Output("PyObject *m;")
Output("PyObject *d;")
Output()
if self.initstuff:
Output("%s", self.initstuff)
Output()
Output("m = Py_InitModule(\"%s\", %s_methods);",
self.name, self.prefix)
Output("d = PyModule_GetDict(m);")
self.createModuleVariables()
OutRbrace()
OutHeader1("End module " + self.name)
def declareModuleVariables(self):
self.errorname = self.prefix + "_Error"
Output("static PyObject *%s;", self.errorname)
def createModuleVariables(self):
Output("""%s = %s;""", self.errorname, self.exceptionInitializer())
Output("""if (%s == NULL ||""", self.errorname)
Output(""" PyDict_SetItemString(d, "Error", %s) != 0)""",
self.errorname)
IndentLevel()
Output("""return;""")
DedentLevel()
for tp in self.typeobjects:
tp.outputTypeObjectInitializer()
if self.variablestuff:
Output("%s", self.variablestuff)
Output()
def exceptionInitializer(self):
return """PyErr_NewException("%s.Error", NULL, NULL)""" % self.name
def _test():
from bgenGenerator import FunctionGenerator
m = Module("spam", "", "#include <stdio.h>")
g = FunctionGenerator(None, "bacon")
m.add(g)
m.generate()
if __name__ == "__main__":
_test()

View File

@@ -0,0 +1,512 @@
from bgenOutput import *
from bgenGeneratorGroup import GeneratorGroup
class ObjectDefinition(GeneratorGroup):
"Spit out code that together defines a new Python object type"
basechain = "NULL"
tp_flags = "Py_TPFLAGS_DEFAULT"
basetype = None
argref = "" # set to "*" if arg to <type>_New should be pointer
argconst = "" # set to "const " if arg to <type>_New should be const
def __init__(self, name, prefix, itselftype):
"""ObjectDefinition constructor. May be extended, but do not override.
- name: the object's official name, e.g. 'SndChannel'.
- prefix: the prefix used for the object's functions and data, e.g. 'SndCh'.
- itselftype: the C type actually contained in the object, e.g. 'SndChannelPtr'.
XXX For official Python data types, rules for the 'Py' prefix are a problem.
"""
GeneratorGroup.__init__(self, prefix or name)
self.name = name
self.itselftype = itselftype
self.objecttype = name + 'Object'
self.typename = name + '_Type'
self.static = "static " # set to "" to make <type>_New and <type>_Convert public
self.modulename = None
if hasattr(self, "assertions"):
self.assertions()
def add(self, g, dupcheck=0):
g.setselftype(self.objecttype, self.itselftype)
GeneratorGroup.add(self, g, dupcheck)
def reference(self):
# In case we are referenced from a module
pass
def setmodulename(self, name):
self.modulename = name
def generate(self):
# XXX This should use long strings and %(varname)s substitution!
OutHeader2("Object type " + self.name)
self.outputCheck()
Output("typedef struct %s {", self.objecttype)
IndentLevel()
Output("PyObject_HEAD")
self.outputStructMembers()
DedentLevel()
Output("} %s;", self.objecttype)
self.outputNew()
self.outputConvert()
self.outputDealloc()
GeneratorGroup.generate(self)
Output()
self.outputMethodChain()
self.outputGetattr()
self.outputSetattr()
self.outputCompare()
self.outputRepr()
self.outputHash()
self.outputPEP253Hooks()
self.outputTypeObject()
OutHeader2("End object type " + self.name)
def outputCheck(self):
sf = self.static and "static "
Output("%sPyTypeObject %s;", sf, self.typename)
Output()
Output("#define %s_Check(x) ((x)->ob_type == &%s || PyObject_TypeCheck((x), &%s))",
self.prefix, self.typename, self.typename)
Output()
def outputMethodChain(self):
Output("%sPyMethodChain %s_chain = { %s_methods, %s };",
self.static, self.prefix, self.prefix, self.basechain)
def outputStructMembers(self):
Output("%s ob_itself;", self.itselftype)
def outputNew(self):
Output()
Output("%sPyObject *%s_New(%s%s %sitself)", self.static, self.prefix,
self.argconst, self.itselftype, self.argref)
OutLbrace()
Output("%s *it;", self.objecttype)
self.outputCheckNewArg()
Output("it = PyObject_NEW(%s, &%s);", self.objecttype, self.typename)
Output("if (it == NULL) return NULL;")
if self.basetype:
Output("/* XXXX Should we tp_init or tp_new our basetype? */")
self.outputInitStructMembers()
Output("return (PyObject *)it;")
OutRbrace()
def outputInitStructMembers(self):
Output("it->ob_itself = %sitself;", self.argref)
def outputCheckNewArg(self):
"Override this method to apply additional checks/conversions"
def outputConvert(self):
Output()
Output("%sint %s_Convert(PyObject *v, %s *p_itself)", self.static, self.prefix,
self.itselftype)
OutLbrace()
self.outputCheckConvertArg()
Output("if (!%s_Check(v))", self.prefix)
OutLbrace()
Output('PyErr_SetString(PyExc_TypeError, "%s required");', self.name)
Output("return 0;")
OutRbrace()
Output("*p_itself = ((%s *)v)->ob_itself;", self.objecttype)
Output("return 1;")
OutRbrace()
def outputCheckConvertArg(self):
"Override this method to apply additional conversions"
def outputDealloc(self):
Output()
Output("static void %s_dealloc(%s *self)", self.prefix, self.objecttype)
OutLbrace()
self.outputCleanupStructMembers()
if self.basetype:
Output("%s.tp_dealloc((PyObject *)self);", self.basetype)
elif hasattr(self, 'output_tp_free'):
# This is a new-style object with tp_free slot
Output("self->ob_type->tp_free((PyObject *)self);")
else:
Output("PyObject_Free((PyObject *)self);")
OutRbrace()
def outputCleanupStructMembers(self):
self.outputFreeIt("self->ob_itself")
def outputFreeIt(self, name):
Output("/* Cleanup of %s goes here */", name)
def outputGetattr(self):
Output()
Output("static PyObject *%s_getattr(%s *self, char *name)", self.prefix, self.objecttype)
OutLbrace()
self.outputGetattrBody()
OutRbrace()
def outputGetattrBody(self):
self.outputGetattrHook()
Output("return Py_FindMethodInChain(&%s_chain, (PyObject *)self, name);",
self.prefix)
def outputGetattrHook(self):
pass
def outputSetattr(self):
Output()
Output("#define %s_setattr NULL", self.prefix)
def outputCompare(self):
Output()
Output("#define %s_compare NULL", self.prefix)
def outputRepr(self):
Output()
Output("#define %s_repr NULL", self.prefix)
def outputHash(self):
Output()
Output("#define %s_hash NULL", self.prefix)
def outputTypeObject(self):
sf = self.static and "static "
Output()
Output("%sPyTypeObject %s = {", sf, self.typename)
IndentLevel()
Output("PyObject_HEAD_INIT(NULL)")
Output("0, /*ob_size*/")
if self.modulename:
Output("\"%s.%s\", /*tp_name*/", self.modulename, self.name)
else:
Output("\"%s\", /*tp_name*/", self.name)
Output("sizeof(%s), /*tp_basicsize*/", self.objecttype)
Output("0, /*tp_itemsize*/")
Output("/* methods */")
Output("(destructor) %s_dealloc, /*tp_dealloc*/", self.prefix)
Output("0, /*tp_print*/")
Output("(getattrfunc) %s_getattr, /*tp_getattr*/", self.prefix)
Output("(setattrfunc) %s_setattr, /*tp_setattr*/", self.prefix)
Output("(cmpfunc) %s_compare, /*tp_compare*/", self.prefix)
Output("(reprfunc) %s_repr, /*tp_repr*/", self.prefix)
Output("(PyNumberMethods *)0, /* tp_as_number */")
Output("(PySequenceMethods *)0, /* tp_as_sequence */")
Output("(PyMappingMethods *)0, /* tp_as_mapping */")
Output("(hashfunc) %s_hash, /*tp_hash*/", self.prefix)
DedentLevel()
Output("};")
def outputTypeObjectInitializer(self):
Output("""%s.ob_type = &PyType_Type;""", self.typename)
if self.basetype:
Output("%s.tp_base = &%s;", self.typename, self.basetype)
Output("if (PyType_Ready(&%s) < 0) return;", self.typename)
Output("""Py_INCREF(&%s);""", self.typename)
Output("PyModule_AddObject(m, \"%s\", (PyObject *)&%s);", self.name, self.typename);
self.outputTypeObjectInitializerCompat()
def outputTypeObjectInitializerCompat(self):
Output("/* Backward-compatible name */")
Output("""Py_INCREF(&%s);""", self.typename);
Output("PyModule_AddObject(m, \"%sType\", (PyObject *)&%s);", self.name, self.typename);
def outputPEP253Hooks(self):
pass
class PEP252Mixin:
getsetlist = []
def assertions(self):
# Check that various things aren't overridden. If they are it could
# signify a bgen-client that has been partially converted to PEP252.
assert self.outputGetattr.im_func == PEP252Mixin.outputGetattr.im_func
assert self.outputSetattr.im_func == PEP252Mixin.outputSetattr.im_func
assert self.outputGetattrBody == None
assert self.outputGetattrHook == None
assert self.basechain == "NULL"
def outputGetattr(self):
pass
outputGetattrBody = None
outputGetattrHook = None
def outputSetattr(self):
pass
def outputMethodChain(self):
# This is a good place to output the getters and setters
self.outputGetSetList()
def outputHook(self, name):
methodname = "outputHook_" + name
if hasattr(self, methodname):
func = getattr(self, methodname)
func()
else:
Output("0, /*%s*/", name)
def outputTypeObject(self):
sf = self.static and "static "
Output()
Output("%sPyTypeObject %s = {", sf, self.typename)
IndentLevel()
Output("PyObject_HEAD_INIT(NULL)")
Output("0, /*ob_size*/")
if self.modulename:
Output("\"%s.%s\", /*tp_name*/", self.modulename, self.name)
else:
Output("\"%s\", /*tp_name*/", self.name)
Output("sizeof(%s), /*tp_basicsize*/", self.objecttype)
Output("0, /*tp_itemsize*/")
Output("/* methods */")
Output("(destructor) %s_dealloc, /*tp_dealloc*/", self.prefix)
Output("0, /*tp_print*/")
Output("(getattrfunc)0, /*tp_getattr*/")
Output("(setattrfunc)0, /*tp_setattr*/")
Output("(cmpfunc) %s_compare, /*tp_compare*/", self.prefix)
Output("(reprfunc) %s_repr, /*tp_repr*/", self.prefix)
Output("(PyNumberMethods *)0, /* tp_as_number */")
Output("(PySequenceMethods *)0, /* tp_as_sequence */")
Output("(PyMappingMethods *)0, /* tp_as_mapping */")
Output("(hashfunc) %s_hash, /*tp_hash*/", self.prefix)
self.outputHook("tp_call")
Output("0, /*tp_str*/")
Output("PyObject_GenericGetAttr, /*tp_getattro*/")
Output("PyObject_GenericSetAttr, /*tp_setattro */")
self.outputHook("tp_as_buffer")
Output("%s, /* tp_flags */", self.tp_flags)
self.outputHook("tp_doc")
self.outputHook("tp_traverse")
self.outputHook("tp_clear")
self.outputHook("tp_richcompare")
self.outputHook("tp_weaklistoffset")
self.outputHook("tp_iter")
self.outputHook("tp_iternext")
Output("%s_methods, /* tp_methods */", self.prefix)
self.outputHook("tp_members")
Output("%s_getsetlist, /*tp_getset*/", self.prefix)
self.outputHook("tp_base")
self.outputHook("tp_dict")
self.outputHook("tp_descr_get")
self.outputHook("tp_descr_set")
self.outputHook("tp_dictoffset")
self.outputHook("tp_init")
self.outputHook("tp_alloc")
self.outputHook("tp_new")
self.outputHook("tp_free")
DedentLevel()
Output("};")
def outputGetSetList(self):
if self.getsetlist:
for name, get, set, doc in self.getsetlist:
if get:
self.outputGetter(name, get)
else:
Output("#define %s_get_%s NULL", self.prefix, name)
Output()
if set:
self.outputSetter(name, set)
else:
Output("#define %s_set_%s NULL", self.prefix, name)
Output()
Output("static PyGetSetDef %s_getsetlist[] = {", self.prefix)
IndentLevel()
for name, get, set, doc in self.getsetlist:
if doc:
doc = '"' + doc + '"'
else:
doc = "NULL"
Output("{\"%s\", (getter)%s_get_%s, (setter)%s_set_%s, %s},",
name, self.prefix, name, self.prefix, name, doc)
Output("{NULL, NULL, NULL, NULL},")
DedentLevel()
Output("};")
else:
Output("#define %s_getsetlist NULL", self.prefix)
Output()
def outputGetter(self, name, code):
Output("static PyObject *%s_get_%s(%s *self, void *closure)",
self.prefix, name, self.objecttype)
OutLbrace()
Output(code)
OutRbrace()
Output()
def outputSetter(self, name, code):
Output("static int %s_set_%s(%s *self, PyObject *v, void *closure)",
self.prefix, name, self.objecttype)
OutLbrace()
Output(code)
Output("return 0;")
OutRbrace()
Output()
class PEP253Mixin(PEP252Mixin):
tp_flags = "Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE"
def outputHook_tp_init(self):
Output("%s_tp_init, /* tp_init */", self.prefix)
def outputHook_tp_alloc(self):
Output("%s_tp_alloc, /* tp_alloc */", self.prefix)
def outputHook_tp_new(self):
Output("%s_tp_new, /* tp_new */", self.prefix)
def outputHook_tp_free(self):
Output("%s_tp_free, /* tp_free */", self.prefix)
def output_tp_initBody_basecall(self):
"""If a type shares its init call with its base type set output_tp_initBody
to output_tp_initBody_basecall"""
if self.basetype:
Output("if (%s.tp_init)", self.basetype)
OutLbrace()
Output("if ( (*%s.tp_init)(_self, _args, _kwds) < 0) return -1;", self.basetype)
OutRbrace()
output_tp_initBody = None
def output_tp_init(self):
if self.output_tp_initBody:
Output("static int %s_tp_init(PyObject *_self, PyObject *_args, PyObject *_kwds)", self.prefix)
OutLbrace()
self.output_tp_initBody()
OutRbrace()
else:
Output("#define %s_tp_init 0", self.prefix)
Output()
output_tp_allocBody = None
def output_tp_alloc(self):
if self.output_tp_allocBody:
Output("static PyObject *%s_tp_alloc(PyTypeObject *type, int nitems)",
self.prefix)
OutLbrace()
self.output_tp_allocBody()
OutRbrace()
else:
Output("#define %s_tp_alloc PyType_GenericAlloc", self.prefix)
Output()
def output_tp_newBody(self):
Output("PyObject *_self;");
Output("%s itself;", self.itselftype);
Output("char *kw[] = {\"itself\", 0};")
Output()
Output("if (!PyArg_ParseTupleAndKeywords(_args, _kwds, \"O&\", kw, %s_Convert, &itself)) return NULL;",
self.prefix);
if self.basetype:
Output("if (%s.tp_new)", self.basetype)
OutLbrace()
Output("if ( (*%s.tp_new)(type, _args, _kwds) == NULL) return NULL;", self.basetype)
Dedent()
Output("} else {")
Indent()
Output("if ((_self = type->tp_alloc(type, 0)) == NULL) return NULL;")
OutRbrace()
else:
Output("if ((_self = type->tp_alloc(type, 0)) == NULL) return NULL;")
Output("((%s *)_self)->ob_itself = itself;", self.objecttype)
Output("return _self;")
def output_tp_new(self):
if self.output_tp_newBody:
Output("static PyObject *%s_tp_new(PyTypeObject *type, PyObject *_args, PyObject *_kwds)", self.prefix)
OutLbrace()
self.output_tp_newBody()
OutRbrace()
else:
Output("#define %s_tp_new PyType_GenericNew", self.prefix)
Output()
output_tp_freeBody = None
def output_tp_free(self):
if self.output_tp_freeBody:
Output("static void %s_tp_free(PyObject *self)", self.prefix)
OutLbrace()
self.output_tp_freeBody()
OutRbrace()
else:
Output("#define %s_tp_free PyObject_Del", self.prefix)
Output()
def outputPEP253Hooks(self):
self.output_tp_init()
self.output_tp_alloc()
self.output_tp_new()
self.output_tp_free()
class GlobalObjectDefinition(ObjectDefinition):
"""Like ObjectDefinition but exports some parts.
XXX Should also somehow generate a .h file for them.
"""
def __init__(self, name, prefix = None, itselftype = None):
ObjectDefinition.__init__(self, name, prefix or name, itselftype or name)
self.static = ""
class ObjectIdentityMixin:
"""A mixin class for objects that makes the identity of ob_itself
govern comparisons and dictionary lookups. Useful if the C object can
be returned by library calls and it is difficult (or impossible) to find
the corresponding Python objects. With this you can create Python object
wrappers on the fly"""
def outputCompare(self):
Output()
Output("static int %s_compare(%s *self, %s *other)", self.prefix, self.objecttype,
self.objecttype)
OutLbrace()
Output("unsigned long v, w;")
Output()
Output("if (!%s_Check((PyObject *)other))", self.prefix)
OutLbrace()
Output("v=(unsigned long)self;")
Output("w=(unsigned long)other;")
OutRbrace()
Output("else")
OutLbrace()
Output("v=(unsigned long)self->ob_itself;")
Output("w=(unsigned long)other->ob_itself;")
OutRbrace()
Output("if( v < w ) return -1;")
Output("if( v > w ) return 1;")
Output("return 0;")
OutRbrace()
def outputHash(self):
Output()
Output("static long %s_hash(%s *self)", self.prefix, self.objecttype)
OutLbrace()
Output("return (long)self->ob_itself;")
OutRbrace()

View File

@@ -0,0 +1,219 @@
"""Output primitives for the binding generator classes.
This should really be a class, but then everybody would be passing
the output object to each other. I chose for the simpler approach
of a module with a global variable. Use SetOutputFile() or
SetOutputFileName() to change the output file.
"""
_NeedClose = 0
def SetOutputFile(file = None, needclose = 0):
"""Call this with an open file object to make it the output file.
Call it without arguments to close the current file (if necessary)
and reset it to sys.stdout.
If the second argument is true, the new file will be explicitly closed
on a subsequence call.
"""
global _File, _NeedClose
if _NeedClose:
tmp = _File
_NeedClose = 0
_File = None
tmp.close()
if file is None:
import sys
file = sys.stdout
_File = file
_NeedClose = file and needclose
def SetOutputFileName(filename = None):
"""Call this with a filename to make it the output file.
Call it without arguments to close the current file (if necessary)
and reset it to sys.stdout.
"""
SetOutputFile()
if filename:
SetOutputFile(open(filename, 'w'), 1)
SetOutputFile() # Initialize _File
_Level = 0 # Indentation level
def GetLevel():
"""Return the current indentation level."""
return _Level
def SetLevel(level):
"""Set the current indentation level.
This does no type or range checking -- use at own risk.
"""
global _Level
_Level = level
def Output(format = "", *args):
VaOutput(format, args)
def VaOutput(format, args):
"""Call this with a format string and argument tuple for the format.
A newline is always added. Each line in the output is indented
to the proper indentation level -- even if the result of the
format expansion contains embedded newlines. Exception: lines
beginning with '#' are not indented -- these are assumed to be
C preprprocessor lines.
"""
text = format % args
if _Level > 0:
indent = '\t' * _Level
lines = text.split('\n')
for i in range(len(lines)):
if lines[i] and lines[i][0] != '#':
lines[i] = indent + lines[i]
text = '\n'.join(lines)
_File.write(text + '\n')
def IndentLevel(by = 1):
"""Increment the indentation level by one.
When called with an argument, adds it to the indentation level.
"""
global _Level
if _Level+by < 0:
raise Error, "indentation underflow (internal error)"
_Level = _Level + by
def DedentLevel(by = 1):
"""Decrement the indentation level by one.
When called with an argument, subtracts it from the indentation level.
"""
IndentLevel(-by)
def OutIndent(format = "", *args):
"""Combine Output() followed by IndentLevel().
If no text is given, acts like lone IndentLevel().
"""
if format: VaOutput(format, args)
IndentLevel()
def OutDedent(format = "", *args):
"""Combine Output() followed by DedentLevel().
If no text is given, acts like loneDedentLevel().
"""
if format: VaOutput(format, args)
DedentLevel()
def OutLbrace(format = "", *args):
"""Like Output, but add a '{' and increase the indentation level.
If no text is given a lone '{' is output.
"""
if format:
format = format + " {"
else:
format = "{"
VaOutput(format, args)
IndentLevel()
def OutRbrace():
"""Decrease the indentation level and output a '}' on a line by itself."""
DedentLevel()
Output("}")
def OutHeader(text, dash):
"""Output a header comment using a given dash character."""
n = 64 - len(text)
Output()
Output("/* %s %s %s */", dash * (n/2), text, dash * (n - n/2))
Output()
def OutHeader1(text):
"""Output a level 1 header comment (uses '=' dashes)."""
OutHeader(text, "=")
def OutHeader2(text):
"""Output a level 2 header comment (uses '-' dashes)."""
OutHeader(text, "-")
def Out(text):
"""Output multiline text that's internally indented.
Pass this a multiline character string. The whitespace before the
first nonblank line of the string will be subtracted from all lines.
The lines are then output using Output(), but without interpretation
of formatting (if you need formatting you can do it before the call).
Recommended use:
Out('''
int main(argc, argv)
int argc;
char *argv;
{
printf("Hello, world\\n");
exit(0);
}
''')
Caveat: the indentation must be consistent -- if you use three tabs
in the first line, (up to) three tabs are removed from following lines,
but a line beginning with 24 spaces is not trimmed at all. Don't use
this as a feature.
"""
# (Don't you love using triple quotes *inside* triple quotes? :-)
lines = text.split('\n')
indent = ""
for line in lines:
if line.strip():
for c in line:
if not c.isspace():
break
indent = indent + c
break
n = len(indent)
for line in lines:
if line[:n] == indent:
line = line[n:]
else:
for c in indent:
if line[:1] <> c: break
line = line[1:]
VaOutput("%s", line)
def _test():
"""Test program. Run when the module is run as a script."""
OutHeader1("test bgenOutput")
Out("""
#include <Python.h>
#include <stdio.h>
main(argc, argv)
int argc;
char **argv;
{
int i;
""")
IndentLevel()
Output("""\
/* Here are a few comment lines.
Just to test indenting multiple lines.
End of the comment lines. */
""")
Output("for (i = 0; i < argc; i++)")
OutLbrace()
Output('printf("argv[%%d] = %%s\\n", i, argv[i]);')
OutRbrace()
Output("exit(0)")
OutRbrace()
OutHeader2("end test")
if __name__ == '__main__':
_test()

View File

@@ -0,0 +1,62 @@
"""Buffers allocated on the stack."""
from bgenBuffer import FixedInputBufferType, FixedOutputBufferType
class StackOutputBufferType(FixedOutputBufferType):
"""Fixed output buffer allocated on the stack -- passed as (buffer, size).
Instantiate with the buffer size as parameter.
"""
def passOutput(self, name):
return "%s__out__, %s" % (name, self.size)
class VarStackOutputBufferType(StackOutputBufferType):
"""Output buffer allocated on the stack -- passed as (buffer, &size).
Instantiate with the buffer size as parameter.
"""
def getSizeDeclarations(self, name):
return []
def getAuxDeclarations(self, name):
return ["int %s__len__ = %s" % (name, self.size)]
def passOutput(self, name):
return "%s__out__, &%s__len__" % (name, name)
def mkvalueArgs(self, name):
return "%s__out__, (int)%s__len__" % (name, name)
class VarVarStackOutputBufferType(VarStackOutputBufferType):
"""Output buffer allocated on the stack -- passed as (buffer, size, &size).
Instantiate with the buffer size as parameter.
"""
def passOutput(self, name):
return "%s__out__, %s__len__, &%s__len__" % (name, name, name)
class ReturnVarStackOutputBufferType(VarStackOutputBufferType):
"""Output buffer allocated on the stack -- passed as (buffer, size) -> size.
Instantiate with the buffer size as parameter.
The function's return value is the size.
(XXX Should have a way to suppress returning it separately, too.)
"""
def passOutput(self, name):
return "%s__out__, %s__len__" % (name, name)
def mkvalueArgs(self, name):
return "%s__out__, (int)_rv" % name

View File

@@ -0,0 +1,67 @@
"""Buffers used to hold null-terminated strings."""
from bgenBuffer import FixedOutputBufferType
from bgenStackBuffer import StackOutputBufferType
from bgenHeapBuffer import HeapOutputBufferType
class StringBufferMixIn:
"""Mix-in class to create various string buffer types.
Strings are character arrays terminated by a null byte.
(For input, this is also covered by stringptr.)
For output, there are again three variants:
- Fixed: size is a constant given in the documentation; or
- Stack: size is passed to the C function but we decide on a size at
code generation time so we can still allocate on the heap); or
- Heap: size is passed to the C function and we let the Python caller
pass a size.
(Note that this doesn't cover output parameters in which a string
pointer is returned. These are actually easier (no allocation) but far
less common. I'll write the classes when there is demand.)
"""
def getSizeDeclarations(self, name):
return []
def getAuxDeclarations(self, name):
return []
def getargsFormat(self):
return "s"
def getargsArgs(self, name):
return "&%s__in__" % name
def mkvalueFormat(self):
return "s"
def mkvalueArgs(self, name):
return "%s__out__" % name
class FixedOutputStringType(StringBufferMixIn, FixedOutputBufferType):
"""Null-terminated output string -- passed without size.
Instantiate with buffer size as parameter.
"""
class StackOutputStringType(StringBufferMixIn, StackOutputBufferType):
"""Null-terminated output string -- passed as (buffer, size).
Instantiate with buffer size as parameter.
"""
class HeapOutputStringType(StringBufferMixIn, HeapOutputBufferType):
"""Null-terminated output string -- passed as (buffer, size).
Instantiate without parameters.
Call from Python with buffer size.
"""

View File

@@ -0,0 +1,328 @@
"""Type classes and a modest collection of standard types."""
from bgenOutput import *
class Type:
"""Define the various things you can do with a C type.
Most methods are intended to be extended or overridden.
"""
def __init__(self, typeName, fmt):
"""Call with the C name and getargs format for the type.
Example: int = Type("int", "i")
"""
self.typeName = typeName
self.fmt = fmt
def declare(self, name, reference=False):
"""Declare a variable of the type with a given name.
Example: int.declare('spam') prints "int spam;"
"""
for decl in self.getArgDeclarations(name, reference):
Output("%s;", decl)
for decl in self.getAuxDeclarations(name):
Output("%s;", decl)
def getArgDeclarations(self, name, reference=False, constmode=False, outmode=False):
"""Return the main part of the declarations for this type: the items
that will be passed as arguments in the C/C++ function call."""
if reference:
ref = "&"
else:
ref = ""
if constmode:
const = "const "
else:
const = ""
if outmode:
out = "*"
else:
out = ""
return ["%s%s%s%s %s" % (const, self.typeName, ref, out, name)]
def getAuxDeclarations(self, name):
"""Return any auxiliary declarations needed for implementing this
type, such as helper variables used to hold sizes, etc. These declarations
are not part of the C/C++ function call interface."""
return []
def getargs(self):
return self.getargsFormat(), self.getargsArgs()
def getargsFormat(self):
"""Return the format for this type for use with PyArg_Parse().
Example: int.getargsFormat() returns the string "i".
(getargs is a very old name for PyArg_Parse, hence the name of this method).
"""
return self.fmt
def getargsArgs(self, name):
"""Return an argument for use with PyArg_Parse().
Example: int.getargsArgs("spam") returns the string "&spam".
"""
return "&" + name
def getargsPreCheck(self, name):
"""Perform any actions needed before calling getargs().
This could include declaring temporary variables and such.
"""
def getargsCheck(self, name):
"""Perform any needed post-[new]getargs() checks.
This is type-dependent; the default does not check for errors.
An example would be a check for a maximum string length, or it
could do post-getargs() copying or conversion."""
def passInput(self, name):
"""Return an argument for passing a variable into a call.
Example: int.passInput("spam") returns the string "spam".
"""
return name
def passOutput(self, name):
"""Return an argument for returning a variable out of a call.
Example: int.passOutput("spam") returns the string "&spam".
"""
return "&" + name
def passReference(self, name):
"""Return an argument for C++ pass-by-reference.
Default is to call passInput().
"""
return self.passInput(name)
def errorCheck(self, name):
"""Check for an error returned in the variable.
This is type-dependent; the default does not check for errors.
An example would be a check for a NULL pointer.
If an error is found, the generated routine should
raise an exception and return NULL.
XXX There should be a way to add error clean-up code.
"""
Output("/* XXX no err check for %s %s */", self.typeName, name)
def mkvalue(self):
return self.mkvalueFormat(), self.mkvalueArgs()
def mkvalueFormat(self):
"""Return the format for this type for use with Py_BuildValue().
This is normally the same as getargsFormat() but it is
a separate function to allow future divergence.
(mkvalue is a very old name for Py_BuildValue, hence the name of this
method).
"""
return self.getargsFormat()
def mkvalueArgs(self, name):
"""Return an argument for use with Py_BuildValue().
Example: int.mkvalueArgs("spam") returns the string "spam".
"""
return name
def mkvaluePreCheck(self, name):
"""Perform any actions needed before calling mkvalue().
This could include declaring temporary variables and such.
"""
def cleanup(self, name):
"""Clean up if necessary.
This is normally empty; it may deallocate buffers etc.
"""
pass
class ByAddressType(Type):
"Simple type that is also passed by address for input"
def passInput(self, name):
return "&%s" % name
# Sometimes it's useful to define a type that's only usable as input or output parameter
class InputOnlyMixIn:
"Mix-in class to boobytrap passOutput"
def passOutput(self, name):
raise RuntimeError, "Type '%s' can only be used for input parameters" % self.typeName
class InputOnlyType(InputOnlyMixIn, Type):
"Same as Type, but only usable for input parameters -- passOutput is boobytrapped"
class OutputOnlyMixIn:
"Mix-in class to boobytrap passInput"
def passInput(self, name):
raise RuntimeError, "Type '%s' can only be used for output parameters" % self.typeName
class OutputOnlyType(OutputOnlyMixIn, Type):
"Same as Type, but only usable for output parameters -- passInput is boobytrapped"
# A modest collection of standard C types.
void = None
char = Type("char", "c")
short = Type("short", "h")
unsigned_short = Type("unsigned short", "H")
int = Type("int", "i")
long = Type("long", "l")
unsigned_long = Type("unsigned long", "l")
float = Type("float", "f")
double = Type("double", "d")
# The most common use of character pointers is a null-terminated string.
# For input, this is easy. For output, and for other uses of char *,
# see the module bgenBuffer.
stringptr = InputOnlyType("char*", "s")
unicodestringptr = InputOnlyType("wchar_t *", "u")
# Some Python related types.
objectptr = Type("PyObject*", "O")
stringobjectptr = Type("PyStringObject*", "S")
# Etc.
class FakeType(InputOnlyType):
"""A type that is not represented in the Python version of the interface.
Instantiate with a value to pass in the call.
"""
def __init__(self, substitute):
self.substitute = substitute
self.typeName = None # Don't show this argument in __doc__ string
def getArgDeclarations(self, name, reference=False, constmode=False, outmode=False):
return []
def getAuxDeclarations(self, name, reference=False):
return []
def getargsFormat(self):
return ""
def getargsArgs(self, name):
return None
def passInput(self, name):
return self.substitute
class OpaqueType(Type):
"""A type represented by an opaque object type, always passed by address.
Instantiate with the type name and the names of the new and convert procs.
If fewer than three arguments are passed, the second argument is used
to derive the new and convert procs by appending _New and _Convert; it
defaults to the first argument.
"""
def __init__(self, name, arg = None, extra = None):
self.typeName = name
if extra is None:
# Two arguments (name, usetype) or one (name)
arg = arg or name
self.new = arg + '_New'
self.convert = arg + '_Convert'
else:
# Three arguments (name, new, convert)
self.new = arg
self.convert = extra
def getargsFormat(self):
return "O&"
def getargsArgs(self, name):
return "%s, &%s" % (self.convert, name)
def passInput(self, name):
return "&%s" % name
def mkvalueFormat(self):
return "O&"
def mkvalueArgs(self, name):
return "%s, &%s" % (self.new, name)
class OpaqueByValueType(OpaqueType):
"""A type represented by an opaque object type, on input passed BY VALUE.
Instantiate with the type name, and optionally an object type name whose
New/Convert functions will be used.
"""
def passInput(self, name):
return name
def mkvalueArgs(self, name):
return "%s, %s" % (self.new, name)
class OpaqueByRefType(OpaqueType):
"""An opaque object type, passed by reference.
Instantiate with the type name, and optionally an object type name whose
New/Convert functions will be used.
"""
def passInput(self, name):
return name
# def passOutput(self, name):
# return name
def mkvalueFormat(self):
return "O"
def mkvalueArgs(self, name):
return "%s(%s)" % (self.new, name)
class OpaqueByValueStructType(OpaqueByValueType):
"""Similar to OpaqueByValueType, but we also pass this to mkvalue by
address, in stead of by value.
"""
def mkvalueArgs(self, name):
return "%s, &%s" % (self.new, name)
class OpaqueArrayType(OpaqueByValueType):
"""A type represented by an opaque object type, with ARRAY passing semantics.
Instantiate with the type name, and optional an object type name whose
New/Convert functions will be used.
"""
def getargsArgs(self, name):
return "%s, %s" % (self.convert, name)
def passOutput(self, name):
return name

View File

@@ -0,0 +1,112 @@
"""Variables, arguments and argument transfer modes etc."""
# Values to represent argument transfer modes
InMode = 1 # input-only argument
OutMode = 2 # output-only argument
InOutMode = 3 # input-output argument
ModeMask = 3 # bits to keep for mode
# Special cases for mode/flags argument
# XXX This is still a mess!
SelfMode = 4+InMode # this is 'self' -- don't declare it
ReturnMode = 8+OutMode # this is the function return value
ErrorMode = 16+OutMode # this is an error status -- turn it into an exception
RefMode = 32
ConstMode = 64
class Variable:
"""A Variable holds a type, a name, a transfer mode and flags.
Most of its methods call the correponding type method with the
variable name.
"""
def __init__(self, type, name = None, flags = InMode):
"""Call with a type, a name and flags.
If name is None, it muse be set later.
flags defaults to InMode.
"""
self.type = type
self.name = name
self.flags = flags
self.mode = flags & ModeMask
def declare(self):
"""Declare the variable if necessary.
If it is "self", it is not declared.
"""
if self.flags == ReturnMode+RefMode:
self.type.declare(self.name, reference=True)
elif self.flags != SelfMode:
self.type.declare(self.name)
def getArgDeclarations(self, fullmodes=False):
refmode = (self.flags & RefMode)
constmode = False
outmode = False
if fullmodes:
constmode = (self.flags & ConstMode)
outmode = (self.flags & OutMode)
return self.type.getArgDeclarations(self.name,
reference=refmode, constmode=constmode, outmode=outmode)
def getAuxDeclarations(self):
return self.type.getAuxDeclarations(self.name)
def getargsFormat(self):
"""Call the type's getargsFormatmethod."""
return self.type.getargsFormat()
def getargsArgs(self):
"""Call the type's getargsArgsmethod."""
return self.type.getargsArgs(self.name)
def getargsCheck(self):
return self.type.getargsCheck(self.name)
def getargsPreCheck(self):
return self.type.getargsPreCheck(self.name)
def passArgument(self):
"""Return the string required to pass the variable as argument.
For "in" arguments, return the variable name.
For "out" and "in out" arguments,
return its name prefixed with "&".
"""
if self.mode == InMode:
return self.type.passInput(self.name)
if self.mode & RefMode:
return self.type.passReference(self.name)
if self.mode in (OutMode, InOutMode):
return self.type.passOutput(self.name)
# XXX Shouldn't get here
return "/*mode?*/" + self.type.passInput(self.name)
def errorCheck(self):
"""Check for an error if necessary.
This only generates code if the variable's mode is ErrorMode.
"""
if self.flags == ErrorMode:
self.type.errorCheck(self.name)
def mkvalueFormat (self):
"""Call the type's mkvalueFormat method."""
return self.type.mkvalueFormat()
def mkvalueArgs(self):
"""Call the type's mkvalueArgs method."""
return self.type.mkvalueArgs(self.name)
def mkvaluePreCheck(self):
return self.type.mkvaluePreCheck(self.name)
def cleanup(self):
"""Call the type's cleanup method."""
return self.type.cleanup(self.name)

View File

@@ -0,0 +1,197 @@
"""\
Augment the "bgen" package with definitions that are useful on the Apple Macintosh.
Intended usage is "from macsupport import *" -- this implies all bgen's goodies.
"""
# Import everything from bgen (for ourselves as well as for re-export)
from bgen import *
# Simple types
Boolean = Type("Boolean", "b")
SignedByte = Type("SignedByte", "b")
Size = Type("Size", "l")
Style = Type("Style", "b")
StyleParameter = Type("StyleParameter", "h")
CharParameter = Type("CharParameter", "h")
TextEncoding = Type("TextEncoding", "l")
ByteCount = Type("ByteCount", "l")
Duration = Type("Duration", "l")
ByteOffset = Type("ByteOffset", "l")
OptionBits = Type("OptionBits", "l")
ItemCount = Type("ItemCount", "l")
PBVersion = Type("PBVersion", "l")
ScriptCode = Type("ScriptCode", "h")
LangCode = Type("LangCode", "h")
RegionCode = Type("RegionCode", "h")
UInt8 = Type("UInt8", "b")
SInt8 = Type("SInt8", "b")
UInt16 = Type("UInt16", "H")
SInt16 = Type("SInt16", "h")
UInt32 = Type("UInt32", "l")
SInt32 = Type("SInt32", "l")
Float32 = Type("Float32", "f")
wide = OpaqueByValueType("wide", "PyMac_Buildwide", "PyMac_Getwide")
wide_ptr = OpaqueType("wide", "PyMac_Buildwide", "PyMac_Getwide")
# Pascal strings
ConstStr255Param = OpaqueArrayType("Str255", "PyMac_BuildStr255", "PyMac_GetStr255")
Str255 = OpaqueArrayType("Str255", "PyMac_BuildStr255", "PyMac_GetStr255")
StringPtr = OpaqueByValueType("StringPtr", "PyMac_BuildStr255", "PyMac_GetStr255")
ConstStringPtr = StringPtr
# File System Specifications
FSSpec_ptr = OpaqueType("FSSpec", "PyMac_BuildFSSpec", "PyMac_GetFSSpec")
FSSpec = OpaqueByValueStructType("FSSpec", "PyMac_BuildFSSpec", "PyMac_GetFSSpec")
FSRef_ptr = OpaqueType("FSRef", "PyMac_BuildFSRef", "PyMac_GetFSRef")
FSRef = OpaqueByValueStructType("FSRef", "PyMac_BuildFSRef", "PyMac_GetFSRef")
# OSType and ResType: 4-byte character strings
def OSTypeType(typename):
return OpaqueByValueType(typename, "PyMac_BuildOSType", "PyMac_GetOSType")
OSType = OSTypeType("OSType")
ResType = OSTypeType("ResType")
FourCharCode = OSTypeType("FourCharCode")
# Version numbers
NumVersion = OpaqueByValueType("NumVersion", "PyMac_BuildNumVersion", "BUG")
# Handles (always resources in our case)
Handle = OpaqueByValueType("Handle", "ResObj")
MenuHandle = OpaqueByValueType("MenuHandle", "MenuObj")
MenuRef = MenuHandle
ControlHandle = OpaqueByValueType("ControlHandle", "CtlObj")
ControlRef = ControlHandle
# Windows and Dialogs
WindowPtr = OpaqueByValueType("WindowPtr", "WinObj")
WindowRef = WindowPtr
DialogPtr = OpaqueByValueType("DialogPtr", "DlgObj")
DialogRef = DialogPtr
ExistingWindowPtr = OpaqueByValueType("WindowPtr", "WinObj_WhichWindow", "BUG")
ExistingDialogPtr = OpaqueByValueType("DialogPtr", "DlgObj_WhichDialog", "BUG")
# NULL pointer passed in as optional storage -- not present in Python version
NullStorage = FakeType("(void *)0")
# More standard datatypes
Fixed = OpaqueByValueType("Fixed", "PyMac_BuildFixed", "PyMac_GetFixed")
# Quickdraw data types
Rect = Rect_ptr = OpaqueType("Rect", "PyMac_BuildRect", "PyMac_GetRect")
Point = OpaqueByValueType("Point", "PyMac_BuildPoint", "PyMac_GetPoint")
Point_ptr = OpaqueType("Point", "PyMac_BuildPoint", "PyMac_GetPoint")
# Event records
EventRecord = OpaqueType("EventRecord", "PyMac_BuildEventRecord", "PyMac_GetEventRecord")
EventRecord_ptr = EventRecord
# CoreFoundation datatypes
CFTypeRef = OpaqueByValueType("CFTypeRef", "CFTypeRefObj")
CFStringRef = OpaqueByValueType("CFStringRef", "CFStringRefObj")
CFMutableStringRef = OpaqueByValueType("CFMutableStringRef", "CFMutableStringRefObj")
CFArrayRef = OpaqueByValueType("CFArrayRef", "CFArrayRefObj")
CFMutableArrayRef = OpaqueByValueType("CFMutableArrayRef", "CFMutableArrayRefObj")
CFDictionaryRef = OpaqueByValueType("CFDictionaryRef", "CFDictionaryRefObj")
CFMutableDictionaryRef = OpaqueByValueType("CFMutableDictionaryRef", "CFMutableDictionaryRefObj")
CFURLRef = OpaqueByValueType("CFURLRef", "CFURLRefObj")
OptionalCFURLRef = OpaqueByValueType("CFURLRef", "OptionalCFURLRefObj")
# OSErr is special because it is turned into an exception
# (Could do this with less code using a variant of mkvalue("O&")?)
class OSErrType(Type):
def errorCheck(self, name):
Output("if (%s != noErr) return PyMac_Error(%s);", name, name)
self.used = 1
OSErr = OSErrType("OSErr", 'h')
OSStatus = OSErrType("OSStatus", 'l')
# Various buffer types
InBuffer = VarInputBufferType('char', 'long', 'l') # (buf, len)
UcharInBuffer = VarInputBufferType('unsigned char', 'long', 'l') # (buf, len)
OptionalInBuffer = OptionalVarInputBufferType('char', 'long', 'l') # (buf, len)
InOutBuffer = HeapInputOutputBufferType('char', 'long', 'l') # (inbuf, outbuf, len)
VarInOutBuffer = VarHeapInputOutputBufferType('char', 'long', 'l') # (inbuf, outbuf, &len)
OutBuffer = HeapOutputBufferType('char', 'long', 'l') # (buf, len)
VarOutBuffer = VarHeapOutputBufferType('char', 'long', 'l') # (buf, &len)
VarVarOutBuffer = VarVarHeapOutputBufferType('char', 'long', 'l') # (buf, len, &len)
# Unicode arguments sometimes have reversed len, buffer (don't understand why Apple did this...)
class VarUnicodeInputBufferType(VarInputBufferType):
def getargsFormat(self):
return "u#"
class VarUnicodeReverseInputBufferType(ReverseInputBufferMixin, VarUnicodeInputBufferType):
pass
UnicodeInBuffer = VarUnicodeInputBufferType('UniChar', 'UniCharCount', 'l')
UnicodeReverseInBuffer = VarUnicodeReverseInputBufferType('UniChar', 'UniCharCount', 'l')
UniChar_ptr = InputOnlyType("UniCharPtr", "u")
# Predefine various pieces of program text to be passed to Module() later:
# Stuff added immediately after the system include files
includestuff = """
#include "pymactoolbox.h"
/* Macro to test whether a weak-loaded CFM function exists */
#define PyMac_PRECHECK(rtn) do { if ( &rtn == NULL ) {\\
PyErr_SetString(PyExc_NotImplementedError, \\
"Not available in this shared library/OS version"); \\
return NULL; \\
}} while(0)
"""
# Stuff added just before the module's init function
finalstuff = """
"""
# Stuff added inside the module's init function
initstuff = """
"""
# Generator classes with a twist -- if the function returns OSErr,
# its mode is manipulated so that it turns into an exception or disappears
# (and its name is changed to _err, for documentation purposes).
# This requires that the OSErr type (defined above) has a non-trivial
# errorCheck method.
class OSErrMixIn:
"Mix-in class to treat OSErr/OSStatus return values special"
def makereturnvar(self):
if self.returntype.__class__ == OSErrType:
return Variable(self.returntype, "_err", ErrorMode)
else:
return Variable(self.returntype, "_rv", OutMode)
class OSErrFunctionGenerator(OSErrMixIn, FunctionGenerator): pass
class OSErrMethodGenerator(OSErrMixIn, MethodGenerator): pass
class WeakLinkMixIn:
"Mix-in to test the function actually exists (!= NULL) before calling"
def precheck(self):
Output('#ifndef %s', self.name)
Output('PyMac_PRECHECK(%s);', self.name)
Output('#endif')
class WeakLinkFunctionGenerator(WeakLinkMixIn, FunctionGenerator): pass
class WeakLinkMethodGenerator(WeakLinkMixIn, MethodGenerator): pass
class OSErrWeakLinkFunctionGenerator(OSErrMixIn, WeakLinkMixIn, FunctionGenerator): pass
class OSErrWeakLinkMethodGenerator(OSErrMixIn, WeakLinkMixIn, MethodGenerator): pass
class MacModule(Module):
"Subclass which gets the exception initializer from macglue.c"
def exceptionInitializer(self):
return "PyMac_GetOSErrException()"

View File

@@ -0,0 +1,849 @@
"""\
Tools for scanning header files in search of function prototypes.
Often, the function prototypes in header files contain enough information
to automatically generate (or reverse-engineer) interface specifications
from them. The conventions used are very vendor specific, but once you've
figured out what they are they are often a great help, and it sure beats
manually entering the interface specifications. (These are needed to generate
the glue used to access the functions from Python.)
In order to make this class useful, almost every component can be overridden.
The defaults are (currently) tuned to scanning Apple Macintosh header files,
although most Mac specific details are contained in header-specific subclasses.
"""
import re
import sys
import os
import fnmatch
from types import *
try:
import MacOS
except ImportError:
MacOS = None
try:
from bgenlocations import CREATOR, INCLUDEDIR
except ImportError:
CREATOR = None
INCLUDEDIR = os.curdir
Error = "scantools.Error"
BEGINHTMLREPORT="""<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<style type="text/css">
.unmatched { }
.commentstripping { color: grey; text-decoration: line-through }
.comment { text-decoration: line-through }
.notcomment { color: black }
.incomplete { color: maroon }
.constant { color: green }
.pyconstant { background-color: yellow }
.blconstant { background-color: yellow; color: red }
.declaration { color: blue }
.pydeclaration { background-color: yellow }
.type { font-style: italic }
.name { font-weight: bold }
.value { font-style: italic }
.arglist { text-decoration: underline }
.blacklisted { background-color: yellow; color: red }
</style>
<title>Bgen scan report</title>
</head>
<body>
<h1>Bgen scan report</h1>
<h2>Legend</h2>
<p>This scan report is intended to help you debug the regular expressions
used by the bgen scanner. It consists of the original ".h" header file(s)
marked up to show you what the regular expressions in the bgen parser matched
for each line. NOTE: comments in the original source files may or may not be
shown.</p>
<p>The typographic conventions of this file are as follows:</p>
<dl>
<dt>comment stripping</dt>
<dd><pre><span class="commentstripping"><span class="notcomment">comment stripping is </span><span class="comment">/* marked up */</span><span class="notcomment"> and the line is repeated if needed</span></span></pre>
<p>If anything here does not appear to happen correctly look at
<tt>comment1_pat</tt> and <tt>comment2_pat</tt>.</p>
</dd>
<dt>constant definitions</dt>
<dd><pre><span class="constant">#define <span class="name">name</span> <span class="value">value</span></pre>
<p>Highlights name and value of the constant. Governed by <tt>sym_pat</tt>.</p>
</dd>
<dt>function declaration</dt>
<dd><pre><span class="declaration"><span class="type">char *</span><span class="name">rindex</span><span class="arglist">(<span class="type">const char *</span><span class="name">s</span>, <span class="type">int </span><span class="name">c</span>)</span>;</span></pre>
<p>Highlights type, name and argument list. <tt>type_pat</tt>,
<tt>name_pat</tt> and <tt>args_pat</tt> are combined into <tt>whole_pat</tt>, which
is what is used here.</p></dd>
</dd>
<dt>incomplete match for function declaration</dt>
<dd><pre><span class="incomplete"><span class="type">char *</span>foo;</span></pre>
<p>The beginning of this looked promising, but it did not match a function declaration.
In other words, it matched <tt>head_pat</tt> but not <tt>whole_pat</tt>. If the next
declaration has also been gobbled up you need to look at <tt>end_pat</tt>.</p>
</dd>
<dt>unrecognized input</dt>
<dd><pre><span class="unmatched">#include "type.h"</span></pre>
<p>If there are function declarations the scanner has missed (i.e. things
are in this class but you want them to be declarations) you need to adapt
<tt>head_pat</tt>.
</dd>
</dl>
<h2>Output</h2>
<pre>
<span class="unmatched">
"""
ENDHTMLREPORT="""</span>
</pre>
</body>
</html>
"""
class Scanner:
# Set to 1 in subclass to debug your scanner patterns.
debug = 0
def __init__(self, input = None, output = None, defsoutput = None):
self.initsilent()
self.initblacklists()
self.initrepairinstructions()
self.initpaths()
self.initfiles()
self.initpatterns()
self.compilepatterns()
self.initosspecifics()
self.initusedtypes()
if output:
self.setoutput(output, defsoutput)
if input:
self.setinput(input)
def initusedtypes(self):
self.usedtypes = {}
def typeused(self, type, mode):
if not self.usedtypes.has_key(type):
self.usedtypes[type] = {}
self.usedtypes[type][mode] = None
def reportusedtypes(self):
types = self.usedtypes.keys()
types.sort()
for type in types:
modes = self.usedtypes[type].keys()
modes.sort()
self.report("%s %s", type, " ".join(modes))
def gentypetest(self, file):
fp = open(file, "w")
fp.write("types=[\n")
types = self.usedtypes.keys()
types.sort()
for type in types:
fp.write("\t'%s',\n"%type)
fp.write("]\n")
fp.write("""missing=0
for t in types:
try:
tt = eval(t)
except NameError:
print "** Missing type:", t
missing = 1
if missing: raise "Missing Types"
""")
fp.close()
def initsilent(self):
self.silent = 1
def error(self, format, *args):
if self.silent >= 0:
print format%args
def report(self, format, *args):
if not self.silent:
print format%args
def writeinitialdefs(self):
pass
def initblacklists(self):
self.blacklistnames = self.makeblacklistnames()
self.blacklisttypes = ["unknown", "-"] + self.makeblacklisttypes()
self.greydictnames = self.greylist2dict(self.makegreylist())
def greylist2dict(self, list):
rv = {}
for define, namelist in list:
for name in namelist:
rv[name] = define
return rv
def makeblacklistnames(self):
return []
def makeblacklisttypes(self):
return []
def makegreylist(self):
return []
def initrepairinstructions(self):
self.repairinstructions = self.makerepairinstructions()
self.inherentpointertypes = self.makeinherentpointertypes()
def makerepairinstructions(self):
"""Parse the repair file into repair instructions.
The file format is simple:
1) use \ to split a long logical line in multiple physical lines
2) everything after the first # on a line is ignored (as comment)
3) empty lines are ignored
4) remaining lines must have exactly 3 colon-separated fields:
functionpattern : argumentspattern : argumentsreplacement
5) all patterns use shell style pattern matching
6) an empty functionpattern means the same as *
7) the other two fields are each comma-separated lists of triples
8) a triple is a space-separated list of 1-3 words
9) a triple with less than 3 words is padded at the end with "*" words
10) when used as a pattern, a triple matches the type, name, and mode
of an argument, respectively
11) when used as a replacement, the words of a triple specify
replacements for the corresponding words of the argument,
with "*" as a word by itself meaning leave the original word
(no other uses of "*" is allowed)
12) the replacement need not have the same number of triples
as the pattern
"""
f = self.openrepairfile()
if not f: return []
print "Reading repair file", repr(f.name), "..."
list = []
lineno = 0
while 1:
line = f.readline()
if not line: break
lineno = lineno + 1
startlineno = lineno
while line[-2:] == '\\\n':
line = line[:-2] + ' ' + f.readline()
lineno = lineno + 1
i = line.find('#')
if i >= 0: line = line[:i]
words = [s.strip() for s in line.split(':')]
if words == ['']: continue
if len(words) <> 3:
print "Line", startlineno,
print ": bad line (not 3 colon-separated fields)"
print repr(line)
continue
[fpat, pat, rep] = words
if not fpat: fpat = "*"
if not pat:
print "Line", startlineno,
print "Empty pattern"
print repr(line)
continue
patparts = [s.strip() for s in pat.split(',')]
repparts = [s.strip() for s in rep.split(',')]
patterns = []
for p in patparts:
if not p:
print "Line", startlineno,
print "Empty pattern part"
print repr(line)
continue
pattern = p.split()
if len(pattern) > 3:
print "Line", startlineno,
print "Pattern part has > 3 words"
print repr(line)
pattern = pattern[:3]
else:
while len(pattern) < 3:
pattern.append("*")
patterns.append(pattern)
replacements = []
for p in repparts:
if not p:
print "Line", startlineno,
print "Empty replacement part"
print repr(line)
continue
replacement = p.split()
if len(replacement) > 3:
print "Line", startlineno,
print "Pattern part has > 3 words"
print repr(line)
replacement = replacement[:3]
else:
while len(replacement) < 3:
replacement.append("*")
replacements.append(replacement)
list.append((fpat, patterns, replacements))
return list
def makeinherentpointertypes(self):
return []
def openrepairfile(self, filename = "REPAIR"):
try:
return open(filename, "rU")
except IOError, msg:
print repr(filename), ":", msg
print "Cannot open repair file -- assume no repair needed"
return None
def initfiles(self):
self.specmine = 0
self.defsmine = 0
self.scanmine = 0
self.htmlmine = 0
self.specfile = sys.stdout
self.defsfile = None
self.scanfile = sys.stdin
self.htmlfile = None
self.lineno = 0
self.line = ""
def initpaths(self):
self.includepath = [os.curdir, INCLUDEDIR]
def initpatterns(self):
self.head_pat = r"^EXTERN_API[^_]"
self.tail_pat = r"[;={}]"
self.type_pat = r"EXTERN_API" + \
r"[ \t\n]*\([ \t\n]*" + \
r"(?P<type>[a-zA-Z0-9_* \t]*[a-zA-Z0-9_*])" + \
r"[ \t\n]*\)[ \t\n]*"
self.name_pat = r"(?P<name>[a-zA-Z0-9_]+)[ \t\n]*"
self.args_pat = r"\((?P<args>([^\(;=\)]+|\([^\(;=\)]*\))*)\)"
self.whole_pat = self.type_pat + self.name_pat + self.args_pat
self.sym_pat = r"^[ \t]*(?P<name>[a-zA-Z0-9_]+)[ \t]*=" + \
r"[ \t]*(?P<defn>[-0-9_a-zA-Z'\"\(][^\t\n,;}]*),?"
self.asplit_pat = r"^(?P<type>.*[^a-zA-Z0-9_])(?P<name>[a-zA-Z0-9_]+)(?P<array>\[\])?$"
self.comment1_pat = r"(?P<rest>.*)//.*"
# note that the next pattern only removes comments that are wholly within one line
self.comment2_pat = r"(?P<rest1>.*)/\*.*\*/(?P<rest2>.*)"
def compilepatterns(self):
for name in dir(self):
if name[-4:] == "_pat":
pat = getattr(self, name)
prog = re.compile(pat)
setattr(self, name[:-4], prog)
def initosspecifics(self):
if MacOS and CREATOR:
self.filetype = 'TEXT'
self.filecreator = CREATOR
else:
self.filetype = self.filecreator = None
def setfiletype(self, filename):
if MacOS and (self.filecreator or self.filetype):
creator, type = MacOS.GetCreatorAndType(filename)
if self.filecreator: creator = self.filecreator
if self.filetype: type = self.filetype
MacOS.SetCreatorAndType(filename, creator, type)
def close(self):
self.closefiles()
def closefiles(self):
self.closespec()
self.closedefs()
self.closescan()
self.closehtml()
def closespec(self):
tmp = self.specmine and self.specfile
self.specfile = None
if tmp: tmp.close()
def closedefs(self):
tmp = self.defsmine and self.defsfile
self.defsfile = None
if tmp: tmp.close()
def closescan(self):
tmp = self.scanmine and self.scanfile
self.scanfile = None
if tmp: tmp.close()
def closehtml(self):
if self.htmlfile: self.htmlfile.write(ENDHTMLREPORT)
tmp = self.htmlmine and self.htmlfile
self.htmlfile = None
if tmp: tmp.close()
def setoutput(self, spec, defs = None):
self.closespec()
self.closedefs()
if spec:
if type(spec) == StringType:
file = self.openoutput(spec)
mine = 1
else:
file = spec
mine = 0
self.specfile = file
self.specmine = mine
if defs:
if type(defs) == StringType:
file = self.openoutput(defs)
mine = 1
else:
file = defs
mine = 0
self.defsfile = file
self.defsmine = mine
def sethtmloutput(self, htmlfile):
self.closehtml()
if htmlfile:
if type(htmlfile) == StringType:
file = self.openoutput(htmlfile)
mine = 1
else:
file = htmlfile
mine = 0
self.htmlfile = file
self.htmlmine = mine
self.htmlfile.write(BEGINHTMLREPORT)
def openoutput(self, filename):
try:
file = open(filename, 'w')
except IOError, arg:
raise IOError, (filename, arg)
self.setfiletype(filename)
return file
def setinput(self, scan = sys.stdin):
if not type(scan) in (TupleType, ListType):
scan = [scan]
self.allscaninputs = scan
self._nextinput()
def _nextinput(self):
if not self.allscaninputs:
return 0
scan = self.allscaninputs[0]
self.allscaninputs = self.allscaninputs[1:]
self.closescan()
if scan:
if type(scan) == StringType:
file = self.openinput(scan)
mine = 1
else:
file = scan
mine = 0
self.scanfile = file
self.scanmine = mine
self.lineno = 0
return 1
def openinput(self, filename):
if not os.path.isabs(filename):
for dir in self.includepath:
fullname = os.path.join(dir, filename)
#self.report("trying full name %r", fullname)
try:
return open(fullname, 'rU')
except IOError:
pass
# If not on the path, or absolute, try default open()
try:
return open(filename, 'rU')
except IOError, arg:
raise IOError, (arg, filename)
def getline(self):
if not self.scanfile:
raise Error, "input file not set"
self.line = self.scanfile.readline()
if not self.line:
if self._nextinput():
return self.getline()
raise EOFError
self.lineno = self.lineno + 1
return self.line
def scan(self):
if not self.scanfile:
self.error("No input file has been specified")
return
inputname = self.scanfile.name
self.report("scanfile = %r", inputname)
if not self.specfile:
self.report("(No interface specifications will be written)")
else:
self.report("specfile = %r", self.specfile.name)
self.specfile.write("# Generated from %r\n\n" % (inputname,))
if not self.defsfile:
self.report("(No symbol definitions will be written)")
else:
self.report("defsfile = %r", (self.defsfile.name,))
self.defsfile.write("# Generated from %r\n\n" % (os.path.split(inputname)[1],))
self.writeinitialdefs()
self.alreadydone = []
try:
while 1:
try: line = self.getline()
except EOFError: break
if self.debug:
self.report("LINE: %r" % (line,))
match = self.comment1.match(line)
if match:
self.htmlreport(line, klass='commentstripping', ranges=[(
match.start('rest'), match.end('rest'), 'notcomment')])
line = match.group('rest')
if self.debug:
self.report("\tafter comment1: %r" % (line,))
match = self.comment2.match(line)
while match:
if match:
self.htmlreport(line, klass='commentstripping', ranges=[
(match.start('rest1'), match.end('rest1'), 'notcomment'),
(match.start('rest2'), match.end('rest2'), 'notcomment')])
line = match.group('rest1')+match.group('rest2')
if self.debug:
self.report("\tafter comment2: %r" % (line,))
match = self.comment2.match(line)
if self.defsfile:
match = self.sym.match(line)
if match:
if self.debug:
self.report("\tmatches sym.")
self.dosymdef(match, line)
continue
match = self.head.match(line)
if match:
if self.debug:
self.report("\tmatches head.")
self.dofuncspec()
continue
self.htmlreport(line, klass='unmatched')
except EOFError:
self.error("Uncaught EOF error")
self.reportusedtypes()
def dosymdef(self, match, line):
name, defn = match.group('name', 'defn')
self.htmlreport(line, klass='constant', ranges=[
(match.start('name'), match.end('name'), 'name'),
(match.start('defn'), match.end('defn'), 'value')])
defn = escape8bit(defn)
if self.debug:
self.report("\tsym: name=%r, defn=%r" % (name, defn))
if not name in self.blacklistnames:
oline = "%s = %s\n" % (name, defn)
self.defsfile.write(oline)
self.htmlreport(oline, klass="pyconstant")
else:
self.defsfile.write("# %s = %s\n" % (name, defn))
self.htmlreport("** no output: name is blacklisted", klass="blconstant")
# XXXX No way to handle greylisted names
def dofuncspec(self):
raw = self.line
while not self.tail.search(raw):
line = self.getline()
if self.debug:
self.report("* CONTINUATION LINE: %r" % (line,))
match = self.comment1.match(line)
if match:
line = match.group('rest')
if self.debug:
self.report("\tafter comment1: %r" % (line,))
match = self.comment2.match(line)
while match:
line = match.group('rest1')+match.group('rest2')
if self.debug:
self.report("\tafter comment1: %r" % (line,))
match = self.comment2.match(line)
raw = raw + line
if self.debug:
self.report("* WHOLE LINE: %r" % (raw,))
self.processrawspec(raw)
return raw
def processrawspec(self, raw):
match = self.whole.search(raw)
if not match:
self.report("Bad raw spec: %r", raw)
if self.debug:
match = self.type.search(raw)
if not match:
self.report("(Type already doesn't match)")
self.htmlreport(raw, klass='incomplete', ranges=[(
match.start('type'), match.end('type'), 'type')])
else:
self.report("(but type matched)")
self.htmlreport(raw, klass='incomplete')
return
type, name, args = match.group('type', 'name', 'args')
ranges=[
(match.start('type'), match.end('type'), 'type'),
(match.start('name'), match.end('name'), 'name'),
(match.start('args'), match.end('args'), 'arglist')]
self.htmlreport(raw, klass='declaration', ranges=ranges)
modifiers = self.getmodifiers(match)
type = self.pythonizename(type)
name = self.pythonizename(name)
if self.checkduplicate(name):
self.htmlreport("*** no output generated: duplicate name", klass="blacklisted")
return
self.report("==> %s %s <==", type, name)
if self.blacklisted(type, name):
self.htmlreport("*** no output generated: function name or return type blacklisted", klass="blacklisted")
self.report("*** %s %s blacklisted", type, name)
return
returnlist = [(type, name, 'ReturnMode')]
returnlist = self.repairarglist(name, returnlist)
[(type, name, returnmode)] = returnlist
arglist = self.extractarglist(args)
arglist = self.repairarglist(name, arglist)
if self.unmanageable(type, name, arglist):
self.htmlreport("*** no output generated: some argument blacklisted", klass="blacklisted")
##for arg in arglist:
## self.report(" %r", arg)
self.report("*** %s %s unmanageable", type, name)
return
if modifiers:
self.generate(type, name, arglist, modifiers)
else:
self.generate(type, name, arglist)
def getmodifiers(self, match):
return []
def checkduplicate(self, name):
if name in self.alreadydone:
self.report("Name has already been defined: %r", name)
return True
self.alreadydone.append(name)
return False
def pythonizename(self, name):
name = re.sub("\*", " ptr", name)
name = name.strip()
name = re.sub("[ \t]+", "_", name)
return name
def extractarglist(self, args):
args = args.strip()
if not args or args == "void":
return []
parts = [s.strip() for s in args.split(",")]
arglist = []
for part in parts:
arg = self.extractarg(part)
arglist.append(arg)
return arglist
def extractarg(self, part):
mode = "InMode"
part = part.strip()
match = self.asplit.match(part)
if not match:
self.error("Indecipherable argument: %r", part)
return ("unknown", part, mode)
type, name, array = match.group('type', 'name', 'array')
if array:
# array matches an optional [] after the argument name
type = type + " ptr "
type = self.pythonizename(type)
return self.modifyarg(type, name, mode)
def modifyarg(self, type, name, mode):
if type[:6] == "const_":
type = type[6:]
elif type[-4:] == "_ptr":
type = type[:-4]
mode = "OutMode"
elif type in self.inherentpointertypes:
mode = "OutMode"
if type[-4:] == "_far":
type = type[:-4]
return type, name, mode
def repairarglist(self, functionname, arglist):
arglist = arglist[:]
i = 0
while i < len(arglist):
for item in self.repairinstructions:
if len(item) == 2:
pattern, replacement = item
functionpat = "*"
else:
functionpat, pattern, replacement = item
if not fnmatch.fnmatchcase(functionname, functionpat):
continue
n = len(pattern)
if i+n > len(arglist): continue
current = arglist[i:i+n]
for j in range(n):
if not self.matcharg(pattern[j], current[j]):
break
else: # All items of the pattern match
new = self.substituteargs(
pattern, replacement, current)
if new is not None:
arglist[i:i+n] = new
i = i+len(new) # No recursive substitutions
break
else: # No patterns match
i = i+1
return arglist
def matcharg(self, patarg, arg):
return len(filter(None, map(fnmatch.fnmatchcase, arg, patarg))) == 3
def substituteargs(self, pattern, replacement, old):
new = []
for k in range(len(replacement)):
item = replacement[k]
newitem = [item[0], item[1], item[2]]
for i in range(3):
if item[i] == '*':
newitem[i] = old[k][i]
elif item[i][:1] == '$':
index = int(item[i][1:]) - 1
newitem[i] = old[index][i]
new.append(tuple(newitem))
##self.report("old: %r", old)
##self.report("new: %r", new)
return new
def generate(self, tp, name, arglist, modifiers=[]):
self.typeused(tp, 'return')
if modifiers:
classname, listname = self.destination(tp, name, arglist, modifiers)
else:
classname, listname = self.destination(tp, name, arglist)
if not classname or not listname:
self.htmlreport("*** no output generated: self.destination() returned None", klass="blacklisted")
return
if not self.specfile:
self.htmlreport("*** no output generated: no output file specified", klass="blacklisted")
return
self.specfile.write("f = %s(%s, %r,\n" % (classname, tp, name))
for atype, aname, amode in arglist:
self.typeused(atype, amode)
self.specfile.write(" (%s, %r, %s),\n" %
(atype, aname, amode))
if self.greydictnames.has_key(name):
self.specfile.write(" condition=%r,\n"%(self.greydictnames[name],))
self.generatemodifiers(classname, name, modifiers)
self.specfile.write(")\n")
self.specfile.write("%s.append(f)\n\n" % listname)
if self.htmlfile:
oline = "Adding to %s:\n%s(returntype=%s, name=%r" % (listname, classname, tp, name)
for atype, aname, amode in arglist:
oline += ",\n (%s, %r, %s)" % (atype, aname, amode)
oline += ")\n"
self.htmlreport(oline, klass="pydeclaration")
def destination(self, type, name, arglist):
return "FunctionGenerator", "functions"
def generatemodifiers(self, classname, name, modifiers):
pass
def blacklisted(self, type, name):
if type in self.blacklisttypes:
##self.report("return type %s is blacklisted", type)
return 1
if name in self.blacklistnames:
##self.report("function name %s is blacklisted", name)
return 1
return 0
def unmanageable(self, type, name, arglist):
for atype, aname, amode in arglist:
if atype in self.blacklisttypes:
self.report("argument type %s is blacklisted", atype)
return 1
return 0
def htmlreport(self, line, klass=None, ranges=None):
if not self.htmlfile: return
if ranges is None:
ranges = []
if klass:
ranges.insert(0, (0, len(line), klass))
oline = ''
i = 0
for c in line:
for b, e, name in ranges:
if b == i:
oline += '<span class="%s">' % name
if e == i:
oline += '</span>'
i += 1
if c == '<': oline += '&lt;'
elif c == '>': oline += '&gt;'
else: oline += c
for b, e, name in ranges:
if b >= i:
oline += '<span class="%s">' % name
if e >= i:
oline += '</span>'
if not line or line[-1] != '\n':
oline += '\n'
self.htmlfile.write(oline)
class Scanner_PreUH3(Scanner):
"""Scanner for Universal Headers before release 3"""
def initpatterns(self):
Scanner.initpatterns(self)
self.head_pat = "^extern pascal[ \t]+" # XXX Mac specific!
self.type_pat = "pascal[ \t\n]+(?P<type>[a-zA-Z0-9_ \t]*[a-zA-Z0-9_])[ \t\n]+"
self.whole_pat = self.type_pat + self.name_pat + self.args_pat
self.sym_pat = "^[ \t]*(?P<name>[a-zA-Z0-9_]+)[ \t]*=" + \
"[ \t]*(?P<defn>[-0-9'\"][^\t\n,;}]*),?"
class Scanner_OSX(Scanner):
"""Scanner for modern (post UH3.3) Universal Headers """
def initpatterns(self):
Scanner.initpatterns(self)
self.head_pat = "^EXTERN_API(_C)?"
self.type_pat = "EXTERN_API(_C)?" + \
"[ \t\n]*\([ \t\n]*" + \
"(?P<type>[a-zA-Z0-9_* \t]*[a-zA-Z0-9_*])" + \
"[ \t\n]*\)[ \t\n]*"
self.whole_pat = self.type_pat + self.name_pat + self.args_pat
self.sym_pat = "^[ \t]*(?P<name>[a-zA-Z0-9_]+)[ \t]*=" + \
"[ \t]*(?P<defn>[-0-9_a-zA-Z'\"\(][^\t\n,;}]*),?"
_8bit = re.compile(r"[\200-\377]")
def escape8bit(s):
if _8bit.search(s) is not None:
out = []
for c in s:
o = ord(c)
if o >= 128:
out.append("\\" + hex(o)[1:])
else:
out.append(c)
s = "".join(out)
return s
def test():
input = "D:Development:THINK C:Mac #includes:Apple #includes:AppleEvents.h"
output = "@aespecs.py"
defsoutput = "@aedefs.py"
s = Scanner(input, output, defsoutput)
s.scan()
if __name__ == '__main__':
test()

View File

@@ -0,0 +1,36 @@
Comments on building tcl/tk for AMD64 with the MS SDK compiler
==============================================================
I did have to build tcl/tk manually.
First, I had to build the nmakehlp.exe helper utility manually by executing
cl nmakehlp.c /link bufferoverflowU.lib
in both the tcl8.4.12\win and tk8.4.12\win directories.
Second, the AMD64 compiler refuses to compile the file
tcl8.4.12\generic\tclExecute.c because it insists on using intrinsics
for the 'ceil' and 'floor' functions:
..\generic\tclExecute.c(394) : error C2099: initializer is not a constant
..\generic\tclExecute.c(398) : error C2099: initializer is not a constant
I did comment out these lines; an alternative would have been to use
the /Oi- compiler flag to disable the intrinsic functions.
The commands then used were these:
svn export http://svn.python.org/projects/external/tcl8.4.12
cd tcl8.4.12\win
REM
echo patch the tcl8.4.12\generic\tclExecute.c file
pause
REM
cl nmakehlp.c /link bufferoverflowU.lib
nmake -f makefile.vc MACHINE=AMD64
nmake -f makefile.vc INSTALLDIR=..\..\tcltk install
cd ..\..
svn export http://svn.python.org/projects/external/tk8.4.12
cd tk8.4.12\win
cl nmakehlp.c /link bufferoverflowU.lib
nmake -f makefile.vc TCLDIR=..\..\tcl8.4.12 MACHINE=AMD64
nmake -f makefile.vc TCLDIR=..\..\tcl8.4.12 INSTALLDIR=..\..\tcltk install
cd ..\..

View File

@@ -0,0 +1,6 @@
@rem Used by the buildbot "compile" step.
cmd /c Tools\buildbot\external-amd64.bat
call "%VS90COMNTOOLS%\..\..\VC\vcvarsall.bat" x86_amd64
cmd /c Tools\buildbot\clean-amd64.bat
vcbuild /useenv PCbuild\kill_python.vcproj "Debug|x64" && PCbuild\amd64\kill_python_d.exe
vcbuild PCbuild\pcbuild.sln "Debug|x64"

View File

@@ -0,0 +1,7 @@
@rem Used by the buildbot "compile" step.
cmd /c Tools\buildbot\external.bat
call "%VS90COMNTOOLS%vsvars32.bat"
cmd /c Tools\buildbot\clean.bat
vcbuild /useenv PCbuild\kill_python.vcproj "Debug|Win32" && PCbuild\kill_python_d.exe
vcbuild /useenv PCbuild\pcbuild.sln "Debug|Win32"

View File

@@ -0,0 +1,20 @@
@rem Used by the buildbot "buildmsi" step.
cmd /c Tools\buildbot\external.bat
@rem build release versions of things
call "%VS90COMNTOOLS%vsvars32.bat"
@rem build Python
vcbuild /useenv PCbuild\pcbuild.sln "Release|Win32"
@rem build the documentation
bash.exe -c 'cd Doc;make PYTHON=python2.5 update htmlhelp'
"%ProgramFiles%\HTML Help Workshop\hhc.exe" Doc\build\htmlhelp\python26a3.hhp
@rem build the MSI file
cd PC
nmake /f icons.mak
cd ..\Tools\msi
del *.msi
nmake /f msisupport.mak
%HOST_PYTHON% msi.py

View File

@@ -0,0 +1,10 @@
@rem Used by the buildbot "clean" step.
call "%VS90COMNTOOLS%\..\..\VC\vcvarsall.bat" x86_amd64
@echo Deleting .pyc/.pyo files ...
del /s Lib\*.pyc Lib\*.pyo
@echo Deleting test leftovers ...
rmdir /s /q build
cd PCbuild
vcbuild /clean pcbuild.sln "Release|x64"
vcbuild /clean pcbuild.sln "Debug|x64"
cd ..

View File

@@ -0,0 +1,10 @@
@rem Used by the buildbot "clean" step.
call "%VS90COMNTOOLS%vsvars32.bat"
@echo Deleting .pyc/.pyo files ...
del /s Lib\*.pyc Lib\*.pyo
@echo Deleting test leftovers ...
rmdir /s /q build
cd PCbuild
vcbuild /clean pcbuild.sln "Release|Win32"
vcbuild /clean pcbuild.sln "Debug|Win32"
cd ..

View File

@@ -0,0 +1,20 @@
@rem Fetches (and builds if necessary) external dependencies
@rem Assume we start inside the Python source directory
call "Tools\buildbot\external-common.bat"
call "%VS90COMNTOOLS%\..\..\VC\vcvarsall.bat" x86_amd64
if not exist tcltk64\bin\tcl85g.dll (
cd tcl-8.5.2.1\win
nmake -f makefile.vc COMPILERFLAGS=-DWINVER=0x0500 DEBUG=1 MACHINE=AMD64 INSTALLDIR=..\..\tcltk64 clean all
nmake -f makefile.vc COMPILERFLAGS=-DWINVER=0x0500 DEBUG=1 MACHINE=AMD64 INSTALLDIR=..\..\tcltk64 install
cd ..\..
)
if not exist tcltk64\bin\tk85g.dll (
cd tk-8.5.2.0\win
nmake -f makefile.vc COMPILERFLAGS=-DWINVER=0x0500 OPTS=noxp DEBUG=1 MACHINE=AMD64 INSTALLDIR=..\..\tcltk64 TCLDIR=..\..\tcl-8.5.2.1 clean
nmake -f makefile.vc COMPILERFLAGS=-DWINVER=0x0500 OPTS=noxp DEBUG=1 MACHINE=AMD64 INSTALLDIR=..\..\tcltk64 TCLDIR=..\..\tcl-8.5.2.1 all
nmake -f makefile.vc COMPILERFLAGS=-DWINVER=0x0500 OPTS=noxp DEBUG=1 MACHINE=AMD64 INSTALLDIR=..\..\tcltk64 TCLDIR=..\..\tcl-8.5.2.1 install
cd ..\..
)

View File

@@ -0,0 +1,45 @@
@rem Common file shared between external.bat and external-amd64.bat. Responsible for
@rem fetching external components into the root\.. buildbot directories.
cd ..
@rem XXX: If you need to force the buildbots to start from a fresh environment, uncomment
@rem the following, check it in, then check it out, comment it out, then check it back in.
@rem if exist bzip2-1.0.5 rd /s/q bzip2-1.0.5
@rem if exist tcltk rd /s/q tcltk
@rem if exist tcltk64 rd /s/q tcltk64
@rem if exist tcl8.4.12 rd /s/q tcl8.4.12
@rem if exist tcl8.4.16 rd /s/q tcl8.4.16
@rem if exist tcl-8.4.18.1 rd /s/q tcl-8.4.18.1
@rem if exist tk8.4.12 rd /s/q tk8.4.12
@rem if exist tk8.4.16 rd /s/q tk8.4.16
@rem if exist tk-8.4.18.1 rd /s/q tk-8.4.18.1
@rem if exist db-4.4.20 rd /s/q db-4.4.20
@rem if exist db-4.7.25.0 rd /s/q db-4.7.25.0
@rem if exist openssl-0.9.8l rd /s/q openssl-0.9.8l
@rem if exist sqlite-3.6.21 rd /s/q sqlite-3.6.21
@rem bzip
if not exist bzip2-1.0.5 (
rd /s/q bzip2-1.0.3
svn export http://svn.python.org/projects/external/bzip2-1.0.5
)
@rem Berkeley DB
if exist db-4.4.20 rd /s/q db-4.4.20
if not exist db-4.7.25.0 svn export http://svn.python.org/projects/external/db-4.7.25.0
@rem OpenSSL
if not exist openssl-0.9.8l svn export http://svn.python.org/projects/external/openssl-0.9.8l
@rem tcl/tk
if not exist tcl-8.5.2.1 (
rd /s/q tcltk tcltk64
svn export http://svn.python.org/projects/external/tcl-8.5.2.1
)
if not exist tk-8.5.2.0 svn export http://svn.python.org/projects/external/tk-8.5.2.0
@rem sqlite3
if not exist sqlite-3.6.21 (
rd /s/q sqlite-source-3.3.4
svn export http://svn.python.org/projects/external/sqlite-3.6.21
)

View File

@@ -0,0 +1,21 @@
@rem Fetches (and builds if necessary) external dependencies
@rem Assume we start inside the Python source directory
call "Tools\buildbot\external-common.bat"
call "%VS90COMNTOOLS%\vsvars32.bat"
if not exist tcltk\bin\tcl85g.dll (
@rem all and install need to be separate invocations, otherwise nmakehlp is not found on install
cd tcl-8.5.2.1\win
nmake -f makefile.vc COMPILERFLAGS=-DWINVER=0x0500 DEBUG=1 INSTALLDIR=..\..\tcltk clean all
nmake -f makefile.vc DEBUG=1 INSTALLDIR=..\..\tcltk install
cd ..\..
)
if not exist tcltk\bin\tk85g.dll (
cd tk-8.5.2.0\win
nmake -f makefile.vc COMPILERFLAGS=-DWINVER=0x0500 OPTS=noxp DEBUG=1 INSTALLDIR=..\..\tcltk TCLDIR=..\..\tcl-8.5.2.1 clean
nmake -f makefile.vc COMPILERFLAGS=-DWINVER=0x0500 OPTS=noxp DEBUG=1 INSTALLDIR=..\..\tcltk TCLDIR=..\..\tcl-8.5.2.1 all
nmake -f makefile.vc COMPILERFLAGS=-DWINVER=0x0500 OPTS=noxp DEBUG=1 INSTALLDIR=..\..\tcltk TCLDIR=..\..\tcl-8.5.2.1 install
cd ..\..
)

View File

@@ -0,0 +1,3 @@
@rem Used by the buildbot "test" step.
cd PCbuild
call rt.bat -q -d -x64 -uall -rw

View File

@@ -0,0 +1,3 @@
@rem Used by the buildbot "test" step.
cd PCbuild
call rt.bat -d -q -uall -rwW

View File

@@ -0,0 +1,609 @@
# -*- coding: utf-8 -*-
# This file should be kept compatible with both Python 2.6 and Python >= 3.0.
from __future__ import division
from __future__ import print_function
"""
ccbench, a Python concurrency benchmark.
"""
import time
import os
import sys
import functools
import itertools
import threading
import subprocess
import socket
from optparse import OptionParser, SUPPRESS_HELP
import platform
# Compatibility
try:
xrange
except NameError:
xrange = range
try:
map = itertools.imap
except AttributeError:
pass
THROUGHPUT_DURATION = 2.0
LATENCY_PING_INTERVAL = 0.1
LATENCY_DURATION = 2.0
BANDWIDTH_PACKET_SIZE = 1024
BANDWIDTH_DURATION = 2.0
def task_pidigits():
"""Pi calculation (Python)"""
_map = map
_count = itertools.count
_islice = itertools.islice
def calc_ndigits(n):
# From http://shootout.alioth.debian.org/
def gen_x():
return _map(lambda k: (k, 4*k + 2, 0, 2*k + 1), _count(1))
def compose(a, b):
aq, ar, as_, at = a
bq, br, bs, bt = b
return (aq * bq,
aq * br + ar * bt,
as_ * bq + at * bs,
as_ * br + at * bt)
def extract(z, j):
q, r, s, t = z
return (q*j + r) // (s*j + t)
def pi_digits():
z = (1, 0, 0, 1)
x = gen_x()
while 1:
y = extract(z, 3)
while y != extract(z, 4):
z = compose(z, next(x))
y = extract(z, 3)
z = compose((10, -10*y, 0, 1), z)
yield y
return list(_islice(pi_digits(), n))
return calc_ndigits, (50, )
def task_regex():
"""regular expression (C)"""
# XXX this task gives horrendous latency results.
import re
# Taken from the `inspect` module
pat = re.compile(r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)', re.MULTILINE)
with open(__file__, "r") as f:
arg = f.read(2000)
def findall(s):
t = time.time()
try:
return pat.findall(s)
finally:
print(time.time() - t)
return pat.findall, (arg, )
def task_sort():
"""list sorting (C)"""
def list_sort(l):
l = l[::-1]
l.sort()
return list_sort, (list(range(1000)), )
def task_compress_zlib():
"""zlib compression (C)"""
import zlib
with open(__file__, "rb") as f:
arg = f.read(5000) * 3
def compress(s):
zlib.decompress(zlib.compress(s, 5))
return compress, (arg, )
def task_compress_bz2():
"""bz2 compression (C)"""
import bz2
with open(__file__, "rb") as f:
arg = f.read(3000) * 2
def compress(s):
bz2.compress(s)
return compress, (arg, )
def task_hashing():
"""SHA1 hashing (C)"""
import hashlib
with open(__file__, "rb") as f:
arg = f.read(5000) * 30
def compute(s):
hashlib.sha1(s).digest()
return compute, (arg, )
throughput_tasks = [task_pidigits, task_regex]
for mod in 'bz2', 'hashlib':
try:
globals()[mod] = __import__(mod)
except ImportError:
globals()[mod] = None
# For whatever reasons, zlib gives irregular results, so we prefer bz2 or
# hashlib if available.
# (NOTE: hashlib releases the GIL from 2.7 and 3.1 onwards)
if bz2 is not None:
throughput_tasks.append(task_compress_bz2)
elif hashlib is not None:
throughput_tasks.append(task_hashing)
else:
throughput_tasks.append(task_compress_zlib)
latency_tasks = throughput_tasks
bandwidth_tasks = [task_pidigits]
class TimedLoop:
def __init__(self, func, args):
self.func = func
self.args = args
def __call__(self, start_time, min_duration, end_event, do_yield=False):
step = 20
niters = 0
duration = 0.0
_time = time.time
_sleep = time.sleep
_func = self.func
_args = self.args
t1 = start_time
while True:
for i in range(step):
_func(*_args)
t2 = _time()
# If another thread terminated, the current measurement is invalid
# => return the previous one.
if end_event:
return niters, duration
niters += step
duration = t2 - start_time
if duration >= min_duration:
end_event.append(None)
return niters, duration
if t2 - t1 < 0.01:
# Minimize interference of measurement on overall runtime
step = step * 3 // 2
elif do_yield:
# OS scheduling of Python threads is sometimes so bad that we
# have to force thread switching ourselves, otherwise we get
# completely useless results.
_sleep(0.0001)
t1 = t2
def run_throughput_test(func, args, nthreads):
assert nthreads >= 1
# Warm up
func(*args)
results = []
loop = TimedLoop(func, args)
end_event = []
if nthreads == 1:
# Pure single-threaded performance, without any switching or
# synchronization overhead.
start_time = time.time()
results.append(loop(start_time, THROUGHPUT_DURATION,
end_event, do_yield=False))
return results
started = False
ready_cond = threading.Condition()
start_cond = threading.Condition()
ready = []
def run():
with ready_cond:
ready.append(None)
ready_cond.notify()
with start_cond:
while not started:
start_cond.wait()
results.append(loop(start_time, THROUGHPUT_DURATION,
end_event, do_yield=True))
threads = []
for i in range(nthreads):
threads.append(threading.Thread(target=run))
for t in threads:
t.setDaemon(True)
t.start()
# We don't want measurements to include thread startup overhead,
# so we arrange for timing to start after all threads are ready.
with ready_cond:
while len(ready) < nthreads:
ready_cond.wait()
with start_cond:
start_time = time.time()
started = True
start_cond.notify(nthreads)
for t in threads:
t.join()
return results
def run_throughput_tests(max_threads):
for task in throughput_tasks:
print(task.__doc__)
print()
func, args = task()
nthreads = 1
baseline_speed = None
while nthreads <= max_threads:
results = run_throughput_test(func, args, nthreads)
# Taking the max duration rather than average gives pessimistic
# results rather than optimistic.
speed = sum(r[0] for r in results) / max(r[1] for r in results)
print("threads=%d: %d" % (nthreads, speed), end="")
if baseline_speed is None:
print(" iterations/s.")
baseline_speed = speed
else:
print(" ( %d %%)" % (speed / baseline_speed * 100))
nthreads += 1
print()
LAT_END = "END"
def _sendto(sock, s, addr):
sock.sendto(s.encode('ascii'), addr)
def _recv(sock, n):
return sock.recv(n).decode('ascii')
def latency_client(addr, nb_pings, interval):
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
_time = time.time
_sleep = time.sleep
def _ping():
_sendto(sock, "%r\n" % _time(), addr)
# The first ping signals the parent process that we are ready.
_ping()
# We give the parent a bit of time to notice.
_sleep(1.0)
for i in range(nb_pings):
_sleep(interval)
_ping()
_sendto(sock, LAT_END + "\n", addr)
def run_latency_client(**kwargs):
cmd_line = [sys.executable, '-E', os.path.abspath(__file__)]
cmd_line.extend(['--latclient', repr(kwargs)])
return subprocess.Popen(cmd_line) #, stdin=subprocess.PIPE,
#stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
def run_latency_test(func, args, nthreads):
# Create a listening socket to receive the pings. We use UDP which should
# be painlessly cross-platform.
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(("127.0.0.1", 0))
addr = sock.getsockname()
interval = LATENCY_PING_INTERVAL
duration = LATENCY_DURATION
nb_pings = int(duration / interval)
results = []
threads = []
end_event = []
start_cond = threading.Condition()
started = False
if nthreads > 0:
# Warm up
func(*args)
results = []
loop = TimedLoop(func, args)
ready = []
ready_cond = threading.Condition()
def run():
with ready_cond:
ready.append(None)
ready_cond.notify()
with start_cond:
while not started:
start_cond.wait()
loop(start_time, duration * 1.5, end_event, do_yield=False)
for i in range(nthreads):
threads.append(threading.Thread(target=run))
for t in threads:
t.setDaemon(True)
t.start()
# Wait for threads to be ready
with ready_cond:
while len(ready) < nthreads:
ready_cond.wait()
# Run the client and wait for the first ping(s) to arrive before
# unblocking the background threads.
chunks = []
process = run_latency_client(addr=sock.getsockname(),
nb_pings=nb_pings, interval=interval)
s = _recv(sock, 4096)
_time = time.time
with start_cond:
start_time = _time()
started = True
start_cond.notify(nthreads)
while LAT_END not in s:
s = _recv(sock, 4096)
t = _time()
chunks.append((t, s))
# Tell the background threads to stop.
end_event.append(None)
for t in threads:
t.join()
process.wait()
for recv_time, chunk in chunks:
# NOTE: it is assumed that a line sent by a client wasn't received
# in two chunks because the lines are very small.
for line in chunk.splitlines():
line = line.strip()
if line and line != LAT_END:
send_time = eval(line)
assert isinstance(send_time, float)
results.append((send_time, recv_time))
return results
def run_latency_tests(max_threads):
for task in latency_tasks:
print("Background CPU task:", task.__doc__)
print()
func, args = task()
nthreads = 0
while nthreads <= max_threads:
results = run_latency_test(func, args, nthreads)
n = len(results)
# We print out milliseconds
lats = [1000 * (t2 - t1) for (t1, t2) in results]
#print(list(map(int, lats)))
avg = sum(lats) / n
dev = (sum((x - avg) ** 2 for x in lats) / n) ** 0.5
print("CPU threads=%d: %d ms. (std dev: %d ms.)" % (nthreads, avg, dev), end="")
print()
#print(" [... from %d samples]" % n)
nthreads += 1
print()
BW_END = "END"
def bandwidth_client(addr, packet_size, duration):
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(("127.0.0.1", 0))
local_addr = sock.getsockname()
_time = time.time
_sleep = time.sleep
def _send_chunk(msg):
_sendto(sock, ("%r#%s\n" % (local_addr, msg)).rjust(packet_size), addr)
# We give the parent some time to be ready.
_sleep(1.0)
try:
start_time = _time()
end_time = start_time + duration * 2.0
i = 0
while _time() < end_time:
_send_chunk(str(i))
s = _recv(sock, packet_size)
assert len(s) == packet_size
i += 1
_send_chunk(BW_END)
finally:
sock.close()
def run_bandwidth_client(**kwargs):
cmd_line = [sys.executable, '-E', os.path.abspath(__file__)]
cmd_line.extend(['--bwclient', repr(kwargs)])
return subprocess.Popen(cmd_line) #, stdin=subprocess.PIPE,
#stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
def run_bandwidth_test(func, args, nthreads):
# Create a listening socket to receive the packets. We use UDP which should
# be painlessly cross-platform.
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(("127.0.0.1", 0))
addr = sock.getsockname()
duration = BANDWIDTH_DURATION
packet_size = BANDWIDTH_PACKET_SIZE
results = []
threads = []
end_event = []
start_cond = threading.Condition()
started = False
if nthreads > 0:
# Warm up
func(*args)
results = []
loop = TimedLoop(func, args)
ready = []
ready_cond = threading.Condition()
def run():
with ready_cond:
ready.append(None)
ready_cond.notify()
with start_cond:
while not started:
start_cond.wait()
loop(start_time, duration * 1.5, end_event, do_yield=False)
for i in range(nthreads):
threads.append(threading.Thread(target=run))
for t in threads:
t.setDaemon(True)
t.start()
# Wait for threads to be ready
with ready_cond:
while len(ready) < nthreads:
ready_cond.wait()
# Run the client and wait for the first packet to arrive before
# unblocking the background threads.
process = run_bandwidth_client(addr=addr,
packet_size=packet_size,
duration=duration)
_time = time.time
# This will also wait for the parent to be ready
s = _recv(sock, packet_size)
remote_addr = eval(s.partition('#')[0])
with start_cond:
start_time = _time()
started = True
start_cond.notify(nthreads)
n = 0
first_time = None
while not end_event and BW_END not in s:
_sendto(sock, s, remote_addr)
s = _recv(sock, packet_size)
if first_time is None:
first_time = _time()
n += 1
end_time = _time()
end_event.append(None)
for t in threads:
t.join()
process.kill()
return (n - 1) / (end_time - first_time)
def run_bandwidth_tests(max_threads):
for task in bandwidth_tasks:
print("Background CPU task:", task.__doc__)
print()
func, args = task()
nthreads = 0
baseline_speed = None
while nthreads <= max_threads:
results = run_bandwidth_test(func, args, nthreads)
speed = results
#speed = len(results) * 1.0 / results[-1][0]
print("CPU threads=%d: %.1f" % (nthreads, speed), end="")
if baseline_speed is None:
print(" packets/s.")
baseline_speed = speed
else:
print(" ( %d %%)" % (speed / baseline_speed * 100))
nthreads += 1
print()
def main():
usage = "usage: %prog [-h|--help] [options]"
parser = OptionParser(usage=usage)
parser.add_option("-t", "--throughput",
action="store_true", dest="throughput", default=False,
help="run throughput tests")
parser.add_option("-l", "--latency",
action="store_true", dest="latency", default=False,
help="run latency tests")
parser.add_option("-b", "--bandwidth",
action="store_true", dest="bandwidth", default=False,
help="run I/O bandwidth tests")
parser.add_option("-i", "--interval",
action="store", type="int", dest="check_interval", default=None,
help="sys.setcheckinterval() value")
parser.add_option("-I", "--switch-interval",
action="store", type="float", dest="switch_interval", default=None,
help="sys.setswitchinterval() value")
parser.add_option("-n", "--num-threads",
action="store", type="int", dest="nthreads", default=4,
help="max number of threads in tests")
# Hidden option to run the pinging and bandwidth clients
parser.add_option("", "--latclient",
action="store", dest="latclient", default=None,
help=SUPPRESS_HELP)
parser.add_option("", "--bwclient",
action="store", dest="bwclient", default=None,
help=SUPPRESS_HELP)
options, args = parser.parse_args()
if args:
parser.error("unexpected arguments")
if options.latclient:
kwargs = eval(options.latclient)
latency_client(**kwargs)
return
if options.bwclient:
kwargs = eval(options.bwclient)
bandwidth_client(**kwargs)
return
if not options.throughput and not options.latency and not options.bandwidth:
options.throughput = options.latency = options.bandwidth = True
if options.check_interval:
sys.setcheckinterval(options.check_interval)
if options.switch_interval:
sys.setswitchinterval(options.switch_interval)
print("== %s %s (%s) ==" % (
platform.python_implementation(),
platform.python_version(),
platform.python_build()[0],
))
# Processor identification often has repeated spaces
cpu = ' '.join(platform.processor().split())
print("== %s %s on '%s' ==" % (
platform.machine(),
platform.system(),
cpu,
))
print()
if options.throughput:
print("--- Throughput ---")
print()
run_throughput_tests(options.nthreads)
if options.latency:
print("--- Latency ---")
print()
run_latency_tests(options.nthreads)
if options.bandwidth:
print("--- I/O bandwidth ---")
print()
run_bandwidth_tests(options.nthreads)
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,8 @@
Fred L. Drake, Jr.
Mark Hammond
Shane Hathaway
Neil Schemenauer
Evan Simpson
Greg Stein
Bill Tutt
Moshe Zadka

View File

@@ -0,0 +1,18 @@
This directory contains support tools for the Python compiler package,
which is now part of the standard library.
compile.py Demo that compiles a Python module into a .pyc file
using the pure-Python compiler code.
demo.py Prints the names of all the methods defined in a module,
as a demonstration of walking through the abstract syntax
tree produced by the parser.
dumppyc.py Dumps the contents of a .pyc file, printing
the attributes of the code object followed by a
code disassembly.
regrtest.py Runs the Python test suite using bytecode generated
by the pure-Python compiler code instead of the
builtin compiler.

View File

@@ -0,0 +1,104 @@
# This file describes the nodes of the AST in ast.py. The module is
# generated by astgen.py.
# The descriptions use the following special notation to describe
# properties of the children:
# * this child is not a node
# ! this child is a sequence that contains nodes in it
# & this child may be set to None
# = ... a default value for the node constructor (optional args)
#
# If you add node types here, please be sure to update the list of
# Node types in Doc/lib/asttable.tex.
Module: doc*, node
Stmt: nodes!
Decorators: nodes!
Function: decorators&, name*, argnames*, defaults!, flags*, doc*, code
Lambda: argnames*, defaults!, flags*, code
Class: name*, bases!, doc*, code, decorators& = None
Pass:
Break:
Continue:
For: assign, list, body, else_&
While: test, body, else_&
With: expr, vars&, body
If: tests!, else_&
IfExp: test, then, else_
Exec: expr, locals&, globals&
From: modname*, names*, level*
Import: names*
Raise: expr1&, expr2&, expr3&
TryFinally: body, final
TryExcept: body, handlers!, else_&
Return: value
Yield: value
Const: value*
Print: nodes!, dest&
Printnl: nodes!, dest&
Discard: expr
AugAssign: node, op*, expr
Assign: nodes!, expr
AssTuple: nodes!
AssList: nodes!
AssName: name*, flags*
AssAttr: expr, attrname*, flags*
ListComp: expr, quals!
ListCompFor: assign, list, ifs!
ListCompIf: test
GenExpr: code
GenExprInner: expr, quals!
GenExprFor: assign, iter, ifs!
GenExprIf: test
List: nodes!
Dict: items!
Not: expr
Compare: expr, ops!
Name: name*
Global: names*
Backquote: expr
Getattr: expr, attrname*
CallFunc: node, args!, star_args& = None, dstar_args& = None
Keyword: name*, expr
Subscript: expr, flags*, subs!
Ellipsis:
Sliceobj: nodes!
Slice: expr, flags*, lower&, upper&
Assert: test, fail&
Tuple: nodes!
Or: nodes!
And: nodes!
Bitor: nodes!
Bitxor: nodes!
Bitand: nodes!
LeftShift: (left, right)
RightShift: (left, right)
Add: (left, right)
Sub: (left, right)
Mul: (left, right)
Div: (left, right)
Mod: (left, right)
Power: (left, right)
FloorDiv: (left, right)
UnaryAdd: expr
UnarySub: expr
Invert: expr
init(Function):
self.varargs = self.kwargs = None
if flags & CO_VARARGS:
self.varargs = 1
if flags & CO_VARKEYWORDS:
self.kwargs = 1
init(Lambda):
self.varargs = self.kwargs = None
if flags & CO_VARARGS:
self.varargs = 1
if flags & CO_VARKEYWORDS:
self.kwargs = 1
init(GenExpr):
self.argnames = ['.0']
self.varargs = self.kwargs = None
init(GenExprFor):
self.is_outmost = False

View File

@@ -0,0 +1,294 @@
"""Generate ast module from specification
This script generates the ast module from a simple specification,
which makes it easy to accomodate changes in the grammar. This
approach would be quite reasonable if the grammar changed often.
Instead, it is rather complex to generate the appropriate code. And
the Node interface has changed more often than the grammar.
"""
import fileinput
import re
import sys
from StringIO import StringIO
SPEC = "ast.txt"
COMMA = ", "
def load_boilerplate(file):
f = open(file)
buf = f.read()
f.close()
i = buf.find('### ''PROLOGUE')
j = buf.find('### ''EPILOGUE')
pro = buf[i+12:j].strip()
epi = buf[j+12:].strip()
return pro, epi
def strip_default(arg):
"""Return the argname from an 'arg = default' string"""
i = arg.find('=')
if i == -1:
return arg
t = arg[:i].strip()
return t
P_NODE = 1
P_OTHER = 2
P_NESTED = 3
P_NONE = 4
class NodeInfo:
"""Each instance describes a specific AST node"""
def __init__(self, name, args):
self.name = name
self.args = args.strip()
self.argnames = self.get_argnames()
self.argprops = self.get_argprops()
self.nargs = len(self.argnames)
self.init = []
def get_argnames(self):
if '(' in self.args:
i = self.args.find('(')
j = self.args.rfind(')')
args = self.args[i+1:j]
else:
args = self.args
return [strip_default(arg.strip())
for arg in args.split(',') if arg]
def get_argprops(self):
"""Each argument can have a property like '*' or '!'
XXX This method modifies the argnames in place!
"""
d = {}
hardest_arg = P_NODE
for i in range(len(self.argnames)):
arg = self.argnames[i]
if arg.endswith('*'):
arg = self.argnames[i] = arg[:-1]
d[arg] = P_OTHER
hardest_arg = max(hardest_arg, P_OTHER)
elif arg.endswith('!'):
arg = self.argnames[i] = arg[:-1]
d[arg] = P_NESTED
hardest_arg = max(hardest_arg, P_NESTED)
elif arg.endswith('&'):
arg = self.argnames[i] = arg[:-1]
d[arg] = P_NONE
hardest_arg = max(hardest_arg, P_NONE)
else:
d[arg] = P_NODE
self.hardest_arg = hardest_arg
if hardest_arg > P_NODE:
self.args = self.args.replace('*', '')
self.args = self.args.replace('!', '')
self.args = self.args.replace('&', '')
return d
def gen_source(self):
buf = StringIO()
print >> buf, "class %s(Node):" % self.name
self._gen_init(buf)
print >> buf
self._gen_getChildren(buf)
print >> buf
self._gen_getChildNodes(buf)
print >> buf
self._gen_repr(buf)
buf.seek(0, 0)
return buf.read()
def _gen_init(self, buf):
if self.args:
argtuple = '(' in self.args
args = self.args if not argtuple else ''.join(self.argnames)
print >> buf, " def __init__(self, %s, lineno=None):" % args
else:
print >> buf, " def __init__(self, lineno=None):"
if self.argnames:
if argtuple:
for idx, name in enumerate(self.argnames):
print >> buf, " self.%s = %s[%s]" % (name, args, idx)
else:
for name in self.argnames:
print >> buf, " self.%s = %s" % (name, name)
print >> buf, " self.lineno = lineno"
# Copy the lines in self.init, indented four spaces. The rstrip()
# business is to get rid of the four spaces if line happens to be
# empty, so that reindent.py is happy with the output.
for line in self.init:
print >> buf, (" " + line).rstrip()
def _gen_getChildren(self, buf):
print >> buf, " def getChildren(self):"
if len(self.argnames) == 0:
print >> buf, " return ()"
else:
if self.hardest_arg < P_NESTED:
clist = COMMA.join(["self.%s" % c
for c in self.argnames])
if self.nargs == 1:
print >> buf, " return %s," % clist
else:
print >> buf, " return %s" % clist
else:
if len(self.argnames) == 1:
print >> buf, " return tuple(flatten(self.%s))" % self.argnames[0]
else:
print >> buf, " children = []"
template = " children.%s(%sself.%s%s)"
for name in self.argnames:
if self.argprops[name] == P_NESTED:
print >> buf, template % ("extend", "flatten(",
name, ")")
else:
print >> buf, template % ("append", "", name, "")
print >> buf, " return tuple(children)"
def _gen_getChildNodes(self, buf):
print >> buf, " def getChildNodes(self):"
if len(self.argnames) == 0:
print >> buf, " return ()"
else:
if self.hardest_arg < P_NESTED:
clist = ["self.%s" % c
for c in self.argnames
if self.argprops[c] == P_NODE]
if len(clist) == 0:
print >> buf, " return ()"
elif len(clist) == 1:
print >> buf, " return %s," % clist[0]
else:
print >> buf, " return %s" % COMMA.join(clist)
else:
print >> buf, " nodelist = []"
template = " nodelist.%s(%sself.%s%s)"
for name in self.argnames:
if self.argprops[name] == P_NONE:
tmp = (" if self.%s is not None:\n"
" nodelist.append(self.%s)")
print >> buf, tmp % (name, name)
elif self.argprops[name] == P_NESTED:
print >> buf, template % ("extend", "flatten_nodes(",
name, ")")
elif self.argprops[name] == P_NODE:
print >> buf, template % ("append", "", name, "")
print >> buf, " return tuple(nodelist)"
def _gen_repr(self, buf):
print >> buf, " def __repr__(self):"
if self.argnames:
fmt = COMMA.join(["%s"] * self.nargs)
if '(' in self.args:
fmt = '(%s)' % fmt
vals = ["repr(self.%s)" % name for name in self.argnames]
vals = COMMA.join(vals)
if self.nargs == 1:
vals = vals + ","
print >> buf, ' return "%s(%s)" %% (%s)' % \
(self.name, fmt, vals)
else:
print >> buf, ' return "%s()"' % self.name
rx_init = re.compile('init\((.*)\):')
def parse_spec(file):
classes = {}
cur = None
for line in fileinput.input(file):
if line.strip().startswith('#'):
continue
mo = rx_init.search(line)
if mo is None:
if cur is None:
# a normal entry
try:
name, args = line.split(':')
except ValueError:
continue
classes[name] = NodeInfo(name, args)
cur = None
else:
# some code for the __init__ method
cur.init.append(line)
else:
# some extra code for a Node's __init__ method
name = mo.group(1)
cur = classes[name]
return sorted(classes.values(), key=lambda n: n.name)
def main():
prologue, epilogue = load_boilerplate(sys.argv[-1])
print prologue
print
classes = parse_spec(SPEC)
for info in classes:
print info.gen_source()
print epilogue
if __name__ == "__main__":
main()
sys.exit(0)
### PROLOGUE
"""Python abstract syntax node definitions
This file is automatically generated by Tools/compiler/astgen.py
"""
from consts import CO_VARARGS, CO_VARKEYWORDS
def flatten(seq):
l = []
for elt in seq:
t = type(elt)
if t is tuple or t is list:
for elt2 in flatten(elt):
l.append(elt2)
else:
l.append(elt)
return l
def flatten_nodes(seq):
return [n for n in flatten(seq) if isinstance(n, Node)]
nodes = {}
class Node:
"""Abstract base class for ast nodes."""
def getChildren(self):
pass # implemented by subclasses
def __iter__(self):
for n in self.getChildren():
yield n
def asList(self): # for backwards compatibility
return self.getChildren()
def getChildNodes(self):
pass # implemented by subclasses
class EmptyNode(Node):
pass
class Expression(Node):
# Expression is an artificial node class to support "eval"
nodes["expression"] = "Expression"
def __init__(self, node):
self.node = node
def getChildren(self):
return self.node,
def getChildNodes(self):
return self.node,
def __repr__(self):
return "Expression(%s)" % (repr(self.node))
### EPILOGUE
for name, obj in globals().items():
if isinstance(obj, type) and issubclass(obj, Node):
nodes[name.lower()] = obj

View File

@@ -0,0 +1,51 @@
import sys
import getopt
from compiler import compileFile, visitor
import profile
def main():
VERBOSE = 0
DISPLAY = 0
PROFILE = 0
CONTINUE = 0
opts, args = getopt.getopt(sys.argv[1:], 'vqdcp')
for k, v in opts:
if k == '-v':
VERBOSE = 1
visitor.ASTVisitor.VERBOSE = visitor.ASTVisitor.VERBOSE + 1
if k == '-q':
if sys.platform[:3]=="win":
f = open('nul', 'wb') # /dev/null fails on Windows...
else:
f = open('/dev/null', 'wb')
sys.stdout = f
if k == '-d':
DISPLAY = 1
if k == '-c':
CONTINUE = 1
if k == '-p':
PROFILE = 1
if not args:
print "no files to compile"
else:
for filename in args:
if VERBOSE:
print filename
try:
if PROFILE:
profile.run('compileFile(%r, %r)' % (filename, DISPLAY),
filename + ".prof")
else:
compileFile(filename, DISPLAY)
except SyntaxError, err:
print err
if err.lineno is not None:
print err.lineno
if not CONTINUE:
sys.exit(-1)
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,38 @@
#! /usr/bin/env python
"""Print names of all methods defined in module
This script demonstrates use of the visitor interface of the compiler
package.
"""
import compiler
class MethodFinder:
"""Print the names of all the methods
Each visit method takes two arguments, the node and its current
scope. The scope is the name of the current class or None.
"""
def visitClass(self, node, scope=None):
self.visit(node.code, node.name)
def visitFunction(self, node, scope=None):
if scope is not None:
print "%s.%s" % (scope, node.name)
self.visit(node.code, None)
def main(files):
mf = MethodFinder()
for file in files:
f = open(file)
buf = f.read()
f.close()
ast = compiler.parse(buf)
compiler.walk(ast, mf)
if __name__ == "__main__":
import sys
main(sys.argv[1:])

View File

@@ -0,0 +1,46 @@
#! /usr/bin/env python
import marshal
import dis
import types
def dump(obj):
print obj
for attr in dir(obj):
if attr.startswith('co_'):
val = getattr(obj, attr)
print "\t", attr, repr(val)
def loadCode(path):
f = open(path)
f.read(8)
co = marshal.load(f)
f.close()
return co
def walk(co, match=None):
if match is None or co.co_name == match:
dump(co)
print
dis.dis(co)
for obj in co.co_consts:
if type(obj) == types.CodeType:
walk(obj, match)
def load(filename, codename=None):
co = loadCode(filename)
walk(co, codename)
if __name__ == "__main__":
import sys
if len(sys.argv) == 3:
filename, codename = sys.argv[1:]
else:
filename = sys.argv[1]
codename = None
if filename.endswith('.py'):
buf = open(filename).read()
co = compile(buf, filename, "exec")
walk(co)
else:
load(filename, codename)

View File

@@ -0,0 +1,78 @@
"""Run the Python regression test using the compiler
This test runs the standard Python test suite using bytecode generated
by this compiler instead of by the builtin compiler.
The regression test is run with the interpreter in verbose mode so
that import problems can be observed easily.
"""
from compiler import compileFile
import os
import sys
import test
import tempfile
def copy_test_suite():
dest = tempfile.mkdtemp()
os.system("cp -r %s/* %s" % (test.__path__[0], dest))
print "Creating copy of test suite in", dest
return dest
def copy_library():
dest = tempfile.mkdtemp()
libdir = os.path.split(test.__path__[0])[0]
print "Found standard library in", libdir
print "Creating copy of standard library in", dest
os.system("cp -r %s/* %s" % (libdir, dest))
return dest
def compile_files(dir):
print "Compiling", dir, "\n\t",
line_len = 10
for file in os.listdir(dir):
base, ext = os.path.splitext(file)
if ext == '.py':
source = os.path.join(dir, file)
line_len = line_len + len(file) + 1
if line_len > 75:
print "\n\t",
line_len = len(source) + 9
print file,
try:
compileFile(source)
except SyntaxError, err:
print err
continue
# make sure the .pyc file is not over-written
os.chmod(source + "c", 444)
elif file == 'CVS':
pass
else:
path = os.path.join(dir, file)
if os.path.isdir(path):
print
print
compile_files(path)
print "\t",
line_len = 10
print
def run_regrtest(lib_dir):
test_dir = os.path.join(lib_dir, "test")
os.chdir(test_dir)
os.system("PYTHONPATH=%s %s -v regrtest.py" % (lib_dir, sys.executable))
def cleanup(dir):
os.system("rm -rf %s" % dir)
def main():
lib_dir = copy_library()
compile_files(lib_dir)
run_regrtest(lib_dir)
raw_input("Cleanup?")
cleanup(lib_dir)
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,43 @@
import compiler
import dis
import types
def extract_code_objects(co):
l = [co]
for const in co.co_consts:
if type(const) == types.CodeType:
l.append(const)
return l
def compare(a, b):
if not (a.co_name == "?" or a.co_name.startswith('<lambda')):
assert a.co_name == b.co_name, (a, b)
if a.co_stacksize != b.co_stacksize:
print "stack mismatch %s: %d vs. %d" % (a.co_name,
a.co_stacksize,
b.co_stacksize)
if a.co_stacksize > b.co_stacksize:
print "good code"
dis.dis(a)
print "bad code"
dis.dis(b)
assert 0
def main(files):
for file in files:
print file
buf = open(file).read()
try:
co1 = compile(buf, file, "exec")
except SyntaxError:
print "skipped"
continue
co2 = compiler.compile(buf, file, "exec")
co1l = extract_code_objects(co1)
co2l = extract_code_objects(co2)
for a, b in zip(co1l, co2l):
compare(a, b)
if __name__ == "__main__":
import sys
main(sys.argv[1:])

View File

@@ -0,0 +1,114 @@
FAQ Wizard
----------
Author: Guido van Rossum <guido@python.org>
Version: 1.0
Date: 6 April 1998
This is a CGI program that maintains a user-editable FAQ. It uses RCS
to keep track of changes to individual FAQ entries. It is fully
configurable; everything you might want to change when using this
program to maintain some other FAQ than the Python FAQ is contained in
the configuration module, faqconf.py.
Note that the bulk of the code is not an executable script; it's an
importable module. The actual script in cgi-bin is minimal.
Files:
faqw.py executable script to be edited and installed in cgi-bin
faqwiz.py main module, lives in same directory as FAQ entry files
faqconf.py main configuration module
faqcust.py additional local customization module (optional)
move-faqwiz.sh Script to move faqwiz entries.
What's New?
-----------
Version 1.0 corrects some minor bugs and uses tab-agnostic
indentation; it is otherwise unchanged from version 0.9.0.
Version 0.9.0 uses the re module (Perl style regular expressions) for
all its regular expression needs, instead of the regex and regsub
modules (Emacs style). This affects the syntax for regular
expressions entered by the user as search strings (with "regular
expression" checked), hence the version number jump.
Setup Information
-----------------
This assumes you are familiar with Python, with your http server, and
with running CGI scripts under your http server. You need Python 1.5
or better.
Select a place where the Python modules that constitute the FAQ wizard
will live (the directory where you unpacked it is an obvious choice).
This will be called the SRCDIR. This directory should not be writable
by other users of your system (since they would be able to execute
arbitrary code by invoking the FAQ wizard's CGI script).
Create a dedicated working directory, preferably one that's not
directly reachable from your http server. This will be called the
FAQDIR. Create a subdirectory named RCS. Make both the working
directory and the RCS subdirectory wrld-writable. (This is essential,
since the FAQ wizard runs as use nobody, and needs to create
additional files here!)
Edit faqconf.py to reflect your setup. You only need to edit the top
part, up till the line of all dashes. The comments should guide you
in your edits. (Actually, you can also choose to add your changes to
faqcust.py and leave faqconf.py alone. This is essential if you are
maintaining multiple FAQs; see below.)
Don't forget to edit the SECTION_TITLES variables to reflect the set
of section titles for your FAQ!
Next, edit faqw.py to reflect the pathname of your Python interpreter
and the values for SRCDIR and FAQDIR that you just chose. Then
install faqw.py in your cgi-bin directory. Make sure that it is
world-executable. You should now be able to connect to the FAQ wizard
by entering the following URL in your web client (subsituting the
appropriate host and port for "your.web.server", and perhaps
specifying a different directory for "cgi-bin" if local conventions so
dictate):
http://your.web.server/cgi-bin/faqw.py
If you are unable to get this working, check your server's error_log
file. The documentation for Python's cgi module in the Python Library
Reference Manual gives plentyu additional information about installing
and debugging CGI scripts, including setup debugging. This
documentation is repeated in the doc string in the cgi module; try
``import cgi; print cgi.__doc__''.
Assuming this works, you should now be able to add the first entry to
your FAQ using the FAQ wizard interface. This creates a file
faq01.001.htp in your working directory and an RCS revision history
file faq01.001.htp,v in the RCS subdirectory. You can now exercise
the other FAQ wizard features (search, index, whole FAQ, what's new,
roulette, and so on).
Maintaining Multiple FAQs
-------------------------
If you have multiple FAQs, you need a separate FAQDIR per FAQ, and a
different customization file per FAQ. The easiest thing to do would
be to have the faqcust.py for each FAQ live in the FAQDIR for that
FAQ, but that creates some security concerns, since the FAQDIR must be
world writable: *if* someone who breaks into your system (or a
legitimate user) manages to edit the faqcust.py file they can get
arbitrary code to execute through the FAQ wizard. Therefore, you will
need a more complex setup.
The best way is probably to have a directory that is only writable by
you for each FAQ, where you place the copy of faqcust.py for that FAQ,
and have a world-writable subdirectory DATA for the data. You then
set FAQDIR to point to the DATA directory and change the faqw.py
bootstrap script to add FAQDIR/.. to sys.path (in front of SRCDIR, so
the dummy faqcust.py from SRCDIR is ignored).
--Guido van Rossum (home page: http://www.python.org/~guido/)

View File

@@ -0,0 +1,577 @@
"""FAQ Wizard customization module.
Edit this file to customize the FAQ Wizard. For normal purposes, you
should only have to change the FAQ section titles and the small group
of parameters below it.
"""
# Titles of FAQ sections
SECTION_TITLES = {
# SectionNumber : SectionTitle; need at least one entry
1: "General information and availability",
}
# Parameters you definitely want to change
SHORTNAME = "Generic" # FAQ name with "FAQ" omitted
PASSWORD = "" # Password for editing
OWNERNAME = "FAQ owner" # Name for feedback
OWNEREMAIL = "nobody@anywhere.org" # Email for feedback
HOMEURL = "http://www.python.org" # Related home page
HOMENAME = "Python home" # Name of related home page
RCSBINDIR = "/usr/local/bin/" # Directory containing RCS commands
# (must end in a slash)
# Parameters you can normally leave alone
MAXHITS = 10 # Max #hits to be shown directly
COOKIE_LIFETIME = 28*24*3600 # Cookie expiration in seconds
# (28*24*3600 = 28 days = 4 weeks)
PROCESS_PREFORMAT = 1 # toggle whether preformatted text
# will replace urls and emails with
# HTML links
# Markers appended to title to indicate recently change
# (may contain HTML, e.g. <IMG>); and corresponding
MARK_VERY_RECENT = " **" # Changed very recently
MARK_RECENT = " *" # Changed recently
DT_VERY_RECENT = 24*3600 # 24 hours
DT_RECENT = 7*24*3600 # 7 days
EXPLAIN_MARKS = """
<P>(Entries marked with ** were changed within the last 24 hours;
entries marked with * were changed within the last 7 days.)
<P>
"""
# Version -- don't change unless you edit faqwiz.py
WIZVERSION = "1.0.4" # FAQ Wizard version
import os, sys
if os.name in ['nt',]:
# On NT we'll probably be running python from a batch file,
# so sys.argv[0] is not helpful
FAQCGI = 'faq.bat' # Relative URL of the FAQ cgi script
# LOGNAME is not typically set on NT
os.environ[ 'LOGNAME' ] = "FAQWizard"
else:
# This parameter is normally overwritten with a dynamic value
FAQCGI = 'faqw.py' # Relative URL of the FAQ cgi script
FAQCGI = os.path.basename(sys.argv[0]) or FAQCGI
del os, sys
# Perl (re module) style regular expression to recognize FAQ entry
# files: group(1) should be the section number, group(2) should be the
# question number. Both should be fixed width so simple-minded
# sorting yields the right order.
OKFILENAME = r"^faq(\d\d)\.(\d\d\d)\.htp$"
# Format to construct a FAQ entry file name
NEWFILENAME = "faq%02d.%03d.htp"
# Load local customizations on top of the previous parameters
try:
from faqcust import *
except ImportError:
pass
# Calculated parameter names
COOKIE_NAME = SHORTNAME + "-FAQ-Wizard" # Name used for Netscape cookie
FAQNAME = SHORTNAME + " FAQ" # Name of the FAQ
# ----------------------------------------------------------------------
# Anything below this point normally needn't be changed; you would
# change this if you were to create e.g. a French translation or if
# you just aren't happy with the text generated by the FAQ Wizard.
# Most strings here are subject to substitution (string%dictionary)
# RCS commands
import os
if os.name in ['nt', ]:
SH_RLOG = RCSBINDIR + "rlog %(file)s < NUL"
SH_RLOG_H = RCSBINDIR + "rlog -h %(file)s < NUL"
SH_RDIFF = RCSBINDIR + "rcsdiff -r%(prev)s -r%(rev)s %(file)s < NUL"
SH_REVISION = RCSBINDIR + "co -p%(rev)s %(file)s < NUL"
### Have to use co -l, or the file is not marked rw on NT
SH_LOCK = RCSBINDIR + "co -l %(file)s < NUL"
SH_CHECKIN = RCSBINDIR + "ci -u %(file)s < %(tfn)s"
else:
SH_RLOG = RCSBINDIR + "rlog %(file)s </dev/null 2>&1"
SH_RLOG_H = RCSBINDIR + "rlog -h %(file)s </dev/null 2>&1"
SH_RDIFF = RCSBINDIR + "rcsdiff -r%(prev)s -r%(rev)s %(file)s </dev/null 2>&1"
SH_REVISION = RCSBINDIR + "co -p%(rev)s %(file)s </dev/null 2>&1"
SH_LOCK = RCSBINDIR + "rcs -l %(file)s </dev/null 2>&1"
SH_CHECKIN = RCSBINDIR + "ci -u %(file)s <%(tfn)s 2>&1"
del os
# Titles for various output pages (not subject to substitution)
T_HOME = FAQNAME + " Wizard " + WIZVERSION
T_ERROR = "Sorry, an error occurred"
T_ROULETTE = FAQNAME + " Roulette"
T_ALL = "The Whole " + FAQNAME
T_INDEX = FAQNAME + " Index"
T_SEARCH = FAQNAME + " Search Results"
T_RECENT = "What's New in the " + FAQNAME
T_SHOW = FAQNAME + " Entry"
T_LOG = "RCS log for %s entry" % FAQNAME
T_REVISION = "RCS revision for %s entry" % FAQNAME
T_DIFF = "RCS diff for %s entry" % FAQNAME
T_ADD = "Add an entry to the " + FAQNAME
T_DELETE = "Deleting an entry from the " + FAQNAME
T_EDIT = FAQNAME + " Edit Wizard"
T_REVIEW = T_EDIT + " - Review Changes"
T_COMMITTED = T_EDIT + " - Changes Committed"
T_COMMITFAILED = T_EDIT + " - Commit Failed"
T_CANTCOMMIT = T_EDIT + " - Commit Rejected"
T_HELP = T_EDIT + " - Help"
# Generic prologue and epilogue
PROLOGUE = '''
<HTML>
<HEAD>
<TITLE>%(title)s</TITLE>
</HEAD>
<BODY
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#AA0000"
VLINK="#906A6A">
<H1>%(title)s</H1>
'''
EPILOGUE = '''
<HR>
<A HREF="%(HOMEURL)s">%(HOMENAME)s</A> /
<A HREF="%(FAQCGI)s?req=home">%(FAQNAME)s Wizard %(WIZVERSION)s</A> /
Feedback to <A HREF="mailto:%(OWNEREMAIL)s">%(OWNERNAME)s</A>
</BODY>
</HTML>
'''
# Home page
HOME = """
<H2>Search the %(FAQNAME)s:</H2>
<BLOCKQUOTE>
<FORM ACTION="%(FAQCGI)s">
<INPUT TYPE=text NAME=query>
<INPUT TYPE=submit VALUE="Search"><BR>
<INPUT TYPE=radio NAME=querytype VALUE=simple CHECKED>
Simple string
/
<INPUT TYPE=radio NAME=querytype VALUE=regex>
Regular expression
/<BR>
<INPUT TYPE=radio NAME=querytype VALUE=anykeywords>
Keywords (any)
/
<INPUT TYPE=radio NAME=querytype VALUE=allkeywords>
Keywords (all)
<BR>
<INPUT TYPE=radio NAME=casefold VALUE=yes CHECKED>
Fold case
/
<INPUT TYPE=radio NAME=casefold VALUE=no>
Case sensitive
<BR>
<INPUT TYPE=hidden NAME=req VALUE=search>
</FORM>
</BLOCKQUOTE>
<HR>
<H2>Other forms of %(FAQNAME)s access:</H2>
<UL>
<LI><A HREF="%(FAQCGI)s?req=index">FAQ index</A>
<LI><A HREF="%(FAQCGI)s?req=all">The whole FAQ</A>
<LI><A HREF="%(FAQCGI)s?req=recent">What's new in the FAQ?</A>
<LI><A HREF="%(FAQCGI)s?req=roulette">FAQ roulette</A>
<LI><A HREF="%(FAQCGI)s?req=add">Add a FAQ entry</A>
<LI><A HREF="%(FAQCGI)s?req=delete">Delete a FAQ entry</A>
</UL>
"""
# Index formatting
INDEX_SECTION = """
<P>
<HR>
<H2>%(sec)s. %(title)s</H2>
<UL>
"""
INDEX_ADDSECTION = """
<P>
<LI><A HREF="%(FAQCGI)s?req=new&amp;section=%(sec)s">Add new entry</A>
(at this point)
"""
INDEX_ENDSECTION = """
</UL>
"""
INDEX_ENTRY = """\
<LI><A HREF="%(FAQCGI)s?req=show&amp;file=%(file)s">%(title)s</A>
"""
LOCAL_ENTRY = """\
<LI><A HREF="#%(sec)s.%(num)s">%(title)s</A>
"""
# Entry formatting
ENTRY_HEADER1 = """
<HR>
<H2><A NAME="%(sec)s.%(num)s">%(title)s</A>\
"""
ENTRY_HEADER2 = """\
</H2>
"""
ENTRY_FOOTER = """
<A HREF="%(FAQCGI)s?req=edit&amp;file=%(file)s">Edit this entry</A> /
<A HREF="%(FAQCGI)s?req=log&amp;file=%(file)s">Log info</A>
"""
ENTRY_LOGINFO = """
/ Last changed on %(last_changed_date)s by
<A HREF="mailto:%(last_changed_email)s">%(last_changed_author)s</A>
"""
# Search
NO_HITS = """
No hits.
"""
ONE_HIT = """
Your search matched the following entry:
"""
FEW_HITS = """
Your search matched the following %(count)s entries:
"""
MANY_HITS = """
Your search matched more than %(MAXHITS)s entries.
The %(count)s matching entries are presented here ordered by section:
"""
# RCS log and diff
LOG = """
Click on a revision line to see the diff between that revision and the
previous one.
"""
REVISIONLINK = """\
<A HREF="%(FAQCGI)s?req=revision&amp;file=%(file)s&amp;rev=%(rev)s"
>%(line)s</A>\
"""
DIFFLINK = """\
(<A HREF="%(FAQCGI)s?req=diff&amp;file=%(file)s&amp;\
prev=%(prev)s&amp;rev=%(rev)s"
>diff -r%(prev)s -r%(rev)s</A>)\
"""
# Recently changed entries
NO_RECENT = """
<HR>
No %(FAQNAME)s entries were changed in the last %(period)s.
"""
VIEW_MENU = """
<HR>
View entries changed in the last...
<UL>
<LI><A HREF="%(FAQCGI)s?req=recent&amp;days=1">24 hours</A>
<LI><A HREF="%(FAQCGI)s?req=recent&amp;days=2">2 days</A>
<LI><A HREF="%(FAQCGI)s?req=recent&amp;days=3">3 days</A>
<LI><A HREF="%(FAQCGI)s?req=recent&amp;days=7">week</A>
<LI><A HREF="%(FAQCGI)s?req=recent&amp;days=28">4 weeks</A>
<LI><A HREF="%(FAQCGI)s?req=recent&amp;days=365250">millennium</A>
</UL>
"""
ONE_RECENT = VIEW_MENU + """
The following %(FAQNAME)s entry was changed in the last %(period)s:
"""
SOME_RECENT = VIEW_MENU + """
The following %(count)s %(FAQNAME)s entries were changed
in the last %(period)s, most recently changed shown first:
"""
TAIL_RECENT = VIEW_MENU
# Last changed banner on "all" (strftime format)
LAST_CHANGED = "Last changed on %c %Z"
# "Compat" command prologue (this has no <BODY> tag)
COMPAT = """
<H1>The whole %(FAQNAME)s</H1>
See also the <A HREF="%(FAQCGI)s?req=home">%(FAQNAME)s Wizard</A>.
<P>
"""
# Editing
EDITHEAD = """
<A HREF="%(FAQCGI)s?req=help">Click for Help</A>
"""
REVIEWHEAD = EDITHEAD
EDITFORM1 = """
<FORM ACTION="%(FAQCGI)s" METHOD=POST>
<INPUT TYPE=hidden NAME=req VALUE=review>
<INPUT TYPE=hidden NAME=file VALUE=%(file)s>
<INPUT TYPE=hidden NAME=editversion VALUE=%(editversion)s>
<HR>
"""
EDITFORM2 = """
Title: <INPUT TYPE=text SIZE=70 NAME=title VALUE="%(title)s"><BR>
<TEXTAREA COLS=72 ROWS=20 NAME=body>%(body)s
</TEXTAREA><BR>
Log message (reason for the change):<BR>
<TEXTAREA COLS=72 ROWS=5 NAME=log>%(log)s
</TEXTAREA><BR>
Please provide the following information for logging purposes:
<TABLE FRAME=none COLS=2>
<TR>
<TD>Name:
<TD><INPUT TYPE=text SIZE=40 NAME=author VALUE="%(author)s">
<TR>
<TD>Email:
<TD><INPUT TYPE=text SIZE=40 NAME=email VALUE="%(email)s">
<TR>
<TD>Password:
<TD><INPUT TYPE=password SIZE=20 NAME=password VALUE="%(password)s">
</TABLE>
<INPUT TYPE=submit NAME=review VALUE="Preview Edit">
Click this button to preview your changes.
"""
EDITFORM3 = """
</FORM>
"""
COMMIT = """
<INPUT TYPE=submit NAME=commit VALUE="Commit">
Click this button to commit your changes.
<HR>
"""
NOCOMMIT_HEAD = """
To commit your changes, please correct the following errors in the
form below and click the Preview Edit button.
<UL>
"""
NOCOMMIT_TAIL = """
</UL>
<HR>
"""
CANTCOMMIT_HEAD = """
Some required information is missing:
<UL>
"""
NEED_PASSWD = "<LI>You must provide the correct password.\n"
NEED_AUTHOR = "<LI>You must enter your name.\n"
NEED_EMAIL = "<LI>You must enter your email address.\n"
NEED_LOG = "<LI>You must enter a log message.\n"
CANTCOMMIT_TAIL = """
</UL>
Please use your browser's Back command to correct the form and commit
again.
"""
NEWCONFLICT = """
<P>
You are creating a new entry, but the entry number specified is not
correct.
<P>
The two most common causes of this problem are:
<UL>
<LI>After creating the entry yourself, you went back in your browser,
edited the entry some more, and clicked Commit again.
<LI>Someone else started creating a new entry in the same section and
committed before you did.
</UL>
(It is also possible that the last entry in the section was physically
deleted, but this should not happen except through manual intervention
by the FAQ maintainer.)
<P>
<A HREF="%(FAQCGI)s?req=new&amp;section=%(sec)s">Click here to try
again.</A>
<P>
"""
VERSIONCONFLICT = """
<P>
You edited version %(editversion)s but the current version is %(version)s.
<P>
The two most common causes of this problem are:
<UL>
<LI>After committing a change, you went back in your browser,
edited the entry some more, and clicked Commit again.
<LI>Someone else started editing the same entry and committed
before you did.
</UL>
<P>
<A HREF="%(FAQCGI)s?req=show&amp;file=%(file)s">Click here to reload
the entry and try again.</A>
<P>
"""
CANTWRITE = """
Can't write file %(file)s (%(why)s).
"""
FILEHEADER = """\
Title: %(title)s
Last-Changed-Date: %(date)s
Last-Changed-Author: %(author)s
Last-Changed-Email: %(email)s
Last-Changed-Remote-Host: %(REMOTE_HOST)s
Last-Changed-Remote-Address: %(REMOTE_ADDR)s
"""
LOGHEADER = """\
Last-Changed-Date: %(date)s
Last-Changed-Author: %(author)s
Last-Changed-Email: %(email)s
Last-Changed-Remote-Host: %(REMOTE_HOST)s
Last-Changed-Remote-Address: %(REMOTE_ADDR)s
%(log)s
"""
COMMITTED = """
Your changes have been committed.
"""
COMMITFAILED = """
Exit status %(sts)s.
"""
# Add/Delete
ADD_HEAD = """
At the moment, new entries can only be added at the end of a section.
This is because the entry numbers are also their
unique identifiers -- it's a bad idea to renumber entries.
<P>
Click on the section to which you want to add a new entry:
<UL>
"""
ADD_SECTION = """\
<LI><A HREF="%(FAQCGI)s?req=new&amp;section=%(section)s">%(section)s. %(title)s</A>
"""
ADD_TAIL = """
</UL>
"""
ROULETTE = """
<P>Hit your browser's Reload button to play again.<P>
"""
DELETE = """
At the moment, there's no direct way to delete entries.
This is because the entry numbers are also their
unique identifiers -- it's a bad idea to renumber entries.
<P>
If you really think an entry needs to be deleted,
change the title to "(deleted)" and make the body
empty (keep the entry number in the title though).
"""
# Help file for the FAQ Edit Wizard
HELP = """
Using the %(FAQNAME)s Edit Wizard speaks mostly for itself. Here are
some answers to questions you are likely to ask:
<P><HR>
<H2>I can review an entry but I can't commit it.</H2>
The commit button only appears if the following conditions are met:
<UL>
<LI>The Name field is not empty.
<LI>The Email field contains at least an @ character.
<LI>The Log message box is not empty.
<LI>The Password field contains the proper password.
</UL>
<P><HR>
<H2>What is the password?</H2>
At the moment, only PSA members will be told the password. This is a
good time to join the PSA! See <A
HREF="http://www.python.org/psa/">the PSA home page</A>.
<P><HR>
<H2>Can I use HTML in the FAQ entry?</H2>
Yes, if you include it in &lt;HTML&rt; and &lt;/HTML&gt; tags.
<P>
Also, if you include a URL or an email address in the text it will
automatigally become an anchor of the right type. Also, *word*
is made italic (but only for single alphabetic words).
<P><HR>
<H2>How do I delineate paragraphs?</H2>
Use blank lines to separate paragraphs.
<P><HR>
<H2>How do I enter example text?</H2>
Any line that begins with a space or tab is assumed to be part of
literal text. Blocks of literal text delineated by blank lines are
placed inside &lt;PRE&gt;...&lt;/PRE&gt;.
"""
# Load local customizations again, in case they set some other variables
try:
from faqcust import *
except ImportError:
pass

View File

@@ -0,0 +1 @@
# Add your customizations here -- modified copies of what's in faqconf.py.

View File

@@ -0,0 +1,33 @@
#!/usr/bin/env python
"""FAQ wizard bootstrap."""
# This is a longer version of the bootstrap script given at the end of
# faqwin.py; it prints timing statistics at the end of the regular CGI
# script's output (so you can monitor how it is doing).
# This script should be placed in your cgi-bin directory and made
# executable.
# You need to edit the first line and the lines that define FAQDIR and
# SRCDIR, below: change /usr/local/bin/python to where your Python
# interpreter lives, change the value for FAQDIR to where your FAQ
# lives, and change the value for SRCDIR to where your faqwiz.py
# module lives. The faqconf.py and faqcust.py files live there, too.
import os
t1 = os.times() # If this doesn't work, just get rid of the timing code!
try:
FAQDIR = "/usr/people/guido/python/FAQ"
SRCDIR = "/usr/people/guido/python/src/Tools/faqwiz"
import os, sys
os.chdir(FAQDIR)
sys.path.insert(0, SRCDIR)
import faqwiz
except SystemExit, n:
sys.exit(n)
except:
t, v, tb = sys.exc_info()
print
import cgi
cgi.print_exception(t, v, tb)

View File

@@ -0,0 +1,841 @@
"""Generic FAQ Wizard.
This is a CGI program that maintains a user-editable FAQ. It uses RCS
to keep track of changes to individual FAQ entries. It is fully
configurable; everything you might want to change when using this
program to maintain some other FAQ than the Python FAQ is contained in
the configuration module, faqconf.py.
Note that this is not an executable script; it's an importable module.
The actual script to place in cgi-bin is faqw.py.
"""
import sys, time, os, stat, re, cgi, faqconf
from faqconf import * # This imports all uppercase names
now = time.time()
class FileError:
def __init__(self, file):
self.file = file
class InvalidFile(FileError):
pass
class NoSuchSection(FileError):
def __init__(self, section):
FileError.__init__(self, NEWFILENAME %(section, 1))
self.section = section
class NoSuchFile(FileError):
def __init__(self, file, why=None):
FileError.__init__(self, file)
self.why = why
def escape(s):
s = s.replace('&', '&amp;')
s = s.replace('<', '&lt;')
s = s.replace('>', '&gt;')
return s
def escapeq(s):
s = escape(s)
s = s.replace('"', '&quot;')
return s
def _interpolate(format, args, kw):
try:
quote = kw['_quote']
except KeyError:
quote = 1
d = (kw,) + args + (faqconf.__dict__,)
m = MagicDict(d, quote)
return format % m
def interpolate(format, *args, **kw):
return _interpolate(format, args, kw)
def emit(format, *args, **kw):
try:
f = kw['_file']
except KeyError:
f = sys.stdout
f.write(_interpolate(format, args, kw))
translate_prog = None
def translate(text, pre=0):
global translate_prog
if not translate_prog:
translate_prog = prog = re.compile(
r'\b(http|ftp|https)://\S+(\b|/)|\b[-.\w]+@[-.\w]+')
else:
prog = translate_prog
i = 0
list = []
while 1:
m = prog.search(text, i)
if not m:
break
j = m.start()
list.append(escape(text[i:j]))
i = j
url = m.group(0)
while url[-1] in '();:,.?\'"<>':
url = url[:-1]
i = i + len(url)
url = escape(url)
if not pre or (pre and PROCESS_PREFORMAT):
if ':' in url:
repl = '<A HREF="%s">%s</A>' % (url, url)
else:
repl = '<A HREF="mailto:%s">%s</A>' % (url, url)
else:
repl = url
list.append(repl)
j = len(text)
list.append(escape(text[i:j]))
return ''.join(list)
def emphasize(line):
return re.sub(r'\*([a-zA-Z]+)\*', r'<I>\1</I>', line)
revparse_prog = None
def revparse(rev):
global revparse_prog
if not revparse_prog:
revparse_prog = re.compile(r'^(\d{1,3})\.(\d{1,4})$')
m = revparse_prog.match(rev)
if not m:
return None
[major, minor] = map(int, m.group(1, 2))
return major, minor
logon = 0
def log(text):
if logon:
logfile = open("logfile", "a")
logfile.write(text + "\n")
logfile.close()
def load_cookies():
if not os.environ.has_key('HTTP_COOKIE'):
return {}
raw = os.environ['HTTP_COOKIE']
words = [s.strip() for s in raw.split(';')]
cookies = {}
for word in words:
i = word.find('=')
if i >= 0:
key, value = word[:i], word[i+1:]
cookies[key] = value
return cookies
def load_my_cookie():
cookies = load_cookies()
try:
value = cookies[COOKIE_NAME]
except KeyError:
return {}
import urllib
value = urllib.unquote(value)
words = value.split('/')
while len(words) < 3:
words.append('')
author = '/'.join(words[:-2])
email = words[-2]
password = words[-1]
return {'author': author,
'email': email,
'password': password}
def send_my_cookie(ui):
name = COOKIE_NAME
value = "%s/%s/%s" % (ui.author, ui.email, ui.password)
import urllib
value = urllib.quote(value)
then = now + COOKIE_LIFETIME
gmt = time.gmtime(then)
path = os.environ.get('SCRIPT_NAME', '/cgi-bin/')
print "Set-Cookie: %s=%s; path=%s;" % (name, value, path),
print time.strftime("expires=%a, %d-%b-%y %X GMT", gmt)
class MagicDict:
def __init__(self, d, quote):
self.__d = d
self.__quote = quote
def __getitem__(self, key):
for d in self.__d:
try:
value = d[key]
if value:
value = str(value)
if self.__quote:
value = escapeq(value)
return value
except KeyError:
pass
return ''
class UserInput:
def __init__(self):
self.__form = cgi.FieldStorage()
#log("\n\nbody: " + self.body)
def __getattr__(self, name):
if name[0] == '_':
raise AttributeError
try:
value = self.__form[name].value
except (TypeError, KeyError):
value = ''
else:
value = value.strip()
setattr(self, name, value)
return value
def __getitem__(self, key):
return getattr(self, key)
class FaqEntry:
def __init__(self, fp, file, sec_num):
self.file = file
self.sec, self.num = sec_num
if fp:
import rfc822
self.__headers = rfc822.Message(fp)
self.body = fp.read().strip()
else:
self.__headers = {'title': "%d.%d. " % sec_num}
self.body = ''
def __getattr__(self, name):
if name[0] == '_':
raise AttributeError
key = '-'.join(name.split('_'))
try:
value = self.__headers[key]
except KeyError:
value = ''
setattr(self, name, value)
return value
def __getitem__(self, key):
return getattr(self, key)
def load_version(self):
command = interpolate(SH_RLOG_H, self)
p = os.popen(command)
version = ''
while 1:
line = p.readline()
if not line:
break
if line[:5] == 'head:':
version = line[5:].strip()
p.close()
self.version = version
def getmtime(self):
if not self.last_changed_date:
return 0
try:
return os.stat(self.file)[stat.ST_MTIME]
except os.error:
return 0
def emit_marks(self):
mtime = self.getmtime()
if mtime >= now - DT_VERY_RECENT:
emit(MARK_VERY_RECENT, self)
elif mtime >= now - DT_RECENT:
emit(MARK_RECENT, self)
def show(self, edit=1):
emit(ENTRY_HEADER1, self)
self.emit_marks()
emit(ENTRY_HEADER2, self)
pre = 0
raw = 0
for line in self.body.split('\n'):
# Allow the user to insert raw html into a FAQ answer
# (Skip Montanaro, with changes by Guido)
tag = line.rstrip().lower()
if tag == '<html>':
raw = 1
continue
if tag == '</html>':
raw = 0
continue
if raw:
print line
continue
if not line.strip():
if pre:
print '</PRE>'
pre = 0
else:
print '<P>'
else:
if not line[0].isspace():
if pre:
print '</PRE>'
pre = 0
else:
if not pre:
print '<PRE>'
pre = 1
if '/' in line or '@' in line:
line = translate(line, pre)
elif '<' in line or '&' in line:
line = escape(line)
if not pre and '*' in line:
line = emphasize(line)
print line
if pre:
print '</PRE>'
pre = 0
if edit:
print '<P>'
emit(ENTRY_FOOTER, self)
if self.last_changed_date:
emit(ENTRY_LOGINFO, self)
print '<P>'
class FaqDir:
entryclass = FaqEntry
__okprog = re.compile(OKFILENAME)
def __init__(self, dir=os.curdir):
self.__dir = dir
self.__files = None
def __fill(self):
if self.__files is not None:
return
self.__files = files = []
okprog = self.__okprog
for file in os.listdir(self.__dir):
if self.__okprog.match(file):
files.append(file)
files.sort()
def good(self, file):
return self.__okprog.match(file)
def parse(self, file):
m = self.good(file)
if not m:
return None
sec, num = m.group(1, 2)
return int(sec), int(num)
def list(self):
# XXX Caller shouldn't modify result
self.__fill()
return self.__files
def open(self, file):
sec_num = self.parse(file)
if not sec_num:
raise InvalidFile(file)
try:
fp = open(file)
except IOError, msg:
raise NoSuchFile(file, msg)
try:
return self.entryclass(fp, file, sec_num)
finally:
fp.close()
def show(self, file, edit=1):
self.open(file).show(edit=edit)
def new(self, section):
if not SECTION_TITLES.has_key(section):
raise NoSuchSection(section)
maxnum = 0
for file in self.list():
sec, num = self.parse(file)
if sec == section:
maxnum = max(maxnum, num)
sec_num = (section, maxnum+1)
file = NEWFILENAME % sec_num
return self.entryclass(None, file, sec_num)
class FaqWizard:
def __init__(self):
self.ui = UserInput()
self.dir = FaqDir()
def go(self):
print 'Content-type: text/html'
req = self.ui.req or 'home'
mname = 'do_%s' % req
try:
meth = getattr(self, mname)
except AttributeError:
self.error("Bad request type %r." % (req,))
else:
try:
meth()
except InvalidFile, exc:
self.error("Invalid entry file name %s" % exc.file)
except NoSuchFile, exc:
self.error("No entry with file name %s" % exc.file)
except NoSuchSection, exc:
self.error("No section number %s" % exc.section)
self.epilogue()
def error(self, message, **kw):
self.prologue(T_ERROR)
emit(message, kw)
def prologue(self, title, entry=None, **kw):
emit(PROLOGUE, entry, kwdict=kw, title=escape(title))
def epilogue(self):
emit(EPILOGUE)
def do_home(self):
self.prologue(T_HOME)
emit(HOME)
def do_debug(self):
self.prologue("FAQ Wizard Debugging")
form = cgi.FieldStorage()
cgi.print_form(form)
cgi.print_environ(os.environ)
cgi.print_directory()
cgi.print_arguments()
def do_search(self):
query = self.ui.query
if not query:
self.error("Empty query string!")
return
if self.ui.querytype == 'simple':
query = re.escape(query)
queries = [query]
elif self.ui.querytype in ('anykeywords', 'allkeywords'):
words = filter(None, re.split('\W+', query))
if not words:
self.error("No keywords specified!")
return
words = map(lambda w: r'\b%s\b' % w, words)
if self.ui.querytype[:3] == 'any':
queries = ['|'.join(words)]
else:
# Each of the individual queries must match
queries = words
else:
# Default to regular expression
queries = [query]
self.prologue(T_SEARCH)
progs = []
for query in queries:
if self.ui.casefold == 'no':
p = re.compile(query)
else:
p = re.compile(query, re.IGNORECASE)
progs.append(p)
hits = []
for file in self.dir.list():
try:
entry = self.dir.open(file)
except FileError:
constants
for p in progs:
if not p.search(entry.title) and not p.search(entry.body):
break
else:
hits.append(file)
if not hits:
emit(NO_HITS, self.ui, count=0)
elif len(hits) <= MAXHITS:
if len(hits) == 1:
emit(ONE_HIT, count=1)
else:
emit(FEW_HITS, count=len(hits))
self.format_all(hits, headers=0)
else:
emit(MANY_HITS, count=len(hits))
self.format_index(hits)
def do_all(self):
self.prologue(T_ALL)
files = self.dir.list()
self.last_changed(files)
self.format_index(files, localrefs=1)
self.format_all(files)
def do_compat(self):
files = self.dir.list()
emit(COMPAT)
self.last_changed(files)
self.format_index(files, localrefs=1)
self.format_all(files, edit=0)
sys.exit(0) # XXX Hack to suppress epilogue
def last_changed(self, files):
latest = 0
for file in files:
entry = self.dir.open(file)
if entry:
mtime = mtime = entry.getmtime()
if mtime > latest:
latest = mtime
print time.strftime(LAST_CHANGED, time.localtime(latest))
emit(EXPLAIN_MARKS)
def format_all(self, files, edit=1, headers=1):
sec = 0
for file in files:
try:
entry = self.dir.open(file)
except NoSuchFile:
continue
if headers and entry.sec != sec:
sec = entry.sec
try:
title = SECTION_TITLES[sec]
except KeyError:
title = "Untitled"
emit("\n<HR>\n<H1>%(sec)s. %(title)s</H1>\n",
sec=sec, title=title)
entry.show(edit=edit)
def do_index(self):
self.prologue(T_INDEX)
files = self.dir.list()
self.last_changed(files)
self.format_index(files, add=1)
def format_index(self, files, add=0, localrefs=0):
sec = 0
for file in files:
try:
entry = self.dir.open(file)
except NoSuchFile:
continue
if entry.sec != sec:
if sec:
if add:
emit(INDEX_ADDSECTION, sec=sec)
emit(INDEX_ENDSECTION, sec=sec)
sec = entry.sec
try:
title = SECTION_TITLES[sec]
except KeyError:
title = "Untitled"
emit(INDEX_SECTION, sec=sec, title=title)
if localrefs:
emit(LOCAL_ENTRY, entry)
else:
emit(INDEX_ENTRY, entry)
entry.emit_marks()
if sec:
if add:
emit(INDEX_ADDSECTION, sec=sec)
emit(INDEX_ENDSECTION, sec=sec)
def do_recent(self):
if not self.ui.days:
days = 1
else:
days = float(self.ui.days)
try:
cutoff = now - days * 24 * 3600
except OverflowError:
cutoff = 0
list = []
for file in self.dir.list():
entry = self.dir.open(file)
if not entry:
continue
mtime = entry.getmtime()
if mtime >= cutoff:
list.append((mtime, file))
list.sort()
list.reverse()
self.prologue(T_RECENT)
if days <= 1:
period = "%.2g hours" % (days*24)
else:
period = "%.6g days" % days
if not list:
emit(NO_RECENT, period=period)
elif len(list) == 1:
emit(ONE_RECENT, period=period)
else:
emit(SOME_RECENT, period=period, count=len(list))
self.format_all(map(lambda (mtime, file): file, list), headers=0)
emit(TAIL_RECENT)
def do_roulette(self):
import random
files = self.dir.list()
if not files:
self.error("No entries.")
return
file = random.choice(files)
self.prologue(T_ROULETTE)
emit(ROULETTE)
self.dir.show(file)
def do_help(self):
self.prologue(T_HELP)
emit(HELP)
def do_show(self):
entry = self.dir.open(self.ui.file)
self.prologue(T_SHOW)
entry.show()
def do_add(self):
self.prologue(T_ADD)
emit(ADD_HEAD)
sections = SECTION_TITLES.items()
sections.sort()
for section, title in sections:
emit(ADD_SECTION, section=section, title=title)
emit(ADD_TAIL)
def do_delete(self):
self.prologue(T_DELETE)
emit(DELETE)
def do_log(self):
entry = self.dir.open(self.ui.file)
self.prologue(T_LOG, entry)
emit(LOG, entry)
self.rlog(interpolate(SH_RLOG, entry), entry)
def rlog(self, command, entry=None):
output = os.popen(command).read()
sys.stdout.write('<PRE>')
athead = 0
lines = output.split('\n')
while lines and not lines[-1]:
del lines[-1]
if lines:
line = lines[-1]
if line[:1] == '=' and len(line) >= 40 and \
line == line[0]*len(line):
del lines[-1]
headrev = None
for line in lines:
if entry and athead and line[:9] == 'revision ':
rev = line[9:].split()
mami = revparse(rev)
if not mami:
print line
else:
emit(REVISIONLINK, entry, rev=rev, line=line)
if mami[1] > 1:
prev = "%d.%d" % (mami[0], mami[1]-1)
emit(DIFFLINK, entry, prev=prev, rev=rev)
if headrev:
emit(DIFFLINK, entry, prev=rev, rev=headrev)
else:
headrev = rev
print
athead = 0
else:
athead = 0
if line[:1] == '-' and len(line) >= 20 and \
line == len(line) * line[0]:
athead = 1
sys.stdout.write('<HR>')
else:
print line
print '</PRE>'
def do_revision(self):
entry = self.dir.open(self.ui.file)
rev = self.ui.rev
mami = revparse(rev)
if not mami:
self.error("Invalid revision number: %r." % (rev,))
self.prologue(T_REVISION, entry)
self.shell(interpolate(SH_REVISION, entry, rev=rev))
def do_diff(self):
entry = self.dir.open(self.ui.file)
prev = self.ui.prev
rev = self.ui.rev
mami = revparse(rev)
if not mami:
self.error("Invalid revision number: %r." % (rev,))
if prev:
if not revparse(prev):
self.error("Invalid previous revision number: %r." % (prev,))
else:
prev = '%d.%d' % (mami[0], mami[1])
self.prologue(T_DIFF, entry)
self.shell(interpolate(SH_RDIFF, entry, rev=rev, prev=prev))
def shell(self, command):
output = os.popen(command).read()
sys.stdout.write('<PRE>')
print escape(output)
print '</PRE>'
def do_new(self):
entry = self.dir.new(section=int(self.ui.section))
entry.version = '*new*'
self.prologue(T_EDIT)
emit(EDITHEAD)
emit(EDITFORM1, entry, editversion=entry.version)
emit(EDITFORM2, entry, load_my_cookie())
emit(EDITFORM3)
entry.show(edit=0)
def do_edit(self):
entry = self.dir.open(self.ui.file)
entry.load_version()
self.prologue(T_EDIT)
emit(EDITHEAD)
emit(EDITFORM1, entry, editversion=entry.version)
emit(EDITFORM2, entry, load_my_cookie())
emit(EDITFORM3)
entry.show(edit=0)
def do_review(self):
send_my_cookie(self.ui)
if self.ui.editversion == '*new*':
sec, num = self.dir.parse(self.ui.file)
entry = self.dir.new(section=sec)
entry.version = "*new*"
if entry.file != self.ui.file:
self.error("Commit version conflict!")
emit(NEWCONFLICT, self.ui, sec=sec, num=num)
return
else:
entry = self.dir.open(self.ui.file)
entry.load_version()
# Check that the FAQ entry number didn't change
if self.ui.title.split()[:1] != entry.title.split()[:1]:
self.error("Don't change the entry number please!")
return
# Check that the edited version is the current version
if entry.version != self.ui.editversion:
self.error("Commit version conflict!")
emit(VERSIONCONFLICT, entry, self.ui)
return
commit_ok = ((not PASSWORD
or self.ui.password == PASSWORD)
and self.ui.author
and '@' in self.ui.email
and self.ui.log)
if self.ui.commit:
if not commit_ok:
self.cantcommit()
else:
self.commit(entry)
return
self.prologue(T_REVIEW)
emit(REVIEWHEAD)
entry.body = self.ui.body
entry.title = self.ui.title
entry.show(edit=0)
emit(EDITFORM1, self.ui, entry)
if commit_ok:
emit(COMMIT)
else:
emit(NOCOMMIT_HEAD)
self.errordetail()
emit(NOCOMMIT_TAIL)
emit(EDITFORM2, self.ui, entry, load_my_cookie())
emit(EDITFORM3)
def cantcommit(self):
self.prologue(T_CANTCOMMIT)
print CANTCOMMIT_HEAD
self.errordetail()
print CANTCOMMIT_TAIL
def errordetail(self):
if PASSWORD and self.ui.password != PASSWORD:
emit(NEED_PASSWD)
if not self.ui.log:
emit(NEED_LOG)
if not self.ui.author:
emit(NEED_AUTHOR)
if not self.ui.email:
emit(NEED_EMAIL)
def commit(self, entry):
file = entry.file
# Normalize line endings in body
if '\r' in self.ui.body:
self.ui.body = re.sub('\r\n?', '\n', self.ui.body)
# Normalize whitespace in title
self.ui.title = ' '.join(self.ui.title.split())
# Check that there were any changes
if self.ui.body == entry.body and self.ui.title == entry.title:
self.error("You didn't make any changes!")
return
# need to lock here because otherwise the file exists and is not writable (on NT)
command = interpolate(SH_LOCK, file=file)
p = os.popen(command)
output = p.read()
try:
os.unlink(file)
except os.error:
pass
try:
f = open(file, 'w')
except IOError, why:
self.error(CANTWRITE, file=file, why=why)
return
date = time.ctime(now)
emit(FILEHEADER, self.ui, os.environ, date=date, _file=f, _quote=0)
f.write('\n')
f.write(self.ui.body)
f.write('\n')
f.close()
import tempfile
tf = tempfile.NamedTemporaryFile()
emit(LOGHEADER, self.ui, os.environ, date=date, _file=tf)
tf.flush()
tf.seek(0)
command = interpolate(SH_CHECKIN, file=file, tfn=tf.name)
log("\n\n" + command)
p = os.popen(command)
output = p.read()
sts = p.close()
log("output: " + output)
log("done: " + str(sts))
log("TempFile:\n" + tf.read() + "end")
if not sts:
self.prologue(T_COMMITTED)
emit(COMMITTED)
else:
self.error(T_COMMITFAILED)
emit(COMMITFAILED, sts=sts)
print '<PRE>%s</PRE>' % escape(output)
try:
os.unlink(tf.name)
except os.error:
pass
entry = self.dir.open(file)
entry.show()
wiz = FaqWizard()
wiz.go()

View File

@@ -0,0 +1,55 @@
#!/bin/sh
#
# Christian Reis <kiko@async.com.br>
#
# Moves
#
# Example:
#
# blackjesus:~> ./move-faqwiz.sh 2\.1 3\.2
# Moving FAQ question 02.001 to 03.002
if [ x$2 = x ]; then
echo "Need 2 args: original_version final_version."
exit 2
fi
if [ ! -d data -o ! -d data/RCS ]; then
echo "Run this inside the faqwiz data/ directory's parent dir."
exit 2
fi
cut_n_pad() {
t=`echo $1 | cut -d. -f $2`
export $3=`echo $t | awk "{ tmp = \\$0; l = length(tmp); for (i = 0; i < $2-l+1; i++) { tmp = "0".tmp } print tmp }"`
}
cut_n_pad $1 1 prefix1
cut_n_pad $1 2 suffix1
cut_n_pad $2 1 prefix2
cut_n_pad $2 2 suffix2
if which tempfile >/dev/null; then
tmpfile=$(tempfile -d .)
elif [ -n "$RANDOM" ]; then
tmpfile=tmp$RANDOM.tmp
else
tmpfile=tmp$$.tmp
fi
file1=faq$prefix1.$suffix1.htp
file2=faq$prefix2.$suffix2.htp
echo "Moving FAQ question $prefix1.$suffix1 to $prefix2.$suffix2"
sed -e "s/$1\./$2\./g" data/$file1 > ${tmpfile}1
sed -e "s/$1\./$2\./g" data/RCS/$file1,v > ${tmpfile}2
if [ -f data/$file2 ]; then
echo "Target FAQ exists. Won't clobber."
exit 2
fi
mv ${tmpfile}1 data/$file2
mv ${tmpfile}2 data/RCS/$file2,v
mv data/$file1 data/$file1.orig
mv data/RCS/$file1,v data/RCS/$file1,v.orig

View File

@@ -0,0 +1,8 @@
framer is a tool to generate boilerplate code for C extension types.
The boilerplate is generated from a specification object written in
Python. The specification uses the class statement to describe the
extension module and any extension types it contains. From the
specification, framer can generate all the boilerplate C code,
including function definitions, argument handling code, and type
objects.

View File

@@ -0,0 +1,6 @@
Add spec for getsets.
Generate a distutils setup script.
Handle operator overloading.
Generate traverse and clear methods for GC.
Handle mapping, sequence, buffer protocols.
Finish the todo list.

View File

@@ -0,0 +1,126 @@
"""Generate the skeleton for cStringIO as an example of framer."""
from framer.bases import Module, Type
from framer.member import member
class cStringIO(Module):
"""A simple fast partial StringIO replacement.
This module provides a simple useful replacement for the StringIO
module that is written in C. It does not provide the full
generality of StringIO, but it provides enough for most
applications and is especially useful in conjunction with the
pickle module.
Usage:
from cStringIO import StringIO
an_output_stream = StringIO()
an_output_stream.write(some_stuff)
...
value = an_output_stream.getvalue()
an_input_stream = StringIO(a_string)
spam = an_input_stream.readline()
spam = an_input_stream.read(5)
an_input_stream.seek(0) # OK, start over
spam = an_input_stream.read() # and read it all
"""
__file__ = "cStringIO.c"
def StringIO(o):
"""Return a StringIO-like stream for reading or writing"""
StringIO.pyarg = "|O"
class InputType(Type):
"Simple type for treating strings as input file streams"
abbrev = "input"
struct = """\
typedef struct {
PyObject_HEAD
char *buf;
int pos;
int size;
PyObject *pbuf;
} InputObject;
"""
def flush(self):
"""Does nothing"""
def getvalue(self):
"""Get the string value.
If use_pos is specified and is a true value, then the
string returned will include only the text up to the
current file position.
"""
def isatty(self):
"""Always returns False"""
def read(self, s):
"""Return s characters or the rest of the string."""
read.pyarg = "|i"
def readline(self):
"""Read one line."""
def readlines(self, hint):
"""Read all lines."""
readlines.pyarg = "|i"
def reset(self):
"""Reset the file position to the beginning."""
def tell(self):
"""Get the current position."""
def truncate(self, pos):
"""Truncate the file at the current position."""
truncate.pyarg = "|i"
def seek(self, position, mode=0):
"""Set the current position.
The optional mode argument can be 0 for absolute, 1 for relative,
and 2 for relative to EOF. The default is absolute.
"""
seek.pyarg = "i|i"
def close(self):
pass
class OutputType(InputType):
"Simple type for output strings."
abbrev = "output"
struct = """\
typedef struct {
PyObject_HEAD
char *buf;
int pos;
int size;
int softspace;
} OutputObject;
"""
softspace = member()
def close(self):
"""Explicitly release resources."""
def write(self, s):
"""Write a string to the file."""
# XXX Hack: writing None resets the buffer
def writelines(self, lines):
"""Write each string in lines."""
cStringIO.gen()

View File

@@ -0,0 +1,6 @@
"""A tool to generate basic framework for C extension types.
The basic ideas is the same as modulator, but the code generates code
using many of the new features introduced in Python 2.2. It also
takes a more declarative approach to generating code.
"""

View File

@@ -0,0 +1,220 @@
"""Provides the Module and Type base classes that user code inherits from."""
__all__ = ["Module", "Type", "member"]
from framer import struct, template
from framer.function import Function, Method
from framer.member import member
from framer.slots import *
from framer.util import cstring, unindent
from types import FunctionType
def sortitems(dict):
L = dict.items()
L.sort()
return L
# The Module and Type classes are implemented using metaclasses,
# because most of the methods are class methods. It is easier to use
# metaclasses than the cumbersome classmethod() builtin. They have
# class methods because they are exposed to user code as base classes.
class BaseMetaclass(type):
"""Shared infrastructure for generating modules and types."""
# just methoddef so far
def dump_methoddef(self, f, functions, vars):
def p(templ, vars=vars): # helper function to generate output
print >> f, templ % vars
if not functions:
return
p(template.methoddef_start)
for name, func in sortitems(functions):
if func.__doc__:
p(template.methoddef_def_doc, func.vars)
else:
p(template.methoddef_def, func.vars)
p(template.methoddef_end)
class ModuleMetaclass(BaseMetaclass):
"""Provides methods for Module class."""
def gen(self):
self.analyze()
self.initvars()
f = open(self.__filename, "w")
self.dump(f)
f.close()
def analyze(self):
self.name = getattr(self, "abbrev", self.__name__)
self.__functions = {}
self.__types = {}
self.__members = False
for name, obj in self.__dict__.iteritems():
if isinstance(obj, FunctionType):
self.__functions[name] = Function(obj, self)
elif isinstance(obj, TypeMetaclass):
obj._TypeMetaclass__module = self.name
obj.analyze()
self.__types[name] = obj
if obj.has_members():
self.__members = True
def initvars(self):
v = self.__vars = {}
filename = getattr(self, "__file__", None)
if filename is None:
filename = self.__name__ + "module.c"
self.__filename = v["FileName"] = filename
name = v["ModuleName"] = self.__name__
v["MethodDefName"] = "%s_methods" % name
v["ModuleDocstring"] = cstring(unindent(self.__doc__))
def dump(self, f):
def p(templ, vars=self.__vars): # helper function to generate output
print >> f, templ % vars
p(template.module_start)
if self.__members:
p(template.member_include)
print >> f
if self.__doc__:
p(template.module_doc)
for name, type in sortitems(self.__types):
type.dump(f)
for name, func in sortitems(self.__functions):
func.dump(f)
self.dump_methoddef(f, self.__functions, self.__vars)
p(template.module_init_start)
for name, type in sortitems(self.__types):
type.dump_init(f)
p("}")
class Module:
__metaclass__ = ModuleMetaclass
class TypeMetaclass(BaseMetaclass):
def dump(self, f):
self.initvars()
# defined after initvars() so that __vars is defined
def p(templ, vars=self.__vars):
print >> f, templ % vars
if self.struct is not None:
print >> f, unindent(self.struct, False)
if self.__doc__:
p(template.docstring)
for name, func in sortitems(self.__methods):
func.dump(f)
self.dump_methoddef(f, self.__methods, self.__vars)
self.dump_memberdef(f)
self.dump_slots(f)
def has_members(self):
if self.__members:
return True
else:
return False
def analyze(self):
# called by ModuleMetaclass analyze()
self.name = getattr(self, "abbrev", self.__name__)
src = getattr(self, "struct", None)
if src is not None:
self.__struct = struct.parse(src)
else:
self.__struct = None
self.__methods = {}
self.__members = {}
for cls in self.__mro__:
for k, v in cls.__dict__.iteritems():
if isinstance(v, FunctionType):
self.__methods[k] = Method(v, self)
if isinstance(v, member):
self.__members[k] = v
assert self.__struct is not None
v.register(k, self.__struct)
self.analyze_slots()
def analyze_slots(self):
self.__slots = {}
for s in Slots:
if s.special is not None:
meth = self.__methods.get(s.special)
if meth is not None:
self.__slots[s] = meth
self.__slots[TP_NAME] = '"%s.%s"' % (self.__module, self.__name__)
if self.__doc__:
self.__slots[TP_DOC] = "%s_doc" % self.name
if self.__struct is not None:
self.__slots[TP_BASICSIZE] = "sizeof(%s)" % self.__struct.name
self.__slots[TP_DEALLOC] = "%s_dealloc" % self.name
if self.__methods:
self.__slots[TP_METHODS] = "%s_methods" % self.name
if self.__members:
self.__slots[TP_MEMBERS] = "%s_members" % self.name
def initvars(self):
v = self.__vars = {}
v["TypeName"] = self.__name__
v["CTypeName"] = "Py%s_Type" % self.__name__
v["MethodDefName"] = self.__slots[TP_METHODS]
if self.__doc__:
v["DocstringVar"] = self.__slots[TP_DOC]
v["Docstring"] = cstring(unindent(self.__doc__))
if self.__struct is not None:
v["StructName"] = self.__struct.name
if self.__members:
v["MemberDefName"] = self.__slots[TP_MEMBERS]
def dump_memberdef(self, f):
def p(templ, vars=self.__vars):
print >> f, templ % vars
if not self.__members:
return
p(template.memberdef_start)
for name, slot in sortitems(self.__members):
slot.dump(f)
p(template.memberdef_end)
def dump_slots(self, f):
def p(templ, vars=self.__vars):
print >> f, templ % vars
if self.struct:
p(template.dealloc_func, {"name" : self.__slots[TP_DEALLOC]})
p(template.type_struct_start)
for s in Slots[:-5]: # XXX
val = self.__slots.get(s, s.default)
ntabs = 4 - (4 + len(val)) / 8
line = " %s,%s/* %s */" % (val, "\t" * ntabs, s.name)
print >> f, line
p(template.type_struct_end)
def dump_init(self, f):
def p(templ):
print >> f, templ % self.__vars
p(template.type_init_type)
p(template.module_add_type)
class Type:
__metaclass__ = TypeMetaclass

View File

@@ -0,0 +1,173 @@
"""Functions."""
from framer import template
from framer.util import cstring, unindent
METH_O = "METH_O"
METH_NOARGS = "METH_NOARGS"
METH_VARARGS = "METH_VARARGS"
def parsefmt(fmt):
for c in fmt:
if c == '|':
continue
yield c
class Argument:
def __init__(self, name):
self.name = name
self.ctype = "PyObject *"
self.default = None
def __str__(self):
return "%s%s" % (self.ctype, self.name)
def setfmt(self, code):
self.ctype = self._codes[code]
if self.ctype[-1] != "*":
self.ctype += " "
_codes = {"O": "PyObject *",
"i": "int",
}
def decl(self):
if self.default is None:
return str(self) + ";"
else:
return "%s = %s;" % (self, self.default)
class _ArgumentList(object):
# these instance variables should be initialized by subclasses
ml_meth = None
fmt = None
def __init__(self, args):
self.args = map(Argument, args)
def __len__(self):
return len(self.args)
def __getitem__(self, i):
return self.args[i]
def dump_decls(self, f):
pass
class NoArgs(_ArgumentList):
def __init__(self, args):
assert len(args) == 0
super(NoArgs, self).__init__(args)
self.ml_meth = METH_NOARGS
def c_args(self):
return "PyObject *self"
class OneArg(_ArgumentList):
def __init__(self, args):
assert len(args) == 1
super(OneArg, self).__init__(args)
self.ml_meth = METH_O
def c_args(self):
return "PyObject *self, %s" % self.args[0]
class VarArgs(_ArgumentList):
def __init__(self, args, fmt=None):
super(VarArgs, self).__init__(args)
self.ml_meth = METH_VARARGS
if fmt is not None:
self.fmt = fmt
i = 0
for code in parsefmt(fmt):
self.args[i].setfmt(code)
i += 1
def c_args(self):
return "PyObject *self, PyObject *args"
def targets(self):
return ", ".join(["&%s" % a.name for a in self.args])
def dump_decls(self, f):
for a in self.args:
print >> f, " %s" % a.decl()
def ArgumentList(func, method):
code = func.func_code
args = code.co_varnames[:code.co_argcount]
if method:
args = args[1:]
pyarg = getattr(func, "pyarg", None)
if pyarg is not None:
args = VarArgs(args, pyarg)
if func.func_defaults:
L = list(func.func_defaults)
ndefault = len(L)
i = len(args) - ndefault
while L:
args[i].default = L.pop(0)
return args
else:
if len(args) == 0:
return NoArgs(args)
elif len(args) == 1:
return OneArg(args)
else:
return VarArgs(args)
class Function:
method = False
def __init__(self, func, parent):
self._func = func
self._parent = parent
self.analyze()
self.initvars()
def dump(self, f):
def p(templ, vars=None): # helper function to generate output
if vars is None:
vars = self.vars
print >> f, templ % vars
if self.__doc__:
p(template.docstring)
d = {"name" : self.vars["CName"],
"args" : self.args.c_args(),
}
p(template.funcdef_start, d)
self.args.dump_decls(f)
if self.args.ml_meth == METH_VARARGS:
p(template.varargs)
p(template.funcdef_end)
def analyze(self):
self.__doc__ = self._func.__doc__
self.args = ArgumentList(self._func, self.method)
def initvars(self):
v = self.vars = {}
v["PythonName"] = self._func.__name__
s = v["CName"] = "%s_%s" % (self._parent.name, self._func.__name__)
v["DocstringVar"] = s + "_doc"
v["MethType"] = self.args.ml_meth
if self.__doc__:
v["Docstring"] = cstring(unindent(self.__doc__))
if self.args.fmt is not None:
v["ArgParse"] = self.args.fmt
v["ArgTargets"] = self.args.targets()
class Method(Function):
method = True

View File

@@ -0,0 +1,73 @@
from framer import template
from framer.util import cstring, unindent
T_SHORT = "T_SHORT"
T_INT = "T_INT"
T_LONG = "T_LONG"
T_FLOAT = "T_FLOAT"
T_DOUBLE = "T_DOUBLE"
T_STRING = "T_STRING"
T_OBJECT = "T_OBJECT"
T_CHAR = "T_CHAR"
T_BYTE = "T_BYTE"
T_UBYTE = "T_UBYTE"
T_UINT = "T_UINT"
T_ULONG = "T_ULONG"
T_STRING_INPLACE = "T_STRING_INPLACE"
T_OBJECT_EX = "T_OBJECT_EX"
RO = READONLY = "READONLY"
READ_RESTRICTED = "READ_RESTRICTED"
WRITE_RESTRICTED = "WRITE_RESTRICTED"
RESTRICT = "RESTRICTED"
c2t = {"int" : T_INT,
"unsigned int" : T_UINT,
"long" : T_LONG,
"unsigned long" : T_LONG,
"float" : T_FLOAT,
"double" : T_DOUBLE,
"char *" : T_CHAR,
"PyObject *" : T_OBJECT,
}
class member(object):
def __init__(self, cname=None, type=None, flags=None, doc=None):
self.type = type
self.flags = flags
self.cname = cname
self.doc = doc
self.name = None
self.struct = None
def register(self, name, struct):
self.name = name
self.struct = struct
self.initvars()
def initvars(self):
v = self.vars = {}
v["PythonName"] = self.name
if self.cname is not None:
v["CName"] = self.cname
else:
v["CName"] = self.name
v["Flags"] = self.flags or "0"
v["Type"] = self.get_type()
if self.doc is not None:
v["Docstring"] = cstring(unindent(self.doc))
v["StructName"] = self.struct.name
def get_type(self):
"""Deduce type code from struct specification if not defined"""
if self.type is not None:
return self.type
ctype = self.struct.get_type(self.name)
return c2t[ctype]
def dump(self, f):
if self.doc is None:
print >> f, template.memberdef_def % self.vars
else:
print >> f, template.memberdef_def_doc % self.vars

View File

@@ -0,0 +1,64 @@
"""Descriptions of all the slots in Python's type objects."""
class Slot(object):
def __init__(self, name, cast=None, special=None, default="0"):
self.name = name
self.cast = cast
self.special = special
self.default = default
Slots = (Slot("ob_size"),
Slot("tp_name"),
Slot("tp_basicsize"),
Slot("tp_itemsize"),
Slot("tp_dealloc", "destructor"),
Slot("tp_print", "printfunc"),
Slot("tp_getattr", "getattrfunc"),
Slot("tp_setattr", "setattrfunc"),
Slot("tp_compare", "cmpfunc", "__cmp__"),
Slot("tp_repr", "reprfunc", "__repr__"),
Slot("tp_as_number"),
Slot("tp_as_sequence"),
Slot("tp_as_mapping"),
Slot("tp_hash", "hashfunc", "__hash__"),
Slot("tp_call", "ternaryfunc", "__call__"),
Slot("tp_str", "reprfunc", "__str__"),
Slot("tp_getattro", "getattrofunc", "__getattr__", # XXX
"PyObject_GenericGetAttr"),
Slot("tp_setattro", "setattrofunc", "__setattr__"),
Slot("tp_as_buffer"),
Slot("tp_flags", default="Py_TPFLAGS_DEFAULT"),
Slot("tp_doc"),
Slot("tp_traverse", "traverseprox"),
Slot("tp_clear", "inquiry"),
Slot("tp_richcompare", "richcmpfunc"),
Slot("tp_weaklistoffset"),
Slot("tp_iter", "getiterfunc", "__iter__"),
Slot("tp_iternext", "iternextfunc", "__next__"), # XXX
Slot("tp_methods"),
Slot("tp_members"),
Slot("tp_getset"),
Slot("tp_base"),
Slot("tp_dict"),
Slot("tp_descr_get", "descrgetfunc"),
Slot("tp_descr_set", "descrsetfunc"),
Slot("tp_dictoffset"),
Slot("tp_init", "initproc", "__init__"),
Slot("tp_alloc", "allocfunc"),
Slot("tp_new", "newfunc"),
Slot("tp_free", "freefunc"),
Slot("tp_is_gc", "inquiry"),
Slot("tp_bases"),
Slot("tp_mro"),
Slot("tp_cache"),
Slot("tp_subclasses"),
Slot("tp_weaklist"),
)
# give some slots symbolic names
TP_NAME = Slots[1]
TP_BASICSIZE = Slots[2]
TP_DEALLOC = Slots[4]
TP_DOC = Slots[20]
TP_METHODS = Slots[27]
TP_MEMBERS = Slots[28]

View File

@@ -0,0 +1,52 @@
"""Rudimentary parser for C struct definitions."""
import re
PyObject_HEAD = "PyObject_HEAD"
PyObject_VAR_HEAD = "PyObject_VAR_HEAD"
rx_name = re.compile("} (\w+);")
class Struct:
def __init__(self, name, head, members):
self.name = name
self.head = head
self.members = members
def get_type(self, name):
for _name, type in self.members:
if name == _name:
return type
raise ValueError, "no member named %s" % name
def parse(s):
"""Parse a C struct definition.
The parser is very restricted in what it will accept.
"""
lines = filter(None, s.split("\n")) # get non-empty lines
assert lines[0].strip() == "typedef struct {"
pyhead = lines[1].strip()
assert (pyhead.startswith("PyObject") and
pyhead.endswith("HEAD"))
members = []
for line in lines[2:]:
line = line.strip()
if line.startswith("}"):
break
assert line.endswith(";")
line = line[:-1]
words = line.split()
name = words[-1]
type = " ".join(words[:-1])
if name[0] == "*":
name = name[1:]
type += " *"
members.append((name, type))
name = None
mo = rx_name.search(line)
assert mo is not None
name = mo.group(1)
return Struct(name, pyhead, members)

View File

@@ -0,0 +1,46 @@
"""Rudimentary parser for C struct definitions."""
import re
PyObject_HEAD = "PyObject_HEAD"
PyObject_VAR_HEAD = "PyObject_VAR_HEAD"
rx_name = re.compile("} (\w+);")
class Struct:
def __init__(self, name, head, members):
self.name = name
self.head = head
self.members = members
def parse(s):
"""Parse a C struct definition.
The parser is very restricted in what it will accept.
"""
lines = filter(None, s.split("\n")) # get non-empty lines
assert lines[0].strip() == "typedef struct {"
pyhead = lines[1].strip()
assert (pyhead.startswith("PyObject") and
pyhead.endswith("HEAD"))
members = []
for line in lines[2:]:
line = line.strip()
if line.startswith("}"):
break
assert line.endswith(";")
line = line[:-1]
words = line.split()
name = words[-1]
type = " ".join(words[:-1])
if name[0] == "*":
name = name[1:]
type += " *"
members.append((name, type))
name = None
mo = rx_name.search(line)
assert mo is not None
name = mo.group(1)
return Struct(name, pyhead, members)

View File

@@ -0,0 +1,102 @@
"""framer's C code templates.
Templates use the following variables:
FileName: name of the file that contains the C source code
ModuleName: name of the module, as in "import ModuleName"
ModuleDocstring: C string containing the module doc string
"""
module_start = '#include "Python.h"'
member_include = '#include "structmember.h"'
module_doc = """\
PyDoc_STRVAR(%(ModuleName)s_doc,
%(ModuleDocstring)s);
"""
methoddef_start = """\
static struct PyMethodDef %(MethodDefName)s[] = {"""
methoddef_def = """\
{"%(PythonName)s", (PyCFunction)%(CName)s, %(MethType)s},"""
methoddef_def_doc = """\
{"%(PythonName)s", (PyCFunction)%(CName)s, %(MethType)s,
%(DocstringVar)s},"""
methoddef_end = """\
{NULL, NULL}
};
"""
memberdef_start = """\
#define OFF(X) offsetof(%(StructName)s, X)
static struct PyMemberDef %(MemberDefName)s[] = {"""
memberdef_def_doc = """\
{"%(PythonName)s", %(Type)s, OFF(%(CName)s), %(Flags)s,
%(Docstring)s},"""
memberdef_def = """\
{"%(PythonName)s", %(Type)s, OFF(%(CName)s), %(Flags)s},"""
memberdef_end = """\
{NULL}
};
#undef OFF
"""
dealloc_func = """static void
%(name)s(PyObject *ob)
{
}
"""
docstring = """\
PyDoc_STRVAR(%(DocstringVar)s,
%(Docstring)s);
"""
funcdef_start = """\
static PyObject *
%(name)s(%(args)s)
{"""
funcdef_end = """\
}
"""
varargs = """\
if (!PyArg_ParseTuple(args, \"%(ArgParse)s:%(PythonName)s\",
%(ArgTargets)s))
return NULL;"""
module_init_start = """\
PyMODINIT_FUNC
init%(ModuleName)s(void)
{
PyObject *mod;
mod = Py_InitModule3("%(ModuleName)s", %(MethodDefName)s,
%(ModuleName)s_doc);
if (mod == NULL)
return;
"""
type_init_type = " %(CTypeName)s.ob_type = &PyType_Type;"
module_add_type = """\
if (!PyObject_SetAttrString(mod, "%(TypeName)s",
(PyObject *)&%(CTypeName)s))
return;
"""
type_struct_start = """\
static PyTypeObject %(CTypeName)s = {
PyObject_HEAD_INIT(0)"""
type_struct_end = """\
};
"""

View File

@@ -0,0 +1,35 @@
def cstring(s, width=70):
"""Return C string representation of a Python string.
width specifies the maximum width of any line of the C string.
"""
L = []
for l in s.split("\n"):
if len(l) < width:
L.append(r'"%s\n"' % l)
return "\n".join(L)
def unindent(s, skipfirst=True):
"""Return an unindented version of a docstring.
Removes indentation on lines following the first one, using the
leading whitespace of the first indented line that is not blank
to determine the indentation.
"""
lines = s.split("\n")
if skipfirst:
first = lines.pop(0)
L = [first]
else:
L = []
indent = None
for l in lines:
ls = l.strip()
if ls:
indent = len(l) - len(ls)
break
L += [l[indent:] for l in lines]
return "\n".join(L)

View File

@@ -0,0 +1,296 @@
THE FREEZE SCRIPT
=================
(Directions for Windows are at the end of this file.)
What is Freeze?
---------------
Freeze make it possible to ship arbitrary Python programs to people
who don't have Python. The shipped file (called a "frozen" version of
your Python program) is an executable, so this only works if your
platform is compatible with that on the receiving end (this is usually
a matter of having the same major operating system revision and CPU
type).
The shipped file contains a Python interpreter and large portions of
the Python run-time. Some measures have been taken to avoid linking
unneeded modules, but the resulting binary is usually not small.
The Python source code of your program (and of the library modules
written in Python that it uses) is not included in the binary --
instead, the compiled byte-code (the instruction stream used
internally by the interpreter) is incorporated. This gives some
protection of your Python source code, though not much -- a
disassembler for Python byte-code is available in the standard Python
library. At least someone running "strings" on your binary won't see
the source.
How does Freeze know which modules to include?
----------------------------------------------
Previous versions of Freeze used a pretty simple-minded algorithm to
find the modules that your program uses, essentially searching for
lines starting with the word "import". It was pretty easy to trick it
into making mistakes, either missing valid import statements, or
mistaking string literals (e.g. doc strings) for import statements.
This has been remedied: Freeze now uses the regular Python parser to
parse the program (and all its modules) and scans the generated byte
code for IMPORT instructions. It may still be confused -- it will not
know about calls to the __import__ built-in function, or about import
statements constructed on the fly and executed using the 'exec'
statement, and it will consider import statements even when they are
unreachable (e.g. "if 0: import foobar").
This new version of Freeze also knows about Python's new package
import mechanism, and uses exactly the same rules to find imported
modules and packages. One exception: if you write 'from package
import *', Python will look into the __all__ variable of the package
to determine which modules are to be imported, while Freeze will do a
directory listing.
One tricky issue: Freeze assumes that the Python interpreter and
environment you're using to run Freeze is the same one that would be
used to run your program, which should also be the same whose sources
and installed files you will learn about in the next section. In
particular, your PYTHONPATH setting should be the same as for running
your program locally. (Tip: if the program doesn't run when you type
"python hello.py" there's little chance of getting the frozen version
to run.)
How do I use Freeze?
--------------------
Normally, you should be able to use it as follows:
python freeze.py hello.py
where hello.py is your program and freeze.py is the main file of
Freeze (in actuality, you'll probably specify an absolute pathname
such as /usr/joe/python/Tools/freeze/freeze.py).
What do I do next?
------------------
Freeze creates a number of files: frozen.c, config.c and Makefile,
plus one file for each Python module that gets included named
M_<module>.c. To produce the frozen version of your program, you can
simply type "make". This should produce a binary file. If the
filename argument to Freeze was "hello.py", the binary will be called
"hello".
Note: you can use the -o option to freeze to specify an alternative
directory where these files are created. This makes it easier to
clean up after you've shipped the frozen binary. You should invoke
"make" in the given directory.
Freezing Tkinter programs
-------------------------
Unfortunately, it is currently not possible to freeze programs that
use Tkinter without a Tcl/Tk installation. The best way to ship a
frozen Tkinter program is to decide in advance where you are going
to place the Tcl and Tk library files in the distributed setup, and
then declare these directories in your frozen Python program using
the TCL_LIBRARY, TK_LIBRARY and TIX_LIBRARY environment variables.
For example, assume you will ship your frozen program in the directory
<root>/bin/windows-x86 and will place your Tcl library files
in <root>/lib/tcl8.2 and your Tk library files in <root>/lib/tk8.2. Then
placing the following lines in your frozen Python script before importing
Tkinter or Tix would set the environment correctly for Tcl/Tk/Tix:
import os
import os.path
RootDir = os.path.dirname(os.path.dirname(os.getcwd()))
import sys
if sys.platform == "win32":
sys.path = ['', '..\\..\\lib\\python-2.0']
os.environ['TCL_LIBRARY'] = RootDir + '\\lib\\tcl8.2'
os.environ['TK_LIBRARY'] = RootDir + '\\lib\\tk8.2'
os.environ['TIX_LIBRARY'] = RootDir + '\\lib\\tix8.1'
elif sys.platform == "linux2":
sys.path = ['', '../../lib/python-2.0']
os.environ['TCL_LIBRARY'] = RootDir + '/lib/tcl8.2'
os.environ['TK_LIBRARY'] = RootDir + '/lib/tk8.2'
os.environ['TIX_LIBRARY'] = RootDir + '/lib/tix8.1'
elif sys.platform == "solaris":
sys.path = ['', '../../lib/python-2.0']
os.environ['TCL_LIBRARY'] = RootDir + '/lib/tcl8.2'
os.environ['TK_LIBRARY'] = RootDir + '/lib/tk8.2'
os.environ['TIX_LIBRARY'] = RootDir + '/lib/tix8.1'
This also adds <root>/lib/python-2.0 to your Python path
for any Python files such as _tkinter.pyd you may need.
Note that the dynamic libraries (such as tcl82.dll tk82.dll python20.dll
under Windows, or libtcl8.2.so and libtcl8.2.so under Unix) are required
at program load time, and are searched by the operating system loader
before Python can be started. Under Windows, the environment
variable PATH is consulted, and under Unix, it may be the
environment variable LD_LIBRARY_PATH and/or the system
shared library cache (ld.so). An additional preferred directory for
finding the dynamic libraries is built into the .dll or .so files at
compile time - see the LIB_RUNTIME_DIR variable in the Tcl makefile.
The OS must find the dynamic libraries or your frozen program won't start.
Usually I make sure that the .so or .dll files are in the same directory
as the executable, but this may not be foolproof.
A workaround to installing your Tcl library files with your frozen
executable would be possible, in which the Tcl/Tk library files are
incorporated in a frozen Python module as string literals and written
to a temporary location when the program runs; this is currently left
as an exercise for the reader. An easier approach is to freeze the
Tcl/Tk/Tix code into the dynamic libraries using the Tcl ET code,
or the Tix Stand-Alone-Module code. Of course, you can also simply
require that Tcl/Tk is required on the target installation, but be
careful that the version corresponds.
There are some caveats using frozen Tkinter applications:
Under Windows if you use the -s windows option, writing
to stdout or stderr is an error.
The Tcl [info nameofexecutable] will be set to where the
program was frozen, not where it is run from.
The global variables argc and argv do not exist.
A warning about shared library modules
--------------------------------------
When your Python installation uses shared library modules such as
_tkinter.pyd, these will not be incorporated in the frozen program.
Again, the frozen program will work when you test it, but it won't
work when you ship it to a site without a Python installation.
Freeze prints a warning when this is the case at the end of the
freezing process:
Warning: unknown modules remain: ...
When this occurs, the best thing to do is usually to rebuild Python
using static linking only. Or use the approach described in the previous
section to declare a library path using sys.path, and place the modules
such as _tkinter.pyd there.
Troubleshooting
---------------
If you have trouble using Freeze for a large program, it's probably
best to start playing with a really simple program first (like the file
hello.py). If you can't get that to work there's something
fundamentally wrong -- perhaps you haven't installed Python. To do a
proper install, you should do "make install" in the Python root
directory.
Usage under Windows 95 or NT
----------------------------
Under Windows 95 or NT, you *must* use the -p option and point it to
the top of the Python source tree.
WARNING: the resulting executable is not self-contained; it requires
the Python DLL, currently PYTHON20.DLL (it does not require the
standard library of .py files though). It may also require one or
more extension modules loaded from .DLL or .PYD files; the module
names are printed in the warning message about remaining unknown
modules.
The driver script generates a Makefile that works with the Microsoft
command line C compiler (CL). To compile, run "nmake"; this will
build a target "hello.exe" if the source was "hello.py". Only the
files frozenmain.c and frozen.c are used; no config.c is generated or
used, since the standard DLL is used.
In order for this to work, you must have built Python using the VC++
(Developer Studio) 5.0 compiler. The provided project builds
python20.lib in the subdirectory pcbuild\Release of thje Python source
tree, and this is where the generated Makefile expects it to be. If
this is not the case, you can edit the Makefile or (probably better)
winmakemakefile.py (e.g., if you are using the 4.2 compiler, the
python20.lib file is generated in the subdirectory vc40 of the Python
source tree).
It is possible to create frozen programs that don't have a console
window, by specifying the option '-s windows'. See the Usage below.
Usage
-----
Here is a list of all of the options (taken from freeze.__doc__):
usage: freeze [options...] script [module]...
Options:
-p prefix: This is the prefix used when you ran ``make install''
in the Python build directory.
(If you never ran this, freeze won't work.)
The default is whatever sys.prefix evaluates to.
It can also be the top directory of the Python source
tree; then -P must point to the build tree.
-P exec_prefix: Like -p but this is the 'exec_prefix', used to
install objects etc. The default is whatever sys.exec_prefix
evaluates to, or the -p argument if given.
If -p points to the Python source tree, -P must point
to the build tree, if different.
-e extension: A directory containing additional .o files that
may be used to resolve modules. This directory
should also have a Setup file describing the .o files.
On Windows, the name of a .INI file describing one
or more extensions is passed.
More than one -e option may be given.
-o dir: Directory where the output files are created; default '.'.
-m: Additional arguments are module names instead of filenames.
-a package=dir: Additional directories to be added to the package's
__path__. Used to simulate directories added by the
package at runtime (eg, by OpenGL and win32com).
More than one -a option may be given for each package.
-l file: Pass the file to the linker (windows only)
-d: Debugging mode for the module finder.
-q: Make the module finder totally quiet.
-h: Print this help message.
-x module Exclude the specified module.
-i filename: Include a file with additional command line options. Used
to prevent command lines growing beyond the capabilities of
the shell/OS. All arguments specified in filename
are read and the -i option replaced with the parsed
params (note - quoting args in this file is NOT supported)
-s subsystem: Specify the subsystem (For Windows only.);
'console' (default), 'windows', 'service' or 'com_dll'
-w: Toggle Windows (NT or 95) behavior.
(For debugging only -- on a win32 platform, win32 behavior
is automatic.)
Arguments:
script: The Python script to be executed by the resulting binary.
module ...: Additional Python modules (referenced by pathname)
that will be included in the resulting binary. These
may be .py or .pyc files. If -m is specified, these are
module names that are search in the path instead.
--Guido van Rossum (home page: http://www.python.org/~guido/)

View File

@@ -0,0 +1,47 @@
_orig_open = open
class _BkFile:
def __init__(self, file, mode, bufsize):
import os
self.__filename = file
self.__backup = file + '~'
try:
os.unlink(self.__backup)
except os.error:
pass
try:
os.rename(file, self.__backup)
except os.error:
self.__backup = None
self.__file = _orig_open(file, mode, bufsize)
self.closed = self.__file.closed
self.fileno = self.__file.fileno
self.flush = self.__file.flush
self.isatty = self.__file.isatty
self.mode = self.__file.mode
self.name = self.__file.name
self.read = self.__file.read
self.readinto = self.__file.readinto
self.readline = self.__file.readline
self.readlines = self.__file.readlines
self.seek = self.__file.seek
self.softspace = self.__file.softspace
self.tell = self.__file.tell
self.truncate = self.__file.truncate
self.write = self.__file.write
self.writelines = self.__file.writelines
def close(self):
self.__file.close()
if self.__backup is None:
return
import filecmp
if filecmp.cmp(self.__backup, self.__filename, shallow = 0):
import os
os.unlink(self.__filename)
os.rename(self.__backup, self.__filename)
def open(file, mode = 'r', bufsize = -1):
if 'w' not in mode:
return _orig_open(file, mode, bufsize)
return _BkFile(file, mode, bufsize)

View File

@@ -0,0 +1,90 @@
# Check for a module in a set of extension directories.
# An extension directory should contain a Setup file
# and one or more .o files or a lib.a file.
import os
import parsesetup
def checkextensions(unknown, extensions):
files = []
modules = []
edict = {}
for e in extensions:
setup = os.path.join(e, 'Setup')
liba = os.path.join(e, 'lib.a')
if not os.path.isfile(liba):
liba = None
edict[e] = parsesetup.getsetupinfo(setup), liba
for mod in unknown:
for e in extensions:
(mods, vars), liba = edict[e]
if not mods.has_key(mod):
continue
modules.append(mod)
if liba:
# If we find a lib.a, use it, ignore the
# .o files, and use *all* libraries for
# *all* modules in the Setup file
if liba in files:
break
files.append(liba)
for m in mods.keys():
files = files + select(e, mods, vars,
m, 1)
break
files = files + select(e, mods, vars, mod, 0)
break
return files, modules
def select(e, mods, vars, mod, skipofiles):
files = []
for w in mods[mod]:
w = treatword(w)
if not w:
continue
w = expandvars(w, vars)
for w in w.split():
if skipofiles and w[-2:] == '.o':
continue
# Assume $var expands to absolute pathname
if w[0] not in ('-', '$') and w[-2:] in ('.o', '.a'):
w = os.path.join(e, w)
if w[:2] in ('-L', '-R') and w[2:3] != '$':
w = w[:2] + os.path.join(e, w[2:])
files.append(w)
return files
cc_flags = ['-I', '-D', '-U']
cc_exts = ['.c', '.C', '.cc', '.c++']
def treatword(w):
if w[:2] in cc_flags:
return None
if w[:1] == '-':
return w # Assume loader flag
head, tail = os.path.split(w)
base, ext = os.path.splitext(tail)
if ext in cc_exts:
tail = base + '.o'
w = os.path.join(head, tail)
return w
def expandvars(str, vars):
i = 0
while i < len(str):
i = k = str.find('$', i)
if i < 0:
break
i = i+1
var = str[i:i+1]
i = i+1
if var == '(':
j = str.find(')', i)
if j < 0:
break
var = str[i:j]
i = j+1
if vars.has_key(var):
str = str[:k] + vars[var] + str[i:]
i = k
return str

View File

@@ -0,0 +1,188 @@
"""Extension management for Windows.
Under Windows it is unlikely the .obj files are of use, as special compiler options
are needed (primarily to toggle the behavior of "public" symbols.
I dont consider it worth parsing the MSVC makefiles for compiler options. Even if
we get it just right, a specific freeze application may have specific compiler
options anyway (eg, to enable or disable specific functionality)
So my basic strategy is:
* Have some Windows INI files which "describe" one or more extension modules.
(Freeze comes with a default one for all known modules - but you can specify
your own).
* This description can include:
- The MSVC .dsp file for the extension. The .c source file names
are extraced from there.
- Specific compiler/linker options
- Flag to indicate if Unicode compilation is expected.
At the moment the name and location of this INI file is hardcoded,
but an obvious enhancement would be to provide command line options.
"""
import os, sys
try:
import win32api
except ImportError:
win32api = None # User has already been warned
class CExtension:
"""An abstraction of an extension implemented in C/C++
"""
def __init__(self, name, sourceFiles):
self.name = name
# A list of strings defining additional compiler options.
self.sourceFiles = sourceFiles
# A list of special compiler options to be applied to
# all source modules in this extension.
self.compilerOptions = []
# A list of .lib files the final .EXE will need.
self.linkerLibs = []
def GetSourceFiles(self):
return self.sourceFiles
def AddCompilerOption(self, option):
self.compilerOptions.append(option)
def GetCompilerOptions(self):
return self.compilerOptions
def AddLinkerLib(self, lib):
self.linkerLibs.append(lib)
def GetLinkerLibs(self):
return self.linkerLibs
def checkextensions(unknown, extra_inis, prefix):
# Create a table of frozen extensions
defaultMapName = os.path.join( os.path.split(sys.argv[0])[0], "extensions_win32.ini")
if not os.path.isfile(defaultMapName):
sys.stderr.write("WARNING: %s can not be found - standard extensions may not be found\n" % defaultMapName)
else:
# must go on end, so other inis can override.
extra_inis.append(defaultMapName)
ret = []
for mod in unknown:
for ini in extra_inis:
# print "Looking for", mod, "in", win32api.GetFullPathName(ini),"...",
defn = get_extension_defn( mod, ini, prefix )
if defn is not None:
# print "Yay - found it!"
ret.append( defn )
break
# print "Nope!"
else: # For not broken!
sys.stderr.write("No definition of module %s in any specified map file.\n" % (mod))
return ret
def get_extension_defn(moduleName, mapFileName, prefix):
if win32api is None: return None
os.environ['PYTHONPREFIX'] = prefix
dsp = win32api.GetProfileVal(moduleName, "dsp", "", mapFileName)
if dsp=="":
return None
# We allow environment variables in the file name
dsp = win32api.ExpandEnvironmentStrings(dsp)
# If the path to the .DSP file is not absolute, assume it is relative
# to the description file.
if not os.path.isabs(dsp):
dsp = os.path.join( os.path.split(mapFileName)[0], dsp)
# Parse it to extract the source files.
sourceFiles = parse_dsp(dsp)
if sourceFiles is None:
return None
module = CExtension(moduleName, sourceFiles)
# Put the path to the DSP into the environment so entries can reference it.
os.environ['dsp_path'] = os.path.split(dsp)[0]
os.environ['ini_path'] = os.path.split(mapFileName)[0]
cl_options = win32api.GetProfileVal(moduleName, "cl", "", mapFileName)
if cl_options:
module.AddCompilerOption(win32api.ExpandEnvironmentStrings(cl_options))
exclude = win32api.GetProfileVal(moduleName, "exclude", "", mapFileName)
exclude = exclude.split()
if win32api.GetProfileVal(moduleName, "Unicode", 0, mapFileName):
module.AddCompilerOption('/D UNICODE /D _UNICODE')
libs = win32api.GetProfileVal(moduleName, "libs", "", mapFileName).split()
for lib in libs:
module.AddLinkerLib(win32api.ExpandEnvironmentStrings(lib))
for exc in exclude:
if exc in module.sourceFiles:
modules.sourceFiles.remove(exc)
return module
# Given an MSVC DSP file, locate C source files it uses
# returns a list of source files.
def parse_dsp(dsp):
# print "Processing", dsp
# For now, only support
ret = []
dsp_path, dsp_name = os.path.split(dsp)
try:
lines = open(dsp, "r").readlines()
except IOError, msg:
sys.stderr.write("%s: %s\n" % (dsp, msg))
return None
for line in lines:
fields = line.strip().split("=", 2)
if fields[0]=="SOURCE":
if os.path.splitext(fields[1])[1].lower() in ['.cpp', '.c']:
ret.append( win32api.GetFullPathName(os.path.join(dsp_path, fields[1] ) ) )
return ret
def write_extension_table(fname, modules):
fp = open(fname, "w")
try:
fp.write (ext_src_header)
# Write fn protos
for module in modules:
# bit of a hack for .pyd's as part of packages.
name = module.name.split('.')[-1]
fp.write('extern void init%s(void);\n' % (name) )
# Write the table
fp.write (ext_tab_header)
for module in modules:
name = module.name.split('.')[-1]
fp.write('\t{"%s", init%s},\n' % (name, name) )
fp.write (ext_tab_footer)
fp.write(ext_src_footer)
finally:
fp.close()
ext_src_header = """\
#include "Python.h"
"""
ext_tab_header = """\
static struct _inittab extensions[] = {
"""
ext_tab_footer = """\
/* Sentinel */
{0, 0}
};
"""
ext_src_footer = """\
extern DL_IMPORT(int) PyImport_ExtendInittab(struct _inittab *newtab);
int PyInitFrozenExtensions()
{
return PyImport_ExtendInittab(extensions);
}
"""

View File

@@ -0,0 +1,171 @@
; This is a list of modules generally build as .pyd files.
;
; Each section contains enough information about a module for
; freeze to include the module as a static, built-in module
; in a frozen .EXE/.DLL.
; This is all setup for all the win32 extension modules
; released by Mark Hammond.
; You must ensure that the environment variable PYTHONEX is set
; to point to the root win32 extensions directory
; PYTHONPREFIX must point to the Python build root directory
; (the *parent* of PCbuild); normally the freeze script takes
; care of this.
;--------------------------------------------------------------
;
; Standard Python extension modules
;
; Here are some of the standard Python extensions modules.
; If you need others, add them here
[_socket]
dsp=%PYTHONPREFIX%\PCBuild\_socket.dsp
[_sre]
dsp=%PYTHONPREFIX%\PCBuild\_sre.dsp
[unicodedata]
dsp=%PYTHONPREFIX%\PCBuild\unicodedata.dsp
[mmap]
dsp=%PYTHONPREFIX%\PCBuild\mmap.dsp
[winsound]
dsp=%PYTHONPREFIX%\PCBuild\winsound.dsp
libs=winmm.lib
[parser]
dsp=%PYTHONPREFIX%\PCBuild\parser.dsp
[select]
dsp=%PYTHONPREFIX%\PCBuild\select.dsp
[zlib]
dsp=%PYTHONPREFIX%\PCBuild\zlib.dsp
cl=/I %PYTHONPREFIX%\..\zlib-1.1.4 /D _WINDOWS /D WIN32
libs=%PYTHONPREFIX%\..\zlib-1.1.4\zlib.lib /nodefaultlib:libc
[_winreg]
dsp=%PYTHONPREFIX%\PCBuild\winreg.dsp
libs=advapi32.lib
;--------------------------------------------------------------
;
; Win32 Projects.
;
[perfmon]
dsp=%PYTHONEX%\win32\perfmon.dsp
cl=/I %PYTHONEX%\win32\src
Unicode=1
[pywintypes]
dsp=%PYTHONEX%\win32\pywintypes.dsp
cl=/I %PYTHONEX%\win32\src
libs=ole32.lib oleaut32.lib
[win32api]
dsp=%PYTHONEX%\win32\win32api.dsp
cl=/I %PYTHONEX%\win32\src
libs=kernel32.lib user32.lib shell32.lib advapi32.lib
[win32service]
dsp=%PYTHONEX%\win32\win32service.dsp
cl=/I %PYTHONEX%\win32\src
Unicode=1
libs=advapi32.lib
[win32evtlog]
dsp=%PYTHONEX%\win32\win32evtlog.dsp
cl=/I %PYTHONEX%\win32\src
[win32process]
dsp=%PYTHONEX%\win32\win32process.dsp
cl=/I %PYTHONEX%\win32\src
[win32event]
dsp=%PYTHONEX%\win32\win32event.dsp
cl=/I %PYTHONEX%\win32\src
[win32file]
dsp=%PYTHONEX%\win32\win32file.dsp
cl=/I %PYTHONEX%\win32\src
[win32net]
dsp=%PYTHONEX%\win32\win32net.dsp
cl=/I %PYTHONEX%\win32\src
libs=netapi32.lib
[win32pdh]
dsp=%PYTHONEX%\win32\win32pdh.dsp
cl=/I %PYTHONEX%\win32\src
[win32pipe]
dsp=%PYTHONEX%\win32\win32pipe.dsp
cl=/I %PYTHONEX%\win32\src
[win32security]
dsp=%PYTHONEX%\win32\win32security.dsp
cl=/I %PYTHONEX%\win32\src
[win32service]
dsp=%PYTHONEX%\win32\win32service.dsp
cl=/I %PYTHONEX%\win32\src
[win32trace]
dsp=%PYTHONEX%\win32\win32trace.dsp
cl=/I %PYTHONEX%\win32\src
;--------------------------------------------------------------
;
; COM Projects.
;
[pythoncom]
dsp=%PYTHONEX%\com\win32com.dsp
cl=/I %PYTHONEX%\com\win32com\src\include /I %PYTHONEX%\win32\src
libs=uuid.lib
[win32com.axcontrol.axcontrol]
dsp=%PYTHONEX%\com\axcontrol.dsp
cl=/I %PYTHONEX%\win32\src /I %PYTHONEX%\com\win32com\src\include
[win32com.axscript.axscript]
dsp=%PYTHONEX%\com\Active Scripting.dsp
cl=/I %PYTHONEX%\win32\src /I %PYTHONEX%\com\win32com\src\include
[win32com.axdebug.axdebug]
dsp=%PYTHONEX%\com\Active Debugging.dsp
cl=/I %PYTHONEX%\win32\src /I %PYTHONEX%\com\win32com\src\include
[win32com.mapi.mapi]
dsp=%PYTHONEX%\com\mapi.dsp
cl=/I %PYTHONEX%\win32\src /I %PYTHONEX%\com\win32com\src\include
libs=MBLOGON.lib ADDRLKUP.LIB mapi32.lib version.lib
[win32com.mapi.exchange]
dsp=%PYTHONEX%\com\exchange.dsp
cl=/I %PYTHONEX%\win32\src /I %PYTHONEX%\com\win32com\src\include
libs=MBLOGON.lib ADDRLKUP.LIB exchinst.lib EDKCFG.LIB EDKUTILS.LIB EDKMAPI.LIB mapi32.lib version.lib
[win32com.mapi.exchdapi]
dsp=%PYTHONEX%\com\exchdapi.dsp
cl=/I %PYTHONEX%\win32\src /I %PYTHONEX%\com\win32com\src\include
libs=DAPI.LIB
[servicemanager]
dsp=%PYTHONEX%\win32\PythonService EXE.dsp
Unicode = 1
; Pythonwin
[win32ui]
dsp=%PYTHONEX%\Pythonwin\win32ui.dsp
cl=/D _AFXDLL /D FREEZE_WIN32UI /GX /I %PYTHONEX%\win32\src
libs=mfc42.lib

View File

@@ -0,0 +1,497 @@
#! /usr/bin/env python
"""Freeze a Python script into a binary.
usage: freeze [options...] script [module]...
Options:
-p prefix: This is the prefix used when you ran ``make install''
in the Python build directory.
(If you never ran this, freeze won't work.)
The default is whatever sys.prefix evaluates to.
It can also be the top directory of the Python source
tree; then -P must point to the build tree.
-P exec_prefix: Like -p but this is the 'exec_prefix', used to
install objects etc. The default is whatever sys.exec_prefix
evaluates to, or the -p argument if given.
If -p points to the Python source tree, -P must point
to the build tree, if different.
-e extension: A directory containing additional .o files that
may be used to resolve modules. This directory
should also have a Setup file describing the .o files.
On Windows, the name of a .INI file describing one
or more extensions is passed.
More than one -e option may be given.
-o dir: Directory where the output files are created; default '.'.
-m: Additional arguments are module names instead of filenames.
-a package=dir: Additional directories to be added to the package's
__path__. Used to simulate directories added by the
package at runtime (eg, by OpenGL and win32com).
More than one -a option may be given for each package.
-l file: Pass the file to the linker (windows only)
-d: Debugging mode for the module finder.
-q: Make the module finder totally quiet.
-h: Print this help message.
-x module Exclude the specified module. It will still be imported
by the frozen binary if it exists on the host system.
-X module Like -x, except the module can never be imported by
the frozen binary.
-E: Freeze will fail if any modules can't be found (that
were not excluded using -x or -X).
-i filename: Include a file with additional command line options. Used
to prevent command lines growing beyond the capabilities of
the shell/OS. All arguments specified in filename
are read and the -i option replaced with the parsed
params (note - quoting args in this file is NOT supported)
-s subsystem: Specify the subsystem (For Windows only.);
'console' (default), 'windows', 'service' or 'com_dll'
-w: Toggle Windows (NT or 95) behavior.
(For debugging only -- on a win32 platform, win32 behavior
is automatic.)
-r prefix=f: Replace path prefix.
Replace prefix with f in the source path references
contained in the resulting binary.
Arguments:
script: The Python script to be executed by the resulting binary.
module ...: Additional Python modules (referenced by pathname)
that will be included in the resulting binary. These
may be .py or .pyc files. If -m is specified, these are
module names that are search in the path instead.
NOTES:
In order to use freeze successfully, you must have built Python and
installed it ("make install").
The script should not use modules provided only as shared libraries;
if it does, the resulting binary is not self-contained.
"""
# Import standard modules
import modulefinder
import getopt
import os
import sys
# Import the freeze-private modules
import checkextensions
import makeconfig
import makefreeze
import makemakefile
import parsesetup
import bkfile
# Main program
def main():
# overridable context
prefix = None # settable with -p option
exec_prefix = None # settable with -P option
extensions = []
exclude = [] # settable with -x option
addn_link = [] # settable with -l, but only honored under Windows.
path = sys.path[:]
modargs = 0
debug = 1
odir = ''
win = sys.platform[:3] == 'win'
replace_paths = [] # settable with -r option
error_if_any_missing = 0
# default the exclude list for each platform
if win: exclude = exclude + [
'dos', 'dospath', 'mac', 'macpath', 'macfs', 'MACFS', 'posix',
'os2', 'ce', 'riscos', 'riscosenviron', 'riscospath',
]
fail_import = exclude[:]
# output files
frozen_c = 'frozen.c'
config_c = 'config.c'
target = 'a.out' # normally derived from script name
makefile = 'Makefile'
subsystem = 'console'
# parse command line by first replacing any "-i" options with the
# file contents.
pos = 1
while pos < len(sys.argv)-1:
# last option can not be "-i", so this ensures "pos+1" is in range!
if sys.argv[pos] == '-i':
try:
options = open(sys.argv[pos+1]).read().split()
except IOError, why:
usage("File name '%s' specified with the -i option "
"can not be read - %s" % (sys.argv[pos+1], why) )
# Replace the '-i' and the filename with the read params.
sys.argv[pos:pos+2] = options
pos = pos + len(options) - 1 # Skip the name and the included args.
pos = pos + 1
# Now parse the command line with the extras inserted.
try:
opts, args = getopt.getopt(sys.argv[1:], 'r:a:dEe:hmo:p:P:qs:wX:x:l:')
except getopt.error, msg:
usage('getopt error: ' + str(msg))
# proces option arguments
for o, a in opts:
if o == '-h':
print __doc__
return
if o == '-d':
debug = debug + 1
if o == '-e':
extensions.append(a)
if o == '-m':
modargs = 1
if o == '-o':
odir = a
if o == '-p':
prefix = a
if o == '-P':
exec_prefix = a
if o == '-q':
debug = 0
if o == '-w':
win = not win
if o == '-s':
if not win:
usage("-s subsystem option only on Windows")
subsystem = a
if o == '-x':
exclude.append(a)
if o == '-X':
exclude.append(a)
fail_import.append(a)
if o == '-E':
error_if_any_missing = 1
if o == '-l':
addn_link.append(a)
if o == '-a':
apply(modulefinder.AddPackagePath, tuple(a.split("=", 2)))
if o == '-r':
f,r = a.split("=", 2)
replace_paths.append( (f,r) )
# modules that are imported by the Python runtime
implicits = []
for module in ('site', 'warnings',):
if module not in exclude:
implicits.append(module)
# default prefix and exec_prefix
if not exec_prefix:
if prefix:
exec_prefix = prefix
else:
exec_prefix = sys.exec_prefix
if not prefix:
prefix = sys.prefix
# determine whether -p points to the Python source tree
ishome = os.path.exists(os.path.join(prefix, 'Python', 'ceval.c'))
# locations derived from options
version = sys.version[:3]
if win:
extensions_c = 'frozen_extensions.c'
if ishome:
print "(Using Python source directory)"
binlib = exec_prefix
incldir = os.path.join(prefix, 'Include')
config_h_dir = exec_prefix
config_c_in = os.path.join(prefix, 'Modules', 'config.c.in')
frozenmain_c = os.path.join(prefix, 'Python', 'frozenmain.c')
makefile_in = os.path.join(exec_prefix, 'Makefile')
if win:
frozendllmain_c = os.path.join(exec_prefix, 'Pc\\frozen_dllmain.c')
else:
binlib = os.path.join(exec_prefix,
'lib', 'python%s' % version, 'config')
incldir = os.path.join(prefix, 'include', 'python%s' % version)
config_h_dir = os.path.join(exec_prefix, 'include',
'python%s' % version)
config_c_in = os.path.join(binlib, 'config.c.in')
frozenmain_c = os.path.join(binlib, 'frozenmain.c')
makefile_in = os.path.join(binlib, 'Makefile')
frozendllmain_c = os.path.join(binlib, 'frozen_dllmain.c')
supp_sources = []
defines = []
includes = ['-I' + incldir, '-I' + config_h_dir]
# sanity check of directories and files
check_dirs = [prefix, exec_prefix, binlib, incldir]
if not win:
# These are not directories on Windows.
check_dirs = check_dirs + extensions
for dir in check_dirs:
if not os.path.exists(dir):
usage('needed directory %s not found' % dir)
if not os.path.isdir(dir):
usage('%s: not a directory' % dir)
if win:
files = supp_sources + extensions # extensions are files on Windows.
else:
files = [config_c_in, makefile_in] + supp_sources
for file in supp_sources:
if not os.path.exists(file):
usage('needed file %s not found' % file)
if not os.path.isfile(file):
usage('%s: not a plain file' % file)
if not win:
for dir in extensions:
setup = os.path.join(dir, 'Setup')
if not os.path.exists(setup):
usage('needed file %s not found' % setup)
if not os.path.isfile(setup):
usage('%s: not a plain file' % setup)
# check that enough arguments are passed
if not args:
usage('at least one filename argument required')
# check that file arguments exist
for arg in args:
if arg == '-m':
break
# if user specified -m on the command line before _any_
# file names, then nothing should be checked (as the
# very first file should be a module name)
if modargs:
break
if not os.path.exists(arg):
usage('argument %s not found' % arg)
if not os.path.isfile(arg):
usage('%s: not a plain file' % arg)
# process non-option arguments
scriptfile = args[0]
modules = args[1:]
# derive target name from script name
base = os.path.basename(scriptfile)
base, ext = os.path.splitext(base)
if base:
if base != scriptfile:
target = base
else:
target = base + '.bin'
# handle -o option
base_frozen_c = frozen_c
base_config_c = config_c
base_target = target
if odir and not os.path.isdir(odir):
try:
os.mkdir(odir)
print "Created output directory", odir
except os.error, msg:
usage('%s: mkdir failed (%s)' % (odir, str(msg)))
base = ''
if odir:
base = os.path.join(odir, '')
frozen_c = os.path.join(odir, frozen_c)
config_c = os.path.join(odir, config_c)
target = os.path.join(odir, target)
makefile = os.path.join(odir, makefile)
if win: extensions_c = os.path.join(odir, extensions_c)
# Handle special entry point requirements
# (on Windows, some frozen programs do not use __main__, but
# import the module directly. Eg, DLLs, Services, etc
custom_entry_point = None # Currently only used on Windows
python_entry_is_main = 1 # Is the entry point called __main__?
# handle -s option on Windows
if win:
import winmakemakefile
try:
custom_entry_point, python_entry_is_main = \
winmakemakefile.get_custom_entry_point(subsystem)
except ValueError, why:
usage(why)
# Actual work starts here...
# collect all modules of the program
dir = os.path.dirname(scriptfile)
path[0] = dir
mf = modulefinder.ModuleFinder(path, debug, exclude, replace_paths)
if win and subsystem=='service':
# If a Windows service, then add the "built-in" module.
mod = mf.add_module("servicemanager")
mod.__file__="dummy.pyd" # really built-in to the resulting EXE
for mod in implicits:
mf.import_hook(mod)
for mod in modules:
if mod == '-m':
modargs = 1
continue
if modargs:
if mod[-2:] == '.*':
mf.import_hook(mod[:-2], None, ["*"])
else:
mf.import_hook(mod)
else:
mf.load_file(mod)
# Add the main script as either __main__, or the actual module name.
if python_entry_is_main:
mf.run_script(scriptfile)
else:
mf.load_file(scriptfile)
if debug > 0:
mf.report()
print
dict = mf.modules
if error_if_any_missing:
missing = mf.any_missing()
if missing:
sys.exit("There are some missing modules: %r" % missing)
# generate output for frozen modules
files = makefreeze.makefreeze(base, dict, debug, custom_entry_point,
fail_import)
# look for unfrozen modules (builtin and of unknown origin)
builtins = []
unknown = []
mods = dict.keys()
mods.sort()
for mod in mods:
if dict[mod].__code__:
continue
if not dict[mod].__file__:
builtins.append(mod)
else:
unknown.append(mod)
# search for unknown modules in extensions directories (not on Windows)
addfiles = []
frozen_extensions = [] # Windows list of modules.
if unknown or (not win and builtins):
if not win:
addfiles, addmods = \
checkextensions.checkextensions(unknown+builtins,
extensions)
for mod in addmods:
if mod in unknown:
unknown.remove(mod)
builtins.append(mod)
else:
# Do the windows thang...
import checkextensions_win32
# Get a list of CExtension instances, each describing a module
# (including its source files)
frozen_extensions = checkextensions_win32.checkextensions(
unknown, extensions, prefix)
for mod in frozen_extensions:
unknown.remove(mod.name)
# report unknown modules
if unknown:
sys.stderr.write('Warning: unknown modules remain: %s\n' %
' '.join(unknown))
# windows gets different treatment
if win:
# Taking a shortcut here...
import winmakemakefile, checkextensions_win32
checkextensions_win32.write_extension_table(extensions_c,
frozen_extensions)
# Create a module definition for the bootstrap C code.
xtras = [frozenmain_c, os.path.basename(frozen_c),
frozendllmain_c, os.path.basename(extensions_c)] + files
maindefn = checkextensions_win32.CExtension( '__main__', xtras )
frozen_extensions.append( maindefn )
outfp = open(makefile, 'w')
try:
winmakemakefile.makemakefile(outfp,
locals(),
frozen_extensions,
os.path.basename(target))
finally:
outfp.close()
return
# generate config.c and Makefile
builtins.sort()
infp = open(config_c_in)
outfp = bkfile.open(config_c, 'w')
try:
makeconfig.makeconfig(infp, outfp, builtins)
finally:
outfp.close()
infp.close()
cflags = ['$(OPT)']
cppflags = defines + includes
libs = [os.path.join(binlib, 'libpython$(VERSION).a')]
somevars = {}
if os.path.exists(makefile_in):
makevars = parsesetup.getmakevars(makefile_in)
for key in makevars.keys():
somevars[key] = makevars[key]
somevars['CFLAGS'] = ' '.join(cflags) # override
somevars['CPPFLAGS'] = ' '.join(cppflags) # override
files = [base_config_c, base_frozen_c] + \
files + supp_sources + addfiles + libs + \
['$(MODLIBS)', '$(LIBS)', '$(SYSLIBS)']
outfp = bkfile.open(makefile, 'w')
try:
makemakefile.makemakefile(outfp, somevars, files, base_target)
finally:
outfp.close()
# Done!
if odir:
print 'Now run "make" in', odir,
print 'to build the target:', base_target
else:
print 'Now run "make" to build the target:', base_target
# Print usage message and exit
def usage(msg):
sys.stdout = sys.stderr
print "Error:", msg
print "Use ``%s -h'' for help" % sys.argv[0]
sys.exit(2)
main()

View File

@@ -0,0 +1 @@
print 'Hello world...'

View File

@@ -0,0 +1,60 @@
import re
import sys
# Write the config.c file
never = ['marshal', '__main__', '__builtin__', 'sys', 'exceptions', '_warnings']
def makeconfig(infp, outfp, modules, with_ifdef=0):
m1 = re.compile('-- ADDMODULE MARKER 1 --')
m2 = re.compile('-- ADDMODULE MARKER 2 --')
while 1:
line = infp.readline()
if not line: break
outfp.write(line)
if m1 and m1.search(line):
m1 = None
for mod in modules:
if mod in never:
continue
if with_ifdef:
outfp.write("#ifndef init%s\n"%mod)
outfp.write('extern void init%s(void);\n' % mod)
if with_ifdef:
outfp.write("#endif\n")
elif m2 and m2.search(line):
m2 = None
for mod in modules:
if mod in never:
continue
outfp.write('\t{"%s", init%s},\n' %
(mod, mod))
if m1:
sys.stderr.write('MARKER 1 never found\n')
elif m2:
sys.stderr.write('MARKER 2 never found\n')
# Test program.
def test():
if not sys.argv[3:]:
print 'usage: python makeconfig.py config.c.in outputfile',
print 'modulename ...'
sys.exit(2)
if sys.argv[1] == '-':
infp = sys.stdin
else:
infp = open(sys.argv[1])
if sys.argv[2] == '-':
outfp = sys.stdout
else:
outfp = open(sys.argv[2], 'w')
makeconfig(infp, outfp, sys.argv[3:])
if outfp != sys.stdout:
outfp.close()
if infp != sys.stdin:
infp.close()
if __name__ == '__main__':
test()

View File

@@ -0,0 +1,90 @@
import marshal
import bkfile
# Write a file containing frozen code for the modules in the dictionary.
header = """
#include "Python.h"
static struct _frozen _PyImport_FrozenModules[] = {
"""
trailer = """\
{0, 0, 0} /* sentinel */
};
"""
# if __debug__ == 0 (i.e. -O option given), set Py_OptimizeFlag in frozen app.
default_entry_point = """
int
main(int argc, char **argv)
{
extern int Py_FrozenMain(int, char **);
""" + ((not __debug__ and """
Py_OptimizeFlag++;
""") or "") + """
PyImport_FrozenModules = _PyImport_FrozenModules;
return Py_FrozenMain(argc, argv);
}
"""
def makefreeze(base, dict, debug=0, entry_point=None, fail_import=()):
if entry_point is None: entry_point = default_entry_point
done = []
files = []
mods = dict.keys()
mods.sort()
for mod in mods:
m = dict[mod]
mangled = "__".join(mod.split("."))
if m.__code__:
file = 'M_' + mangled + '.c'
outfp = bkfile.open(base + file, 'w')
files.append(file)
if debug:
print "freezing", mod, "..."
str = marshal.dumps(m.__code__)
size = len(str)
if m.__path__:
# Indicate package by negative size
size = -size
done.append((mod, mangled, size))
writecode(outfp, mangled, str)
outfp.close()
if debug:
print "generating table of frozen modules"
outfp = bkfile.open(base + 'frozen.c', 'w')
for mod, mangled, size in done:
outfp.write('extern unsigned char M_%s[];\n' % mangled)
outfp.write(header)
for mod, mangled, size in done:
outfp.write('\t{"%s", M_%s, %d},\n' % (mod, mangled, size))
outfp.write('\n')
# The following modules have a NULL code pointer, indicating
# that the prozen program should not search for them on the host
# system. Importing them will *always* raise an ImportError.
# The zero value size is never used.
for mod in fail_import:
outfp.write('\t{"%s", NULL, 0},\n' % (mod,))
outfp.write(trailer)
outfp.write(entry_point)
outfp.close()
return files
# Write a C initializer for a module containing the frozen python code.
# The array is called M_<mod>.
def writecode(outfp, mod, str):
outfp.write('unsigned char M_%s[] = {' % mod)
for i in range(0, len(str), 16):
outfp.write('\n\t')
for c in str[i:i+16]:
outfp.write('%d,' % ord(c))
outfp.write('\n};\n')
## def writecode(outfp, mod, str):
## outfp.write('unsigned char M_%s[%d] = "%s";\n' % (mod, len(str),
## '\\"'.join(map(lambda s: repr(s)[1:-1], str.split('"')))))

View File

@@ -0,0 +1,29 @@
# Write the actual Makefile.
import os
def makemakefile(outfp, makevars, files, target):
outfp.write("# Makefile generated by freeze.py script\n\n")
keys = makevars.keys()
keys.sort()
for key in keys:
outfp.write("%s=%s\n" % (key, makevars[key]))
outfp.write("\nall: %s\n\n" % target)
deps = []
for i in range(len(files)):
file = files[i]
if file[-2:] == '.c':
base = os.path.basename(file)
dest = base[:-2] + '.o'
outfp.write("%s: %s\n" % (dest, file))
outfp.write("\t$(CC) $(CFLAGS) $(CPPFLAGS) -c %s\n" % file)
files[i] = dest
deps.append(dest)
outfp.write("\n%s: %s\n" % (target, ' '.join(deps)))
outfp.write("\t$(LINKCC) $(LDFLAGS) $(LINKFORSHARED) %s -o %s $(LDLAST)\n" %
(' '.join(files), target))
outfp.write("\nclean:\n\t-rm -f *.o %s\n" % target)

View File

@@ -0,0 +1,112 @@
# Parse Makefiles and Python Setup(.in) files.
import re
# Extract variable definitions from a Makefile.
# Return a dictionary mapping names to values.
# May raise IOError.
makevardef = re.compile('^([a-zA-Z0-9_]+)[ \t]*=(.*)')
def getmakevars(filename):
variables = {}
fp = open(filename)
pendingline = ""
try:
while 1:
line = fp.readline()
if pendingline:
line = pendingline + line
pendingline = ""
if not line:
break
if line.endswith('\\\n'):
pendingline = line[:-2]
matchobj = makevardef.match(line)
if not matchobj:
continue
(name, value) = matchobj.group(1, 2)
# Strip trailing comment
i = value.find('#')
if i >= 0:
value = value[:i]
value = value.strip()
variables[name] = value
finally:
fp.close()
return variables
# Parse a Python Setup(.in) file.
# Return two dictionaries, the first mapping modules to their
# definitions, the second mapping variable names to their values.
# May raise IOError.
setupvardef = re.compile('^([a-zA-Z0-9_]+)=(.*)')
def getsetupinfo(filename):
modules = {}
variables = {}
fp = open(filename)
pendingline = ""
try:
while 1:
line = fp.readline()
if pendingline:
line = pendingline + line
pendingline = ""
if not line:
break
# Strip comments
i = line.find('#')
if i >= 0:
line = line[:i]
if line.endswith('\\\n'):
pendingline = line[:-2]
continue
matchobj = setupvardef.match(line)
if matchobj:
(name, value) = matchobj.group(1, 2)
variables[name] = value.strip()
else:
words = line.split()
if words:
modules[words[0]] = words[1:]
finally:
fp.close()
return modules, variables
# Test the above functions.
def test():
import sys
import os
if not sys.argv[1:]:
print 'usage: python parsesetup.py Makefile*|Setup* ...'
sys.exit(2)
for arg in sys.argv[1:]:
base = os.path.basename(arg)
if base[:8] == 'Makefile':
print 'Make style parsing:', arg
v = getmakevars(arg)
prdict(v)
elif base[:5] == 'Setup':
print 'Setup style parsing:', arg
m, v = getsetupinfo(arg)
prdict(m)
prdict(v)
else:
print arg, 'is neither a Makefile nor a Setup file'
print '(name must begin with "Makefile" or "Setup")'
def prdict(d):
keys = d.keys()
keys.sort()
for key in keys:
value = d[key]
print "%-15s" % key, str(value)
if __name__ == '__main__':
test()

View File

@@ -0,0 +1,119 @@
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252">
<META NAME="Generator" CONTENT="Microsoft Word 97">
<TITLE>win32</TITLE>
<META NAME="Version" CONTENT="8.0.3410">
<META NAME="Date" CONTENT="10/11/96">
<META NAME="Template" CONTENT="D:\Program Files\Microsoft Office\Office\HTML.DOT">
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#ffffff">
<H1>Freeze for Win32</H1>
<P>This document describes how to use Freeze for the Win32 platform. </P>
<P>Freeze itself is a Python tool for creating stand-alone executables from Python source code. This document does not attempt to document freeze itself - only the win32 specific changes.</P>
<H2>Frozen programs under Win32</H2>
<P>Frozen programs under Win32 can (theoretically) freeze any type of program supported by Python on Win32 - At the moment, Console .EXE and NT Service .EXE programs are supported. GUI Python programs and COM .EXE programs are very nearly all ready to go.</P>
<H3>Program Dependencies</H3>
<P>The person freezing the program has control over what external DLLs are required by a frozen program. The following dependencies are supported:</P>
<H4>Minimal frozen programs</H4>
<P>These programs freeze only .py files in your program. All external DLLs are required at run-time. This includes all .pyd/.dll modules used by your program, Python20.dll, and msvcrt.dll. </P>
<P>A small Python program would typically create a .EXE around 300kb.</P>
<H4>Frozen Extension programs</H4>
<B><I><P>Note:</B></I> For Python1.5.1, you must get a patch from Guido to import.c for this to work.</P>
<P>These programs also freeze in the sources from all .pyd and .dll files used at runtime. This means the resulting .EXE is only dependent on Python20.dll and msvcrt.dll.</P>
<P>A small Python program using win32api, win32con and one or 2 other win32 extensions would typically create a .EXE around 400kb.</P>
<H4>Completely frozen programs</H4>
<P>Completely stand-alone programs, as is the default on Unix systems. These are currently not supported, mainly as the size of a decent Python program gets very large. However, by tweaking the existing Unix support, this would not be difficult to do.</P>
<H2>Freezing Extension Modules</H2>
<P>By default, a file in the main "freeze" directory called "extensions_win32.ini" is used to obtain information about frozen extensions. A typical entry is:</P>
<CODE><P>[win32api]</P>
<P>dsp=%PYTHONEX%\win32\win32api.dsp</P>
<P>cl=/I %PYTHONEX%\win32\src</P>
<P>libs=kernel32.lib user32.lib shell32.lib advapi32.lib</P>
</CODE><P>&nbsp;</P>
<P>This entry indicates that the win32api extension module is defined in the MSVC project file "<CODE>%PYTHONEX%\win32\win32api.dsp</CODE>". Note the use of<CODE> </CODE>"<CODE>%PYTHONEX%" </CODE>- most strings are substituted with environment variables. In this case, it is assumed variable PYTHONEX points to the main "Python Extensions" source directory (which is assumed to be in the same structure as the release of the extension sources).</P>
<P>An entry in a .INI file can also control specific compiler options, and also the .lib files necessary to be linked with the application.</P>
<H3>Freezing Extension Module Considerations</H3>
<P>To prevent freezing extension modules, simply exclude that module using the freeze "-x" switch.</P>
<P>Occasionally, it will be necessary to explicitly include dependent modules. For example, many win32 modules are dependent on the "pywintypes" module - for example, the win32api module. In this case, the module may be explicitly included using the freeze "-m" option.</P>
<H3>Freezing win32com and PythonCOM</H3>
<P>PythonCOM.dll can be frozen as long as you are not implementing COM Servers. Ie, you can freeze programs which control other applications, but can not implement programs that are themselves controlled by other applications.</P>
<P>If you use any of the win32com .pyd extensions (ex, axscript, mapi, internet, axcontrol), then you will need to specify an additional "-a" option to point to the win32comext directory. There is an example below.</P>
<P>The use of the "win32com.client.gencache" module is not supported (although could be quite easily??)</P>
<H2>Examples</H2>
<P>Before we start, we must:</P>
<CODE><P>D:\temp\delme&gt;set PYTHONEX=d:\src\pythonex</P>
</CODE><H3>Helloworld.py</H3>
<H4>Source Code</H4><DIR>
<DIR>
<CODE><P>import sys</P>
<P>&nbsp;</P>
<P>print " ".join( ["Hello", "world"] + sys.argv[1:] )</P></DIR>
</DIR>
</CODE><H4>Command Line used</H4><DIR>
<DIR>
<FONT FACE="Courier New" SIZE=2><P>\src\python-1.5.1\tools\freeze\freeze.py helloworld.py</P>
<P>nmake</P></DIR>
</DIR>
</FONT><P>Resulting helloworld.exe: 114,688 bytes.</P>
<H3>Helloworld2.py</H3>
<P>Uses win32api. Demonstrates requirement for pywintypes, and difference between freezing extensions and not.</P>
<H4>Source Code</H4><DIR>
<DIR>
<P>import win32api</P>
<P>print "Hello from", win32api.GetComputerName()</P></DIR>
</DIR>
<H4>Command Line used</H4>
<P>By default, win32api will be frozen in with the .EXE. If you do not provide the "pywintypes" inclusion, then the link step will fail looking for all the pywintypes modules.</P><DIR>
<DIR>
<FONT FACE="Courier New" SIZE=2><P>\src\python-1.5.1\tools\freeze\freeze.py helloworld2.py -m pywintypes</P>
<P>nmake</P></DIR>
</DIR>
</FONT><P>Resulting helloworld2.exe: 167,936 bytes</P>
<P>Simply adding win32con to the mix gives an EXE of size: 352,768 bytes.</P>
<H4>Command Line used</H4>
<P>Using this build, we are dependent at runtime on the win32api.pyd and pywintypes15.dll files.</P><DIR>
<DIR>
<P>\src\python-1.5.1\tools\freeze\freeze.py -x win32api helloworld.py</P></DIR>
</DIR>
<P>Resulting helloworld2.exe: 114,688</P>
<P>Adding win32con to this build results in a size of: 252,928</P>
<H3>Testmapi.py</H3>
<P>Uses MAPI, a PythonCOM extension, and win32api.</P>
<H4>Source Code</H4>
<P>from win32com.mapi import mapi</P>
<P>import win32api</P>
<P>mapi.MAPIInitialize( (mapi.MAPI_INIT_VERSION, 0) )</P>
<P>print "Hello from", win32api.GetComputerName()</P>
<P>mapi.MAPIUninitialize()</P>
<H4>Command Line used</H4>
<P>As it does not import pythoncom or pywintypes itself, they must be specified. As it uses the win32comext directory, -a must be used. If you have built the win32com extensions from sources, then the second -a is required.</P><DIR>
<DIR>
<CODE><P>\src\python-1.5.1\tools\freeze\freeze.py -a win32com=%PYTHONEX%\com\win32comext -a win32com.mapi=%PYTHONEX%\com\build\release testmapi.py -m pywintypes -m pythoncom</P></DIR>
</DIR>
</CODE><P>Resulting testmapi.exe: 352,768 bytes</P>
<H3>PipeTestService.py</H3>
<P>This is a standard Python demo in the Win32 extensions. It can be found in the "win32\demos\service" directory.</P>
<H4>Command Line used</H4>
<P>This will create a native NT Service EXE, dependent only on the main Python20.dll. All other modules are built-in to the final .EXE</P><DIR>
<DIR>
<CODE><P>\src\python-1.5.1\tools\freeze\freeze.py -s service %PYTHONEX%\win32\demos\service\pipeTestService.py</P></DIR>
</DIR>
<P>Resulting pipeTestService.exe: </CODE><FONT FACE="Courier New" SIZE=2>533,504 bytes.</P></FONT></BODY>
</HTML>

View File

@@ -0,0 +1,147 @@
import sys, os
# Template used then the program is a GUI program
WINMAINTEMPLATE = """
#include <windows.h>
int WINAPI WinMain(
HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // pointer to command line
int nCmdShow // show state of window
)
{
extern int Py_FrozenMain(int, char **);
PyImport_FrozenModules = _PyImport_FrozenModules;
return Py_FrozenMain(__argc, __argv);
}
"""
SERVICETEMPLATE = """
extern int PythonService_main(int, char **);
int main( int argc, char **argv)
{
PyImport_FrozenModules = _PyImport_FrozenModules;
return PythonService_main(argc, argv);
}
"""
subsystem_details = {
# -s flag : (C entry point template), (is it __main__?), (is it a DLL?)
'console' : (None, 1, 0),
'windows' : (WINMAINTEMPLATE, 1, 0),
'service' : (SERVICETEMPLATE, 0, 0),
'com_dll' : ("", 0, 1),
}
def get_custom_entry_point(subsystem):
try:
return subsystem_details[subsystem][:2]
except KeyError:
raise ValueError, "The subsystem %s is not known" % subsystem
def makemakefile(outfp, vars, files, target):
save = sys.stdout
try:
sys.stdout = outfp
realwork(vars, files, target)
finally:
sys.stdout = save
def realwork(vars, moddefns, target):
version_suffix = "%r%r" % sys.version_info[:2]
print "# Makefile for Microsoft Visual C++ generated by freeze.py script"
print
print 'target = %s' % target
print 'pythonhome = %s' % vars['prefix']
print
print 'DEBUG=0 # Set to 1 to use the _d versions of Python.'
print '!IF $(DEBUG)'
print 'debug_suffix=_d'
print 'c_debug=/Zi /Od /DDEBUG /D_DEBUG'
print 'l_debug=/DEBUG'
print 'temp_dir=Build\\Debug'
print '!ELSE'
print 'debug_suffix='
print 'c_debug=/Ox'
print 'l_debug='
print 'temp_dir=Build\\Release'
print '!ENDIF'
print
print '# The following line assumes you have built Python using the standard instructions'
print '# Otherwise fix the following line to point to the library.'
print 'pythonlib = "$(pythonhome)/pcbuild/python%s$(debug_suffix).lib"' % version_suffix
print
# We only ever write one "entry point" symbol - either
# "main" or "WinMain". Therefore, there is no need to
# pass a subsystem switch to the linker as it works it
# out all by itself. However, the subsystem _does_ determine
# the file extension and additional linker flags.
target_link_flags = ""
target_ext = ".exe"
if subsystem_details[vars['subsystem']][2]:
target_link_flags = "-dll"
target_ext = ".dll"
print "# As the target uses Python%s.dll, we must use this compiler option!" % version_suffix
print "cdl = /MD"
print
print "all: $(target)$(debug_suffix)%s" % (target_ext)
print
print '$(temp_dir):'
print ' if not exist $(temp_dir)\. mkdir $(temp_dir)'
print
objects = []
libs = ["shell32.lib", "comdlg32.lib", "wsock32.lib", "user32.lib", "oleaut32.lib"]
for moddefn in moddefns:
print "# Module", moddefn.name
for file in moddefn.sourceFiles:
base = os.path.basename(file)
base, ext = os.path.splitext(base)
objects.append(base + ".obj")
print '$(temp_dir)\%s.obj: "%s"' % (base, file)
print "\t@$(CC) -c -nologo /Fo$* $(cdl) $(c_debug) /D BUILD_FREEZE",
print '"-I$(pythonhome)/Include" "-I$(pythonhome)/PC" \\'
print "\t\t$(cflags) $(cdebug) $(cinclude) \\"
extra = moddefn.GetCompilerOptions()
if extra:
print "\t\t%s \\" % (' '.join(extra),)
print '\t\t"%s"' % file
print
# Add .lib files this module needs
for modlib in moddefn.GetLinkerLibs():
if modlib not in libs:
libs.append(modlib)
print "ADDN_LINK_FILES=",
for addn in vars['addn_link']: print '"%s"' % (addn),
print ; print
print "OBJS=",
for obj in objects: print '"$(temp_dir)\%s"' % (obj),
print ; print
print "LIBS=",
for lib in libs: print '"%s"' % (lib),
print ; print
print "$(target)$(debug_suffix)%s: $(temp_dir) $(OBJS)" % (target_ext)
print "\tlink -out:$(target)$(debug_suffix)%s %s" % (target_ext, target_link_flags), "@<<"
print "\t$(OBJS)"
print "\t$(LIBS)"
print "\t$(ADDN_LINK_FILES)"
print "\t$(pythonlib) $(lcustom) $(l_debug)"
print "\t$(resources)"
print "<<"
print
print "clean:"
print "\t-rm -f *.obj"
print "\t-rm -f $(target).exe"

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,73 @@
#!/usr/bin/env python
"""
Convert the X11 locale.alias file into a mapping dictionary suitable
for locale.py.
Written by Marc-Andre Lemburg <mal@genix.com>, 2004-12-10.
"""
import locale
# Location of the alias file
LOCALE_ALIAS = '/usr/share/X11/locale/locale.alias'
def parse(filename):
f = open(filename)
lines = f.read().splitlines()
data = {}
for line in lines:
line = line.strip()
if not line:
continue
if line[:1] == '#':
continue
locale, alias = line.split()
# Strip ':'
if locale[-1] == ':':
locale = locale[:-1]
# Lower-case locale
locale = locale.lower()
# Ignore one letter locale mappings (except for 'c')
if len(locale) == 1 and locale != 'c':
continue
# Normalize encoding, if given
if '.' in locale:
lang, encoding = locale.split('.')[:2]
encoding = encoding.replace('-', '')
encoding = encoding.replace('_', '')
locale = lang + '.' + encoding
if encoding.lower() == 'utf8':
# Ignore UTF-8 mappings - this encoding should be
# available for all locales
continue
data[locale] = alias
return data
def pprint(data):
items = data.items()
items.sort()
for k,v in items:
print ' %-40s%r,' % ('%r:' % k, v)
def print_differences(data, olddata):
items = olddata.items()
items.sort()
for k, v in items:
if not data.has_key(k):
print '# removed %r' % k
elif olddata[k] != data[k]:
print '# updated %r -> %r to %r' % \
(k, olddata[k], data[k])
# Additions are not mentioned
if __name__ == '__main__':
data = locale.locale_alias.copy()
data.update(parse(LOCALE_ALIAS))
print_differences(data, locale.locale_alias)
print
print 'locale_alias = {'
pprint(data)
print '}'

View File

@@ -0,0 +1,226 @@
#! /usr/bin/env python
# -*- coding: iso-8859-1 -*-
# Written by Martin v. L<>wis <loewis@informatik.hu-berlin.de>
"""Generate binary message catalog from textual translation description.
This program converts a textual Uniforum-style message catalog (.po file) into
a binary GNU catalog (.mo file). This is essentially the same function as the
GNU msgfmt program, however, it is a simpler implementation.
Usage: msgfmt.py [OPTIONS] filename.po
Options:
-o file
--output-file=file
Specify the output file to write to. If omitted, output will go to a
file named filename.mo (based off the input file name).
-h
--help
Print this message and exit.
-V
--version
Display version information and exit.
"""
import sys
import os
import getopt
import struct
import array
__version__ = "1.1"
MESSAGES = {}
def usage(code, msg=''):
print >> sys.stderr, __doc__
if msg:
print >> sys.stderr, msg
sys.exit(code)
def add(id, str, fuzzy):
"Add a non-fuzzy translation to the dictionary."
global MESSAGES
if not fuzzy and str:
MESSAGES[id] = str
def generate():
"Return the generated output."
global MESSAGES
keys = MESSAGES.keys()
# the keys are sorted in the .mo file
keys.sort()
offsets = []
ids = strs = ''
for id in keys:
# For each string, we need size and file offset. Each string is NUL
# terminated; the NUL does not count into the size.
offsets.append((len(ids), len(id), len(strs), len(MESSAGES[id])))
ids += id + '\0'
strs += MESSAGES[id] + '\0'
output = ''
# The header is 7 32-bit unsigned integers. We don't use hash tables, so
# the keys start right after the index tables.
# translated string.
keystart = 7*4+16*len(keys)
# and the values start after the keys
valuestart = keystart + len(ids)
koffsets = []
voffsets = []
# The string table first has the list of keys, then the list of values.
# Each entry has first the size of the string, then the file offset.
for o1, l1, o2, l2 in offsets:
koffsets += [l1, o1+keystart]
voffsets += [l2, o2+valuestart]
offsets = koffsets + voffsets
output = struct.pack("Iiiiiii",
0x950412deL, # Magic
0, # Version
len(keys), # # of entries
7*4, # start of key index
7*4+len(keys)*8, # start of value index
0, 0) # size and offset of hash table
output += array.array("i", offsets).tostring()
output += ids
output += strs
return output
def make(filename, outfile):
ID = 1
STR = 2
# Compute .mo name from .po name and arguments
if filename.endswith('.po'):
infile = filename
else:
infile = filename + '.po'
if outfile is None:
outfile = os.path.splitext(infile)[0] + '.mo'
try:
lines = open(infile).readlines()
except IOError, msg:
print >> sys.stderr, msg
sys.exit(1)
section = None
fuzzy = 0
# Parse the catalog
lno = 0
for l in lines:
lno += 1
# If we get a comment line after a msgstr, this is a new entry
if l[0] == '#' and section == STR:
add(msgid, msgstr, fuzzy)
section = None
fuzzy = 0
# Record a fuzzy mark
if l[:2] == '#,' and 'fuzzy' in l:
fuzzy = 1
# Skip comments
if l[0] == '#':
continue
# Now we are in a msgid section, output previous section
if l.startswith('msgid') and not l.startswith('msgid_plural'):
if section == STR:
add(msgid, msgstr, fuzzy)
section = ID
l = l[5:]
msgid = msgstr = ''
is_plural = False
# This is a message with plural forms
elif l.startswith('msgid_plural'):
if section != ID:
print >> sys.stderr, 'msgid_plural not preceeded by msgid on %s:%d' %\
(infile, lno)
sys.exit(1)
l = l[12:]
msgid += '\0' # separator of singular and plural
is_plural = True
# Now we are in a msgstr section
elif l.startswith('msgstr'):
section = STR
if l.startswith('msgstr['):
if not is_plural:
print >> sys.stderr, 'plural without msgid_plural on %s:%d' %\
(infile, lno)
sys.exit(1)
l = l.split(']', 1)[1]
if msgstr:
msgstr += '\0' # Separator of the various plural forms
else:
if is_plural:
print >> sys.stderr, 'indexed msgstr required for plural on %s:%d' %\
(infile, lno)
sys.exit(1)
l = l[6:]
# Skip empty lines
l = l.strip()
if not l:
continue
# XXX: Does this always follow Python escape semantics?
l = eval(l)
if section == ID:
msgid += l
elif section == STR:
msgstr += l
else:
print >> sys.stderr, 'Syntax error on %s:%d' % (infile, lno), \
'before:'
print >> sys.stderr, l
sys.exit(1)
# Add last entry
if section == STR:
add(msgid, msgstr, fuzzy)
# Compute output
output = generate()
try:
open(outfile,"wb").write(output)
except IOError,msg:
print >> sys.stderr, msg
def main():
try:
opts, args = getopt.getopt(sys.argv[1:], 'hVo:',
['help', 'version', 'output-file='])
except getopt.error, msg:
usage(1, msg)
outfile = None
# parse options
for opt, arg in opts:
if opt in ('-h', '--help'):
usage(0)
elif opt in ('-V', '--version'):
print >> sys.stderr, "msgfmt.py", __version__
sys.exit(0)
elif opt in ('-o', '--output-file'):
outfile = arg
# do it
if not args:
print >> sys.stderr, 'No input file given'
print >> sys.stderr, "Try `msgfmt --help' for more information."
return
for filename in args:
make(filename, outfile)
if __name__ == '__main__':
main()

View File

@@ -0,0 +1,669 @@
#! /usr/bin/env python
# -*- coding: iso-8859-1 -*-
# Originally written by Barry Warsaw <barry@zope.com>
#
# Minimally patched to make it even more xgettext compatible
# by Peter Funk <pf@artcom-gmbh.de>
#
# 2002-11-22 J<>rgen Hermann <jh@web.de>
# Added checks that _() only contains string literals, and
# command line args are resolved to module lists, i.e. you
# can now pass a filename, a module or package name, or a
# directory (including globbing chars, important for Win32).
# Made docstring fit in 80 chars wide displays using pydoc.
#
# for selftesting
try:
import fintl
_ = fintl.gettext
except ImportError:
_ = lambda s: s
__doc__ = _("""pygettext -- Python equivalent of xgettext(1)
Many systems (Solaris, Linux, Gnu) provide extensive tools that ease the
internationalization of C programs. Most of these tools are independent of
the programming language and can be used from within Python programs.
Martin von Loewis' work[1] helps considerably in this regard.
There's one problem though; xgettext is the program that scans source code
looking for message strings, but it groks only C (or C++). Python
introduces a few wrinkles, such as dual quoting characters, triple quoted
strings, and raw strings. xgettext understands none of this.
Enter pygettext, which uses Python's standard tokenize module to scan
Python source code, generating .pot files identical to what GNU xgettext[2]
generates for C and C++ code. From there, the standard GNU tools can be
used.
A word about marking Python strings as candidates for translation. GNU
xgettext recognizes the following keywords: gettext, dgettext, dcgettext,
and gettext_noop. But those can be a lot of text to include all over your
code. C and C++ have a trick: they use the C preprocessor. Most
internationalized C source includes a #define for gettext() to _() so that
what has to be written in the source is much less. Thus these are both
translatable strings:
gettext("Translatable String")
_("Translatable String")
Python of course has no preprocessor so this doesn't work so well. Thus,
pygettext searches only for _() by default, but see the -k/--keyword flag
below for how to augment this.
[1] http://www.python.org/workshops/1997-10/proceedings/loewis.html
[2] http://www.gnu.org/software/gettext/gettext.html
NOTE: pygettext attempts to be option and feature compatible with GNU
xgettext where ever possible. However some options are still missing or are
not fully implemented. Also, xgettext's use of command line switches with
option arguments is broken, and in these cases, pygettext just defines
additional switches.
Usage: pygettext [options] inputfile ...
Options:
-a
--extract-all
Extract all strings.
-d name
--default-domain=name
Rename the default output file from messages.pot to name.pot.
-E
--escape
Replace non-ASCII characters with octal escape sequences.
-D
--docstrings
Extract module, class, method, and function docstrings. These do
not need to be wrapped in _() markers, and in fact cannot be for
Python to consider them docstrings. (See also the -X option).
-h
--help
Print this help message and exit.
-k word
--keyword=word
Keywords to look for in addition to the default set, which are:
%(DEFAULTKEYWORDS)s
You can have multiple -k flags on the command line.
-K
--no-default-keywords
Disable the default set of keywords (see above). Any keywords
explicitly added with the -k/--keyword option are still recognized.
--no-location
Do not write filename/lineno location comments.
-n
--add-location
Write filename/lineno location comments indicating where each
extracted string is found in the source. These lines appear before
each msgid. The style of comments is controlled by the -S/--style
option. This is the default.
-o filename
--output=filename
Rename the default output file from messages.pot to filename. If
filename is `-' then the output is sent to standard out.
-p dir
--output-dir=dir
Output files will be placed in directory dir.
-S stylename
--style stylename
Specify which style to use for location comments. Two styles are
supported:
Solaris # File: filename, line: line-number
GNU #: filename:line
The style name is case insensitive. GNU style is the default.
-v
--verbose
Print the names of the files being processed.
-V
--version
Print the version of pygettext and exit.
-w columns
--width=columns
Set width of output to columns.
-x filename
--exclude-file=filename
Specify a file that contains a list of strings that are not be
extracted from the input files. Each string to be excluded must
appear on a line by itself in the file.
-X filename
--no-docstrings=filename
Specify a file that contains a list of files (one per line) that
should not have their docstrings extracted. This is only useful in
conjunction with the -D option above.
If `inputfile' is -, standard input is read.
""")
import os
import imp
import sys
import glob
import time
import getopt
import token
import tokenize
import operator
__version__ = '1.5'
default_keywords = ['_']
DEFAULTKEYWORDS = ', '.join(default_keywords)
EMPTYSTRING = ''
# The normal pot-file header. msgmerge and Emacs's po-mode work better if it's
# there.
pot_header = _('''\
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR ORGANIZATION
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\\n"
"POT-Creation-Date: %(time)s\\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n"
"Language-Team: LANGUAGE <LL@li.org>\\n"
"MIME-Version: 1.0\\n"
"Content-Type: text/plain; charset=CHARSET\\n"
"Content-Transfer-Encoding: ENCODING\\n"
"Generated-By: pygettext.py %(version)s\\n"
''')
def usage(code, msg=''):
print >> sys.stderr, __doc__ % globals()
if msg:
print >> sys.stderr, msg
sys.exit(code)
escapes = []
def make_escapes(pass_iso8859):
global escapes
if pass_iso8859:
# Allow iso-8859 characters to pass through so that e.g. 'msgid
# "H<>he"' would result not result in 'msgid "H\366he"'. Otherwise we
# escape any character outside the 32..126 range.
mod = 128
else:
mod = 256
for i in range(256):
if 32 <= (i % mod) <= 126:
escapes.append(chr(i))
else:
escapes.append("\\%03o" % i)
escapes[ord('\\')] = '\\\\'
escapes[ord('\t')] = '\\t'
escapes[ord('\r')] = '\\r'
escapes[ord('\n')] = '\\n'
escapes[ord('\"')] = '\\"'
def escape(s):
global escapes
s = list(s)
for i in range(len(s)):
s[i] = escapes[ord(s[i])]
return EMPTYSTRING.join(s)
def safe_eval(s):
# unwrap quotes, safely
return eval(s, {'__builtins__':{}}, {})
def normalize(s):
# This converts the various Python string types into a format that is
# appropriate for .po files, namely much closer to C style.
lines = s.split('\n')
if len(lines) == 1:
s = '"' + escape(s) + '"'
else:
if not lines[-1]:
del lines[-1]
lines[-1] = lines[-1] + '\n'
for i in range(len(lines)):
lines[i] = escape(lines[i])
lineterm = '\\n"\n"'
s = '""\n"' + lineterm.join(lines) + '"'
return s
def containsAny(str, set):
"""Check whether 'str' contains ANY of the chars in 'set'"""
return 1 in [c in str for c in set]
def _visit_pyfiles(list, dirname, names):
"""Helper for getFilesForName()."""
# get extension for python source files
if not globals().has_key('_py_ext'):
global _py_ext
_py_ext = [triple[0] for triple in imp.get_suffixes()
if triple[2] == imp.PY_SOURCE][0]
# don't recurse into CVS directories
if 'CVS' in names:
names.remove('CVS')
# add all *.py files to list
list.extend(
[os.path.join(dirname, file) for file in names
if os.path.splitext(file)[1] == _py_ext]
)
def _get_modpkg_path(dotted_name, pathlist=None):
"""Get the filesystem path for a module or a package.
Return the file system path to a file for a module, and to a directory for
a package. Return None if the name is not found, or is a builtin or
extension module.
"""
# split off top-most name
parts = dotted_name.split('.', 1)
if len(parts) > 1:
# we have a dotted path, import top-level package
try:
file, pathname, description = imp.find_module(parts[0], pathlist)
if file: file.close()
except ImportError:
return None
# check if it's indeed a package
if description[2] == imp.PKG_DIRECTORY:
# recursively handle the remaining name parts
pathname = _get_modpkg_path(parts[1], [pathname])
else:
pathname = None
else:
# plain name
try:
file, pathname, description = imp.find_module(
dotted_name, pathlist)
if file:
file.close()
if description[2] not in [imp.PY_SOURCE, imp.PKG_DIRECTORY]:
pathname = None
except ImportError:
pathname = None
return pathname
def getFilesForName(name):
"""Get a list of module files for a filename, a module or package name,
or a directory.
"""
if not os.path.exists(name):
# check for glob chars
if containsAny(name, "*?[]"):
files = glob.glob(name)
list = []
for file in files:
list.extend(getFilesForName(file))
return list
# try to find module or package
name = _get_modpkg_path(name)
if not name:
return []
if os.path.isdir(name):
# find all python files in directory
list = []
os.path.walk(name, _visit_pyfiles, list)
return list
elif os.path.exists(name):
# a single file
return [name]
return []
class TokenEater:
def __init__(self, options):
self.__options = options
self.__messages = {}
self.__state = self.__waiting
self.__data = []
self.__lineno = -1
self.__freshmodule = 1
self.__curfile = None
def __call__(self, ttype, tstring, stup, etup, line):
# dispatch
## import token
## print >> sys.stderr, 'ttype:', token.tok_name[ttype], \
## 'tstring:', tstring
self.__state(ttype, tstring, stup[0])
def __waiting(self, ttype, tstring, lineno):
opts = self.__options
# Do docstring extractions, if enabled
if opts.docstrings and not opts.nodocstrings.get(self.__curfile):
# module docstring?
if self.__freshmodule:
if ttype == tokenize.STRING:
self.__addentry(safe_eval(tstring), lineno, isdocstring=1)
self.__freshmodule = 0
elif ttype not in (tokenize.COMMENT, tokenize.NL):
self.__freshmodule = 0
return
# class docstring?
if ttype == tokenize.NAME and tstring in ('class', 'def'):
self.__state = self.__suiteseen
return
if ttype == tokenize.NAME and tstring in opts.keywords:
self.__state = self.__keywordseen
def __suiteseen(self, ttype, tstring, lineno):
# ignore anything until we see the colon
if ttype == tokenize.OP and tstring == ':':
self.__state = self.__suitedocstring
def __suitedocstring(self, ttype, tstring, lineno):
# ignore any intervening noise
if ttype == tokenize.STRING:
self.__addentry(safe_eval(tstring), lineno, isdocstring=1)
self.__state = self.__waiting
elif ttype not in (tokenize.NEWLINE, tokenize.INDENT,
tokenize.COMMENT):
# there was no class docstring
self.__state = self.__waiting
def __keywordseen(self, ttype, tstring, lineno):
if ttype == tokenize.OP and tstring == '(':
self.__data = []
self.__lineno = lineno
self.__state = self.__openseen
else:
self.__state = self.__waiting
def __openseen(self, ttype, tstring, lineno):
if ttype == tokenize.OP and tstring == ')':
# We've seen the last of the translatable strings. Record the
# line number of the first line of the strings and update the list
# of messages seen. Reset state for the next batch. If there
# were no strings inside _(), then just ignore this entry.
if self.__data:
self.__addentry(EMPTYSTRING.join(self.__data))
self.__state = self.__waiting
elif ttype == tokenize.STRING:
self.__data.append(safe_eval(tstring))
elif ttype not in [tokenize.COMMENT, token.INDENT, token.DEDENT,
token.NEWLINE, tokenize.NL]:
# warn if we see anything else than STRING or whitespace
print >> sys.stderr, _(
'*** %(file)s:%(lineno)s: Seen unexpected token "%(token)s"'
) % {
'token': tstring,
'file': self.__curfile,
'lineno': self.__lineno
}
self.__state = self.__waiting
def __addentry(self, msg, lineno=None, isdocstring=0):
if lineno is None:
lineno = self.__lineno
if not msg in self.__options.toexclude:
entry = (self.__curfile, lineno)
self.__messages.setdefault(msg, {})[entry] = isdocstring
def set_filename(self, filename):
self.__curfile = filename
self.__freshmodule = 1
def write(self, fp):
options = self.__options
timestamp = time.strftime('%Y-%m-%d %H:%M+%Z')
# The time stamp in the header doesn't have the same format as that
# generated by xgettext...
print >> fp, pot_header % {'time': timestamp, 'version': __version__}
# Sort the entries. First sort each particular entry's keys, then
# sort all the entries by their first item.
reverse = {}
for k, v in self.__messages.items():
keys = v.keys()
keys.sort()
reverse.setdefault(tuple(keys), []).append((k, v))
rkeys = reverse.keys()
rkeys.sort()
for rkey in rkeys:
rentries = reverse[rkey]
rentries.sort()
for k, v in rentries:
isdocstring = 0
# If the entry was gleaned out of a docstring, then add a
# comment stating so. This is to aid translators who may wish
# to skip translating some unimportant docstrings.
if reduce(operator.__add__, v.values()):
isdocstring = 1
# k is the message string, v is a dictionary-set of (filename,
# lineno) tuples. We want to sort the entries in v first by
# file name and then by line number.
v = v.keys()
v.sort()
if not options.writelocations:
pass
# location comments are different b/w Solaris and GNU:
elif options.locationstyle == options.SOLARIS:
for filename, lineno in v:
d = {'filename': filename, 'lineno': lineno}
print >>fp, _(
'# File: %(filename)s, line: %(lineno)d') % d
elif options.locationstyle == options.GNU:
# fit as many locations on one line, as long as the
# resulting line length doesn't exceeds 'options.width'
locline = '#:'
for filename, lineno in v:
d = {'filename': filename, 'lineno': lineno}
s = _(' %(filename)s:%(lineno)d') % d
if len(locline) + len(s) <= options.width:
locline = locline + s
else:
print >> fp, locline
locline = "#:" + s
if len(locline) > 2:
print >> fp, locline
if isdocstring:
print >> fp, '#, docstring'
print >> fp, 'msgid', normalize(k)
print >> fp, 'msgstr ""\n'
def main():
global default_keywords
try:
opts, args = getopt.getopt(
sys.argv[1:],
'ad:DEhk:Kno:p:S:Vvw:x:X:',
['extract-all', 'default-domain=', 'escape', 'help',
'keyword=', 'no-default-keywords',
'add-location', 'no-location', 'output=', 'output-dir=',
'style=', 'verbose', 'version', 'width=', 'exclude-file=',
'docstrings', 'no-docstrings',
])
except getopt.error, msg:
usage(1, msg)
# for holding option values
class Options:
# constants
GNU = 1
SOLARIS = 2
# defaults
extractall = 0 # FIXME: currently this option has no effect at all.
escape = 0
keywords = []
outpath = ''
outfile = 'messages.pot'
writelocations = 1
locationstyle = GNU
verbose = 0
width = 78
excludefilename = ''
docstrings = 0
nodocstrings = {}
options = Options()
locations = {'gnu' : options.GNU,
'solaris' : options.SOLARIS,
}
# parse options
for opt, arg in opts:
if opt in ('-h', '--help'):
usage(0)
elif opt in ('-a', '--extract-all'):
options.extractall = 1
elif opt in ('-d', '--default-domain'):
options.outfile = arg + '.pot'
elif opt in ('-E', '--escape'):
options.escape = 1
elif opt in ('-D', '--docstrings'):
options.docstrings = 1
elif opt in ('-k', '--keyword'):
options.keywords.append(arg)
elif opt in ('-K', '--no-default-keywords'):
default_keywords = []
elif opt in ('-n', '--add-location'):
options.writelocations = 1
elif opt in ('--no-location',):
options.writelocations = 0
elif opt in ('-S', '--style'):
options.locationstyle = locations.get(arg.lower())
if options.locationstyle is None:
usage(1, _('Invalid value for --style: %s') % arg)
elif opt in ('-o', '--output'):
options.outfile = arg
elif opt in ('-p', '--output-dir'):
options.outpath = arg
elif opt in ('-v', '--verbose'):
options.verbose = 1
elif opt in ('-V', '--version'):
print _('pygettext.py (xgettext for Python) %s') % __version__
sys.exit(0)
elif opt in ('-w', '--width'):
try:
options.width = int(arg)
except ValueError:
usage(1, _('--width argument must be an integer: %s') % arg)
elif opt in ('-x', '--exclude-file'):
options.excludefilename = arg
elif opt in ('-X', '--no-docstrings'):
fp = open(arg)
try:
while 1:
line = fp.readline()
if not line:
break
options.nodocstrings[line[:-1]] = 1
finally:
fp.close()
# calculate escapes
make_escapes(options.escape)
# calculate all keywords
options.keywords.extend(default_keywords)
# initialize list of strings to exclude
if options.excludefilename:
try:
fp = open(options.excludefilename)
options.toexclude = fp.readlines()
fp.close()
except IOError:
print >> sys.stderr, _(
"Can't read --exclude-file: %s") % options.excludefilename
sys.exit(1)
else:
options.toexclude = []
# resolve args to module lists
expanded = []
for arg in args:
if arg == '-':
expanded.append(arg)
else:
expanded.extend(getFilesForName(arg))
args = expanded
# slurp through all the files
eater = TokenEater(options)
for filename in args:
if filename == '-':
if options.verbose:
print _('Reading standard input')
fp = sys.stdin
closep = 0
else:
if options.verbose:
print _('Working on %s') % filename
fp = open(filename)
closep = 1
try:
eater.set_filename(filename)
try:
tokenize.tokenize(fp.readline, eater)
except tokenize.TokenError, e:
print >> sys.stderr, '%s: %s, line %d, column %d' % (
e[0], filename, e[1][0], e[1][1])
finally:
if closep:
fp.close()
# write the output
if options.outfile == '-':
fp = sys.stdout
closep = 0
else:
if options.outpath:
options.outfile = os.path.join(options.outpath, options.outfile)
fp = open(options.outfile, 'w')
closep = 1
try:
eater.write(fp)
finally:
if closep:
fp.close()
if __name__ == '__main__':
main()
# some more test strings
_(u'a unicode string')
# this one creates a warning
_('*** Seen unexpected token "%(token)s"') % {'token': 'test'}
_('more' 'than' 'one' 'string')

View File

@@ -0,0 +1,539 @@
# -*- coding: utf-8 -*-
# This file should be kept compatible with both Python 2.6 and Python >= 3.0.
import time
import os
import re
import sys
import hashlib
import functools
import itertools
from optparse import OptionParser
out = sys.stdout
TEXT_ENCODING = 'utf8'
NEWLINES = 'lf'
# Compatibility
try:
xrange
except NameError:
xrange = range
def text_open(fn, mode, encoding=None):
try:
return open(fn, mode, encoding=encoding or TEXT_ENCODING)
except TypeError:
return open(fn, mode)
def get_file_sizes():
for s in ['20 KB', '400 KB', '10 MB']:
size, unit = s.split()
size = int(size) * {'KB': 1024, 'MB': 1024 ** 2}[unit]
yield s.replace(' ', ''), size
def get_binary_files():
return ((name + ".bin", size) for name, size in get_file_sizes())
def get_text_files():
return (("%s-%s-%s.txt" % (name, TEXT_ENCODING, NEWLINES), size)
for name, size in get_file_sizes())
def with_open_mode(mode):
def decorate(f):
f.file_open_mode = mode
return f
return decorate
def with_sizes(*sizes):
def decorate(f):
f.file_sizes = sizes
return f
return decorate
# Here begin the tests
@with_open_mode("r")
@with_sizes("medium")
def read_bytewise(f):
""" read one unit at a time """
f.seek(0)
while f.read(1):
pass
@with_open_mode("r")
@with_sizes("medium")
def read_small_chunks(f):
""" read 20 units at a time """
f.seek(0)
while f.read(20):
pass
@with_open_mode("r")
@with_sizes("medium")
def read_big_chunks(f):
""" read 4096 units at a time """
f.seek(0)
while f.read(4096):
pass
@with_open_mode("r")
@with_sizes("small", "medium", "large")
def read_whole_file(f):
""" read whole contents at once """
f.seek(0)
while f.read():
pass
@with_open_mode("rt")
@with_sizes("medium")
def read_lines(f):
""" read one line at a time """
f.seek(0)
for line in f:
pass
@with_open_mode("r")
@with_sizes("medium")
def seek_forward_bytewise(f):
""" seek forward one unit at a time """
f.seek(0, 2)
size = f.tell()
f.seek(0, 0)
for i in xrange(0, size - 1):
f.seek(i, 0)
@with_open_mode("r")
@with_sizes("medium")
def seek_forward_blockwise(f):
""" seek forward 1000 units at a time """
f.seek(0, 2)
size = f.tell()
f.seek(0, 0)
for i in xrange(0, size - 1, 1000):
f.seek(i, 0)
@with_open_mode("rb")
@with_sizes("medium")
def read_seek_bytewise(f):
""" alternate read & seek one unit """
f.seek(0)
while f.read(1):
f.seek(1, 1)
@with_open_mode("rb")
@with_sizes("medium")
def read_seek_blockwise(f):
""" alternate read & seek 1000 units """
f.seek(0)
while f.read(1000):
f.seek(1000, 1)
@with_open_mode("w")
@with_sizes("small")
def write_bytewise(f, source):
""" write one unit at a time """
for i in xrange(0, len(source)):
f.write(source[i:i+1])
@with_open_mode("w")
@with_sizes("medium")
def write_small_chunks(f, source):
""" write 20 units at a time """
for i in xrange(0, len(source), 20):
f.write(source[i:i+20])
@with_open_mode("w")
@with_sizes("medium")
def write_medium_chunks(f, source):
""" write 4096 units at a time """
for i in xrange(0, len(source), 4096):
f.write(source[i:i+4096])
@with_open_mode("w")
@with_sizes("large")
def write_large_chunks(f, source):
""" write 1e6 units at a time """
for i in xrange(0, len(source), 1000000):
f.write(source[i:i+1000000])
@with_open_mode("w+")
@with_sizes("small")
def modify_bytewise(f, source):
""" modify one unit at a time """
f.seek(0)
for i in xrange(0, len(source)):
f.write(source[i:i+1])
@with_open_mode("w+")
@with_sizes("medium")
def modify_small_chunks(f, source):
""" modify 20 units at a time """
f.seek(0)
for i in xrange(0, len(source), 20):
f.write(source[i:i+20])
@with_open_mode("w+")
@with_sizes("medium")
def modify_medium_chunks(f, source):
""" modify 4096 units at a time """
f.seek(0)
for i in xrange(0, len(source), 4096):
f.write(source[i:i+4096])
@with_open_mode("wb+")
@with_sizes("medium")
def modify_seek_forward_bytewise(f, source):
""" alternate write & seek one unit """
f.seek(0)
for i in xrange(0, len(source), 2):
f.write(source[i:i+1])
f.seek(i+2)
@with_open_mode("wb+")
@with_sizes("medium")
def modify_seek_forward_blockwise(f, source):
""" alternate write & seek 1000 units """
f.seek(0)
for i in xrange(0, len(source), 2000):
f.write(source[i:i+1000])
f.seek(i+2000)
# XXX the 2 following tests don't work with py3k's text IO
@with_open_mode("wb+")
@with_sizes("medium")
def read_modify_bytewise(f, source):
""" alternate read & write one unit """
f.seek(0)
for i in xrange(0, len(source), 2):
f.read(1)
f.write(source[i+1:i+2])
@with_open_mode("wb+")
@with_sizes("medium")
def read_modify_blockwise(f, source):
""" alternate read & write 1000 units """
f.seek(0)
for i in xrange(0, len(source), 2000):
f.read(1000)
f.write(source[i+1000:i+2000])
read_tests = [
read_bytewise, read_small_chunks, read_lines, read_big_chunks,
None, read_whole_file, None,
seek_forward_bytewise, seek_forward_blockwise,
read_seek_bytewise, read_seek_blockwise,
]
write_tests = [
write_bytewise, write_small_chunks, write_medium_chunks, write_large_chunks,
]
modify_tests = [
modify_bytewise, modify_small_chunks, modify_medium_chunks,
None,
modify_seek_forward_bytewise, modify_seek_forward_blockwise,
read_modify_bytewise, read_modify_blockwise,
]
def run_during(duration, func):
_t = time.time
n = 0
start = os.times()
start_timestamp = _t()
real_start = start[4] or start_timestamp
while True:
func()
n += 1
if _t() - start_timestamp > duration:
break
end = os.times()
real = (end[4] if start[4] else time.time()) - real_start
return n, real, sum(end[0:2]) - sum(start[0:2])
def warm_cache(filename):
with open(filename, "rb") as f:
f.read()
def run_all_tests(options):
def print_label(filename, func):
name = re.split(r'[-.]', filename)[0]
out.write(
("[%s] %s... "
% (name.center(7), func.__doc__.strip())
).ljust(52))
out.flush()
def print_results(size, n, real, cpu):
bw = n * float(size) / 1024 ** 2 / real
bw = ("%4d MB/s" if bw > 100 else "%.3g MB/s") % bw
out.write(bw.rjust(12) + "\n")
if cpu < 0.90 * real:
out.write(" warning: test above used only %d%% CPU, "
"result may be flawed!\n" % (100.0 * cpu / real))
def run_one_test(name, size, open_func, test_func, *args):
mode = test_func.file_open_mode
print_label(name, test_func)
if "w" not in mode or "+" in mode:
warm_cache(name)
with open_func(name) as f:
n, real, cpu = run_during(1.5, lambda: test_func(f, *args))
print_results(size, n, real, cpu)
def run_test_family(tests, mode_filter, files, open_func, *make_args):
for test_func in tests:
if test_func is None:
out.write("\n")
continue
if mode_filter in test_func.file_open_mode:
continue
for s in test_func.file_sizes:
name, size = files[size_names[s]]
#name += file_ext
args = tuple(f(name, size) for f in make_args)
run_one_test(name, size,
open_func, test_func, *args)
size_names = {
"small": 0,
"medium": 1,
"large": 2,
}
binary_files = list(get_binary_files())
text_files = list(get_text_files())
if "b" in options:
print("Binary unit = one byte")
if "t" in options:
print("Text unit = one character (%s-decoded)" % TEXT_ENCODING)
# Binary reads
if "b" in options and "r" in options:
print("\n** Binary input **\n")
run_test_family(read_tests, "t", binary_files, lambda fn: open(fn, "rb"))
# Text reads
if "t" in options and "r" in options:
print("\n** Text input **\n")
run_test_family(read_tests, "b", text_files, lambda fn: text_open(fn, "r"))
# Binary writes
if "b" in options and "w" in options:
print("\n** Binary append **\n")
def make_test_source(name, size):
with open(name, "rb") as f:
return f.read()
run_test_family(write_tests, "t", binary_files,
lambda fn: open(os.devnull, "wb"), make_test_source)
# Text writes
if "t" in options and "w" in options:
print("\n** Text append **\n")
def make_test_source(name, size):
with text_open(name, "r") as f:
return f.read()
run_test_family(write_tests, "b", text_files,
lambda fn: text_open(os.devnull, "w"), make_test_source)
# Binary overwrites
if "b" in options and "w" in options:
print("\n** Binary overwrite **\n")
def make_test_source(name, size):
with open(name, "rb") as f:
return f.read()
run_test_family(modify_tests, "t", binary_files,
lambda fn: open(fn, "r+b"), make_test_source)
# Text overwrites
if "t" in options and "w" in options:
print("\n** Text overwrite **\n")
def make_test_source(name, size):
with text_open(name, "r") as f:
return f.read()
run_test_family(modify_tests, "b", text_files,
lambda fn: open(fn, "r+"), make_test_source)
def prepare_files():
print("Preparing files...")
# Binary files
for name, size in get_binary_files():
if os.path.isfile(name) and os.path.getsize(name) == size:
continue
with open(name, "wb") as f:
f.write(os.urandom(size))
# Text files
chunk = []
with text_open(__file__, "rU", encoding='utf8') as f:
for line in f:
if line.startswith("# <iobench text chunk marker>"):
break
else:
raise RuntimeError(
"Couldn't find chunk marker in %s !" % __file__)
if NEWLINES == "all":
it = itertools.cycle(["\n", "\r", "\r\n"])
else:
it = itertools.repeat(
{"cr": "\r", "lf": "\n", "crlf": "\r\n"}[NEWLINES])
chunk = "".join(line.replace("\n", next(it)) for line in f)
if isinstance(chunk, bytes):
chunk = chunk.decode('utf8')
chunk = chunk.encode(TEXT_ENCODING)
for name, size in get_text_files():
if os.path.isfile(name) and os.path.getsize(name) == size:
continue
head = chunk * (size // len(chunk))
tail = chunk[:size % len(chunk)]
# Adjust tail to end on a character boundary
while True:
try:
tail.decode(TEXT_ENCODING)
break
except UnicodeDecodeError:
tail = tail[:-1]
with open(name, "wb") as f:
f.write(head)
f.write(tail)
def main():
global TEXT_ENCODING, NEWLINES
usage = "usage: %prog [-h|--help] [options]"
parser = OptionParser(usage=usage)
parser.add_option("-b", "--binary",
action="store_true", dest="binary", default=False,
help="run binary I/O tests")
parser.add_option("-t", "--text",
action="store_true", dest="text", default=False,
help="run text I/O tests")
parser.add_option("-r", "--read",
action="store_true", dest="read", default=False,
help="run read tests")
parser.add_option("-w", "--write",
action="store_true", dest="write", default=False,
help="run write & modify tests")
parser.add_option("-E", "--encoding",
action="store", dest="encoding", default=None,
help="encoding for text tests (default: %s)" % TEXT_ENCODING)
parser.add_option("-N", "--newlines",
action="store", dest="newlines", default='lf',
help="line endings for text tests "
"(one of: {lf (default), cr, crlf, all})")
options, args = parser.parse_args()
if args:
parser.error("unexpected arguments")
NEWLINES = options.newlines.lower()
if NEWLINES not in ('lf', 'cr', 'crlf', 'all'):
parser.error("invalid 'newlines' option: %r" % NEWLINES)
test_options = ""
if options.read:
test_options += "r"
if options.write:
test_options += "w"
elif not options.read:
test_options += "rw"
if options.text:
test_options += "t"
if options.binary:
test_options += "b"
elif not options.text:
test_options += "tb"
if options.encoding:
TEXT_ENCODING = options.encoding
prepare_files()
run_all_tests(test_options)
if __name__ == "__main__":
main()
# -- This part to exercise text reading. Don't change anything! --
# <iobench text chunk marker>
"""
1.
Gáttir allar,
áðr gangi fram,
um skoðask skyli,
um skyggnast skyli,
því at óvíst er at vita,
hvar óvinir
sitja á fleti fyrir.
2.
Gefendr heilir!
Gestr er inn kominn,
hvar skal sitja sjá?
Mjök er bráðr,
sá er á bröndum skal
síns of freista frama.
3.
Elds er þörf,
þeims inn er kominn
ok á kné kalinn;
matar ok váða
er manni þörf,
þeim er hefr um fjall farit.
4.
Vatns er þörf,
þeim er til verðar kemr,
þerru ok þjóðlaðar,
góðs of æðis,
ef sér geta mætti,
orðs ok endrþögu.
5.
Vits er þörf,
þeim er víða ratar;
dælt er heima hvat;
at augabragði verðr,
sá er ekki kann
ok með snotrum sitr.
6.
At hyggjandi sinni
skyli-t maðr hræsinn vera,
heldr gætinn at geði;
þá er horskr ok þögull
kemr heimisgarða til,
sjaldan verðr víti vörum,
því at óbrigðra vin
fær maðr aldregi
en mannvit mikit.
7.
Inn vari gestr,
er til verðar kemr,
þunnu hljóði þegir,
eyrum hlýðir,
en augum skoðar;
svá nýsisk fróðra hverr fyrir.
8.
Hinn er sæll,
er sér of getr
lof ok líknstafi;
ódælla er við þat,
er maðr eiga skal
annars brjóstum í.
"""
"""
C'est revenir tard, je le sens, sur un sujet trop rebattu et déjà presque oublié. Mon état, qui ne me permet plus aucun travail suivi, mon aversion pour le genre polémique, ont causé ma lenteur à écrire et ma répugnance à publier. J'aurais même tout à fait supprimé ces Lettres, ou plutôt je lie les aurais point écrites, s'il n'eût été question que de moi : Mais ma patrie ne m'est pas tellement devenue étrangère que je puisse voir tranquillement opprimer ses citoyens, surtout lorsqu'ils n'ont compromis leurs droits qu'en défendant ma cause. Je serais le dernier des hommes si dans une telle occasion j'écoutais un sentiment qui n'est plus ni douceur ni patience, mais faiblesse et lâcheté, dans celui qu'il empêche de remplir son devoir.
Rien de moins important pour le public, j'en conviens, que la matière de ces lettres. La constitution d'une petite République, le sort d'un petit particulier, l'exposé de quelques injustices, la réfutation de quelques sophismes ; tout cela n'a rien en soi d'assez considérable pour mériter beaucoup de lecteurs : mais si mes sujets sont petits mes objets sont grands, et dignes de l'attention de tout honnête homme. Laissons Genève à sa place, et Rousseau dans sa dépression ; mais la religion, mais la liberté, la justice ! voilà, qui que vous soyez, ce qui n'est pas au-dessous de vous.
Qu'on ne cherche pas même ici dans le style le dédommagement de l'aridité de la matière. Ceux que quelques traits heureux de ma plume ont si fort irrités trouveront de quoi s'apaiser dans ces lettres, L'honneur de défendre un opprimé eût enflammé mon coeur si j'avais parlé pour un autre. Réduit au triste emploi de me défendre moi-même, j'ai dû me borner à raisonner ; m'échauffer eût été m'avilir. J'aurai donc trouvé grâce en ce point devant ceux qui s'imaginent qu'il est essentiel à la vérité d'être dite froidement ; opinion que pourtant j'ai peine à comprendre. Lorsqu'une vive persuasion nous anime, le moyen d'employer un langage glacé ? Quand Archimède tout transporté courait nu dans les rues de Syracuse, en avait-il moins trouvé la vérité parce qu'il se passionnait pour elle ? Tout au contraire, celui qui la sent ne peut s'abstenir de l'adorer ; celui qui demeure froid ne l'a pas vue.
Quoi qu'il en soit, je prie les lecteurs de vouloir bien mettre à part mon beau style, et d'examiner seulement si je raisonne bien ou mal ; car enfin, de cela seul qu'un auteur s'exprime en bons termes, je ne vois pas comment il peut s'ensuivre que cet auteur ne sait ce qu'il dit.
"""

View File

@@ -0,0 +1,25 @@
Packaging Python as a Microsoft Installer Package (MSI)
=======================================================
Using this library, Python can be packaged as a MS-Windows
MSI file. To generate an installer package, you need
a build tree. By default, the build tree root directory
is assumed to be in "../..". This location can be changed
by adding a file config.py; see the beginning of msi.py
for additional customization options.
The packaging process assumes that binaries have been
generated according to the instructions in PCBuild/README.txt,
and that you have either Visual Studio or the Platform SDK
installed. In addition, you need the Python COM extensions,
either from PythonWin, or from ActivePython.
To invoke the script, open a cmd.exe window which has
cabarc.exe in its PATH (e.g. "Visual Studio .NET 2003
Command Prompt"). Then invoke
<path-to-python.exe> msi.py
If everything succeeds, pythonX.Y.Z.msi is generated
in the current directory.

View File

@@ -0,0 +1,44 @@
Additional Conditions for this Windows binary build
---------------------------------------------------
This program is linked with and uses Microsoft Distributable Code,
copyrighted by Microsoft Corporation. The Microsoft Distributable Code
includes the following files:
msvcr90.dll
msvcp90.dll
msvcm90.dll
If you further distribute programs that include the Microsoft
Distributable Code, you must comply with the restrictions on
distribution specified by Microsoft. In particular, you must require
distributors and external end users to agree to terms that protect the
Microsoft Distributable Code at least as much as Microsoft's own
requirements for the Distributable Code. See Microsoft's documentation
(included in its developer tools and on its website at microsoft.com)
for specific details.
Redistribution of the Windows binary build of the Python interpreter
complies with this agreement, provided that you do not:
- alter any copyright, trademark or patent notice in Microsoft's
Distributable Code;
- use Microsoft's trademarks in your programs' names or in a way that
suggests your programs come from or are endorsed by Microsoft;
- distribute Microsoft's Distributable Code to run on a platform other
than Microsoft operating systems, run-time technologies or application
platforms; or
- include Microsoft Distributable Code in malicious, deceptive or
unlawful programs.
These restrictions apply only to the Microsoft Distributable Code as
defined above, not to Python itself or any programs running on the
Python interpreter. The redistribution of the Python interpreter and
libraries is governed by the Python Software License included with this
file, or by other licenses as marked.

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,692 @@
# Microsoft Installer Library
# (C) 2003 Martin v. Loewis
import win32com.client.gencache
import win32com.client
import pythoncom, pywintypes
from win32com.client import constants
import re, string, os, sets, glob, subprocess, sys, _winreg, struct
try:
basestring
except NameError:
basestring = (str, unicode)
# Partially taken from Wine
datasizemask= 0x00ff
type_valid= 0x0100
type_localizable= 0x0200
typemask= 0x0c00
type_long= 0x0000
type_short= 0x0400
type_string= 0x0c00
type_binary= 0x0800
type_nullable= 0x1000
type_key= 0x2000
# XXX temporary, localizable?
knownbits = datasizemask | type_valid | type_localizable | \
typemask | type_nullable | type_key
# Summary Info Property IDs
PID_CODEPAGE=1
PID_TITLE=2
PID_SUBJECT=3
PID_AUTHOR=4
PID_KEYWORDS=5
PID_COMMENTS=6
PID_TEMPLATE=7
PID_LASTAUTHOR=8
PID_REVNUMBER=9
PID_LASTPRINTED=11
PID_CREATE_DTM=12
PID_LASTSAVE_DTM=13
PID_PAGECOUNT=14
PID_WORDCOUNT=15
PID_CHARCOUNT=16
PID_APPNAME=18
PID_SECURITY=19
def reset():
global _directories
_directories = sets.Set()
def EnsureMSI():
win32com.client.gencache.EnsureModule('{000C1092-0000-0000-C000-000000000046}', 1033, 1, 0)
def EnsureMSM():
try:
win32com.client.gencache.EnsureModule('{0ADDA82F-2C26-11D2-AD65-00A0C9AF11A6}', 0, 1, 0)
except pywintypes.com_error:
win32com.client.gencache.EnsureModule('{0ADDA82F-2C26-11D2-AD65-00A0C9AF11A6}', 0, 2, 0)
_Installer=None
def MakeInstaller():
global _Installer
if _Installer is None:
EnsureMSI()
_Installer = win32com.client.Dispatch('WindowsInstaller.Installer',
resultCLSID='{000C1090-0000-0000-C000-000000000046}')
return _Installer
_Merge=None
def MakeMerge2():
global _Merge
if _Merge is None:
EnsureMSM()
_Merge = win32com.client.Dispatch("Msm.Merge2.1")
return _Merge
class Table:
def __init__(self, name):
self.name = name
self.fields = []
def add_field(self, index, name, type):
self.fields.append((index,name,type))
def sql(self):
fields = []
keys = []
self.fields.sort()
fields = [None]*len(self.fields)
for index, name, type in self.fields:
index -= 1
unk = type & ~knownbits
if unk:
print "%s.%s unknown bits %x" % (self.name, name, unk)
size = type & datasizemask
dtype = type & typemask
if dtype == type_string:
if size:
tname="CHAR(%d)" % size
else:
tname="CHAR"
elif dtype == type_short:
assert size==2
tname = "SHORT"
elif dtype == type_long:
assert size==4
tname="LONG"
elif dtype == type_binary:
assert size==0
tname="OBJECT"
else:
tname="unknown"
print "%s.%sunknown integer type %d" % (self.name, name, size)
if type & type_nullable:
flags = ""
else:
flags = " NOT NULL"
if type & type_localizable:
flags += " LOCALIZABLE"
fields[index] = "`%s` %s%s" % (name, tname, flags)
if type & type_key:
keys.append("`%s`" % name)
fields = ", ".join(fields)
keys = ", ".join(keys)
return "CREATE TABLE %s (%s PRIMARY KEY %s)" % (self.name, fields, keys)
def create(self, db):
v = db.OpenView(self.sql())
v.Execute(None)
v.Close()
class Binary:
def __init__(self, fname):
self.name = fname
def __repr__(self):
return 'msilib.Binary(os.path.join(dirname,"%s"))' % self.name
def gen_schema(destpath, schemapath):
d = MakeInstaller()
schema = d.OpenDatabase(schemapath,
win32com.client.constants.msiOpenDatabaseModeReadOnly)
# XXX ORBER BY
v=schema.OpenView("SELECT * FROM _Columns")
curtable=None
tables = []
v.Execute(None)
f = open(destpath, "wt")
f.write("from msilib import Table\n")
while 1:
r=v.Fetch()
if not r:break
name=r.StringData(1)
if curtable != name:
f.write("\n%s = Table('%s')\n" % (name,name))
curtable = name
tables.append(name)
f.write("%s.add_field(%d,'%s',%d)\n" %
(name, r.IntegerData(2), r.StringData(3), r.IntegerData(4)))
v.Close()
f.write("\ntables=[%s]\n\n" % (", ".join(tables)))
# Fill the _Validation table
f.write("_Validation_records = [\n")
v = schema.OpenView("SELECT * FROM _Validation")
v.Execute(None)
while 1:
r = v.Fetch()
if not r:break
# Table, Column, Nullable
f.write("(%s,%s,%s," %
(`r.StringData(1)`, `r.StringData(2)`, `r.StringData(3)`))
def put_int(i):
if r.IsNull(i):f.write("None, ")
else:f.write("%d," % r.IntegerData(i))
def put_str(i):
if r.IsNull(i):f.write("None, ")
else:f.write("%s," % `r.StringData(i)`)
put_int(4) # MinValue
put_int(5) # MaxValue
put_str(6) # KeyTable
put_int(7) # KeyColumn
put_str(8) # Category
put_str(9) # Set
put_str(10)# Description
f.write("),\n")
f.write("]\n\n")
f.close()
def gen_sequence(destpath, msipath):
dir = os.path.dirname(destpath)
d = MakeInstaller()
seqmsi = d.OpenDatabase(msipath,
win32com.client.constants.msiOpenDatabaseModeReadOnly)
v = seqmsi.OpenView("SELECT * FROM _Tables");
v.Execute(None)
f = open(destpath, "w")
print >>f, "import msilib,os;dirname=os.path.dirname(__file__)"
tables = []
while 1:
r = v.Fetch()
if not r:break
table = r.StringData(1)
tables.append(table)
f.write("%s = [\n" % table)
v1 = seqmsi.OpenView("SELECT * FROM `%s`" % table)
v1.Execute(None)
info = v1.ColumnInfo(constants.msiColumnInfoTypes)
while 1:
r = v1.Fetch()
if not r:break
rec = []
for i in range(1,r.FieldCount+1):
if r.IsNull(i):
rec.append(None)
elif info.StringData(i)[0] in "iI":
rec.append(r.IntegerData(i))
elif info.StringData(i)[0] in "slSL":
rec.append(r.StringData(i))
elif info.StringData(i)[0]=="v":
size = r.DataSize(i)
bytes = r.ReadStream(i, size, constants.msiReadStreamBytes)
bytes = bytes.encode("latin-1") # binary data represented "as-is"
if table == "Binary":
fname = rec[0]+".bin"
open(os.path.join(dir,fname),"wb").write(bytes)
rec.append(Binary(fname))
else:
rec.append(bytes)
else:
raise "Unsupported column type", info.StringData(i)
f.write(repr(tuple(rec))+",\n")
v1.Close()
f.write("]\n\n")
v.Close()
f.write("tables=%s\n" % repr(map(str,tables)))
f.close()
class _Unspecified:pass
def change_sequence(seq, action, seqno=_Unspecified, cond = _Unspecified):
"Change the sequence number of an action in a sequence list"
for i in range(len(seq)):
if seq[i][0] == action:
if cond is _Unspecified:
cond = seq[i][1]
if seqno is _Unspecified:
seqno = seq[i][2]
seq[i] = (action, cond, seqno)
return
raise ValueError, "Action not found in sequence"
def add_data(db, table, values):
d = MakeInstaller()
v = db.OpenView("SELECT * FROM `%s`" % table)
count = v.ColumnInfo(0).FieldCount
r = d.CreateRecord(count)
for value in values:
assert len(value) == count, value
for i in range(count):
field = value[i]
if isinstance(field, (int, long)):
r.SetIntegerData(i+1,field)
elif isinstance(field, basestring):
r.SetStringData(i+1,field)
elif field is None:
pass
elif isinstance(field, Binary):
r.SetStream(i+1, field.name)
else:
raise TypeError, "Unsupported type %s" % field.__class__.__name__
v.Modify(win32com.client.constants.msiViewModifyInsert, r)
r.ClearData()
v.Close()
def add_stream(db, name, path):
d = MakeInstaller()
v = db.OpenView("INSERT INTO _Streams (Name, Data) VALUES ('%s', ?)" % name)
r = d.CreateRecord(1)
r.SetStream(1, path)
v.Execute(r)
v.Close()
def init_database(name, schema,
ProductName, ProductCode, ProductVersion,
Manufacturer,
request_uac = False):
try:
os.unlink(name)
except OSError:
pass
ProductCode = ProductCode.upper()
d = MakeInstaller()
# Create the database
db = d.OpenDatabase(name,
win32com.client.constants.msiOpenDatabaseModeCreate)
# Create the tables
for t in schema.tables:
t.create(db)
# Fill the validation table
add_data(db, "_Validation", schema._Validation_records)
# Initialize the summary information, allowing atmost 20 properties
si = db.GetSummaryInformation(20)
si.SetProperty(PID_TITLE, "Installation Database")
si.SetProperty(PID_SUBJECT, ProductName)
si.SetProperty(PID_AUTHOR, Manufacturer)
si.SetProperty(PID_TEMPLATE, msi_type)
si.SetProperty(PID_REVNUMBER, gen_uuid())
if request_uac:
wc = 2 # long file names, compressed, original media
else:
wc = 2 | 8 # +never invoke UAC
si.SetProperty(PID_WORDCOUNT, wc)
si.SetProperty(PID_PAGECOUNT, 200)
si.SetProperty(PID_APPNAME, "Python MSI Library")
# XXX more properties
si.Persist()
add_data(db, "Property", [
("ProductName", ProductName),
("ProductCode", ProductCode),
("ProductVersion", ProductVersion),
("Manufacturer", Manufacturer),
("ProductLanguage", "1033")])
db.Commit()
return db
def add_tables(db, module):
for table in module.tables:
add_data(db, table, getattr(module, table))
def make_id(str):
#str = str.replace(".", "_") # colons are allowed
str = str.replace(" ", "_")
str = str.replace("-", "_")
str = str.replace("+", "_")
if str[0] in string.digits:
str = "_"+str
assert re.match("^[A-Za-z_][A-Za-z0-9_.]*$", str), "FILE"+str
return str
def gen_uuid():
return str(pythoncom.CreateGuid())
class CAB:
def __init__(self, name):
self.name = name
self.file = open(name+".txt", "wt")
self.filenames = sets.Set()
self.index = 0
def gen_id(self, dir, file):
logical = _logical = make_id(file)
pos = 1
while logical in self.filenames:
logical = "%s.%d" % (_logical, pos)
pos += 1
self.filenames.add(logical)
return logical
def append(self, full, file, logical = None):
if os.path.isdir(full):
return
if not logical:
logical = self.gen_id(dir, file)
self.index += 1
if full.find(" ")!=-1:
print >>self.file, '"%s" %s' % (full, logical)
else:
print >>self.file, '%s %s' % (full, logical)
return self.index, logical
def commit(self, db):
self.file.close()
try:
os.unlink(self.name+".cab")
except OSError:
pass
for k, v in [(r"Software\Microsoft\VisualStudio\7.1\Setup\VS", "VS7CommonBinDir"),
(r"Software\Microsoft\VisualStudio\8.0\Setup\VS", "VS7CommonBinDir"),
(r"Software\Microsoft\VisualStudio\9.0\Setup\VS", "VS7CommonBinDir"),
(r"Software\Microsoft\Win32SDK\Directories", "Install Dir"),
]:
try:
key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, k)
dir = _winreg.QueryValueEx(key, v)[0]
_winreg.CloseKey(key)
except (WindowsError, IndexError):
continue
cabarc = os.path.join(dir, r"Bin", "cabarc.exe")
if not os.path.exists(cabarc):
continue
break
else:
print "WARNING: cabarc.exe not found in registry"
cabarc = "cabarc.exe"
cmd = r'"%s" -m lzx:21 n %s.cab @%s.txt' % (cabarc, self.name, self.name)
p = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in p.stdout:
if line.startswith(" -- adding "):
sys.stdout.write(".")
else:
sys.stdout.write(line)
sys.stdout.flush()
if not os.path.exists(self.name+".cab"):
raise IOError, "cabarc failed"
add_data(db, "Media",
[(1, self.index, None, "#"+self.name, None, None)])
add_stream(db, self.name, self.name+".cab")
os.unlink(self.name+".txt")
os.unlink(self.name+".cab")
db.Commit()
_directories = sets.Set()
class Directory:
def __init__(self, db, cab, basedir, physical, _logical, default, componentflags=None):
"""Create a new directory in the Directory table. There is a current component
at each point in time for the directory, which is either explicitly created
through start_component, or implicitly when files are added for the first
time. Files are added into the current component, and into the cab file.
To create a directory, a base directory object needs to be specified (can be
None), the path to the physical directory, and a logical directory name.
Default specifies the DefaultDir slot in the directory table. componentflags
specifies the default flags that new components get."""
index = 1
_logical = make_id(_logical)
logical = _logical
while logical in _directories:
logical = "%s%d" % (_logical, index)
index += 1
_directories.add(logical)
self.db = db
self.cab = cab
self.basedir = basedir
self.physical = physical
self.logical = logical
self.component = None
self.short_names = sets.Set()
self.ids = sets.Set()
self.keyfiles = {}
self.componentflags = componentflags
if basedir:
self.absolute = os.path.join(basedir.absolute, physical)
blogical = basedir.logical
else:
self.absolute = physical
blogical = None
add_data(db, "Directory", [(logical, blogical, default)])
def start_component(self, component = None, feature = None, flags = None, keyfile = None, uuid=None):
"""Add an entry to the Component table, and make this component the current for this
directory. If no component name is given, the directory name is used. If no feature
is given, the current feature is used. If no flags are given, the directory's default
flags are used. If no keyfile is given, the KeyPath is left null in the Component
table."""
if flags is None:
flags = self.componentflags
if uuid is None:
uuid = gen_uuid()
else:
uuid = uuid.upper()
if component is None:
component = self.logical
self.component = component
if Win64:
flags |= 256
if keyfile:
keyid = self.cab.gen_id(self.absolute, keyfile)
self.keyfiles[keyfile] = keyid
else:
keyid = None
add_data(self.db, "Component",
[(component, uuid, self.logical, flags, None, keyid)])
if feature is None:
feature = current_feature
add_data(self.db, "FeatureComponents",
[(feature.id, component)])
def make_short(self, file):
file = re.sub(r'[\?|><:/*"+,;=\[\]]', '_', file) # restrictions on short names
parts = file.split(".")
if len(parts)>1:
suffix = parts[-1].upper()
else:
suffix = None
prefix = parts[0].upper()
if len(prefix) <= 8 and (not suffix or len(suffix)<=3):
if suffix:
file = prefix+"."+suffix
else:
file = prefix
assert file not in self.short_names
else:
prefix = prefix[:6]
if suffix:
suffix = suffix[:3]
pos = 1
while 1:
if suffix:
file = "%s~%d.%s" % (prefix, pos, suffix)
else:
file = "%s~%d" % (prefix, pos)
if file not in self.short_names: break
pos += 1
assert pos < 10000
if pos in (10, 100, 1000):
prefix = prefix[:-1]
self.short_names.add(file)
return file
def add_file(self, file, src=None, version=None, language=None):
"""Add a file to the current component of the directory, starting a new one
one if there is no current component. By default, the file name in the source
and the file table will be identical. If the src file is specified, it is
interpreted relative to the current directory. Optionally, a version and a
language can be specified for the entry in the File table."""
if not self.component:
self.start_component(self.logical, current_feature)
if not src:
# Allow relative paths for file if src is not specified
src = file
file = os.path.basename(file)
absolute = os.path.join(self.absolute, src)
assert not re.search(r'[\?|><:/*]"', file) # restrictions on long names
if self.keyfiles.has_key(file):
logical = self.keyfiles[file]
else:
logical = None
sequence, logical = self.cab.append(absolute, file, logical)
assert logical not in self.ids
self.ids.add(logical)
short = self.make_short(file)
full = "%s|%s" % (short, file)
filesize = os.stat(absolute).st_size
# constants.msidbFileAttributesVital
# Compressed omitted, since it is the database default
# could add r/o, system, hidden
attributes = 512
add_data(self.db, "File",
[(logical, self.component, full, filesize, version,
language, attributes, sequence)])
if not version:
# Add hash if the file is not versioned
filehash = MakeInstaller().FileHash(absolute, 0)
add_data(self.db, "MsiFileHash",
[(logical, 0, filehash.IntegerData(1),
filehash.IntegerData(2), filehash.IntegerData(3),
filehash.IntegerData(4))])
# Automatically remove .pyc/.pyo files on uninstall (2)
# XXX: adding so many RemoveFile entries makes installer unbelievably
# slow. So instead, we have to use wildcard remove entries
# if file.endswith(".py"):
# add_data(self.db, "RemoveFile",
# [(logical+"c", self.component, "%sC|%sc" % (short, file),
# self.logical, 2),
# (logical+"o", self.component, "%sO|%so" % (short, file),
# self.logical, 2)])
def glob(self, pattern, exclude = None):
"""Add a list of files to the current component as specified in the
glob pattern. Individual files can be excluded in the exclude list."""
files = glob.glob1(self.absolute, pattern)
for f in files:
if exclude and f in exclude: continue
self.add_file(f)
return files
def remove_pyc(self):
"Remove .pyc/.pyo files on uninstall"
add_data(self.db, "RemoveFile",
[(self.component+"c", self.component, "*.pyc", self.logical, 2),
(self.component+"o", self.component, "*.pyo", self.logical, 2)])
def removefile(self, key, pattern):
"Add a RemoveFile entry"
add_data(self.db, "RemoveFile", [(self.component+key, self.component, pattern, self.logical, 2)])
class Feature:
def __init__(self, db, id, title, desc, display, level = 1,
parent=None, directory = None, attributes=0):
self.id = id
if parent:
parent = parent.id
add_data(db, "Feature",
[(id, parent, title, desc, display,
level, directory, attributes)])
def set_current(self):
global current_feature
current_feature = self
class Control:
def __init__(self, dlg, name):
self.dlg = dlg
self.name = name
def event(self, ev, arg, cond = "1", order = None):
add_data(self.dlg.db, "ControlEvent",
[(self.dlg.name, self.name, ev, arg, cond, order)])
def mapping(self, ev, attr):
add_data(self.dlg.db, "EventMapping",
[(self.dlg.name, self.name, ev, attr)])
def condition(self, action, condition):
add_data(self.dlg.db, "ControlCondition",
[(self.dlg.name, self.name, action, condition)])
class RadioButtonGroup(Control):
def __init__(self, dlg, name, property):
self.dlg = dlg
self.name = name
self.property = property
self.index = 1
def add(self, name, x, y, w, h, text, value = None):
if value is None:
value = name
add_data(self.dlg.db, "RadioButton",
[(self.property, self.index, value,
x, y, w, h, text, None)])
self.index += 1
class Dialog:
def __init__(self, db, name, x, y, w, h, attr, title, first, default, cancel):
self.db = db
self.name = name
self.x, self.y, self.w, self.h = x,y,w,h
add_data(db, "Dialog", [(name, x,y,w,h,attr,title,first,default,cancel)])
def control(self, name, type, x, y, w, h, attr, prop, text, next, help):
add_data(self.db, "Control",
[(self.name, name, type, x, y, w, h, attr, prop, text, next, help)])
return Control(self, name)
def text(self, name, x, y, w, h, attr, text):
return self.control(name, "Text", x, y, w, h, attr, None,
text, None, None)
def bitmap(self, name, x, y, w, h, text):
return self.control(name, "Bitmap", x, y, w, h, 1, None, text, None, None)
def line(self, name, x, y, w, h):
return self.control(name, "Line", x, y, w, h, 1, None, None, None, None)
def pushbutton(self, name, x, y, w, h, attr, text, next):
return self.control(name, "PushButton", x, y, w, h, attr, None, text, next, None)
def radiogroup(self, name, x, y, w, h, attr, prop, text, next):
add_data(self.db, "Control",
[(self.name, name, "RadioButtonGroup",
x, y, w, h, attr, prop, text, next, None)])
return RadioButtonGroup(self, name, prop)
def checkbox(self, name, x, y, w, h, attr, prop, text, next):
return self.control(name, "CheckBox", x, y, w, h, attr, prop, text, next, None)
def pe_type(path):
header = open(path, "rb").read(1000)
# offset of PE header is at offset 0x3c
pe_offset = struct.unpack("<i", header[0x3c:0x40])[0]
assert header[pe_offset:pe_offset+4] == "PE\0\0"
machine = struct.unpack("<H", header[pe_offset+4:pe_offset+6])[0]
return machine
def set_arch_from_file(path):
global msi_type, Win64, arch_ext
machine = pe_type(path)
if machine == 0x14c:
# i386
msi_type = "Intel"
Win64 = 0
arch_ext = ''
elif machine == 0x200:
# Itanium
msi_type = "Intel64"
Win64 = 1
arch_ext = '.ia64'
elif machine == 0x8664:
# AMD64
msi_type = "x64"
Win64 = 1
arch_ext = '.amd64'
else:
raise ValueError, "Unsupported architecture"
msi_type += ";1033"

View File

@@ -0,0 +1,93 @@
#include "windows.h"
#include "msiquery.h"
/* Print a debug message to the installer log file.
* To see the debug messages, install with
* msiexec /i pythonxy.msi /l*v python.log
*/
static UINT debug(MSIHANDLE hInstall, LPCSTR msg)
{
MSIHANDLE hRec = MsiCreateRecord(1);
if (!hRec || MsiRecordSetStringA(hRec, 1, msg) != ERROR_SUCCESS) {
return ERROR_INSTALL_FAILURE;
}
MsiProcessMessage(hInstall, INSTALLMESSAGE_INFO, hRec);
MsiCloseHandle(hRec);
return ERROR_SUCCESS;
}
/* Check whether the TARGETDIR exists and is a directory.
* Set TargetExists appropriately.
*/
UINT __declspec(dllexport) __stdcall CheckDir(MSIHANDLE hInstall)
{
#define PSIZE 1024
WCHAR wpath[PSIZE];
char path[PSIZE];
UINT result;
DWORD size = PSIZE;
DWORD attributes;
result = MsiGetPropertyW(hInstall, L"TARGETDIR", wpath, &size);
if (result != ERROR_SUCCESS)
return result;
wpath[size] = L'\0';
path[size] = L'\0';
attributes = GetFileAttributesW(wpath);
if (attributes == INVALID_FILE_ATTRIBUTES ||
!(attributes & FILE_ATTRIBUTE_DIRECTORY))
{
return MsiSetPropertyA(hInstall, "TargetExists", "0");
} else {
return MsiSetPropertyA(hInstall, "TargetExists", "1");
}
}
/* Update the state of the REGISTRY.tcl component according to the
* Extension and TclTk features. REGISTRY.tcl must be installed
* if both features are installed, and must be absent otherwise.
*/
UINT __declspec(dllexport) __stdcall UpdateEditIDLE(MSIHANDLE hInstall)
{
INSTALLSTATE ext_old, ext_new, tcl_old, tcl_new, reg_new;
UINT result;
result = MsiGetFeatureStateA(hInstall, "Extensions", &ext_old, &ext_new);
if (result != ERROR_SUCCESS)
return result;
result = MsiGetFeatureStateA(hInstall, "TclTk", &tcl_old, &tcl_new);
if (result != ERROR_SUCCESS)
return result;
/* If the current state is Absent, and the user did not select
the feature in the UI, Installer apparently sets the "selected"
state to unknown. Update it to the current value, then. */
if (ext_new == INSTALLSTATE_UNKNOWN)
ext_new = ext_old;
if (tcl_new == INSTALLSTATE_UNKNOWN)
tcl_new = tcl_old;
// XXX consider current state of REGISTRY.tcl?
if (((tcl_new == INSTALLSTATE_LOCAL) ||
(tcl_new == INSTALLSTATE_SOURCE) ||
(tcl_new == INSTALLSTATE_DEFAULT)) &&
((ext_new == INSTALLSTATE_LOCAL) ||
(ext_new == INSTALLSTATE_SOURCE) ||
(ext_new == INSTALLSTATE_DEFAULT))) {
reg_new = INSTALLSTATE_SOURCE;
} else {
reg_new = INSTALLSTATE_ABSENT;
}
result = MsiSetComponentStateA(hInstall, "REGISTRY.tcl", reg_new);
return result;
}
BOOL APIENTRY DllMain(HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved)
{
return TRUE;
}

View File

@@ -0,0 +1,9 @@
# /OPT: REF and ICF are added by VS.NET by default
msisupport.dll: msisupport.obj
link.exe /OUT:msisupport.dll /INCREMENTAL:NO /NOLOGO /DLL /SUBSYSTEM:WINDOWS /OPT:REF /OPT:ICF msisupport.obj msi.lib kernel32.lib
# We request a static CRT, so that there will be no CRT dependencies
# for the target system. We cannot do without a CRT, since it provides
# the DLL entry point.
msisupport.obj: msisupport.c
cl /O2 /D WIN32 /D NDEBUG /D _WINDOWS /MT /W3 /c msisupport.c

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,126 @@
AdminExecuteSequence = [
(u'InstallInitialize', None, 1500),
(u'InstallFinalize', None, 6600),
(u'InstallFiles', None, 4000),
(u'InstallAdminPackage', None, 3900),
(u'FileCost', None, 900),
(u'CostInitialize', None, 800),
(u'CostFinalize', None, 1000),
(u'InstallValidate', None, 1400),
]
AdminUISequence = [
(u'FileCost', None, 900),
(u'CostInitialize', None, 800),
(u'CostFinalize', None, 1000),
(u'ExecuteAction', None, 1300),
(u'ExitDialog', None, -1),
(u'FatalError', None, -3),
(u'UserExit', None, -2),
]
AdvtExecuteSequence = [
(u'InstallInitialize', None, 1500),
(u'InstallFinalize', None, 6600),
(u'CostInitialize', None, 800),
(u'CostFinalize', None, 1000),
(u'InstallValidate', None, 1400),
(u'CreateShortcuts', None, 4500),
(u'MsiPublishAssemblies', None, 6250),
(u'PublishComponents', None, 6200),
(u'PublishFeatures', None, 6300),
(u'PublishProduct', None, 6400),
(u'RegisterClassInfo', None, 4600),
(u'RegisterExtensionInfo', None, 4700),
(u'RegisterMIMEInfo', None, 4900),
(u'RegisterProgIdInfo', None, 4800),
]
InstallExecuteSequence = [
(u'InstallInitialize', None, 1500),
(u'InstallFinalize', None, 6600),
(u'InstallFiles', None, 4000),
(u'FileCost', None, 900),
(u'CostInitialize', None, 800),
(u'CostFinalize', None, 1000),
(u'InstallValidate', None, 1400),
(u'CreateShortcuts', None, 4500),
(u'MsiPublishAssemblies', None, 6250),
(u'PublishComponents', None, 6200),
(u'PublishFeatures', None, 6300),
(u'PublishProduct', None, 6400),
(u'RegisterClassInfo', None, 4600),
(u'RegisterExtensionInfo', None, 4700),
(u'RegisterMIMEInfo', None, 4900),
(u'RegisterProgIdInfo', None, 4800),
(u'AllocateRegistrySpace', u'NOT Installed', 1550),
(u'AppSearch', None, 400),
(u'BindImage', None, 4300),
(u'CCPSearch', u'NOT Installed', 500),
(u'CreateFolders', None, 3700),
(u'DeleteServices', u'VersionNT', 2000),
(u'DuplicateFiles', None, 4210),
(u'FindRelatedProducts', None, 200),
(u'InstallODBC', None, 5400),
(u'InstallServices', u'VersionNT', 5800),
(u'IsolateComponents', None, 950),
(u'LaunchConditions', None, 100),
(u'MigrateFeatureStates', None, 1200),
(u'MoveFiles', None, 3800),
(u'PatchFiles', None, 4090),
(u'ProcessComponents', None, 1600),
(u'RegisterComPlus', None, 5700),
(u'RegisterFonts', None, 5300),
(u'RegisterProduct', None, 6100),
(u'RegisterTypeLibraries', None, 5500),
(u'RegisterUser', None, 6000),
(u'RemoveDuplicateFiles', None, 3400),
(u'RemoveEnvironmentStrings', None, 3300),
(u'RemoveExistingProducts', None, 6700),
(u'RemoveFiles', None, 3500),
(u'RemoveFolders', None, 3600),
(u'RemoveIniValues', None, 3100),
(u'RemoveODBC', None, 2400),
(u'RemoveRegistryValues', None, 2600),
(u'RemoveShortcuts', None, 3200),
(u'RMCCPSearch', u'NOT Installed', 600),
(u'SelfRegModules', None, 5600),
(u'SelfUnregModules', None, 2200),
(u'SetODBCFolders', None, 1100),
(u'StartServices', u'VersionNT', 5900),
(u'StopServices', u'VersionNT', 1900),
(u'MsiUnpublishAssemblies', None, 1750),
(u'UnpublishComponents', None, 1700),
(u'UnpublishFeatures', None, 1800),
(u'UnregisterClassInfo', None, 2700),
(u'UnregisterComPlus', None, 2100),
(u'UnregisterExtensionInfo', None, 2800),
(u'UnregisterFonts', None, 2500),
(u'UnregisterMIMEInfo', None, 3000),
(u'UnregisterProgIdInfo', None, 2900),
(u'UnregisterTypeLibraries', None, 2300),
(u'ValidateProductID', None, 700),
(u'WriteEnvironmentStrings', None, 5200),
(u'WriteIniValues', None, 5100),
(u'WriteRegistryValues', None, 5000),
]
InstallUISequence = [
(u'FileCost', None, 900),
(u'CostInitialize', None, 800),
(u'CostFinalize', None, 1000),
(u'ExecuteAction', None, 1300),
(u'ExitDialog', None, -1),
(u'FatalError', None, -3),
(u'UserExit', None, -2),
(u'AppSearch', None, 400),
(u'CCPSearch', u'NOT Installed', 500),
(u'FindRelatedProducts', None, 200),
(u'IsolateComponents', None, 950),
(u'LaunchConditions', None, 100),
(u'MigrateFeatureStates', None, 1200),
(u'RMCCPSearch', u'NOT Installed', 600),
(u'ValidateProductID', None, 700),
]
tables=['AdminExecuteSequence', 'AdminUISequence', 'AdvtExecuteSequence', 'InstallExecuteSequence', 'InstallUISequence']

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,58 @@
# This should be extended for each Python release.
# The product code must change whenever the name of the MSI file
# changes, and when new component codes are issued for existing
# components. See "Changing the Product Code". As we change the
# component codes with every build, we need a new product code
# each time. For intermediate (snapshot) releases, they are automatically
# generated. For official releases, we record the product codes,
# so people can refer to them.
product_codes = {
'2.5.101': '{bc14ce3e-5e72-4a64-ac1f-bf59a571898c}', # 2.5a1
'2.5.102': '{5eed51c1-8e9d-4071-94c5-b40de5d49ba5}', # 2.5a2
'2.5.103': '{73dcd966-ffec-415f-bb39-8342c1f47017}', # 2.5a3
'2.5.111': '{c797ecf8-a8e6-4fec-bb99-526b65f28626}', # 2.5b1
'2.5.112': '{32beb774-f625-439d-b587-7187487baf15}', # 2.5b2
'2.5.113': '{89f23918-11cf-4f08-be13-b9b2e6463fd9}', # 2.5b3
'2.5.121': '{8e9321bc-6b24-48a3-8fd4-c95f8e531e5f}', # 2.5c1
'2.5.122': '{a6cd508d-9599-45da-a441-cbffa9f7e070}', # 2.5c2
'2.5.150': '{0a2c5854-557e-48c8-835a-3b9f074bdcaa}', # 2.5.0
'2.5.1121':'{0378b43e-6184-4c2f-be1a-4a367781cd54}', # 2.5.1c1
'2.5.1150':'{31800004-6386-4999-a519-518f2d78d8f0}', # 2.5.1
'2.5.2150':'{6304a7da-1132-4e91-a343-a296269eab8a}', # 2.5.2c1
'2.5.2150':'{6b976adf-8ae8-434e-b282-a06c7f624d2f}', # 2.5.2
'2.6.101': '{0ba82e1b-52fd-4e03-8610-a6c76238e8a8}', # 2.6a1
'2.6.102': '{3b27e16c-56db-4570-a2d3-e9a26180c60b}', # 2.6a2
'2.6.103': '{cd06a9c5-bde5-4bd7-9874-48933997122a}', # 2.6a3
'2.6.104': '{dc6ed634-474a-4a50-a547-8de4b7491e53}', # 2.6a4
'2.6.111': '{3f82079a-5bee-4c4a-8a41-8292389e24ae}', # 2.6b1
'2.6.112': '{8a0e5970-f3e6-4737-9a2b-bc5ff0f15fb5}', # 2.6b2
'2.6.113': '{df4f5c21-6fcc-4540-95de-85feba634e76}', # 2.6b3
'2.6.121': '{bbd34464-ddeb-4028-99e5-f16c4a8fbdb3}', # 2.6c1
'2.6.122': '{8f64787e-a023-4c60-bfee-25d3a3f592c6}', # 2.6c2
'2.6.150': '{110eb5c4-e995-4cfb-ab80-a5f315bea9e8}', # 2.6.0
'2.6.1150':'{9cc89170-000b-457d-91f1-53691f85b223}', # 2.6.1
'2.6.2121':'{adac412b-b209-4c15-b6ab-dca1b6e47144}', # 2.6.2c1
'2.6.2150':'{24aab420-4e30-4496-9739-3e216f3de6ae}', # 2.6.2
'2.6.3121':'{a73e0254-dcda-4fe4-bf37-c7e1c4f4ebb6}', # 2.6.3c1
'2.6.3150':'{3d9ac095-e115-4e94-bdef-7f7edf17697d}', # 2.6.3
'2.6.4121':'{727de605-0359-4606-a94b-c2033652379b}', # 2.6.4c1
'2.6.4122':'{4f7603c6-6352-4299-a398-150a31b19acc}', # 2.6.4c2
'2.6.4150':'{e7394a0f-3f80-45b1-87fc-abcd51893246}', # 2.6.4
'2.6.5121':'{e0e273d7-7598-4701-8325-c90c069fd5ff}', # 2.6.5c1
'2.6.5122':'{fa227b76-0671-4dc6-b826-c2ff2a70dfd5}', # 2.6.5c2
'2.6.5150':'{4723f199-fa64-4233-8e6e-9fccc95a18ee}', # 2.6.5
'2.7.101': '{eca1bbef-432c-49ae-a667-c213cc7bbf22}', # 2.7a1
'2.7.102': '{21ce16ed-73c4-460d-9b11-522f417b2090}', # 2.7a2
'2.7.103': '{6e7dbd55-ba4a-48ac-a688-6c75db4d7500}', # 2.7a3
'2.7.104': '{ee774ba3-74a5-48d9-b425-b35a287260c8}', # 2.7a4
'2.7.111': '{9cfd9ec7-a9c7-4980-a1c6-054fc6493eb3}', # 2.7b1
'2.7.112': '{9a72faf6-c304-4165-8595-9291ff30cac6}', # 2.7b2
'2.7.121': '{f530c94a-dd53-4de9-948e-b632b9cb48d2}', # 2.7c1
'2.7.122': '{f80905d2-dd8d-4b8e-8a40-c23c93dca07d}', # 2.7c2
'2.7.150': '{20c31435-2a0a-4580-be8b-ac06fc243ca4}', # 2.7.0
'2.7.1121':'{60a4036a-374c-4fd2-84b9-bfae7db03931}', # 2.7.1rc1
'2.7.1122':'{5965e7d1-5584-4de9-b13a-694e0b2ee3a6}', # 2.7.1rc2
'2.7.1150':'{32939827-d8e5-470a-b126-870db3c69fdf}', # 2.7.1
'2.7.2121':'{B2E1F06E-F719-4786-972A-488A336EB2A0}', # 2.7.2rc1
'2.7.2150':'{2E295B5B-1AD4-4d36-97C2-A316084722CF}', # 2.7.2
}

View File

@@ -0,0 +1,777 @@
from pybench import Test
class SimpleIntegerArithmetic(Test):
version = 2.0
operations = 5 * (3 + 5 + 5 + 3 + 3 + 3)
rounds = 120000
def test(self):
for i in xrange(self.rounds):
a = 2
b = 3
c = 3
c = a + b
c = b + c
c = c + a
c = a + b
c = b + c
c = c - a
c = a - b
c = b - c
c = c - a
c = b - c
c = a / b
c = b / a
c = c / b
c = a * b
c = b * a
c = c * b
c = a / b
c = b / a
c = c / b
a = 2
b = 3
c = 3
c = a + b
c = b + c
c = c + a
c = a + b
c = b + c
c = c - a
c = a - b
c = b - c
c = c - a
c = b - c
c = a / b
c = b / a
c = c / b
c = a * b
c = b * a
c = c * b
c = a / b
c = b / a
c = c / b
a = 2
b = 3
c = 3
c = a + b
c = b + c
c = c + a
c = a + b
c = b + c
c = c - a
c = a - b
c = b - c
c = c - a
c = b - c
c = a / b
c = b / a
c = c / b
c = a * b
c = b * a
c = c * b
c = a / b
c = b / a
c = c / b
a = 2
b = 3
c = 3
c = a + b
c = b + c
c = c + a
c = a + b
c = b + c
c = c - a
c = a - b
c = b - c
c = c - a
c = b - c
c = a / b
c = b / a
c = c / b
c = a * b
c = b * a
c = c * b
c = a / b
c = b / a
c = c / b
a = 2
b = 3
c = 3
c = a + b
c = b + c
c = c + a
c = a + b
c = b + c
c = c - a
c = a - b
c = b - c
c = c - a
c = b - c
c = a / b
c = b / a
c = c / b
c = a * b
c = b * a
c = c * b
c = a / b
c = b / a
c = c / b
def calibrate(self):
for i in xrange(self.rounds):
pass
class SimpleFloatArithmetic(Test):
version = 2.0
operations = 5 * (3 + 5 + 5 + 3 + 3 + 3)
rounds = 120000
def test(self):
for i in xrange(self.rounds):
a = 2.1
b = 3.3332
c = 3.14159
c = a + b
c = b + c
c = c + a
c = a + b
c = b + c
c = c - a
c = a - b
c = b - c
c = c - a
c = b - c
c = a / b
c = b / a
c = c / b
c = a * b
c = b * a
c = c * b
c = a / b
c = b / a
c = c / b
a = 2.1
b = 3.3332
c = 3.14159
c = a + b
c = b + c
c = c + a
c = a + b
c = b + c
c = c - a
c = a - b
c = b - c
c = c - a
c = b - c
c = a / b
c = b / a
c = c / b
c = a * b
c = b * a
c = c * b
c = a / b
c = b / a
c = c / b
a = 2.1
b = 3.3332
c = 3.14159
c = a + b
c = b + c
c = c + a
c = a + b
c = b + c
c = c - a
c = a - b
c = b - c
c = c - a
c = b - c
c = a / b
c = b / a
c = c / b
c = a * b
c = b * a
c = c * b
c = a / b
c = b / a
c = c / b
a = 2.1
b = 3.3332
c = 3.14159
c = a + b
c = b + c
c = c + a
c = a + b
c = b + c
c = c - a
c = a - b
c = b - c
c = c - a
c = b - c
c = a / b
c = b / a
c = c / b
c = a * b
c = b * a
c = c * b
c = a / b
c = b / a
c = c / b
a = 2.1
b = 3.3332
c = 3.14159
c = a + b
c = b + c
c = c + a
c = a + b
c = b + c
c = c - a
c = a - b
c = b - c
c = c - a
c = b - c
c = a / b
c = b / a
c = c / b
c = a * b
c = b * a
c = c * b
c = a / b
c = b / a
c = c / b
def calibrate(self):
for i in xrange(self.rounds):
pass
class SimpleIntFloatArithmetic(Test):
version = 2.0
operations = 5 * (3 + 5 + 5 + 3 + 3 + 3)
rounds = 120000
def test(self):
for i in xrange(self.rounds):
a = 2
b = 3
c = 3.14159
c = a + b
c = b + c
c = c + a
c = a + b
c = b + c
c = c - a
c = a - b
c = b - c
c = c - a
c = b - c
c = a / b
c = b / a
c = c / b
c = a * b
c = b * a
c = c * b
c = a / b
c = b / a
c = c / b
a = 2
b = 3
c = 3.14159
c = a + b
c = b + c
c = c + a
c = a + b
c = b + c
c = c - a
c = a - b
c = b - c
c = c - a
c = b - c
c = a / b
c = b / a
c = c / b
c = a * b
c = b * a
c = c * b
c = a / b
c = b / a
c = c / b
a = 2
b = 3
c = 3.14159
c = a + b
c = b + c
c = c + a
c = a + b
c = b + c
c = c - a
c = a - b
c = b - c
c = c - a
c = b - c
c = a / b
c = b / a
c = c / b
c = a * b
c = b * a
c = c * b
c = a / b
c = b / a
c = c / b
a = 2
b = 3
c = 3.14159
c = a + b
c = b + c
c = c + a
c = a + b
c = b + c
c = c - a
c = a - b
c = b - c
c = c - a
c = b - c
c = a / b
c = b / a
c = c / b
c = a * b
c = b * a
c = c * b
c = a / b
c = b / a
c = c / b
a = 2
b = 3
c = 3.14159
c = a + b
c = b + c
c = c + a
c = a + b
c = b + c
c = c - a
c = a - b
c = b - c
c = c - a
c = b - c
c = a / b
c = b / a
c = c / b
c = a * b
c = b * a
c = c * b
c = a / b
c = b / a
c = c / b
def calibrate(self):
for i in xrange(self.rounds):
pass
class SimpleLongArithmetic(Test):
version = 2.0
operations = 5 * (3 + 5 + 5 + 3 + 3 + 3)
rounds = 60000
def test(self):
for i in xrange(self.rounds):
a = 2220001L
b = 100001L
c = 30005L
c = a + b
c = b + c
c = c + a
c = a + b
c = b + c
c = c - a
c = a - b
c = b - c
c = c - a
c = b - c
c = a / b
c = b / a
c = c / b
c = a * b
c = b * a
c = c * b
c = a / b
c = b / a
c = c / b
a = 2220001L
b = 100001L
c = 30005L
c = a + b
c = b + c
c = c + a
c = a + b
c = b + c
c = c - a
c = a - b
c = b - c
c = c - a
c = b - c
c = a / b
c = b / a
c = c / b
c = a * b
c = b * a
c = c * b
c = a / b
c = b / a
c = c / b
a = 2220001L
b = 100001L
c = 30005L
c = a + b
c = b + c
c = c + a
c = a + b
c = b + c
c = c - a
c = a - b
c = b - c
c = c - a
c = b - c
c = a / b
c = b / a
c = c / b
c = a * b
c = b * a
c = c * b
c = a / b
c = b / a
c = c / b
a = 2220001L
b = 100001L
c = 30005L
c = a + b
c = b + c
c = c + a
c = a + b
c = b + c
c = c - a
c = a - b
c = b - c
c = c - a
c = b - c
c = a / b
c = b / a
c = c / b
c = a * b
c = b * a
c = c * b
c = a / b
c = b / a
c = c / b
a = 2220001L
b = 100001L
c = 30005L
c = a + b
c = b + c
c = c + a
c = a + b
c = b + c
c = c - a
c = a - b
c = b - c
c = c - a
c = b - c
c = a / b
c = b / a
c = c / b
c = a * b
c = b * a
c = c * b
c = a / b
c = b / a
c = c / b
def calibrate(self):
for i in xrange(self.rounds):
pass
class SimpleComplexArithmetic(Test):
version = 2.0
operations = 5 * (3 + 5 + 5 + 3 + 3 + 3)
rounds = 80000
def test(self):
for i in xrange(self.rounds):
a = 2 + 3j
b = 2.5 + 4.5j
c = 1.2 + 6.2j
c = a + b
c = b + c
c = c + a
c = a + b
c = b + c
c = c - a
c = a - b
c = b - c
c = c - a
c = b - c
c = a / b
c = b / a
c = c / b
c = a * b
c = b * a
c = c * b
c = a / b
c = b / a
c = c / b
a = 2 + 3j
b = 2.5 + 4.5j
c = 1.2 + 6.2j
c = a + b
c = b + c
c = c + a
c = a + b
c = b + c
c = c - a
c = a - b
c = b - c
c = c - a
c = b - c
c = a / b
c = b / a
c = c / b
c = a * b
c = b * a
c = c * b
c = a / b
c = b / a
c = c / b
a = 2 + 3j
b = 2.5 + 4.5j
c = 1.2 + 6.2j
c = a + b
c = b + c
c = c + a
c = a + b
c = b + c
c = c - a
c = a - b
c = b - c
c = c - a
c = b - c
c = a / b
c = b / a
c = c / b
c = a * b
c = b * a
c = c * b
c = a / b
c = b / a
c = c / b
a = 2 + 3j
b = 2.5 + 4.5j
c = 1.2 + 6.2j
c = a + b
c = b + c
c = c + a
c = a + b
c = b + c
c = c - a
c = a - b
c = b - c
c = c - a
c = b - c
c = a / b
c = b / a
c = c / b
c = a * b
c = b * a
c = c * b
c = a / b
c = b / a
c = c / b
a = 2 + 3j
b = 2.5 + 4.5j
c = 1.2 + 6.2j
c = a + b
c = b + c
c = c + a
c = a + b
c = b + c
c = c - a
c = a - b
c = b - c
c = c - a
c = b - c
c = a / b
c = b / a
c = c / b
c = a * b
c = b * a
c = c * b
c = a / b
c = b / a
c = c / b
def calibrate(self):
for i in xrange(self.rounds):
pass

View File

@@ -0,0 +1,560 @@
from pybench import Test
class PythonFunctionCalls(Test):
version = 2.0
operations = 5*(1+4+4+2)
rounds = 60000
def test(self):
global f,f1,g,h
# define functions
def f():
pass
def f1(x):
pass
def g(a,b,c):
return a,b,c
def h(a,b,c,d=1,e=2,f=3):
return d,e,f
# do calls
for i in xrange(self.rounds):
f()
f1(i)
f1(i)
f1(i)
f1(i)
g(i,i,i)
g(i,i,i)
g(i,i,i)
g(i,i,i)
h(i,i,3,i,i)
h(i,i,i,2,i,3)
f()
f1(i)
f1(i)
f1(i)
f1(i)
g(i,i,i)
g(i,i,i)
g(i,i,i)
g(i,i,i)
h(i,i,3,i,i)
h(i,i,i,2,i,3)
f()
f1(i)
f1(i)
f1(i)
f1(i)
g(i,i,i)
g(i,i,i)
g(i,i,i)
g(i,i,i)
h(i,i,3,i,i)
h(i,i,i,2,i,3)
f()
f1(i)
f1(i)
f1(i)
f1(i)
g(i,i,i)
g(i,i,i)
g(i,i,i)
g(i,i,i)
h(i,i,3,i,i)
h(i,i,i,2,i,3)
f()
f1(i)
f1(i)
f1(i)
f1(i)
g(i,i,i)
g(i,i,i)
g(i,i,i)
g(i,i,i)
h(i,i,3,i,i)
h(i,i,i,2,i,3)
def calibrate(self):
global f,f1,g,h
# define functions
def f():
pass
def f1(x):
pass
def g(a,b,c):
return a,b,c
def h(a,b,c,d=1,e=2,f=3):
return d,e,f
# do calls
for i in xrange(self.rounds):
pass
###
class ComplexPythonFunctionCalls(Test):
version = 2.0
operations = 4*5
rounds = 100000
def test(self):
# define functions
def f(a,b,c,d=1,e=2,f=3):
return f
args = 1,2
kwargs = dict(c=3,d=4,e=5)
# do calls
for i in xrange(self.rounds):
f(a=i,b=i,c=i)
f(f=i,e=i,d=i,c=2,b=i,a=3)
f(1,b=i,**kwargs)
f(*args,**kwargs)
f(a=i,b=i,c=i)
f(f=i,e=i,d=i,c=2,b=i,a=3)
f(1,b=i,**kwargs)
f(*args,**kwargs)
f(a=i,b=i,c=i)
f(f=i,e=i,d=i,c=2,b=i,a=3)
f(1,b=i,**kwargs)
f(*args,**kwargs)
f(a=i,b=i,c=i)
f(f=i,e=i,d=i,c=2,b=i,a=3)
f(1,b=i,**kwargs)
f(*args,**kwargs)
f(a=i,b=i,c=i)
f(f=i,e=i,d=i,c=2,b=i,a=3)
f(1,b=i,**kwargs)
f(*args,**kwargs)
def calibrate(self):
# define functions
def f(a,b,c,d=1,e=2,f=3):
return f
args = 1,2
kwargs = dict(c=3,d=4,e=5)
# do calls
for i in xrange(self.rounds):
pass
###
class BuiltinFunctionCalls(Test):
version = 2.0
operations = 5*(2+5+5+5)
rounds = 60000
def test(self):
# localize functions
f0 = globals
f1 = hash
f2 = cmp
f3 = range
# do calls
for i in xrange(self.rounds):
f0()
f0()
f1(i)
f1(i)
f1(i)
f1(i)
f1(i)
f2(1,2)
f2(1,2)
f2(1,2)
f2(1,2)
f2(1,2)
f3(1,3,2)
f3(1,3,2)
f3(1,3,2)
f3(1,3,2)
f3(1,3,2)
f0()
f0()
f1(i)
f1(i)
f1(i)
f1(i)
f1(i)
f2(1,2)
f2(1,2)
f2(1,2)
f2(1,2)
f2(1,2)
f3(1,3,2)
f3(1,3,2)
f3(1,3,2)
f3(1,3,2)
f3(1,3,2)
f0()
f0()
f1(i)
f1(i)
f1(i)
f1(i)
f1(i)
f2(1,2)
f2(1,2)
f2(1,2)
f2(1,2)
f2(1,2)
f3(1,3,2)
f3(1,3,2)
f3(1,3,2)
f3(1,3,2)
f3(1,3,2)
f0()
f0()
f1(i)
f1(i)
f1(i)
f1(i)
f1(i)
f2(1,2)
f2(1,2)
f2(1,2)
f2(1,2)
f2(1,2)
f3(1,3,2)
f3(1,3,2)
f3(1,3,2)
f3(1,3,2)
f3(1,3,2)
f0()
f0()
f1(i)
f1(i)
f1(i)
f1(i)
f1(i)
f2(1,2)
f2(1,2)
f2(1,2)
f2(1,2)
f2(1,2)
f3(1,3,2)
f3(1,3,2)
f3(1,3,2)
f3(1,3,2)
f3(1,3,2)
def calibrate(self):
# localize functions
f0 = dir
f1 = hash
f2 = range
f3 = range
# do calls
for i in xrange(self.rounds):
pass
###
class PythonMethodCalls(Test):
version = 2.0
operations = 5*(6 + 5 + 4)
rounds = 30000
def test(self):
class c:
x = 2
s = 'string'
def f(self):
return self.x
def j(self,a,b):
self.y = a
self.t = b
return self.y
def k(self,a,b,c=3):
self.y = a
self.s = b
self.t = c
o = c()
for i in xrange(self.rounds):
o.f()
o.f()
o.f()
o.f()
o.f()
o.f()
o.j(i,i)
o.j(i,i)
o.j(i,2)
o.j(i,2)
o.j(2,2)
o.k(i,i)
o.k(i,2)
o.k(i,2,3)
o.k(i,i,c=4)
o.f()
o.f()
o.f()
o.f()
o.f()
o.f()
o.j(i,i)
o.j(i,i)
o.j(i,2)
o.j(i,2)
o.j(2,2)
o.k(i,i)
o.k(i,2)
o.k(i,2,3)
o.k(i,i,c=4)
o.f()
o.f()
o.f()
o.f()
o.f()
o.f()
o.j(i,i)
o.j(i,i)
o.j(i,2)
o.j(i,2)
o.j(2,2)
o.k(i,i)
o.k(i,2)
o.k(i,2,3)
o.k(i,i,c=4)
o.f()
o.f()
o.f()
o.f()
o.f()
o.f()
o.j(i,i)
o.j(i,i)
o.j(i,2)
o.j(i,2)
o.j(2,2)
o.k(i,i)
o.k(i,2)
o.k(i,2,3)
o.k(i,i,c=4)
o.f()
o.f()
o.f()
o.f()
o.f()
o.f()
o.j(i,i)
o.j(i,i)
o.j(i,2)
o.j(i,2)
o.j(2,2)
o.k(i,i)
o.k(i,2)
o.k(i,2,3)
o.k(i,i,c=4)
def calibrate(self):
class c:
x = 2
s = 'string'
def f(self):
return self.x
def j(self,a,b):
self.y = a
self.t = b
def k(self,a,b,c=3):
self.y = a
self.s = b
self.t = c
o = c
for i in xrange(self.rounds):
pass
###
class Recursion(Test):
version = 2.0
operations = 5
rounds = 100000
def test(self):
global f
def f(x):
if x > 1:
return f(x-1)
return 1
for i in xrange(self.rounds):
f(10)
f(10)
f(10)
f(10)
f(10)
def calibrate(self):
global f
def f(x):
if x > 0:
return f(x-1)
return 1
for i in xrange(self.rounds):
pass
### Test to make Fredrik happy...
if __name__ == '__main__':
import timeit
if 0:
timeit.TestClass = PythonFunctionCalls
timeit.main(['-s', 'test = TestClass(); test.rounds = 1000',
'test.test()'])
else:
setup = """\
global f,f1,g,h
# define functions
def f():
pass
def f1(x):
pass
def g(a,b,c):
return a,b,c
def h(a,b,c,d=1,e=2,f=3):
return d,e,f
i = 1
"""
test = """\
f()
f1(i)
f1(i)
f1(i)
f1(i)
g(i,i,i)
g(i,i,i)
g(i,i,i)
g(i,i,i)
h(i,i,3,i,i)
h(i,i,i,2,i,3)
f()
f1(i)
f1(i)
f1(i)
f1(i)
g(i,i,i)
g(i,i,i)
g(i,i,i)
g(i,i,i)
h(i,i,3,i,i)
h(i,i,i,2,i,3)
f()
f1(i)
f1(i)
f1(i)
f1(i)
g(i,i,i)
g(i,i,i)
g(i,i,i)
g(i,i,i)
h(i,i,3,i,i)
h(i,i,i,2,i,3)
f()
f1(i)
f1(i)
f1(i)
f1(i)
g(i,i,i)
g(i,i,i)
g(i,i,i)
g(i,i,i)
h(i,i,3,i,i)
h(i,i,i,2,i,3)
f()
f1(i)
f1(i)
f1(i)
f1(i)
g(i,i,i)
g(i,i,i)
g(i,i,i)
g(i,i,i)
h(i,i,3,i,i)
h(i,i,i,2,i,3)
"""
timeit.main(['-s', setup,
test])

View File

@@ -0,0 +1,634 @@
""" CommandLine - Get and parse command line options
NOTE: This still is very much work in progress !!!
Different version are likely to be incompatible.
TODO:
* Incorporate the changes made by (see Inbox)
* Add number range option using srange()
"""
__copyright__ = """\
Copyright (c), 1997-2006, Marc-Andre Lemburg (mal@lemburg.com)
Copyright (c), 2000-2006, eGenix.com Software GmbH (info@egenix.com)
See the documentation for further information on copyrights,
or contact the author. All Rights Reserved.
"""
__version__ = '1.2'
import sys, getopt, string, glob, os, re, exceptions, traceback
### Helpers
def _getopt_flags(options):
""" Convert the option list to a getopt flag string and long opt
list
"""
s = []
l = []
for o in options:
if o.prefix == '-':
# short option
s.append(o.name)
if o.takes_argument:
s.append(':')
else:
# long option
if o.takes_argument:
l.append(o.name+'=')
else:
l.append(o.name)
return string.join(s,''),l
def invisible_input(prompt='>>> '):
""" Get raw input from a terminal without echoing the characters to
the terminal, e.g. for password queries.
"""
import getpass
entry = getpass.getpass(prompt)
if entry is None:
raise KeyboardInterrupt
return entry
def fileopen(name, mode='wb', encoding=None):
""" Open a file using mode.
Default mode is 'wb' meaning to open the file for writing in
binary mode. If encoding is given, I/O to and from the file is
transparently encoded using the given encoding.
Files opened for writing are chmod()ed to 0600.
"""
if name == 'stdout':
return sys.stdout
elif name == 'stderr':
return sys.stderr
elif name == 'stdin':
return sys.stdin
else:
if encoding is not None:
import codecs
f = codecs.open(name, mode, encoding)
else:
f = open(name, mode)
if 'w' in mode:
os.chmod(name, 0600)
return f
def option_dict(options):
""" Return a dictionary mapping option names to Option instances.
"""
d = {}
for option in options:
d[option.name] = option
return d
# Alias
getpasswd = invisible_input
_integerRE = re.compile('\s*(-?\d+)\s*$')
_integerRangeRE = re.compile('\s*(-?\d+)\s*-\s*(-?\d+)\s*$')
def srange(s,
split=string.split,integer=_integerRE,
integerRange=_integerRangeRE):
""" Converts a textual representation of integer numbers and ranges
to a Python list.
Supported formats: 2,3,4,2-10,-1 - -3, 5 - -2
Values are appended to the created list in the order specified
in the string.
"""
l = []
append = l.append
for entry in split(s,','):
m = integer.match(entry)
if m:
append(int(m.groups()[0]))
continue
m = integerRange.match(entry)
if m:
start,end = map(int,m.groups())
l[len(l):] = range(start,end+1)
return l
def abspath(path,
expandvars=os.path.expandvars,expanduser=os.path.expanduser,
join=os.path.join,getcwd=os.getcwd):
""" Return the corresponding absolute path for path.
path is expanded in the usual shell ways before
joining it with the current working directory.
"""
try:
path = expandvars(path)
except AttributeError:
pass
try:
path = expanduser(path)
except AttributeError:
pass
return join(getcwd(), path)
### Option classes
class Option:
""" Option base class. Takes no argument.
"""
default = None
helptext = ''
prefix = '-'
takes_argument = 0
has_default = 0
tab = 15
def __init__(self,name,help=None):
if not name[:1] == '-':
raise TypeError,'option names must start with "-"'
if name[1:2] == '-':
self.prefix = '--'
self.name = name[2:]
else:
self.name = name[1:]
if help:
self.help = help
def __str__(self):
o = self
name = o.prefix + o.name
if o.takes_argument:
name = name + ' arg'
if len(name) > self.tab:
name = name + '\n' + ' ' * (self.tab + 1 + len(o.prefix))
else:
name = '%-*s ' % (self.tab, name)
description = o.help
if o.has_default:
description = description + ' (%s)' % o.default
return '%s %s' % (name, description)
class ArgumentOption(Option):
""" Option that takes an argument.
An optional default argument can be given.
"""
def __init__(self,name,help=None,default=None):
# Basemethod
Option.__init__(self,name,help)
if default is not None:
self.default = default
self.has_default = 1
self.takes_argument = 1
class SwitchOption(Option):
""" Options that can be on or off. Has an optional default value.
"""
def __init__(self,name,help=None,default=None):
# Basemethod
Option.__init__(self,name,help)
if default is not None:
self.default = default
self.has_default = 1
### Application baseclass
class Application:
""" Command line application interface with builtin argument
parsing.
"""
# Options the program accepts (Option instances)
options = []
# Standard settings; these are appended to options in __init__
preset_options = [SwitchOption('-v',
'generate verbose output'),
SwitchOption('-h',
'show this help text'),
SwitchOption('--help',
'show this help text'),
SwitchOption('--debug',
'enable debugging'),
SwitchOption('--copyright',
'show copyright'),
SwitchOption('--examples',
'show examples of usage')]
# The help layout looks like this:
# [header] - defaults to ''
#
# [synopsis] - formatted as '<self.name> %s' % self.synopsis
#
# options:
# [options] - formatted from self.options
#
# [version] - formatted as 'Version:\n %s' % self.version, if given
#
# [about] - defaults to ''
#
# Note: all fields that do not behave as template are formatted
# using the instances dictionary as substitution namespace,
# e.g. %(name)s will be replaced by the applications name.
#
# Header (default to program name)
header = ''
# Name (defaults to program name)
name = ''
# Synopsis (%(name)s is replaced by the program name)
synopsis = '%(name)s [option] files...'
# Version (optional)
version = ''
# General information printed after the possible options (optional)
about = ''
# Examples of usage to show when the --examples option is given (optional)
examples = ''
# Copyright to show
copyright = __copyright__
# Apply file globbing ?
globbing = 1
# Generate debug output ?
debug = 0
# Generate verbose output ?
verbose = 0
# Internal errors to catch
InternalError = exceptions.Exception
# Instance variables:
values = None # Dictionary of passed options (or default values)
# indexed by the options name, e.g. '-h'
files = None # List of passed filenames
optionlist = None # List of passed options
def __init__(self,argv=None):
# Setup application specs
if argv is None:
argv = sys.argv
self.filename = os.path.split(argv[0])[1]
if not self.name:
self.name = os.path.split(self.filename)[1]
else:
self.name = self.name
if not self.header:
self.header = self.name
else:
self.header = self.header
# Init .arguments list
self.arguments = argv[1:]
# Setup Option mapping
self.option_map = option_dict(self.options)
# Append preset options
for option in self.preset_options:
if not self.option_map.has_key(option.name):
self.add_option(option)
# Init .files list
self.files = []
# Start Application
try:
# Process startup
rc = self.startup()
if rc is not None:
raise SystemExit,rc
# Parse command line
rc = self.parse()
if rc is not None:
raise SystemExit,rc
# Start application
rc = self.main()
if rc is None:
rc = 0
except SystemExit,rc:
pass
except KeyboardInterrupt:
print
print '* User Break'
print
rc = 1
except self.InternalError:
print
print '* Internal Error (use --debug to display the traceback)'
if self.debug:
print
traceback.print_exc(20, sys.stdout)
elif self.verbose:
print ' %s: %s' % sys.exc_info()[:2]
print
rc = 1
raise SystemExit,rc
def add_option(self, option):
""" Add a new Option instance to the Application dynamically.
Note that this has to be done *before* .parse() is being
executed.
"""
self.options.append(option)
self.option_map[option.name] = option
def startup(self):
""" Set user defined instance variables.
If this method returns anything other than None, the
process is terminated with the return value as exit code.
"""
return None
def exit(self, rc=0):
""" Exit the program.
rc is used as exit code and passed back to the calling
program. It defaults to 0 which usually means: OK.
"""
raise SystemExit, rc
def parse(self):
""" Parse the command line and fill in self.values and self.files.
After having parsed the options, the remaining command line
arguments are interpreted as files and passed to .handle_files()
for processing.
As final step the option handlers are called in the order
of the options given on the command line.
"""
# Parse arguments
self.values = values = {}
for o in self.options:
if o.has_default:
values[o.prefix+o.name] = o.default
else:
values[o.prefix+o.name] = 0
flags,lflags = _getopt_flags(self.options)
try:
optlist,files = getopt.getopt(self.arguments,flags,lflags)
if self.globbing:
l = []
for f in files:
gf = glob.glob(f)
if not gf:
l.append(f)
else:
l[len(l):] = gf
files = l
self.optionlist = optlist
self.files = files + self.files
except getopt.error,why:
self.help(why)
sys.exit(1)
# Call file handler
rc = self.handle_files(self.files)
if rc is not None:
sys.exit(rc)
# Call option handlers
for optionname, value in optlist:
# Try to convert value to integer
try:
value = string.atoi(value)
except ValueError:
pass
# Find handler and call it (or count the number of option
# instances on the command line)
handlername = 'handle' + string.replace(optionname, '-', '_')
try:
handler = getattr(self, handlername)
except AttributeError:
if value == '':
# count the number of occurances
if values.has_key(optionname):
values[optionname] = values[optionname] + 1
else:
values[optionname] = 1
else:
values[optionname] = value
else:
rc = handler(value)
if rc is not None:
raise SystemExit, rc
# Apply final file check (for backward compatibility)
rc = self.check_files(self.files)
if rc is not None:
sys.exit(rc)
def check_files(self,filelist):
""" Apply some user defined checks on the files given in filelist.
This may modify filelist in place. A typical application
is checking that at least n files are given.
If this method returns anything other than None, the
process is terminated with the return value as exit code.
"""
return None
def help(self,note=''):
self.print_header()
if self.synopsis:
print 'Synopsis:'
# To remain backward compatible:
try:
synopsis = self.synopsis % self.name
except (NameError, KeyError, TypeError):
synopsis = self.synopsis % self.__dict__
print ' ' + synopsis
print
self.print_options()
if self.version:
print 'Version:'
print ' %s' % self.version
print
if self.about:
print string.strip(self.about % self.__dict__)
print
if note:
print '-'*72
print 'Note:',note
print
def notice(self,note):
print '-'*72
print 'Note:',note
print '-'*72
print
def print_header(self):
print '-'*72
print self.header % self.__dict__
print '-'*72
print
def print_options(self):
options = self.options
print 'Options and default settings:'
if not options:
print ' None'
return
long = filter(lambda x: x.prefix == '--', options)
short = filter(lambda x: x.prefix == '-', options)
items = short + long
for o in options:
print ' ',o
print
#
# Example handlers:
#
# If a handler returns anything other than None, processing stops
# and the return value is passed to sys.exit() as argument.
#
# File handler
def handle_files(self,files):
""" This may process the files list in place.
"""
return None
# Short option handler
def handle_h(self,arg):
self.help()
return 0
def handle_v(self, value):
""" Turn on verbose output.
"""
self.verbose = 1
# Handlers for long options have two underscores in their name
def handle__help(self,arg):
self.help()
return 0
def handle__debug(self,arg):
self.debug = 1
# We don't want to catch internal errors:
self.InternalError = None
def handle__copyright(self,arg):
self.print_header()
print string.strip(self.copyright % self.__dict__)
print
return 0
def handle__examples(self,arg):
self.print_header()
if self.examples:
print 'Examples:'
print
print string.strip(self.examples % self.__dict__)
print
else:
print 'No examples available.'
print
return 0
def main(self):
""" Override this method as program entry point.
The return value is passed to sys.exit() as argument. If
it is None, 0 is assumed (meaning OK). Unhandled
exceptions are reported with exit status code 1 (see
__init__ for further details).
"""
return None
# Alias
CommandLine = Application
def _test():
class MyApplication(Application):
header = 'Test Application'
version = __version__
options = [Option('-v','verbose')]
def handle_v(self,arg):
print 'VERBOSE, Yeah !'
cmd = MyApplication()
if not cmd.values['-h']:
cmd.help()
print 'files:',cmd.files
print 'Bye...'
if __name__ == '__main__':
_test()

View File

@@ -0,0 +1,564 @@
from pybench import Test
class IfThenElse(Test):
version = 2.0
operations = 30*3 # hard to say...
rounds = 150000
def test(self):
a,b,c = 1,2,3
for i in xrange(self.rounds):
if a == 1:
if b == 2:
if c != 3:
c = 3
b = 3
else:
c = 2
elif b == 3:
b = 2
a = 2
elif a == 2:
a = 3
else:
a = 1
if a == 1:
if b == 2:
if c != 3:
c = 3
b = 3
else:
c = 2
elif b == 3:
b = 2
a = 2
elif a == 2:
a = 3
else:
a = 1
if a == 1:
if b == 2:
if c != 3:
c = 3
b = 3
else:
c = 2
elif b == 3:
b = 2
a = 2
elif a == 2:
a = 3
else:
a = 1
if a == 1:
if b == 2:
if c != 3:
c = 3
b = 3
else:
c = 2
elif b == 3:
b = 2
a = 2
elif a == 2:
a = 3
else:
a = 1
if a == 1:
if b == 2:
if c != 3:
c = 3
b = 3
else:
c = 2
elif b == 3:
b = 2
a = 2
elif a == 2:
a = 3
else:
a = 1
if a == 1:
if b == 2:
if c != 3:
c = 3
b = 3
else:
c = 2
elif b == 3:
b = 2
a = 2
elif a == 2:
a = 3
else:
a = 1
if a == 1:
if b == 2:
if c != 3:
c = 3
b = 3
else:
c = 2
elif b == 3:
b = 2
a = 2
elif a == 2:
a = 3
else:
a = 1
if a == 1:
if b == 2:
if c != 3:
c = 3
b = 3
else:
c = 2
elif b == 3:
b = 2
a = 2
elif a == 2:
a = 3
else:
a = 1
if a == 1:
if b == 2:
if c != 3:
c = 3
b = 3
else:
c = 2
elif b == 3:
b = 2
a = 2
elif a == 2:
a = 3
else:
a = 1
if a == 1:
if b == 2:
if c != 3:
c = 3
b = 3
else:
c = 2
elif b == 3:
b = 2
a = 2
elif a == 2:
a = 3
else:
a = 1
if a == 1:
if b == 2:
if c != 3:
c = 3
b = 3
else:
c = 2
elif b == 3:
b = 2
a = 2
elif a == 2:
a = 3
else:
a = 1
if a == 1:
if b == 2:
if c != 3:
c = 3
b = 3
else:
c = 2
elif b == 3:
b = 2
a = 2
elif a == 2:
a = 3
else:
a = 1
if a == 1:
if b == 2:
if c != 3:
c = 3
b = 3
else:
c = 2
elif b == 3:
b = 2
a = 2
elif a == 2:
a = 3
else:
a = 1
if a == 1:
if b == 2:
if c != 3:
c = 3
b = 3
else:
c = 2
elif b == 3:
b = 2
a = 2
elif a == 2:
a = 3
else:
a = 1
if a == 1:
if b == 2:
if c != 3:
c = 3
b = 3
else:
c = 2
elif b == 3:
b = 2
a = 2
elif a == 2:
a = 3
else:
a = 1
if a == 1:
if b == 2:
if c != 3:
c = 3
b = 3
else:
c = 2
elif b == 3:
b = 2
a = 2
elif a == 2:
a = 3
else:
a = 1
if a == 1:
if b == 2:
if c != 3:
c = 3
b = 3
else:
c = 2
elif b == 3:
b = 2
a = 2
elif a == 2:
a = 3
else:
a = 1
if a == 1:
if b == 2:
if c != 3:
c = 3
b = 3
else:
c = 2
elif b == 3:
b = 2
a = 2
elif a == 2:
a = 3
else:
a = 1
if a == 1:
if b == 2:
if c != 3:
c = 3
b = 3
else:
c = 2
elif b == 3:
b = 2
a = 2
elif a == 2:
a = 3
else:
a = 1
if a == 1:
if b == 2:
if c != 3:
c = 3
b = 3
else:
c = 2
elif b == 3:
b = 2
a = 2
elif a == 2:
a = 3
else:
a = 1
if a == 1:
if b == 2:
if c != 3:
c = 3
b = 3
else:
c = 2
elif b == 3:
b = 2
a = 2
elif a == 2:
a = 3
else:
a = 1
if a == 1:
if b == 2:
if c != 3:
c = 3
b = 3
else:
c = 2
elif b == 3:
b = 2
a = 2
elif a == 2:
a = 3
else:
a = 1
if a == 1:
if b == 2:
if c != 3:
c = 3
b = 3
else:
c = 2
elif b == 3:
b = 2
a = 2
elif a == 2:
a = 3
else:
a = 1
if a == 1:
if b == 2:
if c != 3:
c = 3
b = 3
else:
c = 2
elif b == 3:
b = 2
a = 2
elif a == 2:
a = 3
else:
a = 1
if a == 1:
if b == 2:
if c != 3:
c = 3
b = 3
else:
c = 2
elif b == 3:
b = 2
a = 2
elif a == 2:
a = 3
else:
a = 1
if a == 1:
if b == 2:
if c != 3:
c = 3
b = 3
else:
c = 2
elif b == 3:
b = 2
a = 2
elif a == 2:
a = 3
else:
a = 1
if a == 1:
if b == 2:
if c != 3:
c = 3
b = 3
else:
c = 2
elif b == 3:
b = 2
a = 2
elif a == 2:
a = 3
else:
a = 1
if a == 1:
if b == 2:
if c != 3:
c = 3
b = 3
else:
c = 2
elif b == 3:
b = 2
a = 2
elif a == 2:
a = 3
else:
a = 1
if a == 1:
if b == 2:
if c != 3:
c = 3
b = 3
else:
c = 2
elif b == 3:
b = 2
a = 2
elif a == 2:
a = 3
else:
a = 1
if a == 1:
if b == 2:
if c != 3:
c = 3
b = 3
else:
c = 2
elif b == 3:
b = 2
a = 2
elif a == 2:
a = 3
else:
a = 1
def calibrate(self):
a,b,c = 1,2,3
for i in xrange(self.rounds):
pass
class NestedForLoops(Test):
version = 2.0
operations = 1000*10*5
rounds = 300
def test(self):
l1 = range(1000)
l2 = range(10)
l3 = range(5)
for i in xrange(self.rounds):
for i in l1:
for j in l2:
for k in l3:
pass
def calibrate(self):
l1 = range(1000)
l2 = range(10)
l3 = range(5)
for i in xrange(self.rounds):
pass
class ForLoops(Test):
version = 2.0
operations = 5 * 5
rounds = 10000
def test(self):
l1 = range(100)
for i in xrange(self.rounds):
for i in l1:
pass
for i in l1:
pass
for i in l1:
pass
for i in l1:
pass
for i in l1:
pass
for i in l1:
pass
for i in l1:
pass
for i in l1:
pass
for i in l1:
pass
for i in l1:
pass
for i in l1:
pass
for i in l1:
pass
for i in l1:
pass
for i in l1:
pass
for i in l1:
pass
for i in l1:
pass
for i in l1:
pass
for i in l1:
pass
for i in l1:
pass
for i in l1:
pass
for i in l1:
pass
for i in l1:
pass
for i in l1:
pass
for i in l1:
pass
for i in l1:
pass
def calibrate(self):
l1 = range(1000)
for i in xrange(self.rounds):
pass

View File

@@ -0,0 +1,504 @@
from pybench import Test
class DictCreation(Test):
version = 2.0
operations = 5*(5 + 5)
rounds = 80000
def test(self):
for i in xrange(self.rounds):
d1 = {}
d2 = {}
d3 = {}
d4 = {}
d5 = {}
d1 = {1:2,3:4,5:6}
d2 = {2:3,4:5,6:7}
d3 = {3:4,5:6,7:8}
d4 = {4:5,6:7,8:9}
d5 = {6:7,8:9,10:11}
d1 = {}
d2 = {}
d3 = {}
d4 = {}
d5 = {}
d1 = {1:2,3:4,5:6}
d2 = {2:3,4:5,6:7}
d3 = {3:4,5:6,7:8}
d4 = {4:5,6:7,8:9}
d5 = {6:7,8:9,10:11}
d1 = {}
d2 = {}
d3 = {}
d4 = {}
d5 = {}
d1 = {1:2,3:4,5:6}
d2 = {2:3,4:5,6:7}
d3 = {3:4,5:6,7:8}
d4 = {4:5,6:7,8:9}
d5 = {6:7,8:9,10:11}
d1 = {}
d2 = {}
d3 = {}
d4 = {}
d5 = {}
d1 = {1:2,3:4,5:6}
d2 = {2:3,4:5,6:7}
d3 = {3:4,5:6,7:8}
d4 = {4:5,6:7,8:9}
d5 = {6:7,8:9,10:11}
d1 = {}
d2 = {}
d3 = {}
d4 = {}
d5 = {}
d1 = {1:2,3:4,5:6}
d2 = {2:3,4:5,6:7}
d3 = {3:4,5:6,7:8}
d4 = {4:5,6:7,8:9}
d5 = {6:7,8:9,10:11}
def calibrate(self):
for i in xrange(self.rounds):
pass
class DictWithStringKeys(Test):
version = 2.0
operations = 5*(6 + 6)
rounds = 200000
def test(self):
d = {}
for i in xrange(self.rounds):
d['abc'] = 1
d['def'] = 2
d['ghi'] = 3
d['jkl'] = 4
d['mno'] = 5
d['pqr'] = 6
d['abc']
d['def']
d['ghi']
d['jkl']
d['mno']
d['pqr']
d['abc'] = 1
d['def'] = 2
d['ghi'] = 3
d['jkl'] = 4
d['mno'] = 5
d['pqr'] = 6
d['abc']
d['def']
d['ghi']
d['jkl']
d['mno']
d['pqr']
d['abc'] = 1
d['def'] = 2
d['ghi'] = 3
d['jkl'] = 4
d['mno'] = 5
d['pqr'] = 6
d['abc']
d['def']
d['ghi']
d['jkl']
d['mno']
d['pqr']
d['abc'] = 1
d['def'] = 2
d['ghi'] = 3
d['jkl'] = 4
d['mno'] = 5
d['pqr'] = 6
d['abc']
d['def']
d['ghi']
d['jkl']
d['mno']
d['pqr']
d['abc'] = 1
d['def'] = 2
d['ghi'] = 3
d['jkl'] = 4
d['mno'] = 5
d['pqr'] = 6
d['abc']
d['def']
d['ghi']
d['jkl']
d['mno']
d['pqr']
def calibrate(self):
d = {}
for i in xrange(self.rounds):
pass
class DictWithFloatKeys(Test):
version = 2.0
operations = 5*(6 + 6)
rounds = 150000
def test(self):
d = {}
for i in xrange(self.rounds):
d[1.234] = 1
d[2.345] = 2
d[3.456] = 3
d[4.567] = 4
d[5.678] = 5
d[6.789] = 6
d[1.234]
d[2.345]
d[3.456]
d[4.567]
d[5.678]
d[6.789]
d[1.234] = 1
d[2.345] = 2
d[3.456] = 3
d[4.567] = 4
d[5.678] = 5
d[6.789] = 6
d[1.234]
d[2.345]
d[3.456]
d[4.567]
d[5.678]
d[6.789]
d[1.234] = 1
d[2.345] = 2
d[3.456] = 3
d[4.567] = 4
d[5.678] = 5
d[6.789] = 6
d[1.234]
d[2.345]
d[3.456]
d[4.567]
d[5.678]
d[6.789]
d[1.234] = 1
d[2.345] = 2
d[3.456] = 3
d[4.567] = 4
d[5.678] = 5
d[6.789] = 6
d[1.234]
d[2.345]
d[3.456]
d[4.567]
d[5.678]
d[6.789]
d[1.234] = 1
d[2.345] = 2
d[3.456] = 3
d[4.567] = 4
d[5.678] = 5
d[6.789] = 6
d[1.234]
d[2.345]
d[3.456]
d[4.567]
d[5.678]
d[6.789]
def calibrate(self):
d = {}
for i in xrange(self.rounds):
pass
class DictWithIntegerKeys(Test):
version = 2.0
operations = 5*(6 + 6)
rounds = 200000
def test(self):
d = {}
for i in xrange(self.rounds):
d[1] = 1
d[2] = 2
d[3] = 3
d[4] = 4
d[5] = 5
d[6] = 6
d[1]
d[2]
d[3]
d[4]
d[5]
d[6]
d[1] = 1
d[2] = 2
d[3] = 3
d[4] = 4
d[5] = 5
d[6] = 6
d[1]
d[2]
d[3]
d[4]
d[5]
d[6]
d[1] = 1
d[2] = 2
d[3] = 3
d[4] = 4
d[5] = 5
d[6] = 6
d[1]
d[2]
d[3]
d[4]
d[5]
d[6]
d[1] = 1
d[2] = 2
d[3] = 3
d[4] = 4
d[5] = 5
d[6] = 6
d[1]
d[2]
d[3]
d[4]
d[5]
d[6]
d[1] = 1
d[2] = 2
d[3] = 3
d[4] = 4
d[5] = 5
d[6] = 6
d[1]
d[2]
d[3]
d[4]
d[5]
d[6]
def calibrate(self):
d = {}
for i in xrange(self.rounds):
pass
class SimpleDictManipulation(Test):
version = 2.0
operations = 5*(6 + 6 + 6 + 6)
rounds = 100000
def test(self):
d = {}
has_key = d.has_key
for i in xrange(self.rounds):
d[0] = 3
d[1] = 4
d[2] = 5
d[3] = 3
d[4] = 4
d[5] = 5
x = d[0]
x = d[1]
x = d[2]
x = d[3]
x = d[4]
x = d[5]
has_key(0)
has_key(2)
has_key(4)
has_key(6)
has_key(8)
has_key(10)
del d[0]
del d[1]
del d[2]
del d[3]
del d[4]
del d[5]
d[0] = 3
d[1] = 4
d[2] = 5
d[3] = 3
d[4] = 4
d[5] = 5
x = d[0]
x = d[1]
x = d[2]
x = d[3]
x = d[4]
x = d[5]
has_key(0)
has_key(2)
has_key(4)
has_key(6)
has_key(8)
has_key(10)
del d[0]
del d[1]
del d[2]
del d[3]
del d[4]
del d[5]
d[0] = 3
d[1] = 4
d[2] = 5
d[3] = 3
d[4] = 4
d[5] = 5
x = d[0]
x = d[1]
x = d[2]
x = d[3]
x = d[4]
x = d[5]
has_key(0)
has_key(2)
has_key(4)
has_key(6)
has_key(8)
has_key(10)
del d[0]
del d[1]
del d[2]
del d[3]
del d[4]
del d[5]
d[0] = 3
d[1] = 4
d[2] = 5
d[3] = 3
d[4] = 4
d[5] = 5
x = d[0]
x = d[1]
x = d[2]
x = d[3]
x = d[4]
x = d[5]
has_key(0)
has_key(2)
has_key(4)
has_key(6)
has_key(8)
has_key(10)
del d[0]
del d[1]
del d[2]
del d[3]
del d[4]
del d[5]
d[0] = 3
d[1] = 4
d[2] = 5
d[3] = 3
d[4] = 4
d[5] = 5
x = d[0]
x = d[1]
x = d[2]
x = d[3]
x = d[4]
x = d[5]
has_key(0)
has_key(2)
has_key(4)
has_key(6)
has_key(8)
has_key(10)
del d[0]
del d[1]
del d[2]
del d[3]
del d[4]
del d[5]
def calibrate(self):
d = {}
has_key = d.has_key
for i in xrange(self.rounds):
pass

View File

@@ -0,0 +1,699 @@
from pybench import Test
class TryRaiseExcept(Test):
version = 2.0
operations = 2 + 3 + 3
rounds = 80000
def test(self):
error = ValueError
for i in xrange(self.rounds):
try:
raise error
except:
pass
try:
raise error
except:
pass
try:
raise error,"something"
except:
pass
try:
raise error,"something"
except:
pass
try:
raise error,"something"
except:
pass
try:
raise error("something")
except:
pass
try:
raise error("something")
except:
pass
try:
raise error("something")
except:
pass
def calibrate(self):
error = ValueError
for i in xrange(self.rounds):
pass
class TryExcept(Test):
version = 2.0
operations = 15 * 10
rounds = 150000
def test(self):
for i in xrange(self.rounds):
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
try:
pass
except:
pass
def calibrate(self):
for i in xrange(self.rounds):
pass
### Test to make Fredrik happy...
if __name__ == '__main__':
import timeit
timeit.TestClass = TryRaiseExcept
timeit.main(['-s', 'test = TestClass(); test.rounds = 1000',
'test.test()'])

View File

@@ -0,0 +1,138 @@
from pybench import Test
# First imports:
import os
import package.submodule
class SecondImport(Test):
version = 2.0
operations = 5 * 5
rounds = 40000
def test(self):
for i in xrange(self.rounds):
import os
import os
import os
import os
import os
import os
import os
import os
import os
import os
import os
import os
import os
import os
import os
import os
import os
import os
import os
import os
import os
import os
import os
import os
import os
def calibrate(self):
for i in xrange(self.rounds):
pass
class SecondPackageImport(Test):
version = 2.0
operations = 5 * 5
rounds = 40000
def test(self):
for i in xrange(self.rounds):
import package
import package
import package
import package
import package
import package
import package
import package
import package
import package
import package
import package
import package
import package
import package
import package
import package
import package
import package
import package
import package
import package
import package
import package
import package
def calibrate(self):
for i in xrange(self.rounds):
pass
class SecondSubmoduleImport(Test):
version = 2.0
operations = 5 * 5
rounds = 40000
def test(self):
for i in xrange(self.rounds):
import package.submodule
import package.submodule
import package.submodule
import package.submodule
import package.submodule
import package.submodule
import package.submodule
import package.submodule
import package.submodule
import package.submodule
import package.submodule
import package.submodule
import package.submodule
import package.submodule
import package.submodule
import package.submodule
import package.submodule
import package.submodule
import package.submodule
import package.submodule
import package.submodule
import package.submodule
import package.submodule
import package.submodule
import package.submodule
def calibrate(self):
for i in xrange(self.rounds):
pass

View File

@@ -0,0 +1,66 @@
from pybench import Test
class CreateInstances(Test):
version = 2.0
operations = 3 + 7 + 4
rounds = 80000
def test(self):
class c:
pass
class d:
def __init__(self,a,b,c):
self.a = a
self.b = b
self.c = c
class e:
def __init__(self,a,b,c=4):
self.a = a
self.b = b
self.c = c
self.d = a
self.e = b
self.f = c
for i in xrange(self.rounds):
o = c()
o1 = c()
o2 = c()
p = d(i,i,3)
p1 = d(i,i,3)
p2 = d(i,3,3)
p3 = d(3,i,3)
p4 = d(i,i,i)
p5 = d(3,i,3)
p6 = d(i,i,i)
q = e(i,i,3)
q1 = e(i,i,3)
q2 = e(i,i,3)
q3 = e(i,i)
def calibrate(self):
class c:
pass
class d:
def __init__(self,a,b,c):
self.a = a
self.b = b
self.c = c
class e:
def __init__(self,a,b,c=4):
self.a = a
self.b = b
self.c = c
self.d = a
self.e = b
self.f = c
for i in xrange(self.rounds):
pass

View File

@@ -0,0 +1,25 @@
pybench License
---------------
This copyright notice and license applies to all files in the pybench
directory of the pybench distribution.
Copyright (c), 1997-2006, Marc-Andre Lemburg (mal@lemburg.com)
Copyright (c), 2000-2006, eGenix.com Software GmbH (info@egenix.com)
All Rights Reserved.
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee or royalty is hereby
granted, provided that the above copyright notice appear in all copies
and that both that copyright notice and this permission notice appear
in supporting documentation or portions thereof, including
modifications, that you make.
THE AUTHOR MARC-ANDRE LEMBURG DISCLAIMS ALL WARRANTIES WITH REGARD TO
THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS, IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL,
INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
WITH THE USE OR PERFORMANCE OF THIS SOFTWARE !

View File

@@ -0,0 +1,350 @@
from pybench import Test
class SimpleListManipulation(Test):
version = 2.0
operations = 5* (6 + 6 + 6)
rounds = 130000
def test(self):
l = []
append = l.append
for i in xrange(self.rounds):
append(2)
append(3)
append(4)
append(2)
append(3)
append(4)
l[0] = 3
l[1] = 4
l[2] = 5
l[3] = 3
l[4] = 4
l[5] = 5
x = l[0]
x = l[1]
x = l[2]
x = l[3]
x = l[4]
x = l[5]
append(2)
append(3)
append(4)
append(2)
append(3)
append(4)
l[0] = 3
l[1] = 4
l[2] = 5
l[3] = 3
l[4] = 4
l[5] = 5
x = l[0]
x = l[1]
x = l[2]
x = l[3]
x = l[4]
x = l[5]
append(2)
append(3)
append(4)
append(2)
append(3)
append(4)
l[0] = 3
l[1] = 4
l[2] = 5
l[3] = 3
l[4] = 4
l[5] = 5
x = l[0]
x = l[1]
x = l[2]
x = l[3]
x = l[4]
x = l[5]
append(2)
append(3)
append(4)
append(2)
append(3)
append(4)
l[0] = 3
l[1] = 4
l[2] = 5
l[3] = 3
l[4] = 4
l[5] = 5
x = l[0]
x = l[1]
x = l[2]
x = l[3]
x = l[4]
x = l[5]
append(2)
append(3)
append(4)
append(2)
append(3)
append(4)
l[0] = 3
l[1] = 4
l[2] = 5
l[3] = 3
l[4] = 4
l[5] = 5
x = l[0]
x = l[1]
x = l[2]
x = l[3]
x = l[4]
x = l[5]
if len(l) > 10000:
# cut down the size
del l[:]
def calibrate(self):
l = []
append = l.append
for i in xrange(self.rounds):
pass
class ListSlicing(Test):
version = 2.0
operations = 25*(3+1+2+1)
rounds = 800
def test(self):
n = range(100)
r = range(25)
for i in xrange(self.rounds):
l = n[:]
for j in r:
m = l[50:]
m = l[:25]
m = l[50:55]
l[:3] = n
m = l[:-1]
m = l[1:]
l[-1:] = n
def calibrate(self):
n = range(100)
r = range(25)
for i in xrange(self.rounds):
for j in r:
pass
class SmallLists(Test):
version = 2.0
operations = 5*(1+ 6 + 6 + 3 + 1)
rounds = 80000
def test(self):
for i in xrange(self.rounds):
l = []
append = l.append
append(2)
append(3)
append(4)
append(2)
append(3)
append(4)
l[0] = 3
l[1] = 4
l[2] = 5
l[3] = 3
l[4] = 4
l[5] = 5
l[:3] = [1,2,3]
m = l[:-1]
m = l[1:]
l[-1:] = [4,5,6]
l = []
append = l.append
append(2)
append(3)
append(4)
append(2)
append(3)
append(4)
l[0] = 3
l[1] = 4
l[2] = 5
l[3] = 3
l[4] = 4
l[5] = 5
l[:3] = [1,2,3]
m = l[:-1]
m = l[1:]
l[-1:] = [4,5,6]
l = []
append = l.append
append(2)
append(3)
append(4)
append(2)
append(3)
append(4)
l[0] = 3
l[1] = 4
l[2] = 5
l[3] = 3
l[4] = 4
l[5] = 5
l[:3] = [1,2,3]
m = l[:-1]
m = l[1:]
l[-1:] = [4,5,6]
l = []
append = l.append
append(2)
append(3)
append(4)
append(2)
append(3)
append(4)
l[0] = 3
l[1] = 4
l[2] = 5
l[3] = 3
l[4] = 4
l[5] = 5
l[:3] = [1,2,3]
m = l[:-1]
m = l[1:]
l[-1:] = [4,5,6]
l = []
append = l.append
append(2)
append(3)
append(4)
append(2)
append(3)
append(4)
l[0] = 3
l[1] = 4
l[2] = 5
l[3] = 3
l[4] = 4
l[5] = 5
l[:3] = [1,2,3]
m = l[:-1]
m = l[1:]
l[-1:] = [4,5,6]
def calibrate(self):
for i in xrange(self.rounds):
pass
class SimpleListComprehensions(Test):
version = 2.0
operations = 6
rounds = 20000
def test(self):
n = range(10) * 10
for i in xrange(self.rounds):
l = [x for x in n]
l = [x for x in n if x]
l = [x for x in n if not x]
l = [x for x in n]
l = [x for x in n if x]
l = [x for x in n if not x]
def calibrate(self):
n = range(10) * 10
for i in xrange(self.rounds):
pass
class NestedListComprehensions(Test):
version = 2.0
operations = 6
rounds = 20000
def test(self):
m = range(10)
n = range(10)
for i in xrange(self.rounds):
l = [x for x in n for y in m]
l = [y for x in n for y in m]
l = [x for x in n for y in m if y]
l = [y for x in n for y in m if x]
l = [x for x in n for y in m if not y]
l = [y for x in n for y in m if not x]
def calibrate(self):
m = range(10)
n = range(10)
for i in xrange(self.rounds):
pass

View File

@@ -0,0 +1,945 @@
from pybench import Test
class SpecialClassAttribute(Test):
version = 2.0
operations = 5*(12 + 12)
rounds = 100000
def test(self):
class c:
pass
for i in xrange(self.rounds):
c.__a = 2
c.__b = 3
c.__c = 4
c.__a = 2
c.__b = 3
c.__c = 4
c.__a = 2
c.__b = 3
c.__c = 4
c.__a = 2
c.__b = 3
c.__c = 4
x = c.__a
x = c.__b
x = c.__c
x = c.__a
x = c.__b
x = c.__c
x = c.__a
x = c.__b
x = c.__c
x = c.__a
x = c.__b
x = c.__c
c.__a = 2
c.__b = 3
c.__c = 4
c.__a = 2
c.__b = 3
c.__c = 4
c.__a = 2
c.__b = 3
c.__c = 4
c.__a = 2
c.__b = 3
c.__c = 4
x = c.__a
x = c.__b
x = c.__c
x = c.__a
x = c.__b
x = c.__c
x = c.__a
x = c.__b
x = c.__c
x = c.__a
x = c.__b
x = c.__c
c.__a = 2
c.__b = 3
c.__c = 4
c.__a = 2
c.__b = 3
c.__c = 4
c.__a = 2
c.__b = 3
c.__c = 4
c.__a = 2
c.__b = 3
c.__c = 4
x = c.__a
x = c.__b
x = c.__c
x = c.__a
x = c.__b
x = c.__c
x = c.__a
x = c.__b
x = c.__c
x = c.__a
x = c.__b
x = c.__c
c.__a = 2
c.__b = 3
c.__c = 4
c.__a = 2
c.__b = 3
c.__c = 4
c.__a = 2
c.__b = 3
c.__c = 4
c.__a = 2
c.__b = 3
c.__c = 4
x = c.__a
x = c.__b
x = c.__c
x = c.__a
x = c.__b
x = c.__c
x = c.__a
x = c.__b
x = c.__c
x = c.__a
x = c.__b
x = c.__c
c.__a = 2
c.__b = 3
c.__c = 4
c.__a = 2
c.__b = 3
c.__c = 4
c.__a = 2
c.__b = 3
c.__c = 4
c.__a = 2
c.__b = 3
c.__c = 4
x = c.__a
x = c.__b
x = c.__c
x = c.__a
x = c.__b
x = c.__c
x = c.__a
x = c.__b
x = c.__c
x = c.__a
x = c.__b
x = c.__c
def calibrate(self):
class c:
pass
for i in xrange(self.rounds):
pass
class NormalClassAttribute(Test):
version = 2.0
operations = 5*(12 + 12)
rounds = 100000
def test(self):
class c:
pass
for i in xrange(self.rounds):
c.a = 2
c.b = 3
c.c = 4
c.a = 2
c.b = 3
c.c = 4
c.a = 2
c.b = 3
c.c = 4
c.a = 2
c.b = 3
c.c = 4
x = c.a
x = c.b
x = c.c
x = c.a
x = c.b
x = c.c
x = c.a
x = c.b
x = c.c
x = c.a
x = c.b
x = c.c
c.a = 2
c.b = 3
c.c = 4
c.a = 2
c.b = 3
c.c = 4
c.a = 2
c.b = 3
c.c = 4
c.a = 2
c.b = 3
c.c = 4
x = c.a
x = c.b
x = c.c
x = c.a
x = c.b
x = c.c
x = c.a
x = c.b
x = c.c
x = c.a
x = c.b
x = c.c
c.a = 2
c.b = 3
c.c = 4
c.a = 2
c.b = 3
c.c = 4
c.a = 2
c.b = 3
c.c = 4
c.a = 2
c.b = 3
c.c = 4
x = c.a
x = c.b
x = c.c
x = c.a
x = c.b
x = c.c
x = c.a
x = c.b
x = c.c
x = c.a
x = c.b
x = c.c
c.a = 2
c.b = 3
c.c = 4
c.a = 2
c.b = 3
c.c = 4
c.a = 2
c.b = 3
c.c = 4
c.a = 2
c.b = 3
c.c = 4
x = c.a
x = c.b
x = c.c
x = c.a
x = c.b
x = c.c
x = c.a
x = c.b
x = c.c
x = c.a
x = c.b
x = c.c
c.a = 2
c.b = 3
c.c = 4
c.a = 2
c.b = 3
c.c = 4
c.a = 2
c.b = 3
c.c = 4
c.a = 2
c.b = 3
c.c = 4
x = c.a
x = c.b
x = c.c
x = c.a
x = c.b
x = c.c
x = c.a
x = c.b
x = c.c
x = c.a
x = c.b
x = c.c
def calibrate(self):
class c:
pass
for i in xrange(self.rounds):
pass
class SpecialInstanceAttribute(Test):
version = 2.0
operations = 5*(12 + 12)
rounds = 100000
def test(self):
class c:
pass
o = c()
for i in xrange(self.rounds):
o.__a__ = 2
o.__b__ = 3
o.__c__ = 4
o.__a__ = 2
o.__b__ = 3
o.__c__ = 4
o.__a__ = 2
o.__b__ = 3
o.__c__ = 4
o.__a__ = 2
o.__b__ = 3
o.__c__ = 4
x = o.__a__
x = o.__b__
x = o.__c__
x = o.__a__
x = o.__b__
x = o.__c__
x = o.__a__
x = o.__b__
x = o.__c__
x = o.__a__
x = o.__b__
x = o.__c__
o.__a__ = 2
o.__b__ = 3
o.__c__ = 4
o.__a__ = 2
o.__b__ = 3
o.__c__ = 4
o.__a__ = 2
o.__b__ = 3
o.__c__ = 4
o.__a__ = 2
o.__b__ = 3
o.__c__ = 4
x = o.__a__
x = o.__b__
x = o.__c__
x = o.__a__
x = o.__b__
x = o.__c__
x = o.__a__
x = o.__b__
x = o.__c__
x = o.__a__
x = o.__b__
x = o.__c__
o.__a__ = 2
o.__b__ = 3
o.__c__ = 4
o.__a__ = 2
o.__b__ = 3
o.__c__ = 4
o.__a__ = 2
o.__b__ = 3
o.__c__ = 4
o.__a__ = 2
o.__b__ = 3
o.__c__ = 4
x = o.__a__
x = o.__b__
x = o.__c__
x = o.__a__
x = o.__b__
x = o.__c__
x = o.__a__
x = o.__b__
x = o.__c__
x = o.__a__
x = o.__b__
x = o.__c__
o.__a__ = 2
o.__b__ = 3
o.__c__ = 4
o.__a__ = 2
o.__b__ = 3
o.__c__ = 4
o.__a__ = 2
o.__b__ = 3
o.__c__ = 4
o.__a__ = 2
o.__b__ = 3
o.__c__ = 4
x = o.__a__
x = o.__b__
x = o.__c__
x = o.__a__
x = o.__b__
x = o.__c__
x = o.__a__
x = o.__b__
x = o.__c__
x = o.__a__
x = o.__b__
x = o.__c__
o.__a__ = 2
o.__b__ = 3
o.__c__ = 4
o.__a__ = 2
o.__b__ = 3
o.__c__ = 4
o.__a__ = 2
o.__b__ = 3
o.__c__ = 4
o.__a__ = 2
o.__b__ = 3
o.__c__ = 4
x = o.__a__
x = o.__b__
x = o.__c__
x = o.__a__
x = o.__b__
x = o.__c__
x = o.__a__
x = o.__b__
x = o.__c__
x = o.__a__
x = o.__b__
x = o.__c__
def calibrate(self):
class c:
pass
o = c()
for i in xrange(self.rounds):
pass
class NormalInstanceAttribute(Test):
version = 2.0
operations = 5*(12 + 12)
rounds = 100000
def test(self):
class c:
pass
o = c()
for i in xrange(self.rounds):
o.a = 2
o.b = 3
o.c = 4
o.a = 2
o.b = 3
o.c = 4
o.a = 2
o.b = 3
o.c = 4
o.a = 2
o.b = 3
o.c = 4
x = o.a
x = o.b
x = o.c
x = o.a
x = o.b
x = o.c
x = o.a
x = o.b
x = o.c
x = o.a
x = o.b
x = o.c
o.a = 2
o.b = 3
o.c = 4
o.a = 2
o.b = 3
o.c = 4
o.a = 2
o.b = 3
o.c = 4
o.a = 2
o.b = 3
o.c = 4
x = o.a
x = o.b
x = o.c
x = o.a
x = o.b
x = o.c
x = o.a
x = o.b
x = o.c
x = o.a
x = o.b
x = o.c
o.a = 2
o.b = 3
o.c = 4
o.a = 2
o.b = 3
o.c = 4
o.a = 2
o.b = 3
o.c = 4
o.a = 2
o.b = 3
o.c = 4
x = o.a
x = o.b
x = o.c
x = o.a
x = o.b
x = o.c
x = o.a
x = o.b
x = o.c
x = o.a
x = o.b
x = o.c
o.a = 2
o.b = 3
o.c = 4
o.a = 2
o.b = 3
o.c = 4
o.a = 2
o.b = 3
o.c = 4
o.a = 2
o.b = 3
o.c = 4
x = o.a
x = o.b
x = o.c
x = o.a
x = o.b
x = o.c
x = o.a
x = o.b
x = o.c
x = o.a
x = o.b
x = o.c
o.a = 2
o.b = 3
o.c = 4
o.a = 2
o.b = 3
o.c = 4
o.a = 2
o.b = 3
o.c = 4
o.a = 2
o.b = 3
o.c = 4
x = o.a
x = o.b
x = o.c
x = o.a
x = o.b
x = o.c
x = o.a
x = o.b
x = o.c
x = o.a
x = o.b
x = o.c
def calibrate(self):
class c:
pass
o = c()
for i in xrange(self.rounds):
pass
class BuiltinMethodLookup(Test):
version = 2.0
operations = 5*(3*5 + 3*5)
rounds = 70000
def test(self):
l = []
d = {}
for i in xrange(self.rounds):
l.append
l.append
l.append
l.append
l.append
l.insert
l.insert
l.insert
l.insert
l.insert
l.sort
l.sort
l.sort
l.sort
l.sort
d.has_key
d.has_key
d.has_key
d.has_key
d.has_key
d.items
d.items
d.items
d.items
d.items
d.get
d.get
d.get
d.get
d.get
l.append
l.append
l.append
l.append
l.append
l.insert
l.insert
l.insert
l.insert
l.insert
l.sort
l.sort
l.sort
l.sort
l.sort
d.has_key
d.has_key
d.has_key
d.has_key
d.has_key
d.items
d.items
d.items
d.items
d.items
d.get
d.get
d.get
d.get
d.get
l.append
l.append
l.append
l.append
l.append
l.insert
l.insert
l.insert
l.insert
l.insert
l.sort
l.sort
l.sort
l.sort
l.sort
d.has_key
d.has_key
d.has_key
d.has_key
d.has_key
d.items
d.items
d.items
d.items
d.items
d.get
d.get
d.get
d.get
d.get
l.append
l.append
l.append
l.append
l.append
l.insert
l.insert
l.insert
l.insert
l.insert
l.sort
l.sort
l.sort
l.sort
l.sort
d.has_key
d.has_key
d.has_key
d.has_key
d.has_key
d.items
d.items
d.items
d.items
d.items
d.get
d.get
d.get
d.get
d.get
l.append
l.append
l.append
l.append
l.append
l.insert
l.insert
l.insert
l.insert
l.insert
l.sort
l.sort
l.sort
l.sort
l.sort
d.has_key
d.has_key
d.has_key
d.has_key
d.has_key
d.items
d.items
d.items
d.items
d.items
d.get
d.get
d.get
d.get
d.get
def calibrate(self):
l = []
d = {}
for i in xrange(self.rounds):
pass

View File

@@ -0,0 +1,75 @@
from pybench import Test
# Check for new-style class support:
try:
class c(object):
pass
except NameError:
raise ImportError
###
class CreateNewInstances(Test):
version = 2.0
operations = 3 + 7 + 4
rounds = 60000
def test(self):
class c(object):
pass
class d(object):
def __init__(self,a,b,c):
self.a = a
self.b = b
self.c = c
class e(object):
def __init__(self,a,b,c=4):
self.a = a
self.b = b
self.c = c
self.d = a
self.e = b
self.f = c
for i in xrange(self.rounds):
o = c()
o1 = c()
o2 = c()
p = d(i,i,3)
p1 = d(i,i,3)
p2 = d(i,3,3)
p3 = d(3,i,3)
p4 = d(i,i,i)
p5 = d(3,i,3)
p6 = d(i,i,i)
q = e(i,i,3)
q1 = e(i,i,3)
q2 = e(i,i,3)
q3 = e(i,i)
def calibrate(self):
class c(object):
pass
class d(object):
def __init__(self,a,b,c):
self.a = a
self.b = b
self.c = c
class e(object):
def __init__(self,a,b,c=4):
self.a = a
self.b = b
self.c = c
self.d = a
self.e = b
self.f = c
for i in xrange(self.rounds):
pass

View File

@@ -0,0 +1,784 @@
from pybench import Test
class CompareIntegers(Test):
version = 2.0
operations = 30 * 5
rounds = 120000
def test(self):
for i in xrange(self.rounds):
2 < 3
2 > 3
2 == 3
2 > 3
2 < 3
2 < 3
2 > 3
2 == 3
2 > 3
2 < 3
2 < 3
2 > 3
2 == 3
2 > 3
2 < 3
2 < 3
2 > 3
2 == 3
2 > 3
2 < 3
2 < 3
2 > 3
2 == 3
2 > 3
2 < 3
2 < 3
2 > 3
2 == 3
2 > 3
2 < 3
2 < 3
2 > 3
2 == 3
2 > 3
2 < 3
2 < 3
2 > 3
2 == 3
2 > 3
2 < 3
2 < 3
2 > 3
2 == 3
2 > 3
2 < 3
2 < 3
2 > 3
2 == 3
2 > 3
2 < 3
2 < 3
2 > 3
2 == 3
2 > 3
2 < 3
2 < 3
2 > 3
2 == 3
2 > 3
2 < 3
2 < 3
2 > 3
2 == 3
2 > 3
2 < 3
2 < 3
2 > 3
2 == 3
2 > 3
2 < 3
2 < 3
2 > 3
2 == 3
2 > 3
2 < 3
2 < 3
2 > 3
2 == 3
2 > 3
2 < 3
2 < 3
2 > 3
2 == 3
2 > 3
2 < 3
2 < 3
2 > 3
2 == 3
2 > 3
2 < 3
2 < 3
2 > 3
2 == 3
2 > 3
2 < 3
2 < 3
2 > 3
2 == 3
2 > 3
2 < 3
2 < 3
2 > 3
2 == 3
2 > 3
2 < 3
2 < 3
2 > 3
2 == 3
2 > 3
2 < 3
2 < 3
2 > 3
2 == 3
2 > 3
2 < 3
2 < 3
2 > 3
2 == 3
2 > 3
2 < 3
2 < 3
2 > 3
2 == 3
2 > 3
2 < 3
2 < 3
2 > 3
2 == 3
2 > 3
2 < 3
2 < 3
2 > 3
2 == 3
2 > 3
2 < 3
2 < 3
2 > 3
2 == 3
2 > 3
2 < 3
2 < 3
2 > 3
2 == 3
2 > 3
2 < 3
2 < 3
2 > 3
2 == 3
2 > 3
2 < 3
def calibrate(self):
for i in xrange(self.rounds):
pass
class CompareFloats(Test):
version = 2.0
operations = 30 * 5
rounds = 80000
def test(self):
for i in xrange(self.rounds):
2.1 < 3.31
2.1 > 3.31
2.1 == 3.31
2.1 > 3.31
2.1 < 3.31
2.1 < 3.31
2.1 > 3.31
2.1 == 3.31
2.1 > 3.31
2.1 < 3.31
2.1 < 3.31
2.1 > 3.31
2.1 == 3.31
2.1 > 3.31
2.1 < 3.31
2.1 < 3.31
2.1 > 3.31
2.1 == 3.31
2.1 > 3.31
2.1 < 3.31
2.1 < 3.31
2.1 > 3.31
2.1 == 3.31
2.1 > 3.31
2.1 < 3.31
2.1 < 3.31
2.1 > 3.31
2.1 == 3.31
2.1 > 3.31
2.1 < 3.31
2.1 < 3.31
2.1 > 3.31
2.1 == 3.31
2.1 > 3.31
2.1 < 3.31
2.1 < 3.31
2.1 > 3.31
2.1 == 3.31
2.1 > 3.31
2.1 < 3.31
2.1 < 3.31
2.1 > 3.31
2.1 == 3.31
2.1 > 3.31
2.1 < 3.31
2.1 < 3.31
2.1 > 3.31
2.1 == 3.31
2.1 > 3.31
2.1 < 3.31
2.1 < 3.31
2.1 > 3.31
2.1 == 3.31
2.1 > 3.31
2.1 < 3.31
2.1 < 3.31
2.1 > 3.31
2.1 == 3.31
2.1 > 3.31
2.1 < 3.31
2.1 < 3.31
2.1 > 3.31
2.1 == 3.31
2.1 > 3.31
2.1 < 3.31
2.1 < 3.31
2.1 > 3.31
2.1 == 3.31
2.1 > 3.31
2.1 < 3.31
2.1 < 3.31
2.1 > 3.31
2.1 == 3.31
2.1 > 3.31
2.1 < 3.31
2.1 < 3.31
2.1 > 3.31
2.1 == 3.31
2.1 > 3.31
2.1 < 3.31
2.1 < 3.31
2.1 > 3.31
2.1 == 3.31
2.1 > 3.31
2.1 < 3.31
2.1 < 3.31
2.1 > 3.31
2.1 == 3.31
2.1 > 3.31
2.1 < 3.31
2.1 < 3.31
2.1 > 3.31
2.1 == 3.31
2.1 > 3.31
2.1 < 3.31
2.1 < 3.31
2.1 > 3.31
2.1 == 3.31
2.1 > 3.31
2.1 < 3.31
2.1 < 3.31
2.1 > 3.31
2.1 == 3.31
2.1 > 3.31
2.1 < 3.31
2.1 < 3.31
2.1 > 3.31
2.1 == 3.31
2.1 > 3.31
2.1 < 3.31
2.1 < 3.31
2.1 > 3.31
2.1 == 3.31
2.1 > 3.31
2.1 < 3.31
2.1 < 3.31
2.1 > 3.31
2.1 == 3.31
2.1 > 3.31
2.1 < 3.31
2.1 < 3.31
2.1 > 3.31
2.1 == 3.31
2.1 > 3.31
2.1 < 3.31
2.1 < 3.31
2.1 > 3.31
2.1 == 3.31
2.1 > 3.31
2.1 < 3.31
2.1 < 3.31
2.1 > 3.31
2.1 == 3.31
2.1 > 3.31
2.1 < 3.31
2.1 < 3.31
2.1 > 3.31
2.1 == 3.31
2.1 > 3.31
2.1 < 3.31
2.1 < 3.31
2.1 > 3.31
2.1 == 3.31
2.1 > 3.31
2.1 < 3.31
2.1 < 3.31
2.1 > 3.31
2.1 == 3.31
2.1 > 3.31
2.1 < 3.31
def calibrate(self):
for i in xrange(self.rounds):
pass
class CompareFloatsIntegers(Test):
version = 2.0
operations = 30 * 5
rounds = 60000
def test(self):
for i in xrange(self.rounds):
2.1 < 4
2.1 > 4
2.1 == 4
2.1 > 4
2.1 < 4
2.1 < 4
2.1 > 4
2.1 == 4
2.1 > 4
2.1 < 4
2.1 < 4
2.1 > 4
2.1 == 4
2.1 > 4
2.1 < 4
2.1 < 4
2.1 > 4
2.1 == 4
2.1 > 4
2.1 < 4
2.1 < 4
2.1 > 4
2.1 == 4
2.1 > 4
2.1 < 4
2.1 < 4
2.1 > 4
2.1 == 4
2.1 > 4
2.1 < 4
2.1 < 4
2.1 > 4
2.1 == 4
2.1 > 4
2.1 < 4
2.1 < 4
2.1 > 4
2.1 == 4
2.1 > 4
2.1 < 4
2.1 < 4
2.1 > 4
2.1 == 4
2.1 > 4
2.1 < 4
2.1 < 4
2.1 > 4
2.1 == 4
2.1 > 4
2.1 < 4
2.1 < 4
2.1 > 4
2.1 == 4
2.1 > 4
2.1 < 4
2.1 < 4
2.1 > 4
2.1 == 4
2.1 > 4
2.1 < 4
2.1 < 4
2.1 > 4
2.1 == 4
2.1 > 4
2.1 < 4
2.1 < 4
2.1 > 4
2.1 == 4
2.1 > 4
2.1 < 4
2.1 < 4
2.1 > 4
2.1 == 4
2.1 > 4
2.1 < 4
2.1 < 4
2.1 > 4
2.1 == 4
2.1 > 4
2.1 < 4
2.1 < 4
2.1 > 4
2.1 == 4
2.1 > 4
2.1 < 4
2.1 < 4
2.1 > 4
2.1 == 4
2.1 > 4
2.1 < 4
2.1 < 4
2.1 > 4
2.1 == 4
2.1 > 4
2.1 < 4
2.1 < 4
2.1 > 4
2.1 == 4
2.1 > 4
2.1 < 4
2.1 < 4
2.1 > 4
2.1 == 4
2.1 > 4
2.1 < 4
2.1 < 4
2.1 > 4
2.1 == 4
2.1 > 4
2.1 < 4
2.1 < 4
2.1 > 4
2.1 == 4
2.1 > 4
2.1 < 4
2.1 < 4
2.1 > 4
2.1 == 4
2.1 > 4
2.1 < 4
2.1 < 4
2.1 > 4
2.1 == 4
2.1 > 4
2.1 < 4
2.1 < 4
2.1 > 4
2.1 == 4
2.1 > 4
2.1 < 4
2.1 < 4
2.1 > 4
2.1 == 4
2.1 > 4
2.1 < 4
2.1 < 4
2.1 > 4
2.1 == 4
2.1 > 4
2.1 < 4
2.1 < 4
2.1 > 4
2.1 == 4
2.1 > 4
2.1 < 4
2.1 < 4
2.1 > 4
2.1 == 4
2.1 > 4
2.1 < 4
def calibrate(self):
for i in xrange(self.rounds):
pass
class CompareLongs(Test):
version = 2.0
operations = 30 * 5
rounds = 70000
def test(self):
for i in xrange(self.rounds):
1234567890L < 3456789012345L
1234567890L > 3456789012345L
1234567890L == 3456789012345L
1234567890L > 3456789012345L
1234567890L < 3456789012345L
1234567890L < 3456789012345L
1234567890L > 3456789012345L
1234567890L == 3456789012345L
1234567890L > 3456789012345L
1234567890L < 3456789012345L
1234567890L < 3456789012345L
1234567890L > 3456789012345L
1234567890L == 3456789012345L
1234567890L > 3456789012345L
1234567890L < 3456789012345L
1234567890L < 3456789012345L
1234567890L > 3456789012345L
1234567890L == 3456789012345L
1234567890L > 3456789012345L
1234567890L < 3456789012345L
1234567890L < 3456789012345L
1234567890L > 3456789012345L
1234567890L == 3456789012345L
1234567890L > 3456789012345L
1234567890L < 3456789012345L
1234567890L < 3456789012345L
1234567890L > 3456789012345L
1234567890L == 3456789012345L
1234567890L > 3456789012345L
1234567890L < 3456789012345L
1234567890L < 3456789012345L
1234567890L > 3456789012345L
1234567890L == 3456789012345L
1234567890L > 3456789012345L
1234567890L < 3456789012345L
1234567890L < 3456789012345L
1234567890L > 3456789012345L
1234567890L == 3456789012345L
1234567890L > 3456789012345L
1234567890L < 3456789012345L
1234567890L < 3456789012345L
1234567890L > 3456789012345L
1234567890L == 3456789012345L
1234567890L > 3456789012345L
1234567890L < 3456789012345L
1234567890L < 3456789012345L
1234567890L > 3456789012345L
1234567890L == 3456789012345L
1234567890L > 3456789012345L
1234567890L < 3456789012345L
1234567890L < 3456789012345L
1234567890L > 3456789012345L
1234567890L == 3456789012345L
1234567890L > 3456789012345L
1234567890L < 3456789012345L
1234567890L < 3456789012345L
1234567890L > 3456789012345L
1234567890L == 3456789012345L
1234567890L > 3456789012345L
1234567890L < 3456789012345L
1234567890L < 3456789012345L
1234567890L > 3456789012345L
1234567890L == 3456789012345L
1234567890L > 3456789012345L
1234567890L < 3456789012345L
1234567890L < 3456789012345L
1234567890L > 3456789012345L
1234567890L == 3456789012345L
1234567890L > 3456789012345L
1234567890L < 3456789012345L
1234567890L < 3456789012345L
1234567890L > 3456789012345L
1234567890L == 3456789012345L
1234567890L > 3456789012345L
1234567890L < 3456789012345L
1234567890L < 3456789012345L
1234567890L > 3456789012345L
1234567890L == 3456789012345L
1234567890L > 3456789012345L
1234567890L < 3456789012345L
1234567890L < 3456789012345L
1234567890L > 3456789012345L
1234567890L == 3456789012345L
1234567890L > 3456789012345L
1234567890L < 3456789012345L
1234567890L < 3456789012345L
1234567890L > 3456789012345L
1234567890L == 3456789012345L
1234567890L > 3456789012345L
1234567890L < 3456789012345L
1234567890L < 3456789012345L
1234567890L > 3456789012345L
1234567890L == 3456789012345L
1234567890L > 3456789012345L
1234567890L < 3456789012345L
1234567890L < 3456789012345L
1234567890L > 3456789012345L
1234567890L == 3456789012345L
1234567890L > 3456789012345L
1234567890L < 3456789012345L
1234567890L < 3456789012345L
1234567890L > 3456789012345L
1234567890L == 3456789012345L
1234567890L > 3456789012345L
1234567890L < 3456789012345L
1234567890L < 3456789012345L
1234567890L > 3456789012345L
1234567890L == 3456789012345L
1234567890L > 3456789012345L
1234567890L < 3456789012345L
1234567890L < 3456789012345L
1234567890L > 3456789012345L
1234567890L == 3456789012345L
1234567890L > 3456789012345L
1234567890L < 3456789012345L
1234567890L < 3456789012345L
1234567890L > 3456789012345L
1234567890L == 3456789012345L
1234567890L > 3456789012345L
1234567890L < 3456789012345L
1234567890L < 3456789012345L
1234567890L > 3456789012345L
1234567890L == 3456789012345L
1234567890L > 3456789012345L
1234567890L < 3456789012345L
1234567890L < 3456789012345L
1234567890L > 3456789012345L
1234567890L == 3456789012345L
1234567890L > 3456789012345L
1234567890L < 3456789012345L
1234567890L < 3456789012345L
1234567890L > 3456789012345L
1234567890L == 3456789012345L
1234567890L > 3456789012345L
1234567890L < 3456789012345L
1234567890L < 3456789012345L
1234567890L > 3456789012345L
1234567890L == 3456789012345L
1234567890L > 3456789012345L
1234567890L < 3456789012345L
1234567890L < 3456789012345L
1234567890L > 3456789012345L
1234567890L == 3456789012345L
1234567890L > 3456789012345L
1234567890L < 3456789012345L
1234567890L < 3456789012345L
1234567890L > 3456789012345L
1234567890L == 3456789012345L
1234567890L > 3456789012345L
1234567890L < 3456789012345L
def calibrate(self):
for i in xrange(self.rounds):
pass

View File

@@ -0,0 +1,368 @@
________________________________________________________________________
PYBENCH - A Python Benchmark Suite
________________________________________________________________________
Extendable suite of of low-level benchmarks for measuring
the performance of the Python implementation
(interpreter, compiler or VM).
pybench is a collection of tests that provides a standardized way to
measure the performance of Python implementations. It takes a very
close look at different aspects of Python programs and let's you
decide which factors are more important to you than others, rather
than wrapping everything up in one number, like the other performance
tests do (e.g. pystone which is included in the Python Standard
Library).
pybench has been used in the past by several Python developers to
track down performance bottlenecks or to demonstrate the impact of
optimizations and new features in Python.
The command line interface for pybench is the file pybench.py. Run
this script with option '--help' to get a listing of the possible
options. Without options, pybench will simply execute the benchmark
and then print out a report to stdout.
Micro-Manual
------------
Run 'pybench.py -h' to see the help screen. Run 'pybench.py' to run
the benchmark suite using default settings and 'pybench.py -f <file>'
to have it store the results in a file too.
It is usually a good idea to run pybench.py multiple times to see
whether the environment, timers and benchmark run-times are suitable
for doing benchmark tests.
You can use the comparison feature of pybench.py ('pybench.py -c
<file>') to check how well the system behaves in comparison to a
reference run.
If the differences are well below 10% for each test, then you have a
system that is good for doing benchmark testings. Of you get random
differences of more than 10% or significant differences between the
values for minimum and average time, then you likely have some
background processes running which cause the readings to become
inconsistent. Examples include: web-browsers, email clients, RSS
readers, music players, backup programs, etc.
If you are only interested in a few tests of the whole suite, you can
use the filtering option, e.g. 'pybench.py -t string' will only
run/show the tests that have 'string' in their name.
This is the current output of pybench.py --help:
"""
------------------------------------------------------------------------
PYBENCH - a benchmark test suite for Python interpreters/compilers.
------------------------------------------------------------------------
Synopsis:
pybench.py [option] files...
Options and default settings:
-n arg number of rounds (10)
-f arg save benchmark to file arg ()
-c arg compare benchmark with the one in file arg ()
-s arg show benchmark in file arg, then exit ()
-w arg set warp factor to arg (10)
-t arg run only tests with names matching arg ()
-C arg set the number of calibration runs to arg (20)
-d hide noise in comparisons (0)
-v verbose output (not recommended) (0)
--with-gc enable garbage collection (0)
--with-syscheck use default sys check interval (0)
--timer arg use given timer (time.time)
-h show this help text
--help show this help text
--debug enable debugging
--copyright show copyright
--examples show examples of usage
Version:
2.0
The normal operation is to run the suite and display the
results. Use -f to save them for later reuse or comparisons.
Available timers:
time.time
time.clock
systimes.processtime
Examples:
python2.1 pybench.py -f p21.pybench
python2.5 pybench.py -f p25.pybench
python pybench.py -s p25.pybench -c p21.pybench
"""
License
-------
See LICENSE file.
Sample output
-------------
"""
-------------------------------------------------------------------------------
PYBENCH 2.0
-------------------------------------------------------------------------------
* using Python 2.4.2
* disabled garbage collection
* system check interval set to maximum: 2147483647
* using timer: time.time
Calibrating tests. Please wait...
Running 10 round(s) of the suite at warp factor 10:
* Round 1 done in 6.388 seconds.
* Round 2 done in 6.485 seconds.
* Round 3 done in 6.786 seconds.
...
* Round 10 done in 6.546 seconds.
-------------------------------------------------------------------------------
Benchmark: 2006-06-12 12:09:25
-------------------------------------------------------------------------------
Rounds: 10
Warp: 10
Timer: time.time
Machine Details:
Platform ID: Linux-2.6.8-24.19-default-x86_64-with-SuSE-9.2-x86-64
Processor: x86_64
Python:
Executable: /usr/local/bin/python
Version: 2.4.2
Compiler: GCC 3.3.4 (pre 3.3.5 20040809)
Bits: 64bit
Build: Oct 1 2005 15:24:35 (#1)
Unicode: UCS2
Test minimum average operation overhead
-------------------------------------------------------------------------------
BuiltinFunctionCalls: 126ms 145ms 0.28us 0.274ms
BuiltinMethodLookup: 124ms 130ms 0.12us 0.316ms
CompareFloats: 109ms 110ms 0.09us 0.361ms
CompareFloatsIntegers: 100ms 104ms 0.12us 0.271ms
CompareIntegers: 137ms 138ms 0.08us 0.542ms
CompareInternedStrings: 124ms 127ms 0.08us 1.367ms
CompareLongs: 100ms 104ms 0.10us 0.316ms
CompareStrings: 111ms 115ms 0.12us 0.929ms
CompareUnicode: 108ms 128ms 0.17us 0.693ms
ConcatStrings: 142ms 155ms 0.31us 0.562ms
ConcatUnicode: 119ms 127ms 0.42us 0.384ms
CreateInstances: 123ms 128ms 1.14us 0.367ms
CreateNewInstances: 121ms 126ms 1.49us 0.335ms
CreateStringsWithConcat: 130ms 135ms 0.14us 0.916ms
CreateUnicodeWithConcat: 130ms 135ms 0.34us 0.361ms
DictCreation: 108ms 109ms 0.27us 0.361ms
DictWithFloatKeys: 149ms 153ms 0.17us 0.678ms
DictWithIntegerKeys: 124ms 126ms 0.11us 0.915ms
DictWithStringKeys: 114ms 117ms 0.10us 0.905ms
ForLoops: 110ms 111ms 4.46us 0.063ms
IfThenElse: 118ms 119ms 0.09us 0.685ms
ListSlicing: 116ms 120ms 8.59us 0.103ms
NestedForLoops: 125ms 137ms 0.09us 0.019ms
NormalClassAttribute: 124ms 136ms 0.11us 0.457ms
NormalInstanceAttribute: 110ms 117ms 0.10us 0.454ms
PythonFunctionCalls: 107ms 113ms 0.34us 0.271ms
PythonMethodCalls: 140ms 149ms 0.66us 0.141ms
Recursion: 156ms 166ms 3.32us 0.452ms
SecondImport: 112ms 118ms 1.18us 0.180ms
SecondPackageImport: 118ms 127ms 1.27us 0.180ms
SecondSubmoduleImport: 140ms 151ms 1.51us 0.180ms
SimpleComplexArithmetic: 128ms 139ms 0.16us 0.361ms
SimpleDictManipulation: 134ms 136ms 0.11us 0.452ms
SimpleFloatArithmetic: 110ms 113ms 0.09us 0.571ms
SimpleIntFloatArithmetic: 106ms 111ms 0.08us 0.548ms
SimpleIntegerArithmetic: 106ms 109ms 0.08us 0.544ms
SimpleListManipulation: 103ms 113ms 0.10us 0.587ms
SimpleLongArithmetic: 112ms 118ms 0.18us 0.271ms
SmallLists: 105ms 116ms 0.17us 0.366ms
SmallTuples: 108ms 128ms 0.24us 0.406ms
SpecialClassAttribute: 119ms 136ms 0.11us 0.453ms
SpecialInstanceAttribute: 143ms 155ms 0.13us 0.454ms
StringMappings: 115ms 121ms 0.48us 0.405ms
StringPredicates: 120ms 129ms 0.18us 2.064ms
StringSlicing: 111ms 127ms 0.23us 0.781ms
TryExcept: 125ms 126ms 0.06us 0.681ms
TryRaiseExcept: 133ms 137ms 2.14us 0.361ms
TupleSlicing: 117ms 120ms 0.46us 0.066ms
UnicodeMappings: 156ms 160ms 4.44us 0.429ms
UnicodePredicates: 117ms 121ms 0.22us 2.487ms
UnicodeProperties: 115ms 153ms 0.38us 2.070ms
UnicodeSlicing: 126ms 129ms 0.26us 0.689ms
-------------------------------------------------------------------------------
Totals: 6283ms 6673ms
"""
________________________________________________________________________
Writing New Tests
________________________________________________________________________
pybench tests are simple modules defining one or more pybench.Test
subclasses.
Writing a test essentially boils down to providing two methods:
.test() which runs .rounds number of .operations test operations each
and .calibrate() which does the same except that it doesn't actually
execute the operations.
Here's an example:
------------------
from pybench import Test
class IntegerCounting(Test):
# Version number of the test as float (x.yy); this is important
# for comparisons of benchmark runs - tests with unequal version
# number will not get compared.
version = 1.0
# The number of abstract operations done in each round of the
# test. An operation is the basic unit of what you want to
# measure. The benchmark will output the amount of run-time per
# operation. Note that in order to raise the measured timings
# significantly above noise level, it is often required to repeat
# sets of operations more than once per test round. The measured
# overhead per test round should be less than 1 second.
operations = 20
# Number of rounds to execute per test run. This should be
# adjusted to a figure that results in a test run-time of between
# 1-2 seconds (at warp 1).
rounds = 100000
def test(self):
""" Run the test.
The test needs to run self.rounds executing
self.operations number of operations each.
"""
# Init the test
a = 1
# Run test rounds
#
# NOTE: Use xrange() for all test loops unless you want to face
# a 20MB process !
#
for i in xrange(self.rounds):
# Repeat the operations per round to raise the run-time
# per operation significantly above the noise level of the
# for-loop overhead.
# Execute 20 operations (a += 1):
a += 1
a += 1
a += 1
a += 1
a += 1
a += 1
a += 1
a += 1
a += 1
a += 1
a += 1
a += 1
a += 1
a += 1
a += 1
a += 1
a += 1
a += 1
a += 1
a += 1
def calibrate(self):
""" Calibrate the test.
This method should execute everything that is needed to
setup and run the test - except for the actual operations
that you intend to measure. pybench uses this method to
measure the test implementation overhead.
"""
# Init the test
a = 1
# Run test rounds (without actually doing any operation)
for i in xrange(self.rounds):
# Skip the actual execution of the operations, since we
# only want to measure the test's administration overhead.
pass
Registering a new test module
-----------------------------
To register a test module with pybench, the classes need to be
imported into the pybench.Setup module. pybench will then scan all the
symbols defined in that module for subclasses of pybench.Test and
automatically add them to the benchmark suite.
Breaking Comparability
----------------------
If a change is made to any individual test that means it is no
longer strictly comparable with previous runs, the '.version' class
variable should be updated. Therefafter, comparisons with previous
versions of the test will list as "n/a" to reflect the change.
Version History
---------------
2.0: rewrote parts of pybench which resulted in more repeatable
timings:
- made timer a parameter
- changed the platform default timer to use high-resolution
timers rather than process timers (which have a much lower
resolution)
- added option to select timer
- added process time timer (using systimes.py)
- changed to use min() as timing estimator (average
is still taken as well to provide an idea of the difference)
- garbage collection is turned off per default
- sys check interval is set to the highest possible value
- calibration is now a separate step and done using
a different strategy that allows measuring the test
overhead more accurately
- modified the tests to each give a run-time of between
100-200ms using warp 10
- changed default warp factor to 10 (from 20)
- compared results with timeit.py and confirmed measurements
- bumped all test versions to 2.0
- updated platform.py to the latest version
- changed the output format a bit to make it look
nicer
- refactored the APIs somewhat
1.3+: Steve Holden added the NewInstances test and the filtering
option during the NeedForSpeed sprint; this also triggered a long
discussion on how to improve benchmark timing and finally
resulted in the release of 2.0
1.3: initial checkin into the Python SVN repository
Have fun,
--
Marc-Andre Lemburg
mal@lemburg.com

View File

@@ -0,0 +1,43 @@
#!/usr/bin/env python
# Setup file for pybench
#
# This file has to import all tests to be run; it is executed as
# Python source file, so you can do all kinds of manipulations here
# rather than having to edit the tests themselves.
#
# Note: Please keep this module compatible to Python 1.5.2.
#
# Tests may include features in later Python versions, but these
# should then be embedded in try-except clauses in this configuration
# module.
# Defaults
Number_of_rounds = 10
Warp_factor = 10
# Import tests
from Arithmetic import *
from Calls import *
from Constructs import *
from Lookups import *
from Instances import *
try:
from NewInstances import *
except ImportError:
pass
from Lists import *
from Tuples import *
from Dict import *
from Exceptions import *
try:
from With import *
except SyntaxError:
pass
from Imports import *
from Strings import *
from Numbers import *
try:
from Unicode import *
except (ImportError, SyntaxError):
pass

View File

@@ -0,0 +1,562 @@
from pybench import Test
from string import join
class ConcatStrings(Test):
version = 2.0
operations = 10 * 5
rounds = 100000
def test(self):
# Make sure the strings are *not* interned
s = join(map(str,range(100)))
t = join(map(str,range(1,101)))
for i in xrange(self.rounds):
t + s
t + s
t + s
t + s
t + s
t + s
t + s
t + s
t + s
t + s
t + s
t + s
t + s
t + s
t + s
t + s
t + s
t + s
t + s
t + s
t + s
t + s
t + s
t + s
t + s
t + s
t + s
t + s
t + s
t + s
t + s
t + s
t + s
t + s
t + s
t + s
t + s
t + s
t + s
t + s
t + s
t + s
t + s
t + s
t + s
t + s
t + s
t + s
t + s
t + s
def calibrate(self):
s = join(map(str,range(100)))
t = join(map(str,range(1,101)))
for i in xrange(self.rounds):
pass
class CompareStrings(Test):
version = 2.0
operations = 10 * 5
rounds = 200000
def test(self):
# Make sure the strings are *not* interned
s = join(map(str,range(10)))
t = join(map(str,range(10))) + "abc"
for i in xrange(self.rounds):
t < s
t > s
t == s
t > s
t < s
t < s
t > s
t == s
t > s
t < s
t < s
t > s
t == s
t > s
t < s
t < s
t > s
t == s
t > s
t < s
t < s
t > s
t == s
t > s
t < s
t < s
t > s
t == s
t > s
t < s
t < s
t > s
t == s
t > s
t < s
t < s
t > s
t == s
t > s
t < s
t < s
t > s
t == s
t > s
t < s
t < s
t > s
t == s
t > s
t < s
def calibrate(self):
s = join(map(str,range(10)))
t = join(map(str,range(10))) + "abc"
for i in xrange(self.rounds):
pass
class CompareInternedStrings(Test):
version = 2.0
operations = 10 * 5
rounds = 300000
def test(self):
# Make sure the strings *are* interned
s = intern(join(map(str,range(10))))
t = s
for i in xrange(self.rounds):
t == s
t == s
t >= s
t > s
t < s
t == s
t == s
t >= s
t > s
t < s
t == s
t == s
t >= s
t > s
t < s
t == s
t == s
t >= s
t > s
t < s
t == s
t == s
t >= s
t > s
t < s
t == s
t == s
t >= s
t > s
t < s
t == s
t == s
t >= s
t > s
t < s
t == s
t == s
t >= s
t > s
t < s
t == s
t == s
t >= s
t > s
t < s
t == s
t == s
t >= s
t > s
t < s
def calibrate(self):
s = intern(join(map(str,range(10))))
t = s
for i in xrange(self.rounds):
pass
class CreateStringsWithConcat(Test):
version = 2.0
operations = 10 * 5
rounds = 200000
def test(self):
for i in xrange(self.rounds):
s = 'om'
s = s + 'xbx'
s = s + 'xcx'
s = s + 'xdx'
s = s + 'xex'
s = s + 'xax'
s = s + 'xbx'
s = s + 'xcx'
s = s + 'xdx'
s = s + 'xex'
s = s + 'xax'
s = s + 'xbx'
s = s + 'xcx'
s = s + 'xdx'
s = s + 'xex'
s = s + 'xax'
s = s + 'xbx'
s = s + 'xcx'
s = s + 'xdx'
s = s + 'xex'
s = s + 'xax'
s = s + 'xbx'
s = s + 'xcx'
s = s + 'xdx'
s = s + 'xex'
s = s + 'xax'
s = s + 'xbx'
s = s + 'xcx'
s = s + 'xdx'
s = s + 'xex'
s = s + 'xax'
s = s + 'xbx'
s = s + 'xcx'
s = s + 'xdx'
s = s + 'xex'
s = s + 'xax'
s = s + 'xbx'
s = s + 'xcx'
s = s + 'xdx'
s = s + 'xex'
s = s + 'xax'
s = s + 'xbx'
s = s + 'xcx'
s = s + 'xdx'
s = s + 'xex'
s = s + 'xax'
s = s + 'xbx'
s = s + 'xcx'
s = s + 'xdx'
s = s + 'xex'
def calibrate(self):
for i in xrange(self.rounds):
pass
class StringSlicing(Test):
version = 2.0
operations = 5 * 7
rounds = 160000
def test(self):
s = join(map(str,range(100)))
for i in xrange(self.rounds):
s[50:]
s[:25]
s[50:55]
s[-1:]
s[:1]
s[2:]
s[11:-11]
s[50:]
s[:25]
s[50:55]
s[-1:]
s[:1]
s[2:]
s[11:-11]
s[50:]
s[:25]
s[50:55]
s[-1:]
s[:1]
s[2:]
s[11:-11]
s[50:]
s[:25]
s[50:55]
s[-1:]
s[:1]
s[2:]
s[11:-11]
s[50:]
s[:25]
s[50:55]
s[-1:]
s[:1]
s[2:]
s[11:-11]
def calibrate(self):
s = join(map(str,range(100)))
for i in xrange(self.rounds):
pass
### String methods
if hasattr('', 'lower'):
class StringMappings(Test):
version = 2.0
operations = 3 * (5 + 4 + 2 + 1)
rounds = 70000
def test(self):
s = join(map(chr,range(20)),'')
t = join(map(chr,range(50)),'')
u = join(map(chr,range(100)),'')
v = join(map(chr,range(256)),'')
for i in xrange(self.rounds):
s.lower()
s.lower()
s.lower()
s.lower()
s.lower()
s.upper()
s.upper()
s.upper()
s.upper()
s.upper()
s.title()
s.title()
s.title()
s.title()
s.title()
t.lower()
t.lower()
t.lower()
t.lower()
t.upper()
t.upper()
t.upper()
t.upper()
t.title()
t.title()
t.title()
t.title()
u.lower()
u.lower()
u.upper()
u.upper()
u.title()
u.title()
v.lower()
v.upper()
v.title()
def calibrate(self):
s = join(map(chr,range(20)),'')
t = join(map(chr,range(50)),'')
u = join(map(chr,range(100)),'')
v = join(map(chr,range(256)),'')
for i in xrange(self.rounds):
pass
class StringPredicates(Test):
version = 2.0
operations = 10 * 7
rounds = 100000
def test(self):
data = ('abc', '123', ' ', '\xe4\xf6\xfc', '\xdf'*10)
len_data = len(data)
for i in xrange(self.rounds):
s = data[i % len_data]
s.isalnum()
s.isalpha()
s.isdigit()
s.islower()
s.isspace()
s.istitle()
s.isupper()
s.isalnum()
s.isalpha()
s.isdigit()
s.islower()
s.isspace()
s.istitle()
s.isupper()
s.isalnum()
s.isalpha()
s.isdigit()
s.islower()
s.isspace()
s.istitle()
s.isupper()
s.isalnum()
s.isalpha()
s.isdigit()
s.islower()
s.isspace()
s.istitle()
s.isupper()
s.isalnum()
s.isalpha()
s.isdigit()
s.islower()
s.isspace()
s.istitle()
s.isupper()
s.isalnum()
s.isalpha()
s.isdigit()
s.islower()
s.isspace()
s.istitle()
s.isupper()
s.isalnum()
s.isalpha()
s.isdigit()
s.islower()
s.isspace()
s.istitle()
s.isupper()
s.isalnum()
s.isalpha()
s.isdigit()
s.islower()
s.isspace()
s.istitle()
s.isupper()
s.isalnum()
s.isalpha()
s.isdigit()
s.islower()
s.isspace()
s.istitle()
s.isupper()
s.isalnum()
s.isalpha()
s.isdigit()
s.islower()
s.isspace()
s.istitle()
s.isupper()
def calibrate(self):
data = ('abc', '123', ' ', '\u1234\u2345\u3456', '\uFFFF'*10)
data = ('abc', '123', ' ', '\xe4\xf6\xfc', '\xdf'*10)
len_data = len(data)
for i in xrange(self.rounds):
s = data[i % len_data]

View File

@@ -0,0 +1,360 @@
from pybench import Test
class TupleSlicing(Test):
version = 2.0
operations = 3 * 25 * 10 * 7
rounds = 500
def test(self):
r = range(25)
t = tuple(range(100))
for i in xrange(self.rounds):
for j in r:
m = t[50:]
m = t[:25]
m = t[50:55]
m = t[:-1]
m = t[1:]
m = t[-10:]
m = t[:10]
m = t[50:]
m = t[:25]
m = t[50:55]
m = t[:-1]
m = t[1:]
m = t[-10:]
m = t[:10]
m = t[50:]
m = t[:25]
m = t[50:55]
m = t[:-1]
m = t[1:]
m = t[-10:]
m = t[:10]
m = t[50:]
m = t[:25]
m = t[50:55]
m = t[:-1]
m = t[1:]
m = t[-10:]
m = t[:10]
m = t[50:]
m = t[:25]
m = t[50:55]
m = t[:-1]
m = t[1:]
m = t[-10:]
m = t[:10]
m = t[50:]
m = t[:25]
m = t[50:55]
m = t[:-1]
m = t[1:]
m = t[-10:]
m = t[:10]
m = t[50:]
m = t[:25]
m = t[50:55]
m = t[:-1]
m = t[1:]
m = t[-10:]
m = t[:10]
m = t[50:]
m = t[:25]
m = t[50:55]
m = t[:-1]
m = t[1:]
m = t[-10:]
m = t[:10]
m = t[50:]
m = t[:25]
m = t[50:55]
m = t[:-1]
m = t[1:]
m = t[-10:]
m = t[:10]
m = t[50:]
m = t[:25]
m = t[50:55]
m = t[:-1]
m = t[1:]
m = t[-10:]
m = t[:10]
m = t[50:]
m = t[:25]
m = t[50:55]
m = t[:-1]
m = t[1:]
m = t[-10:]
m = t[:10]
m = t[50:]
m = t[:25]
m = t[50:55]
m = t[:-1]
m = t[1:]
m = t[-10:]
m = t[:10]
m = t[50:]
m = t[:25]
m = t[50:55]
m = t[:-1]
m = t[1:]
m = t[-10:]
m = t[:10]
m = t[50:]
m = t[:25]
m = t[50:55]
m = t[:-1]
m = t[1:]
m = t[-10:]
m = t[:10]
m = t[50:]
m = t[:25]
m = t[50:55]
m = t[:-1]
m = t[1:]
m = t[-10:]
m = t[:10]
m = t[50:]
m = t[:25]
m = t[50:55]
m = t[:-1]
m = t[1:]
m = t[-10:]
m = t[:10]
m = t[50:]
m = t[:25]
m = t[50:55]
m = t[:-1]
m = t[1:]
m = t[-10:]
m = t[:10]
m = t[50:]
m = t[:25]
m = t[50:55]
m = t[:-1]
m = t[1:]
m = t[-10:]
m = t[:10]
m = t[50:]
m = t[:25]
m = t[50:55]
m = t[:-1]
m = t[1:]
m = t[-10:]
m = t[:10]
m = t[50:]
m = t[:25]
m = t[50:55]
m = t[:-1]
m = t[1:]
m = t[-10:]
m = t[:10]
m = t[50:]
m = t[:25]
m = t[50:55]
m = t[:-1]
m = t[1:]
m = t[-10:]
m = t[:10]
m = t[50:]
m = t[:25]
m = t[50:55]
m = t[:-1]
m = t[1:]
m = t[-10:]
m = t[:10]
m = t[50:]
m = t[:25]
m = t[50:55]
m = t[:-1]
m = t[1:]
m = t[-10:]
m = t[:10]
m = t[50:]
m = t[:25]
m = t[50:55]
m = t[:-1]
m = t[1:]
m = t[-10:]
m = t[:10]
m = t[50:]
m = t[:25]
m = t[50:55]
m = t[:-1]
m = t[1:]
m = t[-10:]
m = t[:10]
m = t[50:]
m = t[:25]
m = t[50:55]
m = t[:-1]
m = t[1:]
m = t[-10:]
m = t[:10]
m = t[50:]
m = t[:25]
m = t[50:55]
m = t[:-1]
m = t[1:]
m = t[-10:]
m = t[:10]
m = t[50:]
m = t[:25]
m = t[50:55]
m = t[:-1]
m = t[1:]
m = t[-10:]
m = t[:10]
m = t[50:]
m = t[:25]
m = t[50:55]
m = t[:-1]
m = t[1:]
m = t[-10:]
m = t[:10]
m = t[50:]
m = t[:25]
m = t[50:55]
m = t[:-1]
m = t[1:]
m = t[-10:]
m = t[:10]
def calibrate(self):
r = range(25)
t = tuple(range(100))
for i in xrange(self.rounds):
for j in r:
pass
class SmallTuples(Test):
version = 2.0
operations = 5*(1 + 3 + 6 + 2)
rounds = 90000
def test(self):
for i in xrange(self.rounds):
t = (1,2,3,4,5,6)
a,b,c,d,e,f = t
a,b,c,d,e,f = t
a,b,c,d,e,f = t
a,b,c = t[:3]
a,b,c = t[:3]
a,b,c = t[:3]
a,b,c = t[:3]
a,b,c = t[:3]
a,b,c = t[:3]
l = list(t)
t = tuple(l)
t = (1,2,3,4,5,6)
a,b,c,d,e,f = t
a,b,c,d,e,f = t
a,b,c,d,e,f = t
a,b,c = t[:3]
a,b,c = t[:3]
a,b,c = t[:3]
a,b,c = t[:3]
a,b,c = t[:3]
a,b,c = t[:3]
l = list(t)
t = tuple(l)
t = (1,2,3,4,5,6)
a,b,c,d,e,f = t
a,b,c,d,e,f = t
a,b,c,d,e,f = t
a,b,c = t[:3]
a,b,c = t[:3]
a,b,c = t[:3]
a,b,c = t[:3]
a,b,c = t[:3]
a,b,c = t[:3]
l = list(t)
t = tuple(l)
t = (1,2,3,4,5,6)
a,b,c,d,e,f = t
a,b,c,d,e,f = t
a,b,c,d,e,f = t
a,b,c = t[:3]
a,b,c = t[:3]
a,b,c = t[:3]
a,b,c = t[:3]
a,b,c = t[:3]
a,b,c = t[:3]
l = list(t)
t = tuple(l)
t = (1,2,3,4,5,6)
a,b,c,d,e,f = t
a,b,c,d,e,f = t
a,b,c,d,e,f = t
a,b,c = t[:3]
a,b,c = t[:3]
a,b,c = t[:3]
a,b,c = t[:3]
a,b,c = t[:3]
a,b,c = t[:3]
l = list(t)
t = tuple(l)
def calibrate(self):
for i in xrange(self.rounds):
pass

Some files were not shown because too many files have changed in this diff Show More