Commit 8d715668 authored by Mark Florisson's avatar Mark Florisson

Merge branch 'master' of https://github.com/cython/cython

parents 5dc75a83 122f3cfb
......@@ -32,3 +32,4 @@ ef9d2c680684d0df7d81f529cda29e9e1741f575 cython-0.10.1
0000000000000000000000000000000000000000 cython-0.10.1
59c67af0674bd93c5fd8958e08c76a9dab9aae37 sage-cythonizes
0000000000000000000000000000000000000000 sage-cythonizes
478f57be445d18fe294db849d7ad317fe7d7658f 0.14.alpha0
......@@ -10,6 +10,7 @@ except NameError:
from distutils.extension import Extension
from Cython import Utils
from Cython.Compiler.Main import Context, CompilationOptions, default_options
# Unfortunately, Python 2.3 doesn't support decorators.
def cached_method(f):
......@@ -160,23 +161,8 @@ def strip_string_literals(code, prefix='__Pyx_L'):
q = min(single_q, double_q)
if q == -1: q = max(single_q, double_q)
# Process comment.
if -1 < hash_mark and (hash_mark < q or q == -1):
end = code.find('\n', hash_mark)
if end == -1:
end = None
new_code.append(code[start:hash_mark+1])
counter += 1
label = "%s%s" % (prefix, counter)
literals[label] = code[hash_mark+1:end]
new_code.append(label)
if end is None:
break
q = end
start = q
# We're done.
elif q == -1:
if q == -1 and hash_mark == -1:
new_code.append(code[start:])
break
......@@ -199,6 +185,21 @@ def strip_string_literals(code, prefix='__Pyx_L'):
start = q
else:
q += 1
# Process comment.
elif -1 != hash_mark and (hash_mark < q or q == -1):
end = code.find('\n', hash_mark)
if end == -1:
end = None
new_code.append(code[start:hash_mark+1])
counter += 1
label = "%s%s" % (prefix, counter)
literals[label] = code[hash_mark+1:end]
new_code.append(label)
if end is None:
break
q = end
start = q
# Open the quote.
else:
......@@ -264,9 +265,15 @@ class DependencyTree(object):
cimports = set(cimports)
externs = set(externs)
for include in includes:
a, b = self.cimports_and_externs(os.path.join(os.path.dirname(filename), include))
cimports.update(a)
externs.update(b)
include_path = os.path.join(os.path.dirname(filename), include)
if not os.path.exists(include_path):
include_path = self.context.find_include_file(include, None)
if include_path:
a, b = self.cimports_and_externs(include_path)
cimports.update(a)
externs.update(b)
else:
print "Unable to locate '%s' referenced from '%s'" % (filename, include)
return tuple(cimports), tuple(externs)
cimports_and_externs = cached_method(cimports_and_externs)
......@@ -377,13 +384,11 @@ def create_dependency_tree(ctx=None):
global _dep_tree
if _dep_tree is None:
if ctx is None:
from Cython.Compiler.Main import Context, CompilationOptions
ctx = Context(["."], CompilationOptions())
ctx = Context(["."], CompilationOptions(default_options))
_dep_tree = DependencyTree(ctx)
return _dep_tree
# TODO: Take common options.
# TODO: Symbolic names (e.g. for numpy.include_dirs()
# This may be useful for advanced users?
def create_extension_list(patterns, ctx=None, aliases=None):
seen = set()
deps = create_dependency_tree(ctx)
......@@ -423,7 +428,11 @@ def create_extension_list(patterns, ctx=None, aliases=None):
seen.add(name)
return module_list
def cythonize(module_list, ctx=None, nthreads=0, aliases=None):
# This is the user-exposed entry point.
def cythonize(module_list, nthreads=0, aliases=None, **options):
c_options = CompilationOptions(**options)
cpp_options = CompilationOptions(**options); cpp_options.cplus = True
ctx = c_options.create_context()
module_list = create_extension_list(module_list, ctx=ctx, aliases=aliases)
deps = create_dependency_tree(ctx)
to_compile = []
......@@ -434,8 +443,10 @@ def cythonize(module_list, ctx=None, nthreads=0, aliases=None):
if ext in ('.pyx', '.py'):
if m.language == 'c++':
c_file = base + '.cpp'
options = cpp_options
else:
c_file = base + '.c'
options = c_options
if os.path.exists(c_file):
c_timestamp = os.path.getmtime(c_file)
else:
......@@ -450,29 +461,29 @@ def cythonize(module_list, ctx=None, nthreads=0, aliases=None):
priority = 2 - (dep in deps.immediate_dependencies(source))
if c_timestamp < dep_timestamp:
print("Compiling %s because it depends on %s" % (source, dep))
to_compile.append((priority, source, c_file))
to_compile.append((priority, source, c_file, options))
new_sources.append(c_file)
else:
new_sources.append(source)
m.sources = new_sources
to_compile.sort()
# TODO: invoke directly
if nthreads:
# Requires multiprocessing (or Python >= 2.6)
try:
import multiprocessing
pool = multiprocessing.Pool(nthreads)
pool.map(cythonize_one_helper, to_compile)
except ImportError:
print("multiprocessing required for parallel cythonization")
nthreads = 0
pool = multiprocessing.Pool(nthreads)
pool.map(cythonize_one_helper, to_compile)
if not nthreads:
for priority, pyx_file, c_file in to_compile:
cythonize_one(pyx_file, c_file)
for priority, pyx_file, c_file, options in to_compile:
cythonize_one(pyx_file, c_file, options)
return module_list
# TODO: Share context? Issue: pyx processing leaks into pxd module
def cythonize_one(pyx_file, c_file, options=None):
from Cython.Compiler.Main import compile, CompilationOptions, default_options
from Cython.Compiler.Main import compile, default_options
from Cython.Compiler.Errors import CompileError, PyrexError
if options is None:
......
......@@ -50,12 +50,13 @@ def unbound_symbols(code, context=None):
if not tree.scope.lookup(name) and not hasattr(__builtin__, name):
unbound.append(name)
return unbound
def get_type(arg, context=None):
py_type = type(arg)
if py_type in [list, tuple, dict, str]:
return py_type.__name__
elif py_type is complex:
return 'double complex'
elif py_type is float:
return 'double'
elif py_type is bool:
......@@ -75,11 +76,10 @@ def get_type(arg, context=None):
return '%s.%s' % (base_type.__module__, base_type.__name__)
return 'object'
# TODO: use locals/globals for unbound variables
def cython_inline(code,
types='aggressive',
lib_dir=os.path.expanduser('~/.cython/inline'),
include_dirs=['.'],
cython_include_dirs=['.'],
locals=None,
globals=None,
**kwds):
......@@ -108,9 +108,6 @@ def cython_inline(code,
arg_sigs = tuple([(get_type(kwds[arg], ctx), arg) for arg in arg_names])
key = code, arg_sigs, sys.version_info, sys.executable, Cython.__version__
module_name = "_cython_inline_" + hashlib.md5(str(key)).hexdigest()
# # TODO: Does this cover all the platforms?
# if (not os.path.exists(os.path.join(lib_dir, module_name + ".so")) and
# not os.path.exists(os.path.join(lib_dir, module_name + ".dll"))):
try:
if not os.path.exists(lib_dir):
os.makedirs(lib_dir)
......@@ -147,7 +144,7 @@ def __invoke(%(params)s):
include_dirs = c_include_dirs)
build_extension = build_ext(Distribution())
build_extension.finalize_options()
build_extension.extensions = cythonize([extension])
build_extension.extensions = cythonize([extension], ctx=ctx)
build_extension.build_temp = os.path.dirname(pyx_file)
build_extension.build_lib = lib_dir
build_extension.run()
......
from Dependencies import cythonize
......@@ -114,7 +114,7 @@ static PyObject* __Pyx_PyRun(PyObject* o, PyObject* globals, PyObject* locals) {
if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
PyDict_SetItemString(globals, "__builtins__", PyEval_GetBuiltins());
PyDict_SetItemString(globals, "__builtins__", PyEval_GetBuiltins());
}
if (PyCode_Check(o)) {
......@@ -123,33 +123,37 @@ static PyObject* __Pyx_PyRun(PyObject* o, PyObject* globals, PyObject* locals) {
"code object passed to exec() may not contain free variables");
goto bad;
}
result = PyEval_EvalCode((PyCodeObject *)o, globals, locals);
#if PY_VERSION_HEX < 0x030200A4
result = PyEval_EvalCode((PyCodeObject *)o, globals, locals);
#else
result = PyEval_EvalCode(o, globals, locals);
#endif
} else {
PyCompilerFlags cf;
cf.cf_flags = 0;
if (PyUnicode_Check(o)) {
if (PyUnicode_Check(o)) {
cf.cf_flags = PyCF_SOURCE_IS_UTF8;
s = PyUnicode_AsUTF8String(o);
if (!s) goto bad;
o = s;
#if PY_MAJOR_VERSION >= 3
} else if (!PyBytes_Check(o)) {
#else
} else if (!PyString_Check(o)) {
#endif
PyErr_SetString(PyExc_TypeError,
"exec: arg 1 must be string, bytes or code object");
goto bad;
}
#if PY_MAJOR_VERSION >= 3
code = PyBytes_AS_STRING(o);
#else
code = PyString_AS_STRING(o);
#endif
if (PyEval_MergeCompilerFlags(&cf)) {
result = PyRun_StringFlags(code, Py_file_input, globals, locals, &cf);
s = PyUnicode_AsUTF8String(o);
if (!s) goto bad;
o = s;
#if PY_MAJOR_VERSION >= 3
} else if (!PyBytes_Check(o)) {
#else
} else if (!PyString_Check(o)) {
#endif
PyErr_SetString(PyExc_TypeError,
"exec: arg 1 must be string, bytes or code object");
goto bad;
}
#if PY_MAJOR_VERSION >= 3
code = PyBytes_AS_STRING(o);
#else
code = PyString_AS_STRING(o);
#endif
if (PyEval_MergeCompilerFlags(&cf)) {
result = PyRun_StringFlags(code, Py_file_input, globals, locals, &cf);
} else {
result = PyRun_String(code, Py_file_input, globals, locals);
result = PyRun_String(code, Py_file_input, globals, locals);
}
Py_XDECREF(s);
}
......@@ -300,6 +304,22 @@ class _BuiltinOverride(object):
self.func_type, self.sig = func_type, sig
self.utility_code = utility_code
class BuiltinAttribute(object):
def __init__(self, py_name, cname=None, field_type=None, field_type_name=None):
self.py_name = py_name
self.cname = cname or py_name
self.field_type_name = field_type_name # can't do the lookup before the type is declared!
self.field_type = field_type
def declare_in_type(self, self_type):
if self.field_type_name is not None:
# lazy type lookup
field_type = builtin_scope.lookup(self.field_type_name).type
else:
field_type = self.field_type or PyrexTypes.py_object_type
entry = self_type.scope.declare(self.py_name, self.cname, field_type, None, 'private')
entry.is_variable = True
class BuiltinFunction(_BuiltinOverride):
def declare_in_scope(self, scope):
func_type, sig = self.func_type, self.sig
......@@ -426,9 +446,10 @@ builtin_types_table = [
("long", "PyLong_Type", []),
("float", "PyFloat_Type", []),
# Until we have a way to access attributes of a type,
# we don't want to make this one builtin.
# ("complex", "PyComplex_Type", []),
("complex", "PyComplex_Type", [BuiltinAttribute('cval', field_type_name = 'Py_complex'),
BuiltinAttribute('real', 'cval.real', field_type = PyrexTypes.c_double_type),
BuiltinAttribute('imag', 'cval.imag', field_type = PyrexTypes.c_double_type),
]),
("bytes", "PyBytes_Type", []),
("str", "PyString_Type", []),
......@@ -447,7 +468,10 @@ builtin_types_table = [
BuiltinMethod("values","T", "O", "PyDict_Values"), # FIXME: Py3 mode?
BuiltinMethod("copy", "T", "T", "PyDict_Copy")]),
("slice", "PySlice_Type", []),
("slice", "PySlice_Type", [BuiltinAttribute('start'),
BuiltinAttribute('stop'),
BuiltinAttribute('step'),
]),
# ("file", "PyFile_Type", []), # not in Py3
("set", "PySet_Type", [BuiltinMethod("clear", "T", "i", "PySet_Clear"),
......@@ -480,6 +504,10 @@ builtin_structs_table = [
("strides", PyrexTypes.c_py_ssize_t_ptr_type),
("suboffsets", PyrexTypes.c_py_ssize_t_ptr_type),
("internal", PyrexTypes.c_void_ptr_type),
]),
('Py_complex', 'Py_complex',
[('real', PyrexTypes.c_double_type),
('imag', PyrexTypes.c_double_type),
])
]
......@@ -497,7 +525,13 @@ def init_builtin_types():
global builtin_types
for name, cname, methods in builtin_types_table:
utility = builtin_utility_code.get(name)
the_type = builtin_scope.declare_builtin_type(name, cname, utility)
if name == 'frozenset':
objstruct_cname = 'PySetObject'
elif name == 'bool':
objstruct_cname = None
else:
objstruct_cname = 'Py%sObject' % name.capitalize()
the_type = builtin_scope.declare_builtin_type(name, cname, utility, objstruct_cname)
builtin_types[name] = the_type
for method in methods:
method.declare_in_type(the_type)
......@@ -512,9 +546,9 @@ def init_builtin_structs():
name, "struct", scope, 1, None, cname = cname)
def init_builtins():
init_builtin_structs()
init_builtin_funcs()
init_builtin_types()
init_builtin_structs()
global list_type, tuple_type, dict_type, set_type, frozenset_type
global bytes_type, str_type, unicode_type
global float_type, bool_type, type_type, complex_type
......
......@@ -37,6 +37,7 @@ Options:
--embed Embed the Python interpreter in a main() method.
-2 Compile based on Python-2 syntax and code semantics.
-3 Compile based on Python-3 syntax and code semantics.
--fatal-errors Abort the compilation on the first error
-X, --directive <name>=<value>[,<name=value,...] Overrides a compiler directive
"""
......@@ -122,6 +123,8 @@ def parse_command_line(args):
options.language_level = 2
elif option == '-3':
options.language_level = 3
elif option == "--fatal-errors":
Options.fatal_errors = True
elif option in ("-X", "--directive"):
try:
options.compiler_directives = Options.parse_directive_list(
......
......@@ -5,6 +5,7 @@
import sys
from Cython.Utils import open_new_file
from DebugFlags import debug_exception_on_error
import Options
class PyrexError(Exception):
......@@ -142,6 +143,8 @@ def report_error(err):
except UnicodeEncodeError:
echo_file.write(line.encode('ASCII', 'replace'))
num_errors = num_errors + 1
if Options.fatal_errors:
raise InternalError, "abort"
def error(position, message):
#print "Errors.error:", repr(position), repr(message) ###
......
......@@ -3451,7 +3451,9 @@ class AttributeNode(ExprNode):
if not target:
self.is_temp = 1
self.result_ctype = py_object_type
elif target and self.obj.type.is_builtin_type:
error(self.pos, "Assignment to an immutable object field")
def analyse_attribute(self, env, obj_type = None):
# Look up attribute and set self.type and self.member.
self.is_py_attr = 0
......@@ -3466,7 +3468,7 @@ class AttributeNode(ExprNode):
if obj_type.is_ptr or obj_type.is_array:
obj_type = obj_type.base_type
self.op = "->"
elif obj_type.is_extension_type:
elif obj_type.is_extension_type or obj_type.is_builtin_type:
self.op = "->"
else:
self.op = "."
......@@ -3558,6 +3560,9 @@ class AttributeNode(ExprNode):
elif obj.type.is_complex:
return "__Pyx_C%s(%s)" % (self.member.upper(), obj_code)
else:
if obj.type.is_builtin_type and self.entry and self.entry.is_variable:
# accessing a field of a builtin type, need to cast better than result_as() does
obj_code = obj.type.cast_code(obj.result(), to_object_struct = True)
return "%s%s%s" % (obj_code, self.op, self.member)
def generate_result_code(self, code):
......@@ -4161,6 +4166,54 @@ class ScopedExprNode(ExprNode):
# this is called with the expr_scope as env
pass
def generate_evaluation_code(self, code):
# set up local variables and free their references on exit
generate_inner_evaluation_code = super(ScopedExprNode, self).generate_evaluation_code
if not self.has_local_scope or not self.expr_scope.var_entries:
# no local variables => delegate, done
generate_inner_evaluation_code(code)
return
code.putln('{ /* enter inner scope */')
py_entries = []
for entry in self.expr_scope.var_entries:
if not entry.in_closure:
code.put_var_declaration(entry)
if entry.type.is_pyobject and entry.used:
py_entries.append(entry)
code.put_init_var_to_py_none(entry)
if not py_entries:
# no local Python references => no cleanup required
generate_inner_evaluation_code(code)
code.putln('} /* exit inner scope */')
return
# must free all local Python references at each exit point
old_loop_labels = tuple(code.new_loop_labels())
old_error_label = code.new_error_label()
generate_inner_evaluation_code(code)
# normal (non-error) exit
for entry in py_entries:
code.put_var_decref(entry)
# error/loop body exit points
exit_scope = code.new_label('exit_scope')
code.put_goto(exit_scope)
for label, old_label in ([(code.error_label, old_error_label)] +
list(zip(code.get_loop_labels(), old_loop_labels))):
if code.label_used(label):
code.put_label(label)
for entry in py_entries:
code.put_var_decref(entry)
code.put_goto(old_label)
code.put_label(exit_scope)
code.putln('} /* exit inner scope */')
code.set_loop_labels(old_loop_labels)
code.error_label = old_error_label
class ComprehensionNode(ScopedExprNode):
subexprs = ["target"]
......@@ -7021,12 +7074,17 @@ class CoerceToPyTypeNode(CoercionNode):
if not arg.type.create_to_py_utility_code(env):
error(arg.pos,
"Cannot convert '%s' to Python object" % arg.type)
if type is not py_object_type:
self.type = py_object_type
elif arg.type.is_string:
self.type = bytes_type
elif arg.type is PyrexTypes.c_py_unicode_type:
self.type = unicode_type
if type is py_object_type:
# be specific about some known types
if arg.type.is_string:
self.type = bytes_type
elif arg.type is PyrexTypes.c_py_unicode_type:
self.type = unicode_type
elif arg.type.is_complex:
self.type = Builtin.complex_type
else:
# FIXME: check that the target type and the resulting type are compatible
pass
gil_message = "Converting to Python object"
......
......@@ -577,8 +577,7 @@ def create_default_resultobj(compilation_source, options):
def run_pipeline(source, options, full_module_name = None):
# Set up context
context = Context(options.include_path, options.compiler_directives,
options.cplus, options.language_level)
context = options.create_context()
# Set up source object
cwd = os.getcwd()
......@@ -649,6 +648,10 @@ class CompilationOptions(object):
self.__dict__.update(defaults)
self.__dict__.update(kw)
def create_context(self):
return Context(self.include_path, self.compiler_directives,
self.cplus, self.language_level)
class CompilationResult(object):
"""
......
......@@ -271,9 +271,7 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
code = globalstate['all_the_rest']
self.generate_cached_builtins_decls(env, code)
# generate lambda function definitions
for node in env.lambda_defs:
node.generate_function_definitions(env, code)
self.generate_lambda_definitions(env, code)
# generate normal function definitions
self.body.generate_function_definitions(env, code)
code.mark_pos(None)
......@@ -2020,7 +2018,8 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
def generate_base_type_import_code(self, env, entry, code):
base_type = entry.type.base_type
if base_type and base_type.module_name != env.qualified_name:
if base_type and base_type.module_name != env.qualified_name \
and not base_type.is_builtin_type:
self.generate_type_import_code(env, base_type, self.pos, code)
def use_type_import_utility_code(self, env):
......@@ -2098,7 +2097,7 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
# unless we let PyType_Ready create the slot wrappers we have
# a significant performance hit. (See trac #561.)
for func in entry.type.scope.pyfunc_entries:
if func.is_special and func.wrapperbase_cname:
if func.is_special and Options.docstrings and func.wrapperbase_cname:
code.putln("{");
code.putln(
'PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&%s, "%s"); %s' % (
......
......@@ -321,7 +321,10 @@ class BlockNode(object):
for entry in entries:
code.globalstate.add_cached_builtin_decl(entry)
del entries[:]
def generate_lambda_definitions(self, env, code):
for node in env.lambda_defs:
node.generate_function_definitions(env, code)
class StatListNode(Node):
# stats a list of StatNode
......@@ -783,6 +786,14 @@ class CSimpleBaseTypeNode(CBaseTypeNode):
error(self.pos, "can only complexify c numeric types")
type = PyrexTypes.CComplexType(type)
type.create_declaration_utility_code(env)
elif type is Builtin.complex_type:
# Special case: optimise builtin complex type into C's
# double complex. The parser cannot do this (as for the
# normal scalar types) as the user may have redeclared the
# 'complex' type. Testing for the exact type here works.
type = PyrexTypes.c_double_complex_type
type.create_declaration_utility_code(env)
self.complex = True
if type:
return type
else:
......@@ -1211,8 +1222,7 @@ class FuncDefNode(StatNode, BlockNode):
# Generate closure function definitions
self.body.generate_function_definitions(lenv, code)
# generate lambda function definitions
for node in lenv.lambda_defs:
node.generate_function_definitions(lenv, code)
self.generate_lambda_definitions(lenv, code)
is_getbuffer_slot = (self.entry.name == "__getbuffer__" and
self.entry.scope.is_c_class_scope)
......@@ -3079,6 +3089,7 @@ class PyClassDefNode(ClassDefNode):
self.target.analyse_target_expression(env, self.classobj)
def generate_function_definitions(self, env, code):
self.generate_lambda_definitions(self.scope, code)
self.body.generate_function_definitions(self.scope, code)
def generate_execution_code(self, code):
......@@ -3181,7 +3192,9 @@ class CClassDefNode(ClassDefNode):
if base_class_entry:
if not base_class_entry.is_type:
error(self.pos, "'%s' is not a type name" % self.base_class_name)
elif not base_class_entry.type.is_extension_type:
elif not base_class_entry.type.is_extension_type and \
not (base_class_entry.type.is_builtin_type and \
base_class_entry.type.objstruct_cname):
error(self.pos, "'%s' is not an extension type" % self.base_class_name)
elif not base_class_entry.type.is_complete():
error(self.pos, "Base class '%s' of type '%s' is incomplete" % (
......@@ -3190,6 +3203,10 @@ class CClassDefNode(ClassDefNode):
base_class_entry.type.scope.directives['final']:
error(self.pos, "Base class '%s' of type '%s' is final" % (
self.base_class_name, self.class_name))
elif base_class_entry.type.is_builtin_type and \
base_class_entry.type.name in ('tuple', 'str', 'bytes'):
error(self.pos, "inheritance from PyVarObject types like '%s' is not currently supported"
% base_class_entry.type.name)
else:
self.base_type = base_class_entry.type
has_body = self.body is not None
......@@ -3242,8 +3259,8 @@ class CClassDefNode(ClassDefNode):
def generate_function_definitions(self, env, code):
if self.body:
self.body.generate_function_definitions(
self.entry.type.scope, code)
self.generate_lambda_definitions(self.scope, code)
self.body.generate_function_definitions(self.scope, code)
def generate_execution_code(self, code):
# This is needed to generate evaluation code for
......
......@@ -18,6 +18,10 @@ generate_cleanup_code = 0
annotate = 0
# This will abort the compilation on the first error occured rather than trying
# to keep going and printing further error messages.
fatal_errors = False
# This will convert statements of the form "for i in range(...)"
# to "for i from ..." when i is a cdef'd integer type, and the direction
# (i.e. sign of step) can be determined.
......
This diff is collapsed.
......@@ -1913,7 +1913,7 @@ def p_c_simple_base_type(s, self_flag, nonempty, templates = None):
name = s.systring
s.next()
else:
name = 'int'
name = 'int' # long [int], short [int], long [int] complex, etc.
if s.sy == 'IDENT' and s.systring == 'complex':
complex = 1
s.next()
......
......@@ -373,16 +373,25 @@ class PyObjectType(PyrexType):
return cname
class BuiltinObjectType(PyObjectType):
# objstruct_cname string Name of PyObject struct
is_builtin_type = 1
has_attributes = 1
base_type = None
module_name = '__builtin__'
def __init__(self, name, cname):
# fields that let it look like an extension type
vtabslot_cname = None
vtabstruct_cname = None
vtabptr_cname = None
typedef_flag = True
is_external = True
def __init__(self, name, cname, objstruct_cname=None):
self.name = name
self.cname = cname
self.typeptr_cname = "&" + cname
self.typeptr_cname = "(&%s)" % cname
self.objstruct_cname = objstruct_cname
def set_scope(self, scope):
self.scope = scope
......@@ -415,16 +424,13 @@ class BuiltinObjectType(PyObjectType):
def type_check_function(self, exact=True):
type_name = self.name
if type_name == 'bool':
return 'PyBool_Check'
if type_name == 'str':
type_check = 'PyString_Check'
elif type_name == 'frozenset':
type_check = 'PyFrozenSet_Check'
else:
type_check = 'Py%s_Check' % type_name.capitalize()
if exact:
if exact and type_name not in ('bool', 'slice'):
type_check += 'Exact'
return type_check
......@@ -448,6 +454,11 @@ class BuiltinObjectType(PyObjectType):
entity_code = "*%s" % entity_code
return self.base_declaration_code(base_code, entity_code)
def cast_code(self, expr_code, to_object_struct = False):
return "((%s*)%s)" % (
to_object_struct and self.objstruct_cname or "PyObject", # self.objstruct_cname may be None
expr_code)
class PyExtensionType(PyObjectType):
#
......@@ -2314,7 +2325,7 @@ modifiers_and_name_to_type = {
(1, 0, "double"): c_double_type,
(1, 1, "double"): c_longdouble_type,
(1, 0, "complex"): c_float_complex_type,
(1, 0, "complex"): c_double_complex_type, # C: float, Python: double => Python wins
(1, 0, "floatcomplex"): c_float_complex_type,
(1, 0, "doublecomplex"): c_double_complex_type,
(1, 1, "doublecomplex"): c_longdouble_complex_type,
......
......@@ -741,9 +741,9 @@ class BuiltinScope(Scope):
entry.as_variable = var_entry
return entry
def declare_builtin_type(self, name, cname, utility_code = None):
def declare_builtin_type(self, name, cname, utility_code = None, objstruct_cname = None):
name = EncodedString(name)
type = PyrexTypes.BuiltinObjectType(name, cname)
type = PyrexTypes.BuiltinObjectType(name, cname, objstruct_cname)
scope = CClassScope(name, outer_scope=None, visibility='extern')
scope.directives = {}
if name == 'bool':
......@@ -1313,7 +1313,9 @@ class GeneratorExpressionScope(Scope):
# the parent scope needs to generate code for the variable, but
# this scope must hold its name exclusively
cname = '%s%s' % (self.genexp_prefix, self.parent_scope.mangle(Naming.var_prefix, name))
entry = self.parent_scope.declare_var(None, type, pos, cname, visibility, is_cdef = True)
entry = self.declare(name, cname, type, pos, visibility)
entry.is_variable = 1
self.var_entries.append(entry)
self.entries[name] = entry
return entry
......
......@@ -105,7 +105,7 @@ class TreeVisitor(object):
code = frame.f_code
method_name = code.co_name
pos = (os.path.basename(code.co_filename),
code.co_firstlineno)
frame.f_lineno)
nodes.append((node, method_name, pos))
last_traceback = stacktrace
stacktrace = stacktrace.tb_next
......
cdef extern from "Python.h":
ctypedef struct Py_complex
ctypedef struct Py_complex:
double imag
double real
############################################################################
# 7.2.5.2 Complex Numbers as Python Objects
......@@ -9,6 +12,12 @@ cdef extern from "Python.h":
# PyComplexObject
# This subtype of PyObject represents a Python complex number object.
ctypedef class __builtin__.complex [object PyComplexObject]:
cdef Py_complex cval
# not making these available to keep them read-only:
#cdef double imag "cval.imag"
#cdef double real "cval.real"
# PyTypeObject PyComplex_Type
# This instance of PyTypeObject represents the Python complex
# number type. It is the same object as complex and
......
cdef extern from "math.h":
enum: M_E
enum: M_LOG2E
enum: M_LOG10E
enum: M_LN2
enum: M_LN10
enum: M_PI
enum: M_PI_2
enum: M_PI_4
enum: M_1_PI
enum: M_2_PI
enum: M_2_SQRTPI
enum: M_SQRT2
enum: M_SQRT1_2
double acos(double x)
double asin(double x)
double atan(double x)
double atan2(double y, double x)
double cos(double x)
double sin(double x)
double tan(double x)
double cosh(double x)
double sinh(double x)
double tanh(double x)
double acosh(double x)
double asinh(double x)
double atanh(double x)
double exp(double x)
double log(double x)
double log10(double x)
double pow(double x, double y)
double sqrt(double x)
__version__ = "0.13+"
__version__ = "0.14.alpha0"
# Void cython.* directives (for case insensitive operating systems).
from Cython.Shadow import *
......@@ -18,7 +18,7 @@ include Demos/Setup.py
include Demos/Makefile*
recursive-include Tools *
recursive-include tests *.pyx *.pxd *.pxi *.py *.h *.BROKEN bugs.txt
recursive-include tests *_lib.cpp
recursive-include tests *_lib.cpp *.srctree
include runtests.py
include Cython/Mac/Makefile
......
......@@ -749,7 +749,8 @@ class EndToEndTest(unittest.TestCase):
def setUp(self):
from Cython.TestUtils import unpack_source_tree
_, self.commands = unpack_source_tree(os.path.join('tests', 'build', self.treefile), self.workdir)
_, self.commands = unpack_source_tree(
os.path.join('tests', 'build', self.treefile), self.workdir)
self.old_dir = os.getcwd()
os.chdir(self.workdir)
if self.workdir not in sys.path:
......@@ -764,11 +765,25 @@ class EndToEndTest(unittest.TestCase):
commands = (self.commands
.replace("CYTHON", "PYTHON %s" % os.path.join(self.cython_root, 'cython.py'))
.replace("PYTHON", sys.executable))
old_path = os.environ.get('PYTHONPATH')
try:
os.environ['PYTHONPATH'] = self.cython_syspath + os.pathsep + (old_path or '')
print(os.environ['PYTHONPATH'])
self.assertEqual(0, os.system(commands))
old_path = os.environ.get('PYTHONPATH')
os.environ['PYTHONPATH'] = os.path.join(self.cython_syspath, (old_path or ''))
for command in commands.split('\n'):
if sys.version_info[:2] >= (2,4):
import subprocess
p = subprocess.Popen(commands,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
shell=True)
out, err = p.communicate()
res = p.returncode
if res != 0:
print(command)
print(out)
print(err)
else:
res = os.system(command)
self.assertEqual(0, res, "non-zero exit status")
finally:
if old_path:
os.environ['PYTHONPATH'] = old_path
......
cdef int f() except -1:
cdef slice s
cdef object z
cdef int i
z = slice
s = slice(1, 2, 3)
z = slice.indices()
i = s.start
i = s.stop
i = s.step
PYTHON setup.py build_ext --inplace
PYTHON -c "import a; a.use_vector([1,2,3])"
######## setup.py ########
from Cython.Build.Dependencies import cythonize
from distutils.core import setup
setup(
ext_modules = cythonize("*.pyx"),
)
######## a.pyx ########
# distutils: language = c++
from libcpp.vector cimport vector
def use_vector(L):
try:
v = new vector[int]()
for a in L:
v.push_back(a)
return v.size()
finally:
del v
PYTHON setup.py build_ext --inplace
PYTHON -c "import a"
######## setup.py ########
from Cython.Build.Dependencies import cythonize
from distutils.core import setup
setup(
ext_modules = cythonize("*.pyx", include_path=['subdir'], compiler_directives={'cdivision': True}),
)
######## a.pyx ########
cimport x
include "y.pxi"
# cdivision from setup.py
def mod_int_c(int a, int b):
return a % b
assert mod_int_c(-1, 10) < 0
######## subdir/x.pxd ########
######## subdir/y.pxi ########
from libc.math cimport (M_E, M_LOG2E, M_LOG10E, M_LN2, M_LN10, M_PI, M_PI_2,
M_PI_4, M_1_PI, M_2_PI, M_2_SQRTPI, M_SQRT2, M_SQRT1_2)
from libc.math cimport (acos, asin, atan, atan2, cos, sin, tan, cosh, sinh,
tanh, acosh, asinh, atanh, exp, log, log10, pow, sqrt)
# current restriction: cannot inherit from PyVarObject (see ticket #152)
cdef class MyTuple(tuple):
pass
cdef class MyBytes(bytes):
pass
cdef class MyStr(str): # only in Py2, but can't know that during compilation
pass
_ERRORS = """
4:5: inheritance from PyVarObject types like 'tuple' is not currently supported
7:5: inheritance from PyVarObject types like 'bytes' is not currently supported
10:5: inheritance from PyVarObject types like 'str' is not currently supported
"""
cdef class MyInt(int):
"""
>>> MyInt(2) == 2
True
>>> MyInt(2).attr is None
True
"""
cdef readonly object attr
cdef class MyFloat(float):
"""
>>> MyFloat(1.0)== 1.0
True
>>> MyFloat(1.0).attr is None
True
"""
cdef readonly object attr
ustring = u'abc'
cdef class MyUnicode(unicode):
"""
>>> MyUnicode(ustring) == ustring
True
>>> MyUnicode(ustring).attr is None
True
"""
cdef readonly object attr
cdef class MyList(list):
"""
>>> MyList([1,2,3]) == [1,2,3]
True
>>> MyList([1,2,3]).attr is None
True
"""
cdef readonly object attr
cdef class MyDict(dict):
"""
>>> MyDict({1:2, 3:4}) == {1:2, 3:4}
True
>>> MyDict({1:2, 3:4}).attr is None
True
"""
cdef readonly object attr
from cpython.complex cimport complex
def complex_attributes():
"""
>>> complex_attributes()
(1.0, 2.0)
"""
cdef complex c = 1+2j
return (c.real, c.imag)
def complex_attributes_assign():
"""
>>> complex_attributes_assign()
(10.0, 20.0)
"""
cdef complex c = 1+2j
c.cval.real, c.cval.imag = 10, 20
return (c.real, c.imag)
def complex_cstruct_assign():
"""
>>> complex_cstruct_assign()
(10.0, 20.0)
"""
cdef complex c = 1+2j
cval = &c.cval
cval.real, cval.imag = 10, 20
return (c.real, c.imag)
def complex_coercion():
"""
>>> complex_coercion()
(1.0, 2.0, 1.0, 2.0)
"""
cdef complex py_c = 1+2j
cdef double complex c_c = py_c
cdef object py = c_c
return (c_c.real, c_c.imag, py.real, py.imag)
def complex_arg(complex c):
"""
>>> complex_arg(1+2j)
(1.0, 2.0)
"""
return (c.real, c.imag)
cimport cython
def unbound_method_lookup():
"""
>>> unbound_method_lookup()
"""
ignore = slice.indices
@cython.test_assert_path_exists('//SingleAssignmentNode//AttributeNode[@is_py_attr = False]')
@cython.test_fail_if_path_exists('//SingleAssignmentNode//AttributeNode[@is_py_attr = True]')
def typed_slice():
"""
>>> typed_slice()
(1, 2, 3)
"""
cdef slice s
cdef object z
cdef Py_ssize_t a,b,c
z = slice
s = slice(1, 2, 3)
s.indices
a = s.start
b = s.stop
c = s.step
return (a,b,c)
@cython.test_fail_if_path_exists('//SingleAssignmentNode//AttributeNode[@is_py_attr = False]')
def plain_object_slice():
"""
>>> plain_object_slice()
(1, 2, 3)
"""
cdef object s
cdef object z
cdef Py_ssize_t a,b,c
s = slice(1, 2, 3)
s.indices
a = s.start
b = s.stop
c = s.step
return (a,b,c)
......@@ -18,7 +18,6 @@ def test_non_optimised():
assert isinstance(A(), foo)
assert isinstance(0, (int, long))
assert not isinstance(u"xyz", (int, long))
assert isinstance(complex(), complex) # FIXME: this should be optimised, too!
return True
@cython.test_assert_path_exists('//PythonCapiCallNode',
......@@ -46,6 +45,7 @@ def test_optimised():
assert isinstance(dict(), dict)
assert isinstance(set(), set)
assert isinstance(slice(0), slice)
assert isinstance(complex(), complex)
assert not isinstance(u"foo", int)
assert isinstance(A, type)
return True
......
cdef int cdef_CONST = 123
CONST = 456
cdef class Foo:
"""
>>> obj = Foo()
>>> obj.id(123)
123
>>> obj.cconst_mul(1)
123
>>> obj.const_mul(1)
456
>>> obj.foo[0](1)
1
"""
id = lambda self, x: x
cconst_mul = lambda self, x: x * cdef_CONST
const_mul = lambda self, x: x * CONST
foo = (lambda x:x,)
class Bar:
"""
>>> obj = Bar()
>>> obj.id(123)
123
>>> obj.cconst_mul(1)
123
>>> obj.const_mul(1)
456
>>> obj.foo[0](1)
1
"""
id = lambda self, x: x
cconst_mul = lambda self, x: x * cdef_CONST
const_mul = lambda self, x: x * CONST
foo = (lambda x:x,)
......@@ -105,6 +105,24 @@ def test_set_discard():
s1.discard(3)
return s1
def test_set_sideeffect_unhashable_failure():
"""
>>> test_set_sideeffect_unhashable_failure()
[2, 4, 5]
"""
L = []
def sideeffect(x):
L.append(x)
return x
def unhashable_value(x):
L.append(x)
return set()
try:
s = set([1,sideeffect(2),3,unhashable_value(4),sideeffect(5)])
except TypeError: pass
else: assert False, "expected exception not raised"
return L
def sorted(it):
# Py3 can't compare strings to ints
chars = []
......
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