Merge lp:~simon-funke/spud/set-option-if-statement into lp:spud

Proposed by Simon Funke
Status: Merged
Approved by: Cian Wilson
Approved revision: 505
Merged at revision: 505
Proposed branch: lp:~simon-funke/spud/set-option-if-statement
Merge into: lp:spud
Diff against target: 248 lines (+103/-46)
2 files modified
python/libspud.c (+74/-45)
python/test_libspud.py (+29/-1)
To merge this branch: bzr merge lp:~simon-funke/spud/set-option-if-statement
Reviewer Review Type Date Requested Status
Cian Wilson Approve
Patrick Farrell Approve
Review via email: mp+81279@code.launchpad.net

Description of the change

Includes some bugfixes in the libspud python bindings.

To post a comment you must log in.
Revision history for this message
Patrick Farrell (pefarrell) wrote :

Yep, looks good to me. The old code was clearly broken.

I'll leave this up here for a day so that Cian can inspect it if he likes, and then you can merge.

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

Looks good to me.

My code, using this branch of spud, is still green on my buildbot.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'python/libspud.c'
2--- python/libspud.c 2011-08-16 15:22:55 +0000
3+++ python/libspud.c 2011-11-04 14:11:27 +0000
4@@ -329,7 +329,6 @@
5 spud_get_option_aux_scalar_or_string(const char *key, int key_len, int type, int rank, int *shape)
6 { // this function is for getting option when the option is of type a scalar or string
7 int outcomeGetOption;
8-
9 if (type == SPUD_DOUBLE){
10 double val;
11 outcomeGetOption = spud_get_option(key, key_len, &val);
12@@ -393,7 +392,7 @@
13 return NULL;
14 }
15 for (n = 0; n < colsize; n++){
16- PyObject* element = Py_BuildValue("f", val[counter]);
17+ PyObject* element = Py_BuildValue("d", val[counter]);
18 PyList_SetItem(pysublist, n, element);
19 counter++;
20 }
21@@ -432,7 +431,7 @@
22 return NULL;
23 }
24 for (n = 0; n < colsize; n++){
25- PyObject* element = Py_BuildValue("f", val[counter]);
26+ PyObject* element = Py_BuildValue("i", val[counter]);
27 PyList_SetItem(pysublist, n, element);
28 counter++;
29 }
30@@ -540,7 +539,7 @@
31 for (j = 0; j < psize; j++){
32 element = -1.0;
33 PyObject* pelement = PyList_GetItem(pylist, j);
34- PyArg_Parse(pelement, "f", &element);
35+ element = PyFloat_AS_DOUBLE(pelement);
36 val[j] = element;
37 }
38 outcomeSetOption = spud_set_option(key, key_len, val, type, rank, shape);
39@@ -601,27 +600,20 @@
40 int i;
41 int j;
42 int counter = 0;
43- int pylistsize = PyList_Size(pylist);
44- shape[0] = pylistsize;
45-
46+
47 int outcomeSetOption;
48- int pysublistsize;
49- PyObject* pysublist = PyList_GetItem(pylist, 0);
50- pysublistsize = PyList_Size(pysublist);
51- int size = pylistsize*pysublistsize;
52+
53+ int size = shape[0]*shape[1];
54
55 double element;
56 double val [size];
57
58- for (i = 0; i < pylistsize; i++){
59+ for (i = 0; i < shape[0]; i++){
60 PyObject* pysublist = PyList_GetItem(pylist, i);
61- pysublistsize = PyList_Size(pysublist);
62- shape[1] = pysublistsize;
63- for (j = 0; j < pysublistsize; j++){
64- element = -1.0;
65+ for (j = 0; j < shape[1]; j++){
66 PyObject* pysublistElement = PyList_GetItem(pysublist, j);
67- PyArg_Parse(pysublistElement, "d", &element);
68- val[counter] = element;
69+ element = PyFloat_AS_DOUBLE(pysublistElement);
70+ val[counter] = (double) element;
71 counter ++;
72 }
73 }
74@@ -636,22 +628,15 @@
75 int i;
76 int j;
77 int counter = 0;
78- int pylistsize = PyList_Size(pylist);
79- shape[0] = pylistsize;
80- int pysublistsize;
81- PyObject* pysublist = PyList_GetItem(pylist, 0);
82- pysublistsize = PyList_Size(pysublist);
83- int size = pylistsize*pysublistsize;
84+ int size = shape[0]*shape[1];
85 int val [size];
86 int outcomeSetOption;
87
88 int element;
89
90- for (i = 0; i < pylistsize; i++){
91+ for (i = 0; i < shape[0]; i++){
92 PyObject* pysublist = PyList_GetItem(pylist, i);
93- pysublistsize = PyList_Size(pysublist);
94- shape[1] = pysublistsize;
95- for (j = 0; j < pysublistsize; j++){
96+ for (j = 0; j < shape[1]; j++){
97 element = 1;
98 PyObject* pysublistElement = PyList_GetItem(pysublist, j);
99 PyArg_Parse(pysublistElement, "i", &element);
100@@ -661,7 +646,7 @@
101 }
102
103 outcomeSetOption = spud_set_option(key, key_len, val, type, rank, shape);
104- return error_checking(outcomeSetOption, "set option aux tensor doubles");
105+ return error_checking(outcomeSetOption, "set option aux tensor ints");
106 }
107
108 static PyObject*
109@@ -689,30 +674,74 @@
110 {
111 const char *key;
112 int key_len;
113- int type;
114- int rank;
115+ int type=-1;
116+ int rank=-1;
117 int shape[2];
118- int outcomeGetRank;
119- int outcomeGetShape;
120- int outcomeGetType;
121 PyObject* firstArg;
122 PyObject* secondArg;
123+
124+ if(PyTuple_GET_SIZE(args)!=2){
125+ PyErr_SetString(SpudError,"Error: set_option takes exactly 2 arguments.");
126+ return NULL;
127+ }
128
129 firstArg = PyTuple_GetItem(args, 0);
130 secondArg = PyTuple_GetItem(args, 1);
131 PyArg_Parse(firstArg, "s", &key);
132 key_len = strlen(key);
133- outcomeGetType = spud_get_option_type(key, key_len, &type);
134- if (error_checking(outcomeGetType, "set option") == NULL){
135- return NULL;
136- }
137- outcomeGetRank = spud_get_option_rank(key, key_len, &rank);
138- if (error_checking(outcomeGetRank, "set option") == NULL){
139- return NULL;
140- }
141- outcomeGetShape = spud_get_option_shape(key, key_len, shape);
142- if (error_checking(outcomeGetShape, "set option") == NULL){
143- return NULL;
144+
145+ if (!spud_have_option(key, key_len)){ //option does not exist yet
146+ int outcomeAddOption = spud_add_option(key, key_len);
147+ error_checking(outcomeAddOption, "set option");
148+ }
149+
150+ if (PyInt_Check(secondArg)){ //just an int
151+ type = SPUD_INT;
152+ rank = 0;
153+ shape[0] = -1;
154+ shape[1] = -1;
155+
156+ }
157+ else if (PyString_Check(secondArg)){// a string
158+ type = SPUD_STRING;
159+ rank = 1;
160+ shape[0] = PyString_GET_SIZE(secondArg);
161+ shape[1] = -1;
162+ }
163+ else if (PyFloat_Check(secondArg)){// a double
164+ type = SPUD_DOUBLE;
165+ rank = 0;
166+ shape[0] = -1;
167+ shape[1] = -1;
168+ }
169+ else if (PyList_Check(secondArg)){
170+ PyObject* listElement = PyList_GetItem(secondArg, 0);
171+ if (PyInt_Check(listElement)){ //list of ints
172+ type = SPUD_INT;
173+ rank = 1;
174+ shape[0] = 1;
175+ shape[1] = -1;
176+ }
177+ else if (PyFloat_Check(listElement)){
178+ type = SPUD_DOUBLE; //list of doubles
179+ rank = 1;
180+ shape[0] = 1;
181+ shape[1] = -1;
182+ }
183+ else if (PyList_Check(listElement)){ //list of lists
184+ int pylistSize = PyList_GET_SIZE(secondArg);
185+ int pysublistSize = PyList_GET_SIZE(listElement);
186+ PyObject* sublistElement = PyList_GetItem(listElement, 0);
187+ if (PyInt_Check(sublistElement)){ //list of lists of ints
188+ type = SPUD_INT;
189+ }
190+ else if (PyFloat_Check(sublistElement)){//list of lists of doubles
191+ type = SPUD_DOUBLE;
192+ }
193+ rank = 2;
194+ shape[0] = pylistSize;
195+ shape[1] = pysublistSize;
196+ }
197 }
198
199 if (rank == 0){ // scalar
200
201=== modified file 'python/test_libspud.py'
202--- python/test_libspud.py 2011-08-16 08:41:17 +0000
203+++ python/test_libspud.py 2011-11-04 14:11:27 +0000
204@@ -42,10 +42,11 @@
205 tensor_path = '/material_phase::Material1/tensor_field::DummyTensor/prescribed/value::WholeMesh/anisotropic_asymmetric/constant'
206 assert libspud.get_option_shape(tensor_path) == (2, 2)
207 assert libspud.get_option_rank(tensor_path) == 2
208+
209 assert libspud.get_option(tensor_path)==[[1.0,2.0],[3.0,4.0]]
210
211 libspud.set_option(tensor_path, [[5.0,6.0,2.0],[7.0,8.0,1.0]])
212-#assert libspud.get_option_shape(tensor_path) == (3,3)
213+assert libspud.get_option_shape(tensor_path) == (2,3)
214 assert libspud.get_option_rank(tensor_path) == 2
215
216 assert(libspud.get_option(tensor_path)==[[5.0, 6.0, 2.0],[7.0, 8.0, 1.0]])
217@@ -86,4 +87,31 @@
218
219 libspud.write_options('test_out.flml')
220
221+libspud.set_option('/test',4)
222+
223+assert libspud.get_option('/test') == 4
224+
225+libspud.set_option('/test',[[4.0,2.0,3.0],[2.0,5.0,6.6]])
226+
227+assert libspud.get_option('/test') == [[4.0,2.0,3.0],[2.0,5.0,6.6]]
228+
229+libspud.set_option('/test',"Hallo")
230+
231+assert libspud.get_option('/test') == "Hallo"
232+
233+libspud.set_option('/test',[1,2,3])
234+
235+assert libspud.get_option('/test') == [1,2,3]
236+
237+libspud.set_option('/test',[2.3,3.3])
238+
239+assert libspud.get_option('/test') == [2.3,3.3]
240+
241+try:
242+ libspud.set_option('/test')
243+ assert False
244+except libspud.SpudError, e:
245+ pass
246+
247+
248 print "All tests passed!"

Subscribers

People subscribed via source and target branches