Commit f0b5aafa authored by Mark Florisson's avatar Mark Florisson

py23 compat

parent 73ed53c9
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
cimport cython cimport cython
cdef class UtilityCodeBase(object): cdef class UtilityCodeBase(object):
pass cdef public object name
cdef class UtilityCode(UtilityCodeBase): cdef class UtilityCode(UtilityCodeBase):
cdef public object proto cdef public object proto
......
...@@ -59,12 +59,8 @@ class UtilityCodeBase(object): ...@@ -59,12 +59,8 @@ class UtilityCodeBase(object):
# @classmethod # @classmethod
def _add_utility(cls, utility, type, lines, begin_lineno): def _add_utility(cls, utility, type, lines, begin_lineno):
if utility: if utility:
if cls.is_cython_utility: # Remember line numbers as least until after templating
# Don't forget our line number code = '' * begin_lineno + ''.join(lines)
code = '' * begin_lineno + ''.join(lines)
else:
# line numbers are not important here
code = ''.join(lines)
if type == 'Proto': if type == 'Proto':
utility[0] = code utility[0] = code
...@@ -96,28 +92,34 @@ class UtilityCodeBase(object): ...@@ -96,28 +92,34 @@ class UtilityCodeBase(object):
f = Utils.open_source_file(filename, encoding='UTF-8') f = Utils.open_source_file(filename, encoding='UTF-8')
try: try:
for lineno, line in enumerate(f): all_lines = f.readlines() # py23
m = re.search(regex, line)
if m:
cls._add_utility(utility, type, lines, begin_lineno)
begin_lineno = lineno + 1
name = m.group(1)
if name.endswith(".proto"):
name = name[:-6]
type = 'Proto'
else:
type = 'Code'
utility = utilities.setdefault(name, [None, None])
utilities[name] = utility
lines = []
else:
lines.append(line)
finally: finally:
f.close() f.close()
for lineno, line in enumerate(all_lines):
# apparently 'line' may be without trailing newline
# (NormalisedNewlineStream.readlines())
line = line.rstrip() + '\n'
m = re.search(regex, line)
if m:
cls._add_utility(utility, type, lines, begin_lineno)
begin_lineno = lineno + 1
name = m.group(1)
if name.endswith(".proto"):
name = name[:-6]
type = 'Proto'
else:
type = 'Code'
utility = utilities.setdefault(name, [None, None])
utilities[name] = utility
lines = []
else:
lines.append(line)
if not utility: if not utility:
raise ValueError("Empty utility code file") raise ValueError("Empty utility code file")
...@@ -190,12 +192,19 @@ class UtilityCodeBase(object): ...@@ -190,12 +192,19 @@ class UtilityCodeBase(object):
proto, impl = utilities[util_code_name] proto, impl = utilities[util_code_name]
if context is not None: if context is not None:
if '__name' not in context:
context['__name'] = util_code_name
if proto: if proto:
proto = tempita.sub(proto, **context) proto = tempita.sub(proto, **context)
if impl: if impl:
impl = tempita.sub(impl, **context) impl = tempita.sub(impl, **context)
return proto, impl if cls.is_cython_utility:
# Remember line numbers
return proto, impl
return proto and proto.lstrip(), impl and impl.lstrip()
load_as_string = classmethod(load_as_string) load_as_string = classmethod(load_as_string)
......
...@@ -565,7 +565,7 @@ class TemplateDef(object): ...@@ -565,7 +565,7 @@ class TemplateDef(object):
else: else:
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.iteritems():
if name not in values: if name not in values:
values[name] = self._template._eval( values[name] = self._template._eval(
...@@ -660,6 +660,7 @@ def lex(s, name=None, trim_whitespace=True, line_offset=0, delimeters=None): ...@@ -660,6 +660,7 @@ def lex(s, name=None, trim_whitespace=True, line_offset=0, delimeters=None):
chunks = [] chunks = []
last = 0 last = 0
last_pos = (1, 1) last_pos = (1, 1)
token_re = re.compile(r'%s|%s' % (re.escape(delimeters[0]), token_re = re.compile(r'%s|%s' % (re.escape(delimeters[0]),
re.escape(delimeters[1]))) re.escape(delimeters[1])))
for match in token_re.finditer(s): for match in token_re.finditer(s):
......
...@@ -130,6 +130,8 @@ class NormalisedNewlineStream(object): ...@@ -130,6 +130,8 @@ class NormalisedNewlineStream(object):
while data: while data:
content.append(data) content.append(data)
data = self.read(0x1000) data = self.read(0x1000)
# TODO: FIXME: Shouldn't this return lines with their newline appended??
return u''.join(content).split(u'\n') return u''.join(content).split(u'\n')
io = None io = None
......
...@@ -20,7 +20,7 @@ def create_array(shape, mode='c'): ...@@ -20,7 +20,7 @@ def create_array(shape, mode='c'):
def slice_contig_indexing(): def slice_contig_indexing():
""" """
>>> print "disabled" >>> print("disabled")
disabled disabled
slice_contig_indexing() slice_contig_indexing()
......
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