Commit 8fce6199 authored by Stefan Behnel's avatar Stefan Behnel

fix some 'python2.6 -3' warnings

parent be696078
...@@ -13,7 +13,13 @@ import bisect, sys ...@@ -13,7 +13,13 @@ import bisect, sys
# redesigned. It doesn't take return, raise, continue, or break into # redesigned. It doesn't take return, raise, continue, or break into
# account. # account.
_END_POS = ((unichr(sys.maxunicode)*10),()) from Cython.Compiler.Scanning import StringSourceDescriptor
try:
_END_POS = (StringSourceDescriptor(unichr(sys.maxunicode)*10, ''),
sys.maxint, sys.maxint)
except AttributeError: # Py3
_END_POS = (StringSourceDescriptor(unichr(sys.maxunicode)*10, ''),
sys.maxsize, sys.maxsize)
class ControlFlow(object): class ControlFlow(object):
......
...@@ -13,6 +13,11 @@ from StringEncoding import EncodedString ...@@ -13,6 +13,11 @@ from StringEncoding import EncodedString
from Errors import error from Errors import error
from ParseTreeTransforms import SkipDeclarations from ParseTreeTransforms import SkipDeclarations
try:
reduce
except NameError:
from functools import reduce
#def unwrap_node(node): #def unwrap_node(node):
# while isinstance(node, ExprNodes.PersistentNode): # while isinstance(node, ExprNodes.PersistentNode):
# node = node.arg # node = node.arg
......
...@@ -232,6 +232,7 @@ class SourceDescriptor(object): ...@@ -232,6 +232,7 @@ class SourceDescriptor(object):
A SourceDescriptor should be considered immutable. A SourceDescriptor should be considered immutable.
""" """
_escaped_description = None _escaped_description = None
_cmp_name = ''
def __str__(self): def __str__(self):
assert False # To catch all places where a descriptor is used directly as a filename assert False # To catch all places where a descriptor is used directly as a filename
...@@ -241,6 +242,27 @@ class SourceDescriptor(object): ...@@ -241,6 +242,27 @@ class SourceDescriptor(object):
self.get_description().encode('ASCII', 'replace').decode("ASCII") self.get_description().encode('ASCII', 'replace').decode("ASCII")
return self._escaped_description return self._escaped_description
def __gt__(self, other):
# this is only used to provide some sort of order
try:
return self._cmp_name > other._cmp_name
except AttributeError:
return False
def __lt__(self, other):
# this is only used to provide some sort of order
try:
return self._cmp_name < other._cmp_name
except AttributeError:
return False
def __le__(self, other):
# this is only used to provide some sort of order
try:
return self._cmp_name <= other._cmp_name
except AttributeError:
return False
class FileSourceDescriptor(SourceDescriptor): class FileSourceDescriptor(SourceDescriptor):
""" """
Represents a code source. A code source is a more generic abstraction Represents a code source. A code source is a more generic abstraction
...@@ -251,6 +273,7 @@ class FileSourceDescriptor(SourceDescriptor): ...@@ -251,6 +273,7 @@ class FileSourceDescriptor(SourceDescriptor):
""" """
def __init__(self, filename): def __init__(self, filename):
self.filename = filename self.filename = filename
self._cmp_name = filename
def get_lines(self): def get_lines(self):
return Utils.open_source_file(self.filename) return Utils.open_source_file(self.filename)
...@@ -278,6 +301,7 @@ class StringSourceDescriptor(SourceDescriptor): ...@@ -278,6 +301,7 @@ class StringSourceDescriptor(SourceDescriptor):
def __init__(self, name, code): def __init__(self, name, code):
self.name = name self.name = name
self.codelines = [x + "\n" for x in code.split("\n")] self.codelines = [x + "\n" for x in code.split("\n")]
self._cmp_name = name
def get_lines(self): def get_lines(self):
return self.codelines return self.codelines
......
...@@ -164,10 +164,13 @@ class Lexicon(object): ...@@ -164,10 +164,13 @@ class Lexicon(object):
# token_number, "Pattern can match 0 input symbols") # token_number, "Pattern can match 0 input symbols")
if isinstance(action_spec, Actions.Action): if isinstance(action_spec, Actions.Action):
action = action_spec action = action_spec
elif callable(action_spec):
action = Actions.Call(action_spec)
else: else:
try:
action_spec.__call__
except AttributeError:
action = Actions.Return(action_spec) action = Actions.Return(action_spec)
else:
action = Actions.Call(action_spec)
final_state = machine.new_state() final_state = machine.new_state()
re.build_machine(machine, initial_state, final_state, re.build_machine(machine, initial_state, final_state,
match_bol = 1, nocase = 0) match_bol = 1, nocase = 0)
......
...@@ -23,7 +23,7 @@ def cmod(a, b): ...@@ -23,7 +23,7 @@ def cmod(a, b):
# Emulated language constructs # Emulated language constructs
def cast(type, arg): def cast(type, arg):
if callable(type): if hasattr(type, '__call__'):
return type(arg) return type(arg)
else: else:
return arg return arg
...@@ -35,7 +35,7 @@ def address(arg): ...@@ -35,7 +35,7 @@ def address(arg):
return pointer(type(arg))([arg]) return pointer(type(arg))([arg])
def declare(type=None, value=None, **kwds): def declare(type=None, value=None, **kwds):
if type and callable(type): if type is not None and hasattr(type, '__call__'):
if value: if value:
return type(value) return type(value)
else: else:
......
...@@ -110,18 +110,18 @@ class UtilityCode(object): ...@@ -110,18 +110,18 @@ class UtilityCode(object):
def write_init_code(self, writer, pos): def write_init_code(self, writer, pos):
if not self.init: if not self.init:
return return
if callable(self.init): if isinstance(self.init, basestring):
self.init(writer, pos)
else:
writer.put(self.init) writer.put(self.init)
else:
self.init(writer, pos)
def write_cleanup_code(self, writer, pos): def write_cleanup_code(self, writer, pos):
if not self.cleanup: if not self.cleanup:
return return
if callable(self.cleanup): if isinstance(self.cleanup, basestring):
self.cleanup(writer, pos)
else:
writer.put(self.cleanup) writer.put(self.cleanup)
else:
self.cleanup(writer, pos)
def specialize(self, pyrex_type=None, **data): def specialize(self, pyrex_type=None, **data):
# Dicts aren't hashable... # Dicts aren't hashable...
......
...@@ -16,7 +16,7 @@ True ...@@ -16,7 +16,7 @@ True
True True
>>> mul() == 1*60*1000 >>> mul() == 1*60*1000
True True
>>> arithm() == 9*2+3*8/6-10 >>> arithm() == 9*2+3*8//6-10
True True
>>> parameters() == _func(-1 -2, - (-3+4), 1*2*3) >>> parameters() == _func(-1 -2, - (-3+4), 1*2*3)
True True
...@@ -52,7 +52,7 @@ def mul(): ...@@ -52,7 +52,7 @@ def mul():
return 1*60*1000 return 1*60*1000
def arithm(): def arithm():
return 9*2+3*8/6-10 return 9*2+3*8//6-10
def parameters(): def parameters():
return _func(-1 -2, - (-3+4), 1*2*3) return _func(-1 -2, - (-3+4), 1*2*3)
......
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