Commit 38abf1b6 authored by Stefan Behnel's avatar Stefan Behnel

make dict iteration compatible with Py2/Py3

parent caa2fe39
...@@ -878,7 +878,7 @@ def cythonize(module_list, exclude=[], nthreads=0, aliases=None, quiet=False, fo ...@@ -878,7 +878,7 @@ def cythonize(module_list, exclude=[], nthreads=0, aliases=None, quiet=False, fo
if exclude_failures: if exclude_failures:
failed_modules = set() failed_modules = set()
for c_file, modules in modules_by_cfile.iteritems(): for c_file, modules in modules_by_cfile.items():
if not os.path.exists(c_file): if not os.path.exists(c_file):
failed_modules.update(modules) failed_modules.update(modules)
elif os.path.getsize(c_file) < 200: elif os.path.getsize(c_file) < 200:
......
...@@ -149,8 +149,7 @@ def cython_inline(code, ...@@ -149,8 +149,7 @@ def cython_inline(code,
if arg is cython_module: if arg is cython_module:
cimports.append('\ncimport cython as %s' % name) cimports.append('\ncimport cython as %s' % name)
del kwds[name] del kwds[name]
arg_names = kwds.keys() arg_names = sorted(kwds)
arg_names.sort()
arg_sigs = tuple([(get_type(kwds[arg], ctx), arg) for arg in arg_names]) arg_sigs = tuple([(get_type(kwds[arg], ctx), arg) for arg in arg_names])
key = orig_code, arg_sigs, sys.version_info, sys.executable, Cython.__version__ key = orig_code, arg_sigs, sys.version_info, sys.executable, Cython.__version__
module_name = "_cython_inline_" + hashlib.md5(str(key).encode('utf-8')).hexdigest() module_name = "_cython_inline_" + hashlib.md5(str(key).encode('utf-8')).hexdigest()
...@@ -282,7 +281,7 @@ except ImportError: ...@@ -282,7 +281,7 @@ except ImportError:
if kwds is not None: if kwds is not None:
all[kwds] = kwd_values all[kwds] = kwd_values
elif kwd_values: elif kwd_values:
raise TypeError("Unexpected keyword arguments: %s" % kwd_values.keys()) raise TypeError("Unexpected keyword arguments: %s" % list(kwd_values))
if defaults is None: if defaults is None:
defaults = () defaults = ()
first_default = len(args) - len(defaults) first_default = len(args) - len(defaults)
......
...@@ -49,25 +49,22 @@ class IntroduceBufferAuxiliaryVars(CythonTransform): ...@@ -49,25 +49,22 @@ class IntroduceBufferAuxiliaryVars(CythonTransform):
# For all buffers, insert extra variables in the scope. # For all buffers, insert extra variables in the scope.
# The variables are also accessible from the buffer_info # The variables are also accessible from the buffer_info
# on the buffer entry # on the buffer entry
bufvars = [entry for name, entry scope_items = scope.entries.items()
in scope.entries.iteritems() bufvars = [entry for name, entry in scope_items if entry.type.is_buffer]
if entry.type.is_buffer]
if len(bufvars) > 0: if len(bufvars) > 0:
bufvars.sort(key=lambda entry: entry.name) bufvars.sort(key=lambda entry: entry.name)
self.buffers_exists = True self.buffers_exists = True
memviewslicevars = [entry for name, entry memviewslicevars = [entry for name, entry in scope_items if entry.type.is_memoryviewslice]
in scope.entries.iteritems()
if entry.type.is_memoryviewslice]
if len(memviewslicevars) > 0: if len(memviewslicevars) > 0:
self.buffers_exists = True self.buffers_exists = True
for (name, entry) in scope.entries.iteritems(): for (name, entry) in scope_items:
if name == 'memoryview' and isinstance(entry.utility_code_definition, CythonUtilityCode): if name == 'memoryview' and isinstance(entry.utility_code_definition, CythonUtilityCode):
self.using_memoryview = True self.using_memoryview = True
break break
del scope_items
if isinstance(node, ModuleNode) and len(bufvars) > 0: if isinstance(node, ModuleNode) and len(bufvars) > 0:
# for now...note that pos is wrong # for now...note that pos is wrong
...@@ -143,13 +140,14 @@ def analyse_buffer_options(globalpos, env, posargs, dictargs, defaults=None, nee ...@@ -143,13 +140,14 @@ def analyse_buffer_options(globalpos, env, posargs, dictargs, defaults=None, nee
if defaults is None: if defaults is None:
defaults = buffer_defaults defaults = buffer_defaults
posargs, dictargs = Interpreter.interpret_compiletime_options(posargs, dictargs, type_env=env, type_args = (0,'dtype')) posargs, dictargs = Interpreter.interpret_compiletime_options(
posargs, dictargs, type_env=env, type_args=(0, 'dtype'))
if len(posargs) > buffer_positional_options_count: if len(posargs) > buffer_positional_options_count:
raise CompileError(posargs[-1][1], ERR_BUF_TOO_MANY) raise CompileError(posargs[-1][1], ERR_BUF_TOO_MANY)
options = {} options = {}
for name, (value, pos) in dictargs.iteritems(): for name, (value, pos) in dictargs.items():
if not name in buffer_options: if not name in buffer_options:
raise CompileError(pos, ERR_BUF_OPTION_UNKNOWN % name) raise CompileError(pos, ERR_BUF_OPTION_UNKNOWN % name)
options[name] = value options[name] = value
......
...@@ -774,8 +774,7 @@ class FunctionState(object): ...@@ -774,8 +774,7 @@ class FunctionState(object):
error case. error case.
""" """
return [(cname, type) return [(cname, type)
for (type, manage_ref), freelist in self.temps_free.items() for (type, manage_ref), freelist in self.temps_free.items() if manage_ref
if manage_ref
for cname in freelist] for cname in freelist]
def start_collecting_temps(self): def start_collecting_temps(self):
...@@ -1303,8 +1302,7 @@ class GlobalState(object): ...@@ -1303,8 +1302,7 @@ class GlobalState(object):
cleanup.putln("Py_CLEAR(%s.method);" % cname) cleanup.putln("Py_CLEAR(%s.method);" % cname)
def generate_string_constants(self): def generate_string_constants(self):
c_consts = [ (len(c.cname), c.cname, c) c_consts = [(len(c.cname), c.cname, c) for c in self.string_const_index.values()]
for c in self.string_const_index.values() ]
c_consts.sort() c_consts.sort()
py_strings = [] py_strings = []
......
...@@ -259,7 +259,7 @@ class ControlFlow(object): ...@@ -259,7 +259,7 @@ class ControlFlow(object):
for entry in block.bounded: for entry in block.bounded:
block.i_kill |= self.assmts[entry].bit block.i_kill |= self.assmts[entry].bit
for assmts in self.assmts.itervalues(): for assmts in self.assmts.values():
self.entry_point.i_gen |= assmts.bit self.entry_point.i_gen |= assmts.bit
self.entry_point.i_output = self.entry_point.i_gen self.entry_point.i_output = self.entry_point.i_gen
...@@ -602,7 +602,7 @@ def check_definitions(flow, compiler_directives): ...@@ -602,7 +602,7 @@ def check_definitions(flow, compiler_directives):
node.cf_maybe_null = False node.cf_maybe_null = False
# Find uninitialized references and cf-hints # Find uninitialized references and cf-hints
for node, entry in references.iteritems(): for node, entry in references.items():
if Uninitialized in node.cf_state: if Uninitialized in node.cf_state:
node.cf_maybe_null = True node.cf_maybe_null = True
if not entry.from_closure and len(node.cf_state) == 1: if not entry.from_closure and len(node.cf_state) == 1:
......
...@@ -413,7 +413,7 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): ...@@ -413,7 +413,7 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
d.setdefault(cython_lineno, []).append(c_lineno + 1) d.setdefault(cython_lineno, []).append(c_lineno + 1)
tb.start('LineNumberMapping') tb.start('LineNumberMapping')
for cython_lineno, c_linenos in sorted(d.iteritems()): for cython_lineno, c_linenos in sorted(d.items()):
attrs = { attrs = {
'c_linenos': ' '.join(map(str, c_linenos)), 'c_linenos': ' '.join(map(str, c_linenos)),
'cython_lineno': str(cython_lineno), 'cython_lineno': str(cython_lineno),
......
...@@ -320,7 +320,6 @@ class Node(object): ...@@ -320,7 +320,6 @@ class Node(object):
else: else:
return repr(x) return repr(x)
attrs = [(key, value) for key, value in self.__dict__.items() 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))
...@@ -1945,8 +1944,7 @@ class FuncDefNode(StatNode, BlockNode): ...@@ -1945,8 +1944,7 @@ class FuncDefNode(StatNode, BlockNode):
# Clean up buffers -- this calls a Python function # Clean up buffers -- this calls a Python function
# so need to save and restore error state # so need to save and restore error state
buffers_present = len(lenv.buffer_entries) > 0 buffers_present = len(lenv.buffer_entries) > 0
memslice_entries = [e for e in lenv.entries.itervalues() #memslice_entries = [e for e in lenv.entries.values() if e.type.is_memoryviewslice]
if e.type.is_memoryviewslice]
if buffers_present: if buffers_present:
code.globalstate.use_utility_code(restore_exception_utility_code) code.globalstate.use_utility_code(restore_exception_utility_code)
code.putln("{ PyObject *__pyx_type, *__pyx_value, *__pyx_tb;") code.putln("{ PyObject *__pyx_type, *__pyx_value, *__pyx_tb;")
...@@ -7405,7 +7403,7 @@ class FromCImportStatNode(StatNode): ...@@ -7405,7 +7403,7 @@ class FromCImportStatNode(StatNode):
env.add_imported_module(module_scope) env.add_imported_module(module_scope)
for pos, name, as_name, kind in self.imported_names: for pos, name, as_name, kind in self.imported_names:
if name == "*": if name == "*":
for local_name, entry in module_scope.entries.items(): for local_name, entry in list(module_scope.entries.items()):
env.add_imported_entry(local_name, entry, pos) env.add_imported_entry(local_name, entry, pos)
else: else:
entry = module_scope.lookup(name) entry = module_scope.lookup(name)
...@@ -7668,7 +7666,7 @@ class ParallelStatNode(StatNode, ParallelNode): ...@@ -7668,7 +7666,7 @@ class ParallelStatNode(StatNode, ParallelNode):
else: else:
self.kwargs = {} self.kwargs = {}
for kw, val in self.kwargs.iteritems(): for kw, val in self.kwargs.items():
if kw not in self.valid_keyword_arguments: if kw not in self.valid_keyword_arguments:
error(self.pos, "Invalid keyword argument: %s" % kw) error(self.pos, "Invalid keyword argument: %s" % kw)
else: else:
...@@ -7709,7 +7707,7 @@ class ParallelStatNode(StatNode, ParallelNode): ...@@ -7709,7 +7707,7 @@ class ParallelStatNode(StatNode, ParallelNode):
This should be called in a post-order fashion during the This should be called in a post-order fashion during the
analyse_expressions phase analyse_expressions phase
""" """
for entry, (pos, op) in self.assignments.iteritems(): for entry, (pos, op) in self.assignments.items():
if self.is_prange and not self.is_parallel: if self.is_prange and not self.is_parallel:
# closely nested prange in a with parallel block, disallow # closely nested prange in a with parallel block, disallow
...@@ -7826,7 +7824,7 @@ class ParallelStatNode(StatNode, ParallelNode): ...@@ -7826,7 +7824,7 @@ class ParallelStatNode(StatNode, ParallelNode):
def initialize_privates_to_nan(self, code, exclude=None): def initialize_privates_to_nan(self, code, exclude=None):
first = True first = True
for entry, (op, lastprivate) in self.privates.iteritems(): for entry, (op, lastprivate) in self.privates.items():
if not op and (not exclude or entry != exclude): if not op and (not exclude or entry != exclude):
invalid_value = entry.type.invalid_value() invalid_value = entry.type.invalid_value()
...@@ -8088,7 +8086,7 @@ class ParallelStatNode(StatNode, ParallelNode): ...@@ -8088,7 +8086,7 @@ class ParallelStatNode(StatNode, ParallelNode):
c = self.begin_of_parallel_control_block_point c = self.begin_of_parallel_control_block_point
temp_count = 0 temp_count = 0
for entry, (op, lastprivate) in self.privates.iteritems(): for entry, (op, lastprivate) in self.privates.items():
if not lastprivate or entry.type.is_pyobject: if not lastprivate or entry.type.is_pyobject:
continue continue
...@@ -8617,7 +8615,7 @@ class ParallelRangeNode(ParallelStatNode): ...@@ -8617,7 +8615,7 @@ class ParallelRangeNode(ParallelStatNode):
code.putln("#ifdef _OPENMP") code.putln("#ifdef _OPENMP")
code.put("#pragma omp for") code.put("#pragma omp for")
for entry, (op, lastprivate) in self.privates.iteritems(): for entry, (op, lastprivate) in self.privates.items():
# Don't declare the index variable as a reduction # Don't declare the index variable as a reduction
if op and op in "+*-&^|" and entry != self.target.entry: if op and op in "+*-&^|" and entry != self.target.entry:
if entry.type.is_pyobject: if entry.type.is_pyobject:
...@@ -8737,7 +8735,7 @@ class CnameDecoratorNode(StatNode): ...@@ -8737,7 +8735,7 @@ class CnameDecoratorNode(StatNode):
scope.scope_prefix = self.cname + "_" scope.scope_prefix = self.cname + "_"
for name, entry in scope.entries.iteritems(): for name, entry in scope.entries.items():
if entry.func_cname: if entry.func_cname:
entry.func_cname = self.mangle(entry.cname) entry.func_cname = self.mangle(entry.cname)
if entry.pyfunc_cname: if entry.pyfunc_cname:
......
...@@ -647,7 +647,7 @@ class InterpretCompilerDirectives(CythonTransform, SkipDeclarations): ...@@ -647,7 +647,7 @@ class InterpretCompilerDirectives(CythonTransform, SkipDeclarations):
special_methods = set(['declare', 'union', 'struct', 'typedef', special_methods = set(['declare', 'union', 'struct', 'typedef',
'sizeof', 'cast', 'pointer', 'compiled', 'sizeof', 'cast', 'pointer', 'compiled',
'NULL', 'fused_type', 'parallel']) 'NULL', 'fused_type', 'parallel'])
special_methods.update(unop_method_nodes.keys()) special_methods.update(unop_method_nodes)
valid_parallel_directives = set([ valid_parallel_directives = set([
"parallel", "parallel",
...@@ -934,7 +934,7 @@ class InterpretCompilerDirectives(CythonTransform, SkipDeclarations): ...@@ -934,7 +934,7 @@ class InterpretCompilerDirectives(CythonTransform, SkipDeclarations):
directives = self._extract_directives(node, 'function') directives = self._extract_directives(node, 'function')
if not directives: if not directives:
return node return node
for name, value in directives.iteritems(): for name, value in directives.items():
if name == 'locals': if name == 'locals':
node.directive_locals = value node.directive_locals = value
elif name not in ('final', 'staticmethod'): elif name not in ('final', 'staticmethod'):
...@@ -2888,7 +2888,7 @@ class DebugTransform(CythonTransform): ...@@ -2888,7 +2888,7 @@ class DebugTransform(CythonTransform):
self.tb.start('Globals') self.tb.start('Globals')
entries = {} entries = {}
for k, v in node.scope.entries.iteritems(): for k, v in node.scope.entries.items():
if (v.qualified_name not in self.visited and not if (v.qualified_name not in self.visited and not
v.name.startswith('__pyx_') and not v.name.startswith('__pyx_') and not
v.type.is_cfunction and not v.type.is_cfunction and not
......
...@@ -57,7 +57,7 @@ def generate_pyx_code_stage_factory(options, result): ...@@ -57,7 +57,7 @@ def generate_pyx_code_stage_factory(options, result):
def inject_pxd_code_stage_factory(context): def inject_pxd_code_stage_factory(context):
def inject_pxd_code_stage(module_node): def inject_pxd_code_stage(module_node):
for name, (statlistnode, scope) in context.pxds.iteritems(): for name, (statlistnode, scope) in context.pxds.items():
module_node.merge_in(statlistnode, scope) module_node.merge_in(statlistnode, scope)
return module_node return module_node
return inject_pxd_code_stage return inject_pxd_code_stage
...@@ -66,7 +66,7 @@ def use_utility_code_definitions(scope, target, seen=None): ...@@ -66,7 +66,7 @@ def use_utility_code_definitions(scope, target, seen=None):
if seen is None: if seen is None:
seen = set() seen = set()
for entry in scope.entries.itervalues(): for entry in scope.entries.values():
if entry in seen: if entry in seen:
continue continue
......
...@@ -4123,7 +4123,7 @@ def merge_template_deductions(a, b): ...@@ -4123,7 +4123,7 @@ def merge_template_deductions(a, b):
if a is None or b is None: if a is None or b is None:
return None return None
all = a all = a
for param, value in b.iteritems(): for param, value in b.items():
if param in all: if param in all:
if a[param] != b[param]: if a[param] != b[param]:
return None return None
......
...@@ -344,7 +344,7 @@ class Scope(object): ...@@ -344,7 +344,7 @@ class Scope(object):
def merge_in(self, other, merge_unused=True, whitelist=None): def merge_in(self, other, merge_unused=True, whitelist=None):
# Use with care... # Use with care...
entries = [] entries = []
for name, entry in other.entries.iteritems(): for name, entry in other.entries.items():
if not whitelist or name in whitelist: if not whitelist or name in whitelist:
if entry.used or merge_unused: if entry.used or merge_unused:
entries.append((name, entry)) entries.append((name, entry))
...@@ -899,7 +899,7 @@ class BuiltinScope(Scope): ...@@ -899,7 +899,7 @@ class BuiltinScope(Scope):
Scope.__init__(self, "__builtin__", PreImportScope(), None) Scope.__init__(self, "__builtin__", PreImportScope(), None)
self.type_names = {} self.type_names = {}
for name, definition in self.builtin_entries.iteritems(): for name, definition in sorted(self.builtin_entries.items()):
cname, type = definition cname, type = definition
self.declare_var(name, type, None, cname) self.declare_var(name, type, None, cname)
......
...@@ -219,7 +219,7 @@ class TreeFragment(object): ...@@ -219,7 +219,7 @@ class TreeFragment(object):
fmt_code = fmt(code) fmt_code = fmt(code)
fmt_pxds = {} fmt_pxds = {}
for key, value in pxds.iteritems(): for key, value in pxds.items():
fmt_pxds[key] = fmt(value) fmt_pxds[key] = fmt(value)
mod = t = parse_from_strings(name, fmt_code, fmt_pxds, level=level, initial_pos=initial_pos) mod = t = parse_from_strings(name, fmt_code, fmt_pxds, level=level, initial_pos=initial_pos)
if level is None: if level is None:
......
...@@ -71,7 +71,7 @@ class Signature(object): ...@@ -71,7 +71,7 @@ class Signature(object):
} }
type_to_format_map = dict( type_to_format_map = dict(
(type_, format_) for format_, type_ in format_map.iteritems()) (type_, format_) for format_, type_ in format_map.items())
error_value_map = { error_value_map = {
'O': "NULL", 'O': "NULL",
......
...@@ -186,7 +186,7 @@ class CythonUtilityCode(Code.UtilityCodeBase): ...@@ -186,7 +186,7 @@ class CythonUtilityCode(Code.UtilityCodeBase):
entries.pop('__builtins__') entries.pop('__builtins__')
entries.pop('__doc__') entries.pop('__doc__')
for name, entry in entries.iteritems(): for name, entry in entries.items():
entry.utility_code_definition = self entry.utility_code_definition = self
entry.used = used entry.used = used
......
...@@ -249,7 +249,7 @@ class VisitorTransform(TreeVisitor): ...@@ -249,7 +249,7 @@ class VisitorTransform(TreeVisitor):
""" """
def visitchildren(self, parent, attrs=None): def visitchildren(self, parent, attrs=None):
result = self._visitchildren(parent, attrs) result = self._visitchildren(parent, attrs)
for attr, newnode in result.iteritems(): for attr, newnode in result.items():
if type(newnode) is not list: if type(newnode) is not list:
setattr(parent, attr, newnode) setattr(parent, attr, newnode)
else: else:
......
...@@ -45,7 +45,7 @@ def print_on_call_decorator(func): ...@@ -45,7 +45,7 @@ def print_on_call_decorator(func):
class TraceMethodCallMeta(type): class TraceMethodCallMeta(type):
def __init__(self, name, bases, dict): def __init__(self, name, bases, dict):
for func_name, func in dict.iteritems(): for func_name, func in dict.items():
if inspect.isfunction(func): if inspect.isfunction(func):
setattr(self, func_name, print_on_call_decorator(func)) setattr(self, func_name, print_on_call_decorator(func))
......
...@@ -381,7 +381,7 @@ class CythonBase(object): ...@@ -381,7 +381,7 @@ class CythonBase(object):
result = {} result = {}
seen = set() seen = set()
for k, v in pyobject_dict.iteritems(): for k, v in pyobject_dict.items():
result[k.proxyval(seen)] = v result[k.proxyval(seen)] = v
return result return result
...@@ -849,9 +849,9 @@ class CyBreak(CythonCommand): ...@@ -849,9 +849,9 @@ class CyBreak(CythonCommand):
def complete(self, text, word): def complete(self, text, word):
# Filter init-module functions (breakpoints can be set using # Filter init-module functions (breakpoints can be set using
# modulename:linenumber). # modulename:linenumber).
names = [n for n, L in self.cy.functions_by_name.iteritems() names = [n for n, L in self.cy.functions_by_name.items()
if any(not f.is_initmodule_function for f in L)] if any(not f.is_initmodule_function for f in L)]
qnames = [n for n, f in self.cy.functions_by_qualified_name.iteritems() qnames = [n for n, f in self.cy.functions_by_qualified_name.items()
if not f.is_initmodule_function] if not f.is_initmodule_function]
if parameters.complete_unqualified: if parameters.complete_unqualified:
...@@ -1142,7 +1142,7 @@ class CyLocals(CythonCommand): ...@@ -1142,7 +1142,7 @@ class CyLocals(CythonCommand):
local_cython_vars = cython_function.locals local_cython_vars = cython_function.locals
max_name_length = len(max(local_cython_vars, key=len)) max_name_length = len(max(local_cython_vars, key=len))
for name, cyvar in sorted(local_cython_vars.iteritems(), key=sortkey): for name, cyvar in sorted(local_cython_vars.items(), key=sortkey):
if self.is_initialized(self.get_cython_function(), cyvar.name): if self.is_initialized(self.get_cython_function(), cyvar.name):
value = gdb.parse_and_eval(cyvar.cname) value = gdb.parse_and_eval(cyvar.cname)
if not value.is_optimized_out: if not value.is_optimized_out:
...@@ -1175,13 +1175,13 @@ class CyGlobals(CyLocals): ...@@ -1175,13 +1175,13 @@ class CyGlobals(CyLocals):
seen = set() seen = set()
print('Python globals:') print('Python globals:')
for k, v in sorted(global_python_dict.iteritems(), key=sortkey): for k, v in sorted(global_python_dict.items(), key=sortkey):
v = v.get_truncated_repr(libpython.MAX_OUTPUT_LEN) v = v.get_truncated_repr(libpython.MAX_OUTPUT_LEN)
seen.add(k) seen.add(k)
print(' %-*s = %s' % (max_name_length, k, v)) print(' %-*s = %s' % (max_name_length, k, v))
print('C globals:') print('C globals:')
for name, cyvar in sorted(module_globals.iteritems(), key=sortkey): for name, cyvar in sorted(module_globals.items(), key=sortkey):
if name not in seen: if name not in seen:
try: try:
value = gdb.parse_and_eval(cyvar.cname) value = gdb.parse_and_eval(cyvar.cname)
...@@ -1204,10 +1204,8 @@ class EvaluateOrExecuteCodeMixin(object): ...@@ -1204,10 +1204,8 @@ class EvaluateOrExecuteCodeMixin(object):
"Fill a remotely allocated dict with values from the Cython C stack" "Fill a remotely allocated dict with values from the Cython C stack"
cython_func = self.get_cython_function() cython_func = self.get_cython_function()
for name, cyvar in cython_func.locals.iteritems(): for name, cyvar in cython_func.locals.items():
if (cyvar.type == PythonObject and if cyvar.type == PythonObject and self.is_initialized(cython_func, name):
self.is_initialized(cython_func, name)):
try: try:
val = gdb.parse_and_eval(cyvar.cname) val = gdb.parse_and_eval(cyvar.cname)
except RuntimeError: except RuntimeError:
......
...@@ -463,7 +463,7 @@ def _write_instance_repr(out, visited, name, pyop_attrdict, address): ...@@ -463,7 +463,7 @@ def _write_instance_repr(out, visited, name, pyop_attrdict, address):
if isinstance(pyop_attrdict, PyDictObjectPtr): if isinstance(pyop_attrdict, PyDictObjectPtr):
out.write('(') out.write('(')
first = True first = True
for pyop_arg, pyop_val in pyop_attrdict.iteritems(): for pyop_arg, pyop_val in pyop_attrdict.items():
if not first: if not first:
out.write(', ') out.write(', ')
first = False first = False
...@@ -483,8 +483,7 @@ class InstanceProxy(object): ...@@ -483,8 +483,7 @@ class InstanceProxy(object):
def __repr__(self): def __repr__(self):
if isinstance(self.attrdict, dict): if isinstance(self.attrdict, dict):
kwargs = ', '.join("%s=%r" % (arg, val) kwargs = ', '.join("%s=%r" % (arg, val) for arg, val in self.attrdict.items())
for arg, val in self.attrdict.iteritems())
return '<%s(%s) at remote 0x%x>' % ( return '<%s(%s) at remote 0x%x>' % (
self.cl_name, kwargs, self.address) self.cl_name, kwargs, self.address)
else: else:
...@@ -693,7 +692,7 @@ class PyDictObjectPtr(PyObjectPtr): ...@@ -693,7 +692,7 @@ class PyDictObjectPtr(PyObjectPtr):
def iteritems(self): def iteritems(self):
''' '''
Yields a sequence of (PyObjectPtr key, PyObjectPtr value) pairs, Yields a sequence of (PyObjectPtr key, PyObjectPtr value) pairs,
analagous to dict.iteritems() analagous to dict.items()
''' '''
for i in safe_range(self.field('ma_mask') + 1): for i in safe_range(self.field('ma_mask') + 1):
ep = self.field('ma_table') + i ep = self.field('ma_table') + i
...@@ -702,6 +701,8 @@ class PyDictObjectPtr(PyObjectPtr): ...@@ -702,6 +701,8 @@ class PyDictObjectPtr(PyObjectPtr):
pyop_key = PyObjectPtr.from_pyobject_ptr(ep['me_key']) pyop_key = PyObjectPtr.from_pyobject_ptr(ep['me_key'])
yield (pyop_key, pyop_value) yield (pyop_key, pyop_value)
items = iteritems
def proxyval(self, visited): def proxyval(self, visited):
# Guard against infinite loops: # Guard against infinite loops:
if self.as_address() in visited: if self.as_address() in visited:
...@@ -709,7 +710,7 @@ class PyDictObjectPtr(PyObjectPtr): ...@@ -709,7 +710,7 @@ class PyDictObjectPtr(PyObjectPtr):
visited.add(self.as_address()) visited.add(self.as_address())
result = {} result = {}
for pyop_key, pyop_value in self.iteritems(): for pyop_key, pyop_value in self.items():
proxy_key = pyop_key.proxyval(visited) proxy_key = pyop_key.proxyval(visited)
proxy_value = pyop_value.proxyval(visited) proxy_value = pyop_value.proxyval(visited)
result[proxy_key] = proxy_value result[proxy_key] = proxy_value
...@@ -724,7 +725,7 @@ class PyDictObjectPtr(PyObjectPtr): ...@@ -724,7 +725,7 @@ class PyDictObjectPtr(PyObjectPtr):
out.write('{') out.write('{')
first = True first = True
for pyop_key, pyop_value in self.iteritems(): for pyop_key, pyop_value in self.items():
if not first: if not first:
out.write(', ') out.write(', ')
first = False first = False
...@@ -924,7 +925,7 @@ class PyFrameObjectPtr(PyObjectPtr): ...@@ -924,7 +925,7 @@ class PyFrameObjectPtr(PyObjectPtr):
return return
pyop_globals = self.pyop_field('f_globals') pyop_globals = self.pyop_field('f_globals')
return pyop_globals.iteritems() return iter(pyop_globals.items())
def iter_builtins(self): def iter_builtins(self):
''' '''
...@@ -935,7 +936,7 @@ class PyFrameObjectPtr(PyObjectPtr): ...@@ -935,7 +936,7 @@ class PyFrameObjectPtr(PyObjectPtr):
return return
pyop_builtins = self.pyop_field('f_builtins') pyop_builtins = self.pyop_field('f_builtins')
return pyop_builtins.iteritems() return iter(pyop_builtins.items())
def get_var_by_name(self, name): def get_var_by_name(self, name):
''' '''
......
...@@ -45,7 +45,7 @@ class Extension(_Extension.Extension): ...@@ -45,7 +45,7 @@ class Extension(_Extension.Extension):
# Translate pyrex_X to cython_X for backwards compatibility. # Translate pyrex_X to cython_X for backwards compatibility.
had_pyrex_options = False had_pyrex_options = False
for key in kw.keys(): for key in list(kw):
if key.startswith('pyrex_'): if key.startswith('pyrex_'):
had_pyrex_options = True had_pyrex_options = True
kw['cython' + key[5:]] = kw.pop(key) kw['cython' + key[5:]] = kw.pop(key)
......
...@@ -32,7 +32,7 @@ def nfa_to_dfa(old_machine, debug=None): ...@@ -32,7 +32,7 @@ def nfa_to_dfa(old_machine, debug=None):
# Seed the process using the initial states of the old machine. # Seed the process using the initial states of the old machine.
# Make the corresponding new states into initial states of the new # Make the corresponding new states into initial states of the new
# machine with the same names. # machine with the same names.
for (key, old_state) in old_machine.initial_states.iteritems(): for (key, old_state) in old_machine.initial_states.items():
new_state = state_map.old_to_new(epsilon_closure(old_state)) new_state = state_map.old_to_new(epsilon_closure(old_state))
new_machine.make_initial_state(key, new_state) new_machine.make_initial_state(key, new_state)
# Tricky bit here: we add things to the end of this list while we're # Tricky bit here: we add things to the end of this list while we're
...@@ -40,10 +40,10 @@ def nfa_to_dfa(old_machine, debug=None): ...@@ -40,10 +40,10 @@ def nfa_to_dfa(old_machine, debug=None):
for new_state in new_machine.states: for new_state in new_machine.states:
transitions = TransitionMap() transitions = TransitionMap()
for old_state in state_map.new_to_old(new_state): for old_state in state_map.new_to_old(new_state):
for event, old_target_states in old_state.transitions.iteritems(): for event, old_target_states in old_state.transitions.items():
if event and old_target_states: if event and old_target_states:
transitions.add_set(event, set_epsilon_closure(old_target_states)) transitions.add_set(event, set_epsilon_closure(old_target_states))
for event, old_states in transitions.iteritems(): for event, old_states in transitions.items():
new_machine.add_transitions(new_state, event, state_map.old_to_new(old_states)) new_machine.add_transitions(new_state, event, state_map.old_to_new(old_states))
if debug: if debug:
debug.write("\n===== State Mapping =====\n") debug.write("\n===== State Mapping =====\n")
......
...@@ -59,7 +59,7 @@ class Machine(object): ...@@ -59,7 +59,7 @@ class Machine(object):
file.write("Plex.Machine:\n") file.write("Plex.Machine:\n")
if self.initial_states is not None: if self.initial_states is not None:
file.write(" Initial states:\n") file.write(" Initial states:\n")
for (name, state) in self.initial_states.iteritems(): for (name, state) in sorted(self.initial_states.items()):
file.write(" '%s': %d\n" % (name, state.number)) file.write(" '%s': %d\n" % (name, state.number))
for s in self.states: for s in self.states:
s.dump(file) s.dump(file)
...@@ -150,11 +150,11 @@ class FastMachine(object): ...@@ -150,11 +150,11 @@ class FastMachine(object):
for old_state in old_machine.states: for old_state in old_machine.states:
new_state = self.new_state() new_state = self.new_state()
old_to_new[old_state] = new_state old_to_new[old_state] = new_state
for name, old_state in old_machine.initial_states.iteritems(): for name, old_state in old_machine.initial_states.items():
initial_states[name] = old_to_new[old_state] initial_states[name] = old_to_new[old_state]
for old_state in old_machine.states: for old_state in old_machine.states:
new_state = old_to_new[old_state] new_state = old_to_new[old_state]
for event, old_state_set in old_state.transitions.iteritems(): for event, old_state_set in old_state.transitions.items():
if old_state_set: if old_state_set:
new_state[event] = old_to_new[old_state_set.keys()[0]] new_state[event] = old_to_new[old_state_set.keys()[0]]
else: else:
...@@ -195,7 +195,7 @@ class FastMachine(object): ...@@ -195,7 +195,7 @@ class FastMachine(object):
def dump(self, file): def dump(self, file):
file.write("Plex.FastMachine:\n") file.write("Plex.FastMachine:\n")
file.write(" Initial states:\n") file.write(" Initial states:\n")
for name, state in self.initial_states.iteritems(): for name, state in sorted(self.initial_states.items()):
file.write(" %s: %s\n" % (repr(name), state['number'])) file.write(" %s: %s\n" % (repr(name), state['number']))
for state in self.states: for state in self.states:
self.dump_state(state, file) self.dump_state(state, file)
...@@ -213,7 +213,7 @@ class FastMachine(object): ...@@ -213,7 +213,7 @@ class FastMachine(object):
def dump_transitions(self, state, file): def dump_transitions(self, state, file):
chars_leading_to_state = {} chars_leading_to_state = {}
special_to_state = {} special_to_state = {}
for (c, s) in state.iteritems(): for (c, s) in state.items():
if len(c) == 1: if len(c) == 1:
chars = chars_leading_to_state.get(id(s), None) chars = chars_leading_to_state.get(id(s), None)
if chars is None: if chars is None:
......
...@@ -110,7 +110,7 @@ class TransitionMap(object): ...@@ -110,7 +110,7 @@ class TransitionMap(object):
result.append(((code0, code1), set)) result.append(((code0, code1), set))
code0 = code1 code0 = code1
i += 2 i += 2
for event, set in self.special.iteritems(): for event, set in self.special.items():
if set: if set:
result.append((event, set)) result.append((event, set))
return iter(result) return iter(result)
...@@ -182,7 +182,7 @@ class TransitionMap(object): ...@@ -182,7 +182,7 @@ class TransitionMap(object):
map_strs.append(state_set_str(map[i])) map_strs.append(state_set_str(map[i]))
i += 1 i += 1
special_strs = {} special_strs = {}
for event, set in self.special.iteritems(): for event, set in self.special.items():
special_strs[event] = state_set_str(set) special_strs[event] = state_set_str(set)
return "[%s]+%s" % ( return "[%s]+%s" % (
','.join(map_strs), ','.join(map_strs),
...@@ -204,7 +204,7 @@ class TransitionMap(object): ...@@ -204,7 +204,7 @@ class TransitionMap(object):
while i < n: while i < n:
self.dump_range(map[i], map[i + 2], map[i + 1], file) self.dump_range(map[i], map[i + 2], map[i + 1], file)
i += 2 i += 2
for event, set in self.special.iteritems(): for event, set in self.special.items():
if set: if set:
if not event: if not event:
event = 'empty' event = 'empty'
......
...@@ -202,7 +202,7 @@ class Template(object): ...@@ -202,7 +202,7 @@ class Template(object):
position=None, name=self.name) position=None, name=self.name)
templ = self.get_template(inherit_template, self) templ = self.get_template(inherit_template, self)
self_ = TemplateObject(self.name) self_ = TemplateObject(self.name)
for name, value in defs.iteritems(): for name, value in defs.items():
setattr(self_, name, value) setattr(self_, name, value)
self_.body = body self_.body = body
ns = ns.copy() ns = ns.copy()
...@@ -391,7 +391,7 @@ def paste_script_template_renderer(content, vars, filename=None): ...@@ -391,7 +391,7 @@ def paste_script_template_renderer(content, vars, filename=None):
class bunch(dict): class bunch(dict):
def __init__(self, **kw): def __init__(self, **kw):
for name, value in kw.iteritems(): for name, value in kw.items():
setattr(self, name, value) setattr(self, name, value)
def __setattr__(self, name, value): def __setattr__(self, name, value):
...@@ -413,12 +413,9 @@ class bunch(dict): ...@@ -413,12 +413,9 @@ class bunch(dict):
return dict.__getitem__(self, key) return dict.__getitem__(self, key)
def __repr__(self): def __repr__(self):
items = [
(k, v) for k, v in self.iteritems()]
items.sort()
return '<%s %s>' % ( return '<%s %s>' % (
self.__class__.__name__, self.__class__.__name__,
' '.join(['%s=%r' % (k, v) for k, v in items])) ' '.join(['%s=%r' % (k, v) for k, v in sorted(self.items())]))
############################################################ ############################################################
## HTML Templating ## HTML Templating
...@@ -467,10 +464,8 @@ def url(v): ...@@ -467,10 +464,8 @@ def url(v):
def attr(**kw): def attr(**kw):
kw = list(kw.iteritems())
kw.sort()
parts = [] parts = []
for name, value in kw: for name, value in sorted(kw.items()):
if value is None: if value is None:
continue continue
if name.endswith('_'): if name.endswith('_'):
...@@ -549,7 +544,7 @@ class TemplateDef(object): ...@@ -549,7 +544,7 @@ class TemplateDef(object):
values = {} values = {}
sig_args, var_args, var_kw, defaults = self._func_signature sig_args, var_args, var_kw, defaults = self._func_signature
extra_kw = {} extra_kw = {}
for name, value in kw.iteritems(): for name, value in kw.items():
if not var_kw and name not in sig_args: if not var_kw and name not in sig_args:
raise TypeError( raise TypeError(
'Unexpected argument %s' % name) 'Unexpected argument %s' % name)
...@@ -572,7 +567,7 @@ class TemplateDef(object): ...@@ -572,7 +567,7 @@ class TemplateDef(object):
raise TypeError( raise TypeError(
'Extra position arguments: %s' 'Extra position arguments: %s'
% ', '.join([repr(v) for v in args])) % ', '.join([repr(v) for v in args]))
for name, value_expr in defaults.iteritems(): for name, value_expr in defaults.items():
if name not in values: if name not in values:
values[name] = self._template._eval( values[name] = self._template._eval(
value_expr, self._ns, self._pos) value_expr, self._ns, self._pos)
......
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