Commit 3588f4a6 authored by Stefan Behnel's avatar Stefan Behnel

import of Pyrex 0.9.6.2

parent 5aab5520
...@@ -19,6 +19,7 @@ class CCodeWriter: ...@@ -19,6 +19,7 @@ class CCodeWriter:
# in_try_finally boolean inside try of try...finally # in_try_finally boolean inside try of try...finally
# filename_table {string : int} for finding filename table indexes # filename_table {string : int} for finding filename table indexes
# filename_list [string] filenames in filename table order # filename_list [string] filenames in filename table order
# exc_vars (string * 3) exception variables for reraise, or None
in_try_finally = 0 in_try_finally = 0
...@@ -32,6 +33,7 @@ class CCodeWriter: ...@@ -32,6 +33,7 @@ class CCodeWriter:
self.error_label = None self.error_label = None
self.filename_table = {} self.filename_table = {}
self.filename_list = [] self.filename_list = []
self.exc_vars = None
def putln(self, code = ""): def putln(self, code = ""):
if self.marker and self.bol: if self.marker and self.bol:
...@@ -156,11 +158,13 @@ class CCodeWriter: ...@@ -156,11 +158,13 @@ class CCodeWriter:
def put_var_declaration(self, entry, static = 0, dll_linkage = None, def put_var_declaration(self, entry, static = 0, dll_linkage = None,
definition = True): definition = True):
#print "Code.put_var_declaration:", entry.name, "definition =", definition ### #print "Code.put_var_declaration:", entry.name, repr(entry.type) ###
visibility = entry.visibility visibility = entry.visibility
if visibility == 'private' and not definition: if visibility == 'private' and not definition:
#print "...private and not definition, skipping" ###
return return
if not entry.used and visibility == "private": if not entry.used and visibility == "private":
#print "not used and private, skipping" ###
return return
storage_class = "" storage_class = ""
if visibility == 'extern': if visibility == 'extern':
...@@ -254,8 +258,6 @@ class CCodeWriter: ...@@ -254,8 +258,6 @@ class CCodeWriter:
def put_init_var_to_py_none(self, entry, template = "%s"): def put_init_var_to_py_none(self, entry, template = "%s"):
code = template % entry.cname code = template % entry.cname
#if entry.type.is_extension_type:
# code = "((PyObject*)%s)" % code
self.put_init_to_py_none(code, entry.type) self.put_init_to_py_none(code, entry.type)
def put_pymethoddef(self, entry, term): def put_pymethoddef(self, entry, term):
...@@ -270,6 +272,10 @@ class CCodeWriter: ...@@ -270,6 +272,10 @@ class CCodeWriter:
doc_code, doc_code,
term)) term))
def put_h_guard(self, guard):
self.putln("#ifndef %s" % guard)
self.putln("#define %s" % guard)
def error_goto(self, pos): def error_goto(self, pos):
lbl = self.error_label lbl = self.error_label
self.use_label(lbl) self.use_label(lbl)
......
...@@ -54,14 +54,19 @@ def close_listing_file(): ...@@ -54,14 +54,19 @@ def close_listing_file():
listing_file.close() listing_file.close()
listing_file = None listing_file = None
def error(position, message): def report(position, message):
#print "Errors.error:", repr(position), repr(message) ###
global num_errors
err = CompileError(position, message) err = CompileError(position, message)
line = "%s\n" % err line = "%s\n" % err
if listing_file: if listing_file:
listing_file.write(line) listing_file.write(line)
if echo_file: if echo_file:
echo_file.write(line) echo_file.write(line)
num_errors = num_errors + 1
return err return err
def warning(position, message):
return report(position, "Warning: %s" % message)
def error(position, message):
global num_errors
num_errors = num_errors + 1
return report(position, message)
This diff is collapsed.
...@@ -17,6 +17,8 @@ import Parsing ...@@ -17,6 +17,8 @@ import Parsing
from Symtab import BuiltinScope, ModuleScope from Symtab import BuiltinScope, ModuleScope
import Code import Code
from Pyrex.Utils import replace_suffix from Pyrex.Utils import replace_suffix
import Builtin
from Pyrex import Utils
verbose = 0 verbose = 0
...@@ -31,7 +33,8 @@ class Context: ...@@ -31,7 +33,8 @@ class Context:
# include_directories [string] # include_directories [string]
def __init__(self, include_directories): def __init__(self, include_directories):
self.modules = {"__builtin__" : BuiltinScope()} #self.modules = {"__builtin__" : BuiltinScope()}
self.modules = {"__builtin__" : Builtin.builtin_scope}
self.include_directories = include_directories self.include_directories = include_directories
def find_module(self, module_name, def find_module(self, module_name,
...@@ -171,22 +174,29 @@ class Context: ...@@ -171,22 +174,29 @@ class Context:
else: else:
c_suffix = ".c" c_suffix = ".c"
result.c_file = replace_suffix(source, c_suffix) result.c_file = replace_suffix(source, c_suffix)
c_stat = None
if result.c_file:
try:
c_stat = os.stat(result.c_file)
except EnvironmentError:
pass
module_name = self.extract_module_name(source) module_name = self.extract_module_name(source)
initial_pos = (source, 1, 0) initial_pos = (source, 1, 0)
scope = self.find_module(module_name, pos = initial_pos, need_pxd = 0) scope = self.find_module(module_name, pos = initial_pos, need_pxd = 0)
errors_occurred = False errors_occurred = False
try: try:
tree = self.parse(source, scope.type_names, pxd = 0) tree = self.parse(source, scope.type_names, pxd = 0)
tree.process_implementation(scope, result) tree.process_implementation(scope, options, result)
except CompileError: except CompileError:
errors_occurred = True errors_occurred = True
Errors.close_listing_file() Errors.close_listing_file()
result.num_errors = Errors.num_errors result.num_errors = Errors.num_errors
if result.num_errors > 0: if result.num_errors > 0:
errors_occurred = True errors_occurred = True
if errors_occurred: if errors_occurred and result.c_file:
try: try:
os.unlink(result.c_file) #os.unlink(result.c_file)
Utils.castrate_file(result.c_file, c_stat)
except EnvironmentError: except EnvironmentError:
pass pass
result.c_file = None result.c_file = None
...@@ -216,6 +226,7 @@ class CompilationOptions: ...@@ -216,6 +226,7 @@ class CompilationOptions:
errors_to_stderr boolean Echo errors to stderr when using .lis errors_to_stderr boolean Echo errors to stderr when using .lis
include_path [string] Directories to search for include files include_path [string] Directories to search for include files
output_file string Name of generated .c file output_file string Name of generated .c file
generate_pxi boolean Generate .pxi file for public declarations
Following options are experimental and only used on MacOSX: Following options are experimental and only used on MacOSX:
...@@ -229,7 +240,11 @@ class CompilationOptions: ...@@ -229,7 +240,11 @@ class CompilationOptions:
self.include_path = [] self.include_path = []
self.objects = [] self.objects = []
if defaults: if defaults:
self.__dict__.update(defaults.__dict__) if isinstance(defaults, CompilationOptions):
defaults = defaults.__dict__
else:
defaults = default_options
self.__dict__.update(defaults)
self.__dict__.update(kw) self.__dict__.update(kw)
...@@ -240,6 +255,7 @@ class CompilationResult: ...@@ -240,6 +255,7 @@ class CompilationResult:
c_file string or None The generated C source file c_file string or None The generated C source file
h_file string or None The generated C header file h_file string or None The generated C header file
i_file string or None The generated .pxi file i_file string or None The generated .pxi file
api_file string or None The generated C API .h file
listing_file string or None File of error messages listing_file string or None File of error messages
object_file string or None Result of compiling the C file object_file string or None Result of compiling the C file
extension_file string or None Result of linking the object file extension_file string or None Result of linking the object file
...@@ -250,6 +266,7 @@ class CompilationResult: ...@@ -250,6 +266,7 @@ class CompilationResult:
self.c_file = None self.c_file = None
self.h_file = None self.h_file = None
self.i_file = None self.i_file = None
self.api_file = None
self.listing_file = None self.listing_file = None
self.object_file = None self.object_file = None
self.extension_file = None self.extension_file = None
...@@ -307,18 +324,19 @@ def main(command_line = 0): ...@@ -307,18 +324,19 @@ def main(command_line = 0):
# #
#------------------------------------------------------------------------ #------------------------------------------------------------------------
default_options = CompilationOptions( default_options = dict(
show_version = 0, show_version = 0,
use_listing_file = 0, use_listing_file = 0,
errors_to_stderr = 1, errors_to_stderr = 1,
c_only = 1, c_only = 1,
obj_only = 1, obj_only = 1,
cplus = 0, cplus = 0,
output_file = None) output_file = None,
generate_pxi = 0)
if sys.platform == "mac": if sys.platform == "mac":
from Pyrex.Mac.MacSystem import c_compile, c_link, CCompilerError from Pyrex.Mac.MacSystem import c_compile, c_link, CCompilerError
default_options.use_listing_file = 1 default_options['use_listing_file'] = 1
elif sys.platform == "darwin": elif sys.platform == "darwin":
from Pyrex.Mac.DarwinSystem import c_compile, c_link, CCompilerError from Pyrex.Mac.DarwinSystem import c_compile, c_link, CCompilerError
else: else:
......
This diff is collapsed.
...@@ -47,8 +47,20 @@ module_cname = pyrex_prefix + "m" ...@@ -47,8 +47,20 @@ module_cname = pyrex_prefix + "m"
moddoc_cname = pyrex_prefix + "mdoc" moddoc_cname = pyrex_prefix + "mdoc"
methtable_cname = pyrex_prefix + "methods" methtable_cname = pyrex_prefix + "methods"
retval_cname = pyrex_prefix + "r" retval_cname = pyrex_prefix + "r"
reqd_kwds_cname = pyrex_prefix + "reqd_kwds"
self_cname = pyrex_prefix + "self" self_cname = pyrex_prefix + "self"
stringtab_cname = pyrex_prefix + "string_tab" stringtab_cname = pyrex_prefix + "string_tab"
vtabslot_cname = pyrex_prefix + "vtab" vtabslot_cname = pyrex_prefix + "vtab"
extern_c_macro = pyrex_prefix.upper() + "EXTERN_C" extern_c_macro = pyrex_prefix.upper() + "EXTERN_C"
exc_type_name = pyrex_prefix + "exc_type"
exc_value_name = pyrex_prefix + "exc_value"
exc_tb_name = pyrex_prefix + "exc_tb"
exc_lineno_name = pyrex_prefix + "exc_lineno"
exc_vars = (exc_type_name, exc_value_name, exc_tb_name)
h_guard_prefix = "__PYX_HAVE__"
api_guard_prefix = "__PYX_HAVE_API__"
api_func_guard = "__PYX_HAVE_API_FUNC_"
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
import cPickle as pickle import cPickle as pickle
import os import os
import platform
import stat import stat
import sys import sys
from time import time from time import time
...@@ -138,7 +139,7 @@ reserved_words = [ ...@@ -138,7 +139,7 @@ reserved_words = [
"raise", "import", "exec", "try", "except", "finally", "raise", "import", "exec", "try", "except", "finally",
"while", "if", "elif", "else", "for", "in", "assert", "while", "if", "elif", "else", "for", "in", "assert",
"and", "or", "not", "is", "in", "lambda", "from", "and", "or", "not", "is", "in", "lambda", "from",
"NULL", "cimport" "NULL", "cimport", "with", "DEF", "IF", "ELIF", "ELSE"
] ]
class Method: class Method:
...@@ -160,7 +161,53 @@ def build_resword_dict(): ...@@ -160,7 +161,53 @@ def build_resword_dict():
#------------------------------------------------------------------ #------------------------------------------------------------------
class CompileTimeScope(object):
def __init__(self, outer = None):
self.entries = {}
self.outer = outer
def declare(self, name, value):
self.entries[name] = value
def lookup_here(self, name):
return self.entries[name]
def lookup(self, name):
try:
return self.lookup_here(name)
except KeyError:
outer = self.outer
if outer:
return outer.lookup(name)
else:
raise
def initial_compile_time_env():
benv = CompileTimeScope()
names = ('UNAME_SYSNAME', 'UNAME_NODENAME', 'UNAME_RELEASE',
'UNAME_VERSION', 'UNAME_MACHINE')
for name, value in zip(names, platform.uname()):
benv.declare(name, value)
import __builtin__
names = ('False', 'True',
'abs', 'bool', 'chr', 'cmp', 'complex', 'dict', 'divmod', 'enumerate',
'float', 'hash', 'hex', 'int', 'len', 'list', 'long', 'map', 'max', 'min',
'oct', 'ord', 'pow', 'range', 'reduce', 'repr', 'round', 'slice', 'str',
'sum', 'tuple', 'xrange', 'zip')
for name in names:
benv.declare(name, getattr(__builtin__, name))
denv = CompileTimeScope(benv)
return denv
#------------------------------------------------------------------
class PyrexScanner(Scanner): class PyrexScanner(Scanner):
# context Context Compilation context
# type_names set Identifiers to be treated as type names
# compile_time_env dict Environment for conditional compilation
# compile_time_eval boolean In a true conditional compilation context
# compile_time_expr boolean In a compile-time expression context
resword_dict = build_resword_dict() resword_dict = build_resword_dict()
...@@ -170,9 +217,15 @@ class PyrexScanner(Scanner): ...@@ -170,9 +217,15 @@ class PyrexScanner(Scanner):
if parent_scanner: if parent_scanner:
self.context = parent_scanner.context self.context = parent_scanner.context
self.type_names = parent_scanner.type_names self.type_names = parent_scanner.type_names
self.compile_time_env = parent_scanner.compile_time_env
self.compile_time_eval = parent_scanner.compile_time_eval
self.compile_time_expr = parent_scanner.compile_time_expr
else: else:
self.context = context self.context = context
self.type_names = type_names self.type_names = type_names
self.compile_time_env = initial_compile_time_env()
self.compile_time_eval = 1
self.compile_time_expr = 0
self.trace = trace_scanner self.trace = trace_scanner
self.indentation_stack = [0] self.indentation_stack = [0]
self.indentation_char = None self.indentation_char = None
...@@ -303,6 +356,15 @@ class PyrexScanner(Scanner): ...@@ -303,6 +356,15 @@ class PyrexScanner(Scanner):
if self.sy == what: if self.sy == what:
self.next() self.next()
else: else:
self.expected(what, message)
def expect_keyword(self, what, message = None):
if self.sy == 'IDENT' and self.systring == what:
self.next()
else:
self.expected(what, message)
def expected(self, what, message):
if message: if message:
self.error(message) self.error(message)
else: else:
...@@ -316,7 +378,7 @@ class PyrexScanner(Scanner): ...@@ -316,7 +378,7 @@ class PyrexScanner(Scanner):
self.expect('DEDENT', self.expect('DEDENT',
"Expected a decrease in indentation level") "Expected a decrease in indentation level")
def expect_newline(self, message): def expect_newline(self, message = "Expected a newline"):
# Expect either a newline or end of file # Expect either a newline or end of file
if self.sy <> 'EOF': if self.sy <> 'EOF':
self.expect('NEWLINE', message) self.expect('NEWLINE', message)
This diff is collapsed.
...@@ -26,6 +26,7 @@ class Signature: ...@@ -26,6 +26,7 @@ class Signature:
# 'i' int # 'i' int
# 'I' int * # 'I' int *
# 'l' long # 'l' long
# 'Z' Py_ssize_t
# 's' char * # 's' char *
# 'S' char ** # 'S' char **
# 'r' int used only to signal exception # 'r' int used only to signal exception
...@@ -42,6 +43,7 @@ class Signature: ...@@ -42,6 +43,7 @@ class Signature:
'i': PyrexTypes.c_int_type, 'i': PyrexTypes.c_int_type,
'I': PyrexTypes.c_int_ptr_type, 'I': PyrexTypes.c_int_ptr_type,
'l': PyrexTypes.c_long_type, 'l': PyrexTypes.c_long_type,
'Z': PyrexTypes.c_py_ssize_t_type,
's': PyrexTypes.c_char_ptr_type, 's': PyrexTypes.c_char_ptr_type,
'S': PyrexTypes.c_char_ptr_ptr_type, 'S': PyrexTypes.c_char_ptr_ptr_type,
'r': PyrexTypes.c_returncode_type, 'r': PyrexTypes.c_returncode_type,
...@@ -81,23 +83,43 @@ class Signature: ...@@ -81,23 +83,43 @@ class Signature:
def return_type(self): def return_type(self):
return self.format_map[self.ret_format] return self.format_map[self.ret_format]
def exception_value(self):
return self.error_value_map.get(self.ret_format)
def function_type(self):
# Construct a C function type descriptor for this signature
args = []
for i in xrange(self.num_fixed_args()):
arg_type = self.fixed_arg_type(i)
args.append(PyrexTypes.CFuncTypeArg("", arg_type, None))
ret_type = self.return_type()
exc_value = self.exception_value()
return PyrexTypes.CFuncType(ret_type, args, exception_value = exc_value)
class SlotDescriptor: class SlotDescriptor:
# Abstract base class for type slot descriptors. # Abstract base class for type slot descriptors.
# #
# slot_name string Member name of the slot in the type object # slot_name string Member name of the slot in the type object
# is_initialised_dynamically Is initialised by code in the module init function # is_initialised_dynamically Is initialised by code in the module init function
# flag Py_TPFLAGS_XXX value indicating presence of slot
def __init__(self, slot_name, dynamic = 0): def __init__(self, slot_name, dynamic = 0, flag = None):
self.slot_name = slot_name self.slot_name = slot_name
self.is_initialised_dynamically = dynamic self.is_initialised_dynamically = dynamic
self.flag = flag
def generate(self, scope, code): def generate(self, scope, code):
if self.is_initialised_dynamically: if self.is_initialised_dynamically:
value = 0 value = 0
else: else:
value = self.slot_code(scope) value = self.slot_code(scope)
flag = self.flag
if flag:
code.putln("#if Py_TPFLAGS_DEFAULT & %s" % flag)
code.putln("%s, /*%s*/" % (value, self.slot_name)) code.putln("%s, /*%s*/" % (value, self.slot_name))
if flag:
code.putln("#endif")
# Some C implementations have trouble statically # Some C implementations have trouble statically
# initialising a global with a pointer to an extern # initialising a global with a pointer to an extern
...@@ -159,8 +181,8 @@ class MethodSlot(SlotDescriptor): ...@@ -159,8 +181,8 @@ class MethodSlot(SlotDescriptor):
# method_name string The __xxx__ name of the method # method_name string The __xxx__ name of the method
# default string or None Default value of the slot # default string or None Default value of the slot
def __init__(self, signature, slot_name, method_name, default = None): def __init__(self, signature, slot_name, method_name, default = None, flag = None):
SlotDescriptor.__init__(self, slot_name) SlotDescriptor.__init__(self, slot_name, flag = flag)
self.signature = signature self.signature = signature
self.slot_name = slot_name self.slot_name = slot_name
self.method_name = method_name self.method_name = method_name
...@@ -354,18 +376,28 @@ ternaryfunc = Signature("OOO", "O") # typedef PyObject * (*ternaryfunc)(P ...@@ -354,18 +376,28 @@ ternaryfunc = Signature("OOO", "O") # typedef PyObject * (*ternaryfunc)(P
iternaryfunc = Signature("TOO", "O") # typedef PyObject * (*ternaryfunc)(PyObject *, PyObject *, PyObject *); iternaryfunc = Signature("TOO", "O") # typedef PyObject * (*ternaryfunc)(PyObject *, PyObject *, PyObject *);
callfunc = Signature("T*", "O") # typedef PyObject * (*ternaryfunc)(PyObject *, PyObject *, PyObject *); callfunc = Signature("T*", "O") # typedef PyObject * (*ternaryfunc)(PyObject *, PyObject *, PyObject *);
inquiry = Signature("T", "i") # typedef int (*inquiry)(PyObject *); inquiry = Signature("T", "i") # typedef int (*inquiry)(PyObject *);
lenfunc = Signature("T", "Z") # typedef Py_ssize_t (*lenfunc)(PyObject *);
# typedef int (*coercion)(PyObject **, PyObject **); # typedef int (*coercion)(PyObject **, PyObject **);
intargfunc = Signature("Ti", "O") # typedef PyObject *(*intargfunc)(PyObject *, int); intargfunc = Signature("Ti", "O") # typedef PyObject *(*intargfunc)(PyObject *, int);
ssizeargfunc = Signature("TZ", "O") # typedef PyObject *(*ssizeargfunc)(PyObject *, Py_ssize_t);
intintargfunc = Signature("Tii", "O") # typedef PyObject *(*intintargfunc)(PyObject *, int, int); intintargfunc = Signature("Tii", "O") # typedef PyObject *(*intintargfunc)(PyObject *, int, int);
ssizessizeargfunc = Signature("TZZ", "O") # typedef PyObject *(*ssizessizeargfunc)(PyObject *, Py_ssize_t, Py_ssize_t);
intobjargproc = Signature("TiO", 'r') # typedef int(*intobjargproc)(PyObject *, int, PyObject *); intobjargproc = Signature("TiO", 'r') # typedef int(*intobjargproc)(PyObject *, int, PyObject *);
ssizeobjargproc = Signature("TZO", 'r') # typedef int(*ssizeobjargproc)(PyObject *, Py_ssize_t, PyObject *);
intintobjargproc = Signature("TiiO", 'r') # typedef int(*intintobjargproc)(PyObject *, int, int, PyObject *); intintobjargproc = Signature("TiiO", 'r') # typedef int(*intintobjargproc)(PyObject *, int, int, PyObject *);
ssizessizeobjargproc = Signature("TZZO", 'r') # typedef int(*ssizessizeobjargproc)(PyObject *, Py_ssize_t, Py_ssize_t, PyObject *);
intintargproc = Signature("Tii", 'r') intintargproc = Signature("Tii", 'r')
ssizessizeargproc = Signature("TZZ", 'r')
objargfunc = Signature("TO", "O") objargfunc = Signature("TO", "O")
objobjargproc = Signature("TOO", 'r') # typedef int (*objobjargproc)(PyObject *, PyObject *, PyObject *); objobjargproc = Signature("TOO", 'r') # typedef int (*objobjargproc)(PyObject *, PyObject *, PyObject *);
getreadbufferproc = Signature("TiP", 'i') # typedef int (*getreadbufferproc)(PyObject *, int, void **); getreadbufferproc = Signature("TiP", 'i') # typedef int (*getreadbufferproc)(PyObject *, int, void **);
getwritebufferproc = Signature("TiP", 'i') # typedef int (*getwritebufferproc)(PyObject *, int, void **); getwritebufferproc = Signature("TiP", 'i') # typedef int (*getwritebufferproc)(PyObject *, int, void **);
getsegcountproc = Signature("TI", 'i') # typedef int (*getsegcountproc)(PyObject *, int *); getsegcountproc = Signature("TI", 'i') # typedef int (*getsegcountproc)(PyObject *, int *);
getcharbufferproc = Signature("TiS", 'i') # typedef int (*getcharbufferproc)(PyObject *, int, const char **); getcharbufferproc = Signature("TiS", 'i') # typedef int (*getcharbufferproc)(PyObject *, int, const char **);
readbufferproc = Signature("TZP", "Z") # typedef Py_ssize_t (*readbufferproc)(PyObject *, Py_ssize_t, void **);
writebufferproc = Signature("TZP", "Z") # typedef Py_ssize_t (*writebufferproc)(PyObject *, Py_ssize_t, void **);
segcountproc = Signature("TZ", "Z") # typedef Py_ssize_t (*segcountproc)(PyObject *, Py_ssize_t *);
writebufferproc = Signature("TZS", "Z") # typedef Py_ssize_t (*charbufferproc)(PyObject *, Py_ssize_t, char **);
objargproc = Signature("TO", 'r') # typedef int (*objobjproc)(PyObject *, PyObject *); objargproc = Signature("TO", 'r') # typedef int (*objobjproc)(PyObject *, PyObject *);
# typedef int (*visitproc)(PyObject *, void *); # typedef int (*visitproc)(PyObject *, void *);
# typedef int (*traverseproc)(PyObject *, visitproc, void *); # typedef int (*traverseproc)(PyObject *, visitproc, void *);
...@@ -454,14 +486,15 @@ PyNumberMethods = ( ...@@ -454,14 +486,15 @@ PyNumberMethods = (
MethodSlot(binaryfunc, "nb_true_divide", "__truediv__"), MethodSlot(binaryfunc, "nb_true_divide", "__truediv__"),
MethodSlot(ibinaryfunc, "nb_inplace_floor_divide", "__ifloordiv__"), MethodSlot(ibinaryfunc, "nb_inplace_floor_divide", "__ifloordiv__"),
MethodSlot(ibinaryfunc, "nb_inplace_true_divide", "__itruediv__"), MethodSlot(ibinaryfunc, "nb_inplace_true_divide", "__itruediv__"),
MethodSlot(unaryfunc, "nb_index", "__index__", flag = "Py_TPFLAGS_HAVE_INDEX")
) )
PySequenceMethods = ( PySequenceMethods = (
MethodSlot(inquiry, "sq_length", "__len__"), # EmptySlot("sq_length"), # mp_length used instead MethodSlot(lenfunc, "sq_length", "__len__"),
EmptySlot("sq_concat"), # nb_add used instead EmptySlot("sq_concat"), # nb_add used instead
EmptySlot("sq_repeat"), # nb_multiply used instead EmptySlot("sq_repeat"), # nb_multiply used instead
SyntheticSlot("sq_item", ["__getitem__"], "0"), #EmptySlot("sq_item"), # mp_subscript used instead SyntheticSlot("sq_item", ["__getitem__"], "0"), #EmptySlot("sq_item"), # mp_subscript used instead
MethodSlot(intintargfunc, "sq_slice", "__getslice__"), MethodSlot(ssizessizeargfunc, "sq_slice", "__getslice__"),
EmptySlot("sq_ass_item"), # mp_ass_subscript used instead EmptySlot("sq_ass_item"), # mp_ass_subscript used instead
SyntheticSlot("sq_ass_slice", ["__setslice__", "__delslice__"], "0"), SyntheticSlot("sq_ass_slice", ["__setslice__", "__delslice__"], "0"),
MethodSlot(cmpfunc, "sq_contains", "__contains__"), MethodSlot(cmpfunc, "sq_contains", "__contains__"),
...@@ -470,7 +503,7 @@ PySequenceMethods = ( ...@@ -470,7 +503,7 @@ PySequenceMethods = (
) )
PyMappingMethods = ( PyMappingMethods = (
MethodSlot(inquiry, "mp_length", "__len__"), MethodSlot(lenfunc, "mp_length", "__len__"),
MethodSlot(objargfunc, "mp_subscript", "__getitem__"), MethodSlot(objargfunc, "mp_subscript", "__getitem__"),
SyntheticSlot("mp_ass_subscript", ["__setitem__", "__delitem__"], "0"), SyntheticSlot("mp_ass_subscript", ["__setitem__", "__delitem__"], "0"),
) )
...@@ -561,12 +594,12 @@ slot_table = ( ...@@ -561,12 +594,12 @@ slot_table = (
# #
#------------------------------------------------------------------------------------------ #------------------------------------------------------------------------------------------
MethodSlot(initproc, "", "__new__") MethodSlot(initproc, "", "__cinit__")
MethodSlot(destructor, "", "__dealloc__") MethodSlot(destructor, "", "__dealloc__")
MethodSlot(objobjargproc, "", "__setitem__") MethodSlot(objobjargproc, "", "__setitem__")
MethodSlot(objargproc, "", "__delitem__") MethodSlot(objargproc, "", "__delitem__")
MethodSlot(intintobjargproc, "", "__setslice__") MethodSlot(ssizessizeobjargproc, "", "__setslice__")
MethodSlot(intintargproc, "", "__delslice__") MethodSlot(ssizessizeargproc, "", "__delslice__")
MethodSlot(getattrofunc, "", "__getattr__") MethodSlot(getattrofunc, "", "__getattr__")
MethodSlot(setattrofunc, "", "__setattr__") MethodSlot(setattrofunc, "", "__setattr__")
MethodSlot(delattrofunc, "", "__delattr__") MethodSlot(delattrofunc, "", "__delattr__")
......
version = '0.9.5.1a' version = '0.9.6.2'
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
def print_call_chain(*args): def print_call_chain(*args):
import sys import sys
print " ".join(map(str, args)) print " ".join(map(str, args))
f = sys._getframe(2) f = sys._getframe(1)
while f: while f:
name = f.f_code.co_name name = f.f_code.co_name
s = f.f_locals.get('self', None) s = f.f_locals.get('self', None)
......
# July 2002, Graham Fawcett # July 2002, Graham Fawcett
# #
# this hack was inspired by the way Thomas Heller got py2exe # this hack was inspired by the way Thomas Heller got py2exe
# to appear as a distutil command # to appear as a distutil command
# #
# we replace distutils.command.build_ext with our own version # we replace distutils.command.build_ext with our own version
# and keep the old one under the module name _build_ext, # and keep the old one under the module name _build_ext,
# so that *our* build_ext can make use of it. # so that *our* build_ext can make use of it.
from build_ext import build_ext from build_ext import build_ext
from extension import Extension
# Subclasses disutils.command.build_ext, """Pyrex.Distutils.build_ext
# replacing it with a Pyrex version that compiles pyx->c
# before calling the original build_ext command.
# July 2002, Graham Fawcett
# Modified by Darrell Gallion <dgallion1@yahoo.com>
# to allow inclusion of .c files along with .pyx files.
# Pyrex is (c) Greg Ewing.
import distutils.command.build_ext Implements a version of the Distutils 'build_ext' command, for
#import Pyrex.Compiler.Main building Pyrex extension modules."""
from Pyrex.Compiler.Main import CompilationOptions, default_options, compile
from Pyrex.Compiler.Errors import PyrexError
from distutils.dep_util import newer
import os
import sys
def replace_suffix(path, new_suffix): # This module should be kept compatible with Python 2.1.
return os.path.splitext(path)[0] + new_suffix
class build_ext (distutils.command.build_ext.build_ext): __revision__ = "$Id:$"
description = "compile Pyrex scripts, then build C/C++ extensions (compile/link to build directory)" import sys, os, string, re
from types import *
from distutils.core import Command
from distutils.errors import *
from distutils.sysconfig import customize_compiler, get_python_version
from distutils.dep_util import newer_group
from distutils import log
from distutils.dir_util import mkpath
try:
from Pyrex.Compiler.Main \
import CompilationOptions, \
default_options as pyrex_default_options, \
compile as pyrex_compile
from Pyrex.Compiler.Errors import PyrexError
except ImportError:
PyrexError = None
from distutils.command import build_ext as _build_ext
extension_name_re = _build_ext.extension_name_re
show_compilers = _build_ext.show_compilers
class build_ext(_build_ext.build_ext):
description = "build C/C++ and Pyrex extensions (compile/link to build directory)"
sep_by = _build_ext.build_ext.sep_by
user_options = _build_ext.build_ext.user_options
boolean_options = _build_ext.build_ext.boolean_options
help_options = _build_ext.build_ext.help_options
# Add the pyrex specific data.
user_options.extend([
('pyrex-cplus', None,
"generate C++ source files"),
('pyrex-create-listing', None,
"write errors to a listing file"),
('pyrex-include-dirs=', None,
"path to the Pyrex include files" + sep_by),
('pyrex-c-in-temp', None,
"put generated C files in temp directory"),
('pyrex-gen-pxi', None,
"generate .pxi file for public declarations"),
])
boolean_options.extend([
'pyrex-cplus', 'pyrex-create-listing', 'pyrex-c-in-temp'
])
def initialize_options(self):
_build_ext.build_ext.initialize_options(self)
self.pyrex_cplus = 0
self.pyrex_create_listing = 0
self.pyrex_include_dirs = None
self.pyrex_c_in_temp = 0
self.pyrex_gen_pxi = 0
def finalize_options (self): def finalize_options (self):
distutils.command.build_ext.build_ext.finalize_options(self) _build_ext.build_ext.finalize_options(self)
if self.pyrex_include_dirs is None:
# The following hack should no longer be needed. self.pyrex_include_dirs = []
if 0: elif type(self.pyrex_include_dirs) is StringType:
# compiling with mingw32 gets an "initializer not a constant" error self.pyrex_include_dirs = \
# doesn't appear to happen with MSVC! string.split(self.pyrex_include_dirs, os.pathsep)
# so if we are compiling with mingw32, # finalize_options ()
# switch to C++ mode, to avoid the problem
if self.compiler == 'mingw32': def build_extensions(self):
self.swig_cpp = 1 # First, sanity-check the 'extensions' list
self.check_extensions_list(self.extensions)
def swig_sources (self, sources, extension = None): for ext in self.extensions:
if not self.extensions: ext.sources = self.pyrex_sources(ext.sources, ext)
return self.build_extension(ext)
# collect the names of the source (.pyx) files def pyrex_sources(self, sources, extension):
pyx_sources = []
pyx_sources = [source for source in sources if source.endswith('.pyx')] """
other_sources = [source for source in sources if not source.endswith('.pyx')] Walk the list of source files in 'sources', looking for Pyrex
source (.pyx) files. Run Pyrex on all that are found, and return
#suffix = self.swig_cpp and '.cpp' or '.c' a modified 'sources' list with Pyrex source files replaced by the
suffix = '.c' generated C (or C++) files.
for pyx in pyx_sources: """
# should I raise an exception if it doesn't exist?
if os.path.exists(pyx): if PyrexError == None:
source = pyx raise DistutilsPlatformError, \
target = replace_suffix(source, suffix) ("Pyrex does not appear to be installed "
if newer(source, target) or self.force: "on platform '%s'") % os.name
self.pyrex_compile(source)
new_sources = []
return [replace_suffix(src, suffix) for src in pyx_sources] + other_sources pyrex_sources = []
pyrex_targets = {}
def pyrex_compile(self, source):
options = CompilationOptions(default_options, # Setup create_list and cplus from the extension options if
include_path = self.include_dirs) # Pyrex.Distutils.extension.Extension is used, otherwise just
result = compile(source, options) # use what was parsed from the command-line or the configuration file.
if result.num_errors <> 0: # cplus will also be set to true is extension.language is equal to
sys.exit(1) # 'C++' or 'c++'.
#try:
# create_listing = self.pyrex_create_listing or \
# extension.pyrex_create_listing
# cplus = self.pyrex_cplus or \
# extension.pyrex_cplus or \
# (extension.language != None and \
# extension.language.lower() == 'c++')
#except AttributeError:
# create_listing = self.pyrex_create_listing
# cplus = self.pyrex_cplus or \
# (extension.language != None and \
# extension.language.lower() == 'c++')
create_listing = self.pyrex_create_listing or \
getattr(extension, 'pyrex_create_listing', 0)
cplus = self.pyrex_cplus or getattr(extension, 'pyrex_cplus', 0) or \
(extension.language and extension.language.lower() == 'c++')
pyrex_gen_pxi = self.pyrex_gen_pxi or getattr(extension, 'pyrex_gen_pxi', 0)
# Set up the include_path for the Pyres compiler:
# 1. Start with the command line option.
# 2. Add in any (unique) paths from the extension
# pyrex_include_dirs (if Pyrex.Distutils.extension is used).
# 3. Add in any (unique) paths from the extension include_dirs
includes = self.pyrex_include_dirs
try:
for i in extension.pyrex_include_dirs:
if not i in includes:
includes.append(i)
except AttributeError:
pass
for i in extension.include_dirs:
if not i in includes:
includes.append(i)
# Set the target_ext to '.c'. Pyrex will change this to '.cpp' if
# needed.
if cplus:
target_ext = '.cpp'
else:
target_ext = '.c'
# Decide whether to drop the generated C files into the temp dir
# or the source tree.
if not self.inplace and (self.pyrex_c_in_temp
or getattr(extension, 'pyrex_c_in_temp', 0)):
target_dir = os.path.join(self.build_temp, "pyrex")
else:
target_dir = ""
for source in sources:
(base, ext) = os.path.splitext(source)
if ext == ".pyx": # Pyrex source file
new_sources.append(os.path.join(target_dir, base + target_ext))
pyrex_sources.append(source)
pyrex_targets[source] = new_sources[-1]
else:
new_sources.append(source)
if not pyrex_sources:
return new_sources
for source in pyrex_sources:
target = pyrex_targets[source]
source_time = os.stat(source).st_mtime
try:
target_time = os.stat(target).st_mtime
newer = source_time > target_time
except EnvironmentError:
newer = 1
if newer:
log.info("pyrexing %s to %s", source, target)
self.mkpath(os.path.dirname(target))
options = CompilationOptions(pyrex_default_options,
use_listing_file = create_listing,
include_path = includes,
output_file = target,
cplus = cplus,
generate_pxi = pyrex_gen_pxi)
result = pyrex_compile(source, options=options)
return new_sources
# pyrex_sources ()
# class build_ext
...@@ -6,15 +6,20 @@ verbose = 0 ...@@ -6,15 +6,20 @@ verbose = 0
gcc_pendantic = True gcc_pendantic = True
gcc_warnings_are_errors = True gcc_warnings_are_errors = True
gcc_all_warnings = True gcc_all_warnings = True
gcc_optimize = False
import os import os, sys
from Pyrex.Utils import replace_suffix from Pyrex.Utils import replace_suffix
from Pyrex.Compiler.Errors import PyrexError from Pyrex.Compiler.Errors import PyrexError
version_string = "%s.%s" % sys.version_info[:2]
py_include_dirs = [ py_include_dirs = [
"/Library/Frameworks/Python.framework/Headers" "/Library/Frameworks/Python.framework/Versions/%s/Headers" % version_string
] ]
os.environ["MACOSX_DEPLOYMENT_TARGET"] = "10.3"
compilers = ["gcc", "g++"] compilers = ["gcc", "g++"]
compiler_options = \ compiler_options = \
"-g -c -fno-strict-aliasing -Wno-long-double -no-cpp-precomp " \ "-g -c -fno-strict-aliasing -Wno-long-double -no-cpp-precomp " \
...@@ -27,11 +32,16 @@ if gcc_warnings_are_errors: ...@@ -27,11 +32,16 @@ if gcc_warnings_are_errors:
if gcc_all_warnings: if gcc_all_warnings:
compiler_options.append("-Wall") compiler_options.append("-Wall")
compiler_options.append("-Wno-unused-function") compiler_options.append("-Wno-unused-function")
if gcc_optimize:
compiler_options.append("-O")
linkers = ["gcc", "g++"] linkers = ["gcc", "g++"]
linker_options = \ linker_options = \
"-Wl,-F.,-w -bundle -framework Python" \ "-Wl,-F.,-w -bundle -undefined dynamic_lookup" \
.split() .split()
#linker_options = \
# "-Wl,-F.,-w -bundle -framework Python" \
# .split()
class CCompilerError(PyrexError): class CCompilerError(PyrexError):
pass pass
......
...@@ -7,7 +7,7 @@ gcc_pendantic = True ...@@ -7,7 +7,7 @@ gcc_pendantic = True
gcc_warnings_are_errors = True gcc_warnings_are_errors = True
gcc_all_warnings = True gcc_all_warnings = True
import os import os, sys
from Pyrex.Utils import replace_suffix from Pyrex.Utils import replace_suffix
from Pyrex.Compiler.Errors import PyrexError from Pyrex.Compiler.Errors import PyrexError
......
...@@ -14,3 +14,21 @@ def open_new_file(path): ...@@ -14,3 +14,21 @@ def open_new_file(path):
# preserve metadata on the Mac. # preserve metadata on the Mac.
return open(path, "w+") return open(path, "w+")
def castrate_file(path, st):
# Remove junk contents from an output file after a
# failed compilation, but preserve metadata on Mac.
# Also sets access and modification times back to
# those specified by st (a stat struct).
try:
f = open(path, "r+")
except EnvironmentError:
pass
else:
#st = os.stat(path)
f.seek(0, 0)
f.truncate()
f.write(
"#error Do not use this file, it is the result of a failed Pyrex compilation.\n")
f.close()
if st:
os.utime(path, (st.st_atime, st.st_mtime))
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment