Commit ce0cf9b5 authored by Dag Sverre Seljebotn's avatar Dag Sverre Seljebotn

Propagating options through list comprehensions.

Side-effect: Buffer access starts working within list comprehensions.
parent d3b92ce4
...@@ -1595,6 +1595,9 @@ class IndexNode(ExprNode): ...@@ -1595,6 +1595,9 @@ class IndexNode(ExprNode):
code.putln("%s = %s;" % (temp, index.result_code)) code.putln("%s = %s;" % (temp, index.result_code))
# Generate buffer access code using these temps # Generate buffer access code using these temps
import Buffer import Buffer
assert self.options is not None
# The above could happen because child_attrs is wrong somewhere so that
# options are not propagated.
return Buffer.put_buffer_lookup_code(entry=self.base.entry, return Buffer.put_buffer_lookup_code(entry=self.base.entry,
index_signeds=[i.type.signed for i in self.indices], index_signeds=[i.type.signed for i in self.indices],
index_cnames=index_temps, index_cnames=index_temps,
...@@ -2577,6 +2580,8 @@ class ListComprehensionNode(SequenceNode): ...@@ -2577,6 +2580,8 @@ class ListComprehensionNode(SequenceNode):
subexprs = [] subexprs = []
is_sequence_constructor = 0 # not unpackable is_sequence_constructor = 0 # not unpackable
child_attrs = ["loop", "append"]
def analyse_types(self, env): def analyse_types(self, env):
self.type = list_type self.type = list_type
self.is_temp = 1 self.is_temp = 1
...@@ -2602,6 +2607,8 @@ class ListComprehensionNode(SequenceNode): ...@@ -2602,6 +2607,8 @@ class ListComprehensionNode(SequenceNode):
class ListComprehensionAppendNode(ExprNode): class ListComprehensionAppendNode(ExprNode):
# Need to be careful to avoid infinite recursion:
# target must not be in child_attrs/subexprs
subexprs = ['expr'] subexprs = ['expr']
def analyse_types(self, env): def analyse_types(self, env):
......
...@@ -331,7 +331,7 @@ def explicitly_release_buffer(): ...@@ -331,7 +331,7 @@ def explicitly_release_buffer():
print "After release" print "After release"
# #
# Index bounds checking # Getting items and index bounds checking
# #
@testcase @testcase
def get_int_2d(object[int, 2] buf, int i, int j): def get_int_2d(object[int, 2] buf, int i, int j):
...@@ -436,6 +436,15 @@ def set_int_2d(object[int, 2] buf, int i, int j, int value): ...@@ -436,6 +436,15 @@ def set_int_2d(object[int, 2] buf, int i, int j, int value):
""" """
buf[i, j] = value buf[i, j] = value
@testcase
def list_comprehension(object[int] buf, len):
"""
>>> list_comprehension(IntMockBuffer(None, [1,2,3]), 3)
1|2|3
"""
cdef int i
print "|".join([str(buf[i]) for i in range(len)])
# #
# Buffer type mismatch examples. Varying the type and access # Buffer type mismatch examples. Varying the type and access
# method simultaneously, the odds of an interaction is virtually # method simultaneously, the odds of an interaction is virtually
......
# cannot be named "numpy" in order to not clash with the numpy module! # cannot be named "numpy" in order to not clash with the numpy module!
cimport numpy cimport numpy as np
try: try:
import numpy import numpy as np
__doc__ = """ __doc__ = """
>>> basic() >>> basic()
...@@ -25,21 +25,45 @@ try: ...@@ -25,21 +25,45 @@ try:
>>> obj_array() >>> obj_array()
[a 1 {}] [a 1 {}]
a 1 {} a 1 {}
Test various forms of slicing, picking etc.
>>> a = np.arange(10, dtype=np.long).reshape(2, 5)
>>> print_long_2d(a)
0 1 2 3 4
5 6 7 8 9
>>> print_long_2d(a[::-1, ::-1])
9 8 7 6 5
4 3 2 1 0
>>> print_long_2d(a[1:2, 1:3])
6 7
>>> print_long_2d(a[::2, ::2])
0 2 4
>>> print_long_2d(a[::4, :])
0 1 2 3 4
>>> print_long_2d(a[4:1:-1, :])
4 3 2
""" """
except: except:
__doc__ = "" __doc__ = ""
def basic(): def basic():
cdef object[int, 2] buf = numpy.arange(10, dtype='i').reshape((2, 5)) cdef object[int, ndim=2] buf = np.arange(10, dtype='i').reshape((2, 5))
print buf print buf
print buf[0, 2], buf[0, 0], buf[1, 4], buf[1, 0] print buf[0, 2], buf[0, 0], buf[1, 4], buf[1, 0]
def three_dim(): def three_dim():
cdef object[double, 3] buf = numpy.arange(24, dtype='d').reshape((3,2,4)) cdef object[double, ndim=3] buf = np.arange(24, dtype='d').reshape((3,2,4))
print buf print buf
print buf[0, 1, 2], buf[0, 0, 0], buf[1, 1, 1], buf[1, 0, 0] print buf[0, 1, 2], buf[0, 0, 0], buf[1, 1, 1], buf[1, 0, 0]
def obj_array(): def obj_array():
cdef object[object, 1] buf = numpy.array(["a", 1, {}]) cdef object[object, ndim=1] buf = np.array(["a", 1, {}])
print buf print buf
print buf[0], buf[1], buf[2] print buf[0], buf[1], buf[2]
def print_long_2d(np.ndarray[long, 2] arr):
cdef int i, j
for i in range(arr.shape[0]):
print " ".join([arr[i, j] for j in range(arr.shape[1])])
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