From: Stefan Behnel Date: Fri, 13 Mar 2009 12:45:28 +0000 (+0100) Subject: major cleanup to fix Py3 code issues (found by 2to3 and 'python2.6 -3') X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=df9b64317d9fadc2bdb0c23f23e31bc54781136b;p=cython.git major cleanup to fix Py3 code issues (found by 2to3 and 'python2.6 -3') --- diff --git a/Cython/Compiler/Nodes.py b/Cython/Compiler/Nodes.py index eadfc5be..4c8224a8 100644 --- a/Cython/Compiler/Nodes.py +++ b/Cython/Compiler/Nodes.py @@ -2,7 +2,7 @@ # Pyrex - Parse tree nodes # -import string, sys, os, time, copy +import sys, os, time, copy import Code import Builtin @@ -1412,7 +1412,7 @@ class CFuncDefNode(FuncDefNode): arg_decls = ["void"] if cname is None: cname = self.entry.func_cname - entity = type.function_header_code(cname, string.join(arg_decls, ", ")) + entity = type.function_header_code(cname, ', '.join(arg_decls)) if visibility == 'public': dll_linkage = "DL_EXPORT" else: @@ -3790,7 +3790,7 @@ class WhileStatNode(LoopNode, StatNode): def ForStatNode(pos, **kw): - if kw.has_key('iterator'): + if 'iterator' in kw: return ForInStatNode(pos, **kw) else: return ForFromStatNode(pos, **kw) diff --git a/Cython/Distutils/build_ext.py b/Cython/Distutils/build_ext.py index c10ae565..df462983 100644 --- a/Cython/Distutils/build_ext.py +++ b/Cython/Distutils/build_ext.py @@ -7,7 +7,7 @@ building Cython extension modules.""" __revision__ = "$Id:$" -import sys, os, string, re +import sys, os, re from types import * from distutils.core import Command from distutils.errors import * @@ -72,7 +72,7 @@ class build_ext(_build_ext.build_ext): self.pyrex_include_dirs = [] elif type(self.pyrex_include_dirs) is StringType: self.pyrex_include_dirs = \ - string.split(self.pyrex_include_dirs, os.pathsep) + self.pyrex_include_dirs.split(os.pathsep) # finalize_options () def build_extensions(self): diff --git a/Cython/Distutils/extension.py b/Cython/Distutils/extension.py index 5c35557a..b9b73755 100644 --- a/Cython/Distutils/extension.py +++ b/Cython/Distutils/extension.py @@ -5,7 +5,7 @@ Pyrex extension modules in setup scripts.""" __revision__ = "$Id:$" -import os, string, sys +import os, sys from types import * import distutils.extension as _Extension diff --git a/Cython/Mac/MacSystem.py b/Cython/Mac/MacSystem.py index 1f02b970..dccc6534 100644 --- a/Cython/Mac/MacSystem.py +++ b/Cython/Mac/MacSystem.py @@ -2,7 +2,7 @@ # Pyrex -- Mac system interface # -import os, sys, string +import os, sys import aetools from aetools import TalkTo from StdSuites.Standard_Suite import Standard_Suite_Events as Standard_Suite @@ -57,8 +57,8 @@ def do_toolserver_command(command): errn, stat, stdout, stderr = result if errn: raise CCompilerError("ToolServer error: %s" % errn) - stdout = string.replace(stdout, "\r", "\n") - stderr = string.replace(stderr, "\r", "\n") + stdout = stdout.replace("\r", "\n") + stderr = stderr.replace("\r", "\n") if stdout: #print "<<< Begin ToolServer StdOut >>>" sys.stderr.write(stdout) @@ -85,7 +85,7 @@ def c_compile(c_file): command = "%s -opt %s -nomapcr -w off -r %s %s -o %s" % ( c_compiler, c_optimizations, - string.join(include_options), + ' '.join(include_options), c_file, o_file, #e_file @@ -106,9 +106,9 @@ def c_link_list(obj_files): out_file = replace_suffix(obj_files[0], shared_lib_suffix) command = "%s -xm s -export all %s %s %s -o %s" % ( c_linker, - string.join(obj_files), + ' '.join(obj_files), pythoncore, - string.join(libraries), + ' '.join(libraries), out_file) stat = do_toolserver_command(command) if stat: diff --git a/Cython/Mac/TS_Misc_Suite.py b/Cython/Mac/TS_Misc_Suite.py index bbfc0e3f..f6879940 100644 --- a/Cython/Mac/TS_Misc_Suite.py +++ b/Cython/Mac/TS_Misc_Suite.py @@ -20,7 +20,7 @@ class TS_Misc_Suite(object): _code = 'misc' _subcode = 'dosc' - if _arguments: raise TypeError, 'No optional args expected' + if _arguments: raise TypeError('No optional args expected') _arguments['----'] = _object @@ -35,15 +35,15 @@ class TS_Misc_Suite(object): stat = 0 stdout = "" stderr = "" - if _arguments.has_key('errn'): + if 'errn' in _arguments: errn = _arguments['errn'] if errn: errn = aetools.decodeerror(_arguments) - if _arguments.has_key('stat'): + if 'stat' in _arguments: stat = _arguments['stat'] - if _arguments.has_key('----'): + if '----' in _arguments: stdout = _arguments['----'] - if _arguments.has_key('diag'): + if 'diag' in _arguments: stderr = _arguments['diag'] return (errn, stat, stdout, stderr) diff --git a/Cython/Plex/DFA.py b/Cython/Plex/DFA.py index a24d3cea..6d0085a8 100644 --- a/Cython/Plex/DFA.py +++ b/Cython/Plex/DFA.py @@ -29,18 +29,18 @@ def nfa_to_dfa(old_machine, debug = None): # Seed the process using the initial states of the old machine. # Make the corresponding new states into initial states of the new # machine with the same names. - for (key, old_state) in old_machine.initial_states.items(): + for (key, old_state) in old_machine.initial_states.iteritems(): new_state = state_map.old_to_new(epsilon_closure(old_state)) new_machine.make_initial_state(key, new_state) # Tricky bit here: we add things to the end of this list while we're # iterating over it. The iteration stops when closure is achieved. for new_state in new_machine.states: transitions = TransitionMap() - for old_state in state_map.new_to_old(new_state).keys(): - for event, old_target_states in old_state.transitions.items(): + for old_state in state_map.new_to_old(new_state): + for event, old_target_states in old_state.transitions.iteritems(): if event and old_target_states: transitions.add_set(event, set_epsilon_closure(old_target_states)) - for event, old_states in transitions.items(): + for event, old_states in transitions.iteritems(): new_machine.add_transitions(new_state, event, state_map.old_to_new(old_states)) if debug: debug.write("\n===== State Mapping =====\n") @@ -53,8 +53,8 @@ def set_epsilon_closure(state_set): closures of its member states. """ result = {} - for state1 in state_set.keys(): - for state2 in epsilon_closure(state1).keys(): + for state1 in state_set: + for state2 in epsilon_closure(state1): result[state2] = 1 return result @@ -80,7 +80,7 @@ def add_to_epsilon_closure(state_set, state): state_set[state] = 1 state_set_2 = state.transitions.get_epsilon() if state_set_2: - for state2 in state_set_2.keys(): + for state2 in state_set_2: add_to_epsilon_closure(state_set, state2) class StateMap(object): @@ -119,7 +119,7 @@ class StateMap(object): def highest_priority_action(self, state_set): best_action = None best_priority = LOWEST_PRIORITY - for state in state_set.keys(): + for state in state_set: priority = state.action_priority if priority > best_priority: best_action = state.action @@ -142,7 +142,7 @@ class StateMap(object): Convert a set of states into a uniquified sorted tuple suitable for use as a dictionary key. """ - lst = state_set.keys() + lst = list(state_set) lst.sort() return tuple(lst) diff --git a/Cython/Plex/Machines.py b/Cython/Plex/Machines.py index a9c69c63..7bb068ef 100644 --- a/Cython/Plex/Machines.py +++ b/Cython/Plex/Machines.py @@ -6,7 +6,6 @@ # #======================================================================= -import string import sys from sys import maxint from types import TupleType @@ -54,7 +53,7 @@ class Machine(object): file.write("Plex.Machine:\n") if self.initial_states is not None: file.write(" Initial states:\n") - for (name, state) in self.initial_states.items(): + for (name, state) in self.initial_states.iteritems(): file.write(" '%s': %d\n" % (name, state.number)) for s in self.states: s.dump(file) @@ -108,7 +107,6 @@ class Node(object): return "State %d" % self.number def dump(self, file): - import string # Header file.write(" State %d:\n" % self.number) # Transitions @@ -143,11 +141,11 @@ class FastMachine(object): for old_state in old_machine.states: new_state = self.new_state() old_to_new[old_state] = new_state - for name, old_state in old_machine.initial_states.items(): + for name, old_state in old_machine.initial_states.iteritems(): initial_states[name] = old_to_new[old_state] for old_state in old_machine.states: new_state = old_to_new[old_state] - for event, old_state_set in old_state.transitions.items(): + for event, old_state_set in old_state.transitions.iteritems(): if old_state_set: new_state[event] = old_to_new[old_state_set.keys()[0]] else: @@ -188,13 +186,12 @@ class FastMachine(object): def dump(self, file): file.write("Plex.FastMachine:\n") file.write(" Initial states:\n") - for name, state in self.initial_states.items(): + for name, state in self.initial_states.iteritems(): file.write(" %s: %s\n" % (repr(name), state['number'])) for state in self.states: self.dump_state(state, file) def dump_state(self, state, file): - import string # Header file.write(" State %d:\n" % state['number']) # Transitions @@ -207,7 +204,7 @@ class FastMachine(object): def dump_transitions(self, state, file): chars_leading_to_state = {} special_to_state = {} - for (c, s) in state.items(): + for (c, s) in state.iteritems(): if len(c) == 1: chars = chars_leading_to_state.get(id(s), None) if chars is None: @@ -249,7 +246,7 @@ class FastMachine(object): return tuple(result) def ranges_to_string(self, range_list): - return string.join(map(self.range_to_string, range_list), ",") + return ','.join(map(self.range_to_string, range_list)) def range_to_string(self, range_tuple): (c1, c2) = range_tuple diff --git a/Cython/Plex/Regexps.py b/Cython/Plex/Regexps.py index 90dbe934..cee03dbf 100644 --- a/Cython/Plex/Regexps.py +++ b/Cython/Plex/Regexps.py @@ -7,7 +7,6 @@ #======================================================================= import array -import string import types from sys import maxint @@ -330,7 +329,7 @@ class Seq(RE): match_bol = re.match_nl or (match_bol and re.nullable) def calc_str(self): - return "Seq(%s)" % string.join(map(str, self.re_list), ",") + return "Seq(%s)" % ','.join(map(str, self.re_list)) class Alt(RE): @@ -369,7 +368,7 @@ class Alt(RE): re.build_machine(m, initial_state, final_state, 0, nocase) def calc_str(self): - return "Alt(%s)" % string.join(map(str, self.re_list), ",") + return "Alt(%s)" % ','.join(map(str, self.re_list)) class Rep1(RE): @@ -437,7 +436,7 @@ def Str1(s): """ Str1(s) is an RE which matches the literal string |s|. """ - result = apply(Seq, tuple(map(Char, s))) + result = Seq(*tuple(map(Char, s))) result.str = "Str(%s)" % repr(s) return result @@ -449,8 +448,8 @@ def Str(*strs): if len(strs) == 1: return Str1(strs[0]) else: - result = apply(Alt, tuple(map(Str1, strs))) - result.str = "Str(%s)" % string.join(map(repr, strs), ",") + result = Alt(*tuple(map(Str1, strs))) + result.str = "Str(%s)" % ','.join(map(repr, strs)) return result def Any(s): @@ -495,7 +494,7 @@ def Range(s1, s2 = None): ranges = [] for i in range(0, len(s1), 2): ranges.append(CodeRange(ord(s1[i]), ord(s1[i+1]) + 1)) - result = apply(Alt, tuple(ranges)) + result = Alt(*ranges) result.str = "Range(%s)" % repr(s1) return result diff --git a/Cython/Plex/Traditional.py b/Cython/Plex/Traditional.py index 5d8f9130..fe13d3a1 100644 --- a/Cython/Plex/Traditional.py +++ b/Cython/Plex/Traditional.py @@ -41,7 +41,7 @@ class REParser(object): while self.c == '|': self.next() re_list.append(self.parse_seq()) - re = apply(Alt, tuple(re_list)) + re = Alt(*re_list) return re def parse_seq(self): @@ -49,7 +49,7 @@ class REParser(object): re_list = [] while not self.end and not self.c in "|)": re_list.append(self.parse_mod()) - return apply(Seq, tuple(re_list)) + return Seq(*re_list) def parse_mod(self): """Parse a primitive regexp followed by *, +, ? modifiers.""" diff --git a/Cython/Plex/Transitions.py b/Cython/Plex/Transitions.py index 4119e71b..66f7a316 100644 --- a/Cython/Plex/Transitions.py +++ b/Cython/Plex/Transitions.py @@ -6,7 +6,6 @@ # from copy import copy -import string from sys import maxint from types import TupleType @@ -89,10 +88,10 @@ class TransitionMap(object): """ return self.special.get('', none) - def items(self, + def iteritems(self, len = len): """ - Return the mapping as a list of ((code1, code2), state_set) and + Return the mapping as an iterable of ((code1, code2), state_set) and (special_event, state_set) pairs. """ result = [] @@ -108,10 +107,10 @@ class TransitionMap(object): result.append(((code0, code1), set)) code0 = code1 i = i + 2 - for event, set in self.special.items(): + for event, set in self.special.iteritems(): if set: result.append((event, set)) - return result + return iter(result) # ------------------- Private methods -------------------- @@ -178,10 +177,10 @@ class TransitionMap(object): map_strs.append(state_set_str(map[i])) i = i + 1 special_strs = {} - for event, set in self.special.items(): + for event, set in self.special.iteritems(): special_strs[event] = state_set_str(set) return "[%s]+%s" % ( - string.join(map_strs, ","), + ','.join(map_strs), special_strs ) @@ -200,7 +199,7 @@ class TransitionMap(object): while i < n: self.dump_range(map[i], map[i + 2], map[i + 1], file) i = i + 2 - for event, set in self.special.items(): + for event, set in self.special.iteritems(): if set: if not event: event = 'empty' @@ -243,11 +242,7 @@ class TransitionMap(object): # set1[state] = 1 def state_set_str(set): - state_list = set.keys() - str_list = [] - for state in state_list: - str_list.append("S%d" % state.number) - return "[%s]" % string.join(str_list, ",") + return "[%s]" % ','.join(["S%d" % state.number for state in set]) diff --git a/Cython/Shadow.py b/Cython/Shadow.py index 6e6c7508..c6c1a777 100644 --- a/Cython/Shadow.py +++ b/Cython/Shadow.py @@ -57,12 +57,12 @@ class PointerType(CythonType): def __getitem__(self, ix): if ix < 0: - raise IndexError, "negative indexing not allowed in C" + raise IndexError("negative indexing not allowed in C") return self._items[ix] def __setitem__(self, ix, value): if ix < 0: - raise IndexError, "negative indexing not allowed in C" + raise IndexError("negative indexing not allowed in C") self._items[ix] = cast(self._basetype, value) class ArrayType(PointerType): @@ -74,22 +74,22 @@ class ArrayType(PointerType): class StructType(CythonType): def __init__(self, **data): - for key, value in data.items(): + for key, value in data.iteritems(): setattr(self, key, value) def __setattr__(self, key, value): if key in self._members: self.__dict__[key] = cast(self._members[key], value) else: - raise AttributeError, "Struct has no member '%s'" % key + raise AttributeError("Struct has no member '%s'" % key) class UnionType(CythonType): def __init__(self, **data): if len(data) > 0: - raise AttributeError, "Union can only store one field at a time." - for key, value in data.items(): + raise AttributeError("Union can only store one field at a time.") + for key, value in data.iteritems(): setattr(self, key, value) def __setattr__(self, key, value): @@ -98,7 +98,7 @@ class UnionType(CythonType): elif key in self._members: self.__dict__ = {key: cast(self._members[key], value)} else: - raise AttributeError, "Union has no member '%s'" % key + raise AttributeError("Union has no member '%s'" % key) def pointer(basetype): class PointerInstance(PointerType): @@ -114,14 +114,14 @@ def array(basetype, n): def struct(**members): class StructInstance(StructType): _members = members - for key in members.keys(): + for key in members: setattr(StructInstance, key, None) return StructInstance def union(**members): class UnionInstance(UnionType): _members = members - for key in members.keys(): + for key in members: setattr(UnionInstance, key, None) return UnionInstance