Commit 6223893a authored by Stefan Behnel's avatar Stefan Behnel

fix several Py3 and import issues in the compiled modules

parent c8ccb5b1
...@@ -36,6 +36,11 @@ from Cython.Debugging import print_call_chain ...@@ -36,6 +36,11 @@ from Cython.Debugging import print_call_chain
from DebugFlags import debug_disposal_code, debug_temp_alloc, \ from DebugFlags import debug_disposal_code, debug_temp_alloc, \
debug_coercion debug_coercion
try:
from __builtin__ import basestring
except ImportError:
basestring = str # Python 3
class NotConstant(object): class NotConstant(object):
def __repr__(self): def __repr__(self):
return "<NOT CONSTANT>" return "<NOT CONSTANT>"
...@@ -2986,7 +2991,7 @@ class SimpleCallNode(CallNode): ...@@ -2986,7 +2991,7 @@ class SimpleCallNode(CallNode):
return "<error>" return "<error>"
formal_args = func_type.args formal_args = func_type.args
arg_list_code = [] arg_list_code = []
args = zip(formal_args, self.args) args = list(zip(formal_args, self.args))
max_nargs = len(func_type.args) max_nargs = len(func_type.args)
expected_nargs = max_nargs - func_type.optional_arg_count expected_nargs = max_nargs - func_type.optional_arg_count
actual_nargs = len(self.args) actual_nargs = len(self.args)
...@@ -3031,7 +3036,7 @@ class SimpleCallNode(CallNode): ...@@ -3031,7 +3036,7 @@ class SimpleCallNode(CallNode):
self.opt_arg_struct, self.opt_arg_struct,
Naming.pyrex_prefix + "n", Naming.pyrex_prefix + "n",
len(self.args) - expected_nargs)) len(self.args) - expected_nargs))
args = zip(func_type.args, self.args) args = list(zip(func_type.args, self.args))
for formal_arg, actual_arg in args[expected_nargs:actual_nargs]: for formal_arg, actual_arg in args[expected_nargs:actual_nargs]:
code.putln("%s.%s = %s;" % ( code.putln("%s.%s = %s;" % (
self.opt_arg_struct, self.opt_arg_struct,
......
...@@ -3,15 +3,17 @@ ...@@ -3,15 +3,17 @@
# Pyrex - Parse tree nodes # Pyrex - Parse tree nodes
# #
import sys, os, time, copy import cython
from cython import set
cython.declare(sys=object, os=object, time=object, copy=object,
Builtin=object, error=object, warning=object, Naming=object, PyrexTypes=object,
py_object_type=object, ModuleScope=object, LocalScope=object, ClosureScope=object, \
StructOrUnionScope=object, PyClassScope=object, CClassScope=object,
CppClassScope=object, UtilityCode=object, EncodedString=object,
absolute_path_length=cython.Py_ssize_t)
try: import sys, os, time, copy
set
except NameError:
# Python 2.3
from sets import Set as set
import Code
import Builtin import Builtin
from Errors import error, warning, InternalError from Errors import error, warning, InternalError
import Naming import Naming
...@@ -253,7 +255,7 @@ class Node(object): ...@@ -253,7 +255,7 @@ class Node(object):
return repr(x) return repr(x)
attrs = [(key, value) for key, value in self.__dict__.iteritems() if key not in filter_out] attrs = [(key, value) for key, value in self.__dict__.items() if key not in filter_out]
if len(attrs) == 0: if len(attrs) == 0:
return "<%s (0x%x)>" % (self.__class__.__name__, id(self)) return "<%s (0x%x)>" % (self.__class__.__name__, id(self))
else: else:
...@@ -858,7 +860,7 @@ class TemplatedTypeNode(CBaseTypeNode): ...@@ -858,7 +860,7 @@ class TemplatedTypeNode(CBaseTypeNode):
if sys.version_info[0] < 3: if sys.version_info[0] < 3:
# Py 2.x enforces byte strings as keyword arguments ... # Py 2.x enforces byte strings as keyword arguments ...
options = dict([ (name.encode('ASCII'), value) options = dict([ (name.encode('ASCII'), value)
for name, value in options.iteritems() ]) for name, value in options.items() ])
self.type = PyrexTypes.BufferType(base_type, **options) self.type = PyrexTypes.BufferType(base_type, **options)
......
...@@ -28,6 +28,11 @@ try: ...@@ -28,6 +28,11 @@ try:
except ImportError: except ImportError:
from functools import reduce from functools import reduce
try:
from __builtin__ import basestring
except ImportError:
basestring = str # Python 3
class FakePythonEnv(object): class FakePythonEnv(object):
"A fake environment for creating type test nodes etc." "A fake environment for creating type test nodes etc."
nogil = False nogil = False
...@@ -751,7 +756,7 @@ class SwitchTransform(Visitor.VisitorTransform): ...@@ -751,7 +756,7 @@ class SwitchTransform(Visitor.VisitorTransform):
def extract_in_string_conditions(self, string_literal): def extract_in_string_conditions(self, string_literal):
if isinstance(string_literal, ExprNodes.UnicodeNode): if isinstance(string_literal, ExprNodes.UnicodeNode):
charvals = map(ord, set(string_literal.value)) charvals = list(map(ord, set(string_literal.value)))
charvals.sort() charvals.sort()
return [ ExprNodes.IntNode(string_literal.pos, value=str(charval), return [ ExprNodes.IntNode(string_literal.pos, value=str(charval),
constant_result=charval) constant_result=charval)
...@@ -2937,7 +2942,7 @@ class ConstantFolding(Visitor.VisitorTransform, SkipDeclarations): ...@@ -2937,7 +2942,7 @@ class ConstantFolding(Visitor.VisitorTransform, SkipDeclarations):
# check if all children are constant # check if all children are constant
children = self.visitchildren(node) children = self.visitchildren(node)
for child_result in children.itervalues(): for child_result in children.values():
if type(child_result) is list: if type(child_result) is list:
for child in child_result: for child in child_result:
if getattr(child, 'constant_result', not_a_constant) is not_a_constant: if getattr(child, 'constant_result', not_a_constant) is not_a_constant:
......
import cython
from cython import set
cython.declare(copy=object, ModuleNode=object, TreeFragment=object, TemplateTransform=object,
EncodedString=object, error=object, PyrexTypes=object, Naming=object)
from Cython.Compiler.Visitor import VisitorTransform, TreeVisitor from Cython.Compiler.Visitor import VisitorTransform, TreeVisitor
from Cython.Compiler.Visitor import CythonTransform, EnvTransform, ScopeTrackingTransform from Cython.Compiler.Visitor import CythonTransform, EnvTransform, ScopeTrackingTransform
from Cython.Compiler.ModuleNode import ModuleNode from Cython.Compiler.ModuleNode import ModuleNode
...@@ -7,10 +13,8 @@ from Cython.Compiler.UtilNodes import * ...@@ -7,10 +13,8 @@ from Cython.Compiler.UtilNodes import *
from Cython.Compiler.TreeFragment import TreeFragment, TemplateTransform from Cython.Compiler.TreeFragment import TreeFragment, TemplateTransform
from Cython.Compiler.StringEncoding import EncodedString from Cython.Compiler.StringEncoding import EncodedString
from Cython.Compiler.Errors import error, CompileError from Cython.Compiler.Errors import error, CompileError
try: from Cython.Compiler import PyrexTypes, Naming
set
except NameError:
from sets import Set as set
import copy import copy
...@@ -572,12 +576,12 @@ class InterpretCompilerDirectives(CythonTransform, SkipDeclarations): ...@@ -572,12 +576,12 @@ class InterpretCompilerDirectives(CythonTransform, SkipDeclarations):
special_methods = set(['declare', 'union', 'struct', 'typedef', 'sizeof', special_methods = set(['declare', 'union', 'struct', 'typedef', 'sizeof',
'cast', 'pointer', 'compiled', 'NULL'] 'cast', 'pointer', 'compiled', 'NULL']
+ unop_method_nodes.keys()) + list(unop_method_nodes.keys()))
def __init__(self, context, compilation_directive_defaults): def __init__(self, context, compilation_directive_defaults):
super(InterpretCompilerDirectives, self).__init__(context) super(InterpretCompilerDirectives, self).__init__(context)
self.compilation_directive_defaults = {} self.compilation_directive_defaults = {}
for key, value in compilation_directive_defaults.iteritems(): for key, value in compilation_directive_defaults.items():
self.compilation_directive_defaults[unicode(key)] = value self.compilation_directive_defaults[unicode(key)] = value
self.cython_module_names = set() self.cython_module_names = set()
self.directive_names = {} self.directive_names = {}
...@@ -593,7 +597,7 @@ class InterpretCompilerDirectives(CythonTransform, SkipDeclarations): ...@@ -593,7 +597,7 @@ class InterpretCompilerDirectives(CythonTransform, SkipDeclarations):
# Set up processing and handle the cython: comments. # Set up processing and handle the cython: comments.
def visit_ModuleNode(self, node): def visit_ModuleNode(self, node):
for key, value in node.directive_comments.iteritems(): for key, value in node.directive_comments.items():
if not self.check_directive_scope(node.pos, key, 'module'): if not self.check_directive_scope(node.pos, key, 'module'):
self.wrong_scope_error(node.pos, key, 'module') self.wrong_scope_error(node.pos, key, 'module')
del node.directive_comments[key] del node.directive_comments[key]
......
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