From 6223893a7c23f6f1e7326e47f5f3df2c5bb29bd0 Mon Sep 17 00:00:00 2001 From: Stefan Behnel Date: Fri, 26 Nov 2010 15:37:02 +0100 Subject: [PATCH] fix several Py3 and import issues in the compiled modules --- Cython/Compiler/ExprNodes.py | 9 +++++++-- Cython/Compiler/Nodes.py | 20 +++++++++++--------- Cython/Compiler/Optimize.py | 9 +++++++-- Cython/Compiler/ParseTreeTransforms.py | 18 +++++++++++------- 4 files changed, 36 insertions(+), 20 deletions(-) diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py index c11a3ad6..83883bd7 100755 --- a/Cython/Compiler/ExprNodes.py +++ b/Cython/Compiler/ExprNodes.py @@ -36,6 +36,11 @@ from Cython.Debugging import print_call_chain from DebugFlags import debug_disposal_code, debug_temp_alloc, \ debug_coercion +try: + from __builtin__ import basestring +except ImportError: + basestring = str # Python 3 + class NotConstant(object): def __repr__(self): return "" @@ -2986,7 +2991,7 @@ class SimpleCallNode(CallNode): return "" formal_args = func_type.args arg_list_code = [] - args = zip(formal_args, self.args) + args = list(zip(formal_args, self.args)) max_nargs = len(func_type.args) expected_nargs = max_nargs - func_type.optional_arg_count actual_nargs = len(self.args) @@ -3031,7 +3036,7 @@ class SimpleCallNode(CallNode): self.opt_arg_struct, Naming.pyrex_prefix + "n", 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]: code.putln("%s.%s = %s;" % ( self.opt_arg_struct, diff --git a/Cython/Compiler/Nodes.py b/Cython/Compiler/Nodes.py index 0b39523c..b453a27b 100644 --- a/Cython/Compiler/Nodes.py +++ b/Cython/Compiler/Nodes.py @@ -3,15 +3,17 @@ # 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: - set -except NameError: - # Python 2.3 - from sets import Set as set +import sys, os, time, copy -import Code import Builtin from Errors import error, warning, InternalError import Naming @@ -253,7 +255,7 @@ class Node(object): 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: return "<%s (0x%x)>" % (self.__class__.__name__, id(self)) else: @@ -858,7 +860,7 @@ class TemplatedTypeNode(CBaseTypeNode): if sys.version_info[0] < 3: # Py 2.x enforces byte strings as keyword arguments ... 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) diff --git a/Cython/Compiler/Optimize.py b/Cython/Compiler/Optimize.py index 80718245..d95638a5 100644 --- a/Cython/Compiler/Optimize.py +++ b/Cython/Compiler/Optimize.py @@ -28,6 +28,11 @@ try: except ImportError: from functools import reduce +try: + from __builtin__ import basestring +except ImportError: + basestring = str # Python 3 + class FakePythonEnv(object): "A fake environment for creating type test nodes etc." nogil = False @@ -751,7 +756,7 @@ class SwitchTransform(Visitor.VisitorTransform): def extract_in_string_conditions(self, string_literal): if isinstance(string_literal, ExprNodes.UnicodeNode): - charvals = map(ord, set(string_literal.value)) + charvals = list(map(ord, set(string_literal.value))) charvals.sort() return [ ExprNodes.IntNode(string_literal.pos, value=str(charval), constant_result=charval) @@ -2937,7 +2942,7 @@ class ConstantFolding(Visitor.VisitorTransform, SkipDeclarations): # check if all children are constant children = self.visitchildren(node) - for child_result in children.itervalues(): + for child_result in children.values(): if type(child_result) is list: for child in child_result: if getattr(child, 'constant_result', not_a_constant) is not_a_constant: diff --git a/Cython/Compiler/ParseTreeTransforms.py b/Cython/Compiler/ParseTreeTransforms.py index ae8f8ca1..a6047d77 100644 --- a/Cython/Compiler/ParseTreeTransforms.py +++ b/Cython/Compiler/ParseTreeTransforms.py @@ -1,3 +1,9 @@ + +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 CythonTransform, EnvTransform, ScopeTrackingTransform from Cython.Compiler.ModuleNode import ModuleNode @@ -7,10 +13,8 @@ from Cython.Compiler.UtilNodes import * from Cython.Compiler.TreeFragment import TreeFragment, TemplateTransform from Cython.Compiler.StringEncoding import EncodedString from Cython.Compiler.Errors import error, CompileError -try: - set -except NameError: - from sets import Set as set +from Cython.Compiler import PyrexTypes, Naming + import copy @@ -572,12 +576,12 @@ class InterpretCompilerDirectives(CythonTransform, SkipDeclarations): special_methods = set(['declare', 'union', 'struct', 'typedef', 'sizeof', 'cast', 'pointer', 'compiled', 'NULL'] - + unop_method_nodes.keys()) + + list(unop_method_nodes.keys())) def __init__(self, context, compilation_directive_defaults): super(InterpretCompilerDirectives, self).__init__(context) 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.cython_module_names = set() self.directive_names = {} @@ -593,7 +597,7 @@ class InterpretCompilerDirectives(CythonTransform, SkipDeclarations): # Set up processing and handle the cython: comments. 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'): self.wrong_scope_error(node.pos, key, 'module') del node.directive_comments[key] -- 2.26.2