Merge lp:~spud/spud/pythonbindingbug into lp:spud
- pythonbindingbug
- Merge into trunk
Status: | Merged |
---|---|
Approved by: | Patrick Farrell |
Approved revision: | 419 |
Merged at revision: | 420 |
Proposed branch: | lp:~spud/spud/pythonbindingbug |
Merge into: | lp:spud |
Diff against target: |
265 lines (+63/-53) 2 files modified
python/libspud.py.in (+62/-52) python/test_ctypes.py (+1/-1) |
To merge this branch: | bzr merge lp:~spud/spud/pythonbindingbug |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Spud developers | Pending | ||
Review via email:
|
Commit message
Description of the change
These changes should bring the python bindings back into line with the rest of spud.
All the python tests in test_ctypes.py work again now (don't know if they cover all interfaces but I did try to update them all).

Patrick Farrell (pefarrell) wrote : | # |

Cian Wilson (cwilson) wrote : | # |
For some reason this started failing for me last night. I guess I updated wherever I was picking up libspud.py from (I think my co of the fluidity trunk) but it's always been a bit mysterious to me which version I'm actually using.
None of the fluidity short or medium tests (I only grepped for 'import libspud') seem to depend on this, which I could rectify but I'm not sure where buildbot would pick up its version of libspud.py (and dependency on libspud-dev) from.
Saw you had a suggestion of packaging the python interface. Would doing this improve this situation?

Patrick Farrell (pefarrell) wrote : | # |
You can find out where your python interpreter is getting it with
$ python -v -c "import libspud" 2>&1 | grep "import libspud"
import libspud # precompiled from /home/pef/
Yes, I think someone should package the python bindings. There's a nice example in the libadjoint source tree, if anyone cares ;-)
Preview Diff
1 | === modified file 'python/libspud.py.in' | |||
2 | --- python/libspud.py.in 2011-07-06 05:47:12 +0000 | |||
3 | +++ python/libspud.py.in 2011-07-19 05:52:17 +0000 | |||
4 | @@ -59,106 +59,116 @@ | |||
5 | 59 | 59 | ||
6 | 60 | libspud = cdll.LoadLibrary('@prefix@' + '/lib/libspud.so') | 60 | libspud = cdll.LoadLibrary('@prefix@' + '/lib/libspud.so') |
7 | 61 | 61 | ||
9 | 62 | cclear_options = libspud.cspud_clear_options_ | 62 | cclear_options = libspud.spud_clear_options |
10 | 63 | cclear_options.argtypes = [] | 63 | cclear_options.argtypes = [] |
11 | 64 | cclear_options.restype = None | 64 | cclear_options.restype = None |
12 | 65 | 65 | ||
13 | 66 | def clear_options(): | 66 | def clear_options(): |
14 | 67 | cclear_options() | 67 | cclear_options() |
15 | 68 | 68 | ||
19 | 69 | cload_options = libspud.cspud_load_options_ | 69 | cload_options = libspud.spud_load_options |
20 | 70 | cload_options.argtypes = [c_char_p, POINTER(c_int)] | 70 | cload_options.argtypes = [c_char_p, c_int] |
21 | 71 | cload_options.restype = None | 71 | cload_options.restype = c_int |
22 | 72 | 72 | ||
23 | 73 | def load_options(s): | 73 | def load_options(s): |
25 | 74 | cload_options(s, byref(c_int(len(s)))) | 74 | out = cload_options(s, c_int(len(s))) |
26 | 75 | if out != SPUD_NO_ERROR: | ||
27 | 76 | raise spud_exceptions[out] | ||
28 | 75 | 77 | ||
32 | 76 | cwrite_options = libspud.cspud_write_options_ | 78 | cwrite_options = libspud.spud_write_options |
33 | 77 | cwrite_options.argtypes = [c_char_p, POINTER(c_int)] | 79 | cwrite_options.argtypes = [c_char_p, c_int] |
34 | 78 | cwrite_options.restype = None | 80 | cwrite_options.restype = c_int |
35 | 79 | 81 | ||
36 | 80 | def write_options(filename): | 82 | def write_options(filename): |
38 | 81 | cwrite_options(filename, byref(c_int(len(filename)))) | 83 | out = cwrite_options(filename, c_int(len(filename))) |
39 | 84 | if out != SPUD_NO_ERROR: | ||
40 | 85 | raise spud_exceptions[out] | ||
41 | 82 | 86 | ||
44 | 83 | cget_child_name = libspud.cspud_get_child_name_ | 87 | cget_child_name = libspud.spud_get_child_name |
45 | 84 | cget_child_name.argtypes = [c_char_p, POINTER(c_int), POINTER(c_int), c_char_p, POINTER(c_int)] | 88 | cget_child_name.argtypes = [c_char_p, c_int, c_int, c_char_p, c_int] |
46 | 85 | cget_child_name.restype = c_int | 89 | cget_child_name.restype = c_int |
47 | 86 | 90 | ||
48 | 87 | def get_child_name(s, idx): | 91 | def get_child_name(s, idx): |
49 | 88 | val = create_string_buffer(OPTION_PATH_LEN) | 92 | val = create_string_buffer(OPTION_PATH_LEN) |
51 | 89 | out = cget_child_name(s, byref(c_int(len(s))), byref(c_int(idx)), val, byref(c_int(len(val)))) | 93 | out = cget_child_name(s, c_int(len(s)), c_int(idx), val, c_int(len(val))) |
52 | 90 | 94 | ||
53 | 91 | if out != SPUD_NO_ERROR: | 95 | if out != SPUD_NO_ERROR: |
54 | 92 | raise spud_exceptions[out] | 96 | raise spud_exceptions[out] |
55 | 93 | 97 | ||
56 | 94 | return val.value.strip() | 98 | return val.value.strip() |
57 | 95 | 99 | ||
61 | 96 | cnumber_of_children = libspud.cspud_number_of_children_ | 100 | cget_number_of_children = libspud.spud_get_number_of_children |
62 | 97 | cnumber_of_children.argtypes = [c_char_p, POINTER(c_int)] | 101 | cget_number_of_children.argtypes = [c_char_p, c_int, POINTER(c_int)] |
63 | 98 | cnumber_of_children.restype = c_int | 102 | cget_number_of_children.restype = c_int |
64 | 99 | 103 | ||
67 | 100 | def number_of_children(s): | 104 | def get_number_of_children(s): |
68 | 101 | return cnumber_of_children(s, byref(c_int(len(s)))) | 105 | val = c_int() |
69 | 106 | out = cget_number_of_children(s, c_int(len(s)), byref(val)) | ||
70 | 107 | |||
71 | 108 | if out != SPUD_NO_ERROR: | ||
72 | 109 | raise spud_exceptions[out] | ||
73 | 110 | |||
74 | 111 | return val.value | ||
75 | 102 | 112 | ||
78 | 103 | coption_count = libspud.cspud_option_count_ | 113 | coption_count = libspud.spud_option_count |
79 | 104 | coption_count.argtypes = [c_char_p, POINTER(c_int)] | 114 | coption_count.argtypes = [c_char_p, c_int] |
80 | 105 | coption_count.restype = c_int | 115 | coption_count.restype = c_int |
81 | 106 | 116 | ||
82 | 107 | def option_count(s): | 117 | def option_count(s): |
84 | 108 | return coption_count(s, byref(c_int(len(s)))) | 118 | return coption_count(s, c_int(len(s))) |
85 | 109 | 119 | ||
88 | 110 | chave_option = libspud.cspud_have_option_ | 120 | chave_option = libspud.spud_have_option |
89 | 111 | chave_option.argtypes = [c_char_p, POINTER(c_int)] | 121 | chave_option.argtypes = [c_char_p, c_int] |
90 | 112 | chave_option.restype = c_int | 122 | chave_option.restype = c_int |
91 | 113 | 123 | ||
92 | 114 | def have_option(s): | 124 | def have_option(s): |
94 | 115 | out = chave_option(s, byref(c_int(len(s)))) | 125 | out = chave_option(s, c_int(len(s))) |
95 | 116 | 126 | ||
96 | 117 | if out == 1: | 127 | if out == 1: |
97 | 118 | return True | 128 | return True |
98 | 119 | else: | 129 | else: |
99 | 120 | return False | 130 | return False |
100 | 121 | 131 | ||
103 | 122 | cget_option_type = libspud.cspud_get_option_type_ | 132 | cget_option_type = libspud.spud_get_option_type |
104 | 123 | cget_option_type.argtypes = [c_char_p, POINTER(c_int), POINTER(c_int)] | 133 | cget_option_type.argtypes = [c_char_p, c_int, POINTER(c_int)] |
105 | 124 | cget_option_type.restype = c_int | 134 | cget_option_type.restype = c_int |
106 | 125 | 135 | ||
107 | 126 | def get_option_type(s): | 136 | def get_option_type(s): |
108 | 127 | val = c_int() | 137 | val = c_int() |
110 | 128 | out = cget_option_type(s, byref(c_int(len(s))), byref(val)) | 138 | out = cget_option_type(s, c_int(len(s)), byref(val)) |
111 | 129 | 139 | ||
112 | 130 | if out != SPUD_NO_ERROR: | 140 | if out != SPUD_NO_ERROR: |
113 | 131 | raise spud_exceptions[out] | 141 | raise spud_exceptions[out] |
114 | 132 | 142 | ||
115 | 133 | return pytype_map[val.value] | 143 | return pytype_map[val.value] |
116 | 134 | 144 | ||
119 | 135 | cget_option_rank = libspud.cspud_get_option_rank_ | 145 | cget_option_rank = libspud.spud_get_option_rank |
120 | 136 | cget_option_rank.argtypes = [c_char_p, POINTER(c_int), POINTER(c_int)] | 146 | cget_option_rank.argtypes = [c_char_p, c_int, POINTER(c_int)] |
121 | 137 | cget_option_rank.restype = c_int | 147 | cget_option_rank.restype = c_int |
122 | 138 | 148 | ||
123 | 139 | def get_option_rank(s): | 149 | def get_option_rank(s): |
124 | 140 | rank = c_int() | 150 | rank = c_int() |
126 | 141 | out = cget_option_rank(s, byref(c_int(len(s))), byref(rank)) | 151 | out = cget_option_rank(s, c_int(len(s)), byref(rank)) |
127 | 142 | if out != SPUD_NO_ERROR: | 152 | if out != SPUD_NO_ERROR: |
128 | 143 | raise spud_exceptions[out] | 153 | raise spud_exceptions[out] |
129 | 144 | return rank.value | 154 | return rank.value |
130 | 145 | 155 | ||
133 | 146 | cget_option_shape = libspud.cspud_get_option_shape_ | 156 | cget_option_shape = libspud.spud_get_option_shape |
134 | 147 | cget_option_shape.argtypes = [c_char_p, POINTER(c_int), POINTER(c_int)] | 157 | cget_option_shape.argtypes = [c_char_p, c_int, POINTER(c_int)] |
135 | 148 | cget_option_shape.restype = int | 158 | cget_option_shape.restype = int |
136 | 149 | 159 | ||
137 | 150 | def get_option_shape(s): | 160 | def get_option_shape(s): |
138 | 151 | shape_type = c_int * 2 | 161 | shape_type = c_int * 2 |
139 | 152 | shape = shape_type() | 162 | shape = shape_type() |
141 | 153 | out = cget_option_shape(s, byref(c_int(len(s))), shape) | 163 | out = cget_option_shape(s, c_int(len(s)), shape) |
142 | 154 | 164 | ||
143 | 155 | if out != SPUD_NO_ERROR: | 165 | if out != SPUD_NO_ERROR: |
144 | 156 | raise spud_exceptions[out] | 166 | raise spud_exceptions[out] |
145 | 157 | 167 | ||
146 | 158 | return tuple(shape) | 168 | return tuple(shape) |
147 | 159 | 169 | ||
150 | 160 | cget_option = libspud.cspud_get_option_ | 170 | cget_option = libspud.spud_get_option |
151 | 161 | cget_option.argtypes = [c_char_p, POINTER(c_int), c_void_p] | 171 | cget_option.argtypes = [c_char_p, c_int, c_void_p] |
152 | 162 | cget_option.restype = c_int | 172 | cget_option.restype = c_int |
153 | 163 | 173 | ||
154 | 164 | def get_option(s): | 174 | def get_option(s): |
155 | @@ -177,7 +187,7 @@ | |||
156 | 177 | for i in range(rank-1, -1, -1): val_type = val_type*shape[i] | 187 | for i in range(rank-1, -1, -1): val_type = val_type*shape[i] |
157 | 178 | val = val_type() | 188 | val = val_type() |
158 | 179 | 189 | ||
160 | 180 | out = cget_option(s, byref(c_int(len(s))), byref(val)) | 190 | out = cget_option(s, c_int(len(s)), byref(val)) |
161 | 181 | if out != SPUD_NO_ERROR: | 191 | if out != SPUD_NO_ERROR: |
162 | 182 | raise spud_exceptions[out] | 192 | raise spud_exceptions[out] |
163 | 183 | 193 | ||
164 | @@ -189,20 +199,20 @@ | |||
165 | 189 | val_out = val.value | 199 | val_out = val.value |
166 | 190 | return val_out | 200 | return val_out |
167 | 191 | 201 | ||
170 | 192 | cadd_option = libspud.cspud_add_option_ | 202 | cadd_option = libspud.spud_add_option |
171 | 193 | cadd_option.argtypes = [c_char_p, POINTER(c_int)] | 203 | cadd_option.argtypes = [c_char_p, c_int] |
172 | 194 | cadd_option.restype = c_int | 204 | cadd_option.restype = c_int |
173 | 195 | 205 | ||
174 | 196 | def add_option(s): | 206 | def add_option(s): |
176 | 197 | out = cadd_option(s, byref(c_int(len(s)))) | 207 | out = cadd_option(s, c_int(len(s))) |
177 | 198 | 208 | ||
178 | 199 | if out != SPUD_NO_ERROR: | 209 | if out != SPUD_NO_ERROR: |
179 | 200 | raise spud_exceptions[out] | 210 | raise spud_exceptions[out] |
180 | 201 | 211 | ||
181 | 202 | return | 212 | return |
182 | 203 | 213 | ||
185 | 204 | cset_option = libspud.cspud_set_option_ | 214 | cset_option = libspud.spud_set_option |
186 | 205 | cset_option.argtypes = [c_char_p, POINTER(c_int), c_void_p, POINTER(c_int), POINTER(c_int), POINTER(c_int)] | 215 | cset_option.argtypes = [c_char_p, c_int, c_void_p, c_int, c_int, POINTER(c_int)] |
187 | 206 | cset_option.restype = c_int | 216 | cset_option.restype = c_int |
188 | 207 | 217 | ||
189 | 208 | def set_option(s, val): | 218 | def set_option(s, val): |
190 | @@ -246,50 +256,50 @@ | |||
191 | 246 | if py_type is str: | 256 | if py_type is str: |
192 | 247 | shape = shape_type(len(val), -1) | 257 | shape = shape_type(len(val), -1) |
193 | 248 | rank = 1 | 258 | rank = 1 |
195 | 249 | out = cset_option(s, byref(c_int(len(s))), (c_val), byref(c_int(spud_code)), byref(c_int(rank)), shape) | 259 | out = cset_option(s, c_int(len(s)), (c_val), c_int(spud_code), c_int(rank), shape) |
196 | 250 | else: | 260 | else: |
198 | 251 | out = cset_option(s, byref(c_int(len(s))), byref(c_val), byref(c_int(spud_code)), byref(c_int(rank)), shape) | 261 | out = cset_option(s, c_int(len(s)), byref(c_val), c_int(spud_code), c_int(rank), shape) |
199 | 252 | 262 | ||
200 | 253 | if out != SPUD_NO_ERROR: | 263 | if out != SPUD_NO_ERROR: |
201 | 254 | raise spud_exceptions[out] | 264 | raise spud_exceptions[out] |
202 | 255 | 265 | ||
205 | 256 | cset_option_attribute = libspud.cspud_set_option_attribute_ | 266 | cset_option_attribute = libspud.spud_set_option_attribute |
206 | 257 | cset_option_attribute.argtypes = [c_char_p, POINTER(c_int), c_char_p, POINTER(c_int)] | 267 | cset_option_attribute.argtypes = [c_char_p, c_int, c_char_p, c_int] |
207 | 258 | cset_option_attribute.restype = c_int | 268 | cset_option_attribute.restype = c_int |
208 | 259 | 269 | ||
209 | 260 | def set_option_attribute(s, val): | 270 | def set_option_attribute(s, val): |
211 | 261 | out = cset_option_attribute(s, byref(c_int(len(s))), val, byref(c_int(len(val)))) | 271 | out = cset_option_attribute(s, c_int(len(s)), val, c_int(len(val))) |
212 | 262 | 272 | ||
213 | 263 | if out != SPUD_NO_ERROR: | 273 | if out != SPUD_NO_ERROR: |
214 | 264 | raise spud_exceptions[out] | 274 | raise spud_exceptions[out] |
215 | 265 | 275 | ||
216 | 266 | return | 276 | return |
217 | 267 | 277 | ||
220 | 268 | cdelete_option = libspud.cspud_delete_option_ | 278 | cdelete_option = libspud.spud_delete_option |
221 | 269 | cdelete_option.argtypes = [c_char_p, POINTER(c_int)] | 279 | cdelete_option.argtypes = [c_char_p, c_int] |
222 | 270 | cdelete_option.restype = c_int | 280 | cdelete_option.restype = c_int |
223 | 271 | 281 | ||
224 | 272 | def delete_option(s): | 282 | def delete_option(s): |
226 | 273 | out = cdelete_option(s, byref(c_int(len(s)))) | 283 | out = cdelete_option(s, c_int(len(s))) |
227 | 274 | 284 | ||
228 | 275 | if out != SPUD_NO_ERROR: | 285 | if out != SPUD_NO_ERROR: |
229 | 276 | raise spud_exceptions[out] | 286 | raise spud_exceptions[out] |
230 | 277 | 287 | ||
231 | 278 | return | 288 | return |
232 | 279 | 289 | ||
235 | 280 | cmove_option = libspud.cspud_move_option_ | 290 | cmove_option = libspud.spud_move_option |
236 | 281 | cmove_option.argtypes = [c_char_p, POINTER(c_int), c_char_p, POINTER(c_int)] | 291 | cmove_option.argtypes = [c_char_p, c_int, c_char_p, c_int] |
237 | 282 | cmove_option.restype = c_int | 292 | cmove_option.restype = c_int |
238 | 283 | 293 | ||
239 | 284 | def move_option(s1, s2): | 294 | def move_option(s1, s2): |
241 | 285 | out = cmove_option(s1, byref(c_int(len(s1))), s2, byref(c_int(len(s2)))) | 295 | out = cmove_option(s1, c_int(len(s1)), s2, c_int(len(s2))) |
242 | 286 | 296 | ||
243 | 287 | if out != SPUD_NO_ERROR: | 297 | if out != SPUD_NO_ERROR: |
244 | 288 | raise spud_exceptions[out] | 298 | raise spud_exceptions[out] |
245 | 289 | 299 | ||
246 | 290 | return | 300 | return |
247 | 291 | 301 | ||
249 | 292 | cprint_options = libspud.cspud_print_options_ | 302 | cprint_options = libspud.spud_print_options |
250 | 293 | cprint_options.restype = None | 303 | cprint_options.restype = None |
251 | 294 | cprint_options.argtypes = [] | 304 | cprint_options.argtypes = [] |
252 | 295 | 305 | ||
253 | 296 | 306 | ||
254 | === modified file 'python/test_ctypes.py' | |||
255 | --- python/test_ctypes.py 2011-07-06 05:35:39 +0000 | |||
256 | +++ python/test_ctypes.py 2011-07-19 05:52:17 +0000 | |||
257 | @@ -5,7 +5,7 @@ | |||
258 | 5 | 5 | ||
259 | 6 | libspud.print_options() | 6 | libspud.print_options() |
260 | 7 | 7 | ||
262 | 8 | print libspud.number_of_children('/geometry') | 8 | print libspud.get_number_of_children('/geometry') |
263 | 9 | print libspud.get_child_name('geometry', 0) | 9 | print libspud.get_child_name('geometry', 0) |
264 | 10 | 10 | ||
265 | 11 | print libspud.option_count('/problem_type') | 11 | print libspud.option_count('/problem_type') |
Looks good.
I can't believe I didn't request David do it when he proposed merging the recent spud changes ... !