Merge lp:~cwilson/spud/pythonbug into lp:spud

Proposed by Cian Wilson
Status: Merged
Approved by: Patrick Farrell
Approved revision: 405
Merged at revision: 407
Proposed branch: lp:~cwilson/spud/pythonbug
Merge into: lp:spud
Diff against target: 94 lines (+45/-6)
2 files modified
python/libspud.py.in (+35/-5)
python/test_ctypes.py (+10/-1)
To merge this branch: bzr merge lp:~cwilson/spud/pythonbug
Reviewer Review Type Date Requested Status
Patrick Farrell Approve
Review via email: mp+66989@code.launchpad.net

Description of the change

This merge fixes a mangling/out of range issue (for non-square tensors) with get_option in the python libspud interface (bug lp:806331).

Additionally it adds functionality to allow set_option to work for rank>0 objects (bug lp:806332).

Test added to test_ctypes.py.

To post a comment you must log in.
lp:~cwilson/spud/pythonbug updated
405. By Cian Wilson

Oops, removing duplicated derived exception class.

Revision history for this message
Patrick Farrell (pefarrell) wrote :

Looks good. It would be nice if we did numpy arrays too, but it's not essential. If you're clever about your try/exceptions, it doesn't have to have a hard dependency on numpy.

Revision history for this message
Cian Wilson (cwilson) wrote :

True but I'll leave that until it bugs me enough to fix (probably won't take that long).

Thanks Patrick.

Revision history for this message
Florian Rathgeber (florian-rathgeber) wrote :

Patrick, do you still have plans to use numpy arrays for this? Otherwise this should be approved (since it has been merged already) and the 3 related issues closed.

Revision history for this message
Patrick Farrell (pefarrell) wrote :

I approve. I don't need the numpy arrays, so I'm not pushed about them.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'python/libspud.py.in'
2--- python/libspud.py.in 2011-07-05 13:54:45 +0000
3+++ python/libspud.py.in 2011-07-06 05:48:34 +0000
4@@ -173,7 +173,8 @@
5 val_type = ctype_map[type]
6 rank = get_option_rank(s)
7 shape = get_option_shape(s)
8- for i in range(rank): val_type = val_type*shape[i]
9+ # reverse the numbering so we get a rowxcolumn list of lists
10+ for i in range(rank-1, -1, -1): val_type = val_type*shape[i]
11 val = val_type()
12
13 out = cget_option(s, byref(c_int(len(s))), byref(val))
14@@ -205,19 +206,48 @@
15 cset_option.restype = c_int
16
17 def set_option(s, val):
18+ rank = 0
19+ shape_type = c_int * 2
20+ shape = shape_type(1, -1)
21 py_type = type(val)
22+ if py_type not in typepy_map:
23+ if py_type == list:
24+ subval = val
25+ for i in range(2):
26+ if type(subval) == list:
27+ shape[i] = len(subval)
28+ rank += 1
29+ subval = subval[0]
30+ else:
31+ break
32+ py_type = type(subval)
33+ # elif ...
34+ # could put numpy array handling in here
35+ # (they'd be easier but didn't want to
36+ # add a numpy dependency)
37+ else:
38+ raise spud_exceptions[SPUD_TYPE_ERROR]("Unknown value type. Only know about floats, ints, strings and lists thereof.")
39 spud_code = typepy_map[py_type]
40 c_type = ctype_map[py_type]
41- c_val = c_type(val)
42+ # reverse the order of the shape entries as that way we end up with a rows x columns c_type
43+ for i in range(rank-1, -1, -1): c_type = c_type*shape[i]
44+ if rank == 2:
45+ c_val = c_type()
46+ for i in range(shape[0]):
47+ for j in range(shape[1]):
48+ c_val[i][j] = val[i][j]
49+ elif rank == 1:
50+ c_val = c_type()
51+ for i in range(shape[0]):
52+ c_val[i] = val[i]
53+ else:
54+ c_val = c_type(val)
55
56- shape_type = c_int * 2
57 if py_type is str:
58 shape = shape_type(len(val), -1)
59 rank = 1
60 out = cset_option(s, byref(c_int(len(s))), (c_val), byref(c_int(spud_code)), byref(c_int(rank)), shape)
61 else:
62- shape = shape_type(1, -1)
63- rank = 0
64 out = cset_option(s, byref(c_int(len(s))), byref(c_val), byref(c_int(spud_code)), byref(c_int(rank)), shape)
65
66 if out != SPUD_NO_ERROR:
67
68=== modified file 'python/test_ctypes.py'
69--- python/test_ctypes.py 2011-07-05 13:54:45 +0000
70+++ python/test_ctypes.py 2011-07-06 05:48:34 +0000
71@@ -30,13 +30,22 @@
72 print libspud.get_option_rank(list_path)
73 print libspud.get_option(list_path)
74 assert(libspud.get_option(list_path)==[7,8,9,10])
75+libspud.set_option(list_path, [11,12,13,14])
76+print libspud.get_option_shape(list_path)
77+print libspud.get_option_rank(list_path)
78+print libspud.get_option(list_path)
79+assert(libspud.get_option(list_path)==[11,12,13,14])
80
81 tensor_path = '/material_phase::Material1/tensor_field::DummyTensor/prescribed/value::WholeMesh/anisotropic_asymmetric/constant'
82 print libspud.get_option_shape(tensor_path)
83 print libspud.get_option_rank(tensor_path)
84 print libspud.get_option(tensor_path)
85 assert(libspud.get_option(tensor_path)==[[1.0,2.0],[3.0,4.0]])
86-
87+libspud.set_option(tensor_path, [[5.0,6.0],[7.0,8.0]])
88+print libspud.get_option_shape(tensor_path)
89+print libspud.get_option_rank(tensor_path)
90+print libspud.get_option(tensor_path)
91+assert(libspud.get_option(tensor_path)==[[5.0,6.0],[7.0,8.0]])
92
93 try:
94 libspud.add_option('/foo')

Subscribers

People subscribed via source and target branches