Permalink
Browse files

py23 compat

  • Loading branch information...
1 parent 73ed53c commit f0b5aafaf51227f84291733e39e1113a9028752e @markflorisson markflorisson committed Jul 23, 2011
Showing with 41 additions and 29 deletions.
  1. +1 −1 Cython/Compiler/Code.pxd
  2. +35 −26 Cython/Compiler/Code.py
  3. +2 −1 Cython/Tempita/__init__.py
  4. +2 −0 Cython/Utils.py
  5. +1 −1 tests/run/memslice_indexing.pyx
View
@@ -2,7 +2,7 @@
cimport cython
cdef class UtilityCodeBase(object):
- pass
+ cdef public object name
cdef class UtilityCode(UtilityCodeBase):
cdef public object proto
View
@@ -59,12 +59,8 @@ class UtilityCodeBase(object):
# @classmethod
def _add_utility(cls, utility, type, lines, begin_lineno):
if utility:
- if cls.is_cython_utility:
- # Don't forget our line number
- code = '' * begin_lineno + ''.join(lines)
- else:
- # line numbers are not important here
- code = ''.join(lines)
+ # Remember line numbers as least until after templating
+ code = '' * begin_lineno + ''.join(lines)
if type == 'Proto':
utility[0] = code
@@ -96,28 +92,34 @@ def load_utilities_from_file(cls, path):
f = Utils.open_source_file(filename, encoding='UTF-8')
try:
- for lineno, line in enumerate(f):
- 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)
+ all_lines = f.readlines() # py23
finally:
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:
raise ValueError("Empty utility code file")
@@ -190,12 +192,19 @@ def load_as_string(cls, util_code_name, from_file=None, context=None):
proto, impl = utilities[util_code_name]
if context is not None:
+ if '__name' not in context:
+ context['__name'] = util_code_name
+
if proto:
proto = tempita.sub(proto, **context)
if impl:
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)
@@ -565,7 +565,7 @@ def _parse_signature(self, args, kw):
else:
raise TypeError(
'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():
if name not in values:
values[name] = self._template._eval(
@@ -660,6 +660,7 @@ def lex(s, name=None, trim_whitespace=True, line_offset=0, delimeters=None):
chunks = []
last = 0
last_pos = (1, 1)
+
token_re = re.compile(r'%s|%s' % (re.escape(delimeters[0]),
re.escape(delimeters[1])))
for match in token_re.finditer(s):
View
@@ -130,6 +130,8 @@ def readlines(self):
while data:
content.append(data)
data = self.read(0x1000)
+
+ # TODO: FIXME: Shouldn't this return lines with their newline appended??
return u''.join(content).split(u'\n')
io = None
@@ -20,7 +20,7 @@ def create_array(shape, mode='c'):
def slice_contig_indexing():
"""
- >>> print "disabled"
+ >>> print("disabled")
disabled
slice_contig_indexing()

0 comments on commit f0b5aaf

Please sign in to comment.