Merge lp:~spud/spud/pythonbindingbug into lp:spud

Proposed by Cian Wilson
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
Reviewer Review Type Date Requested Status
Spud developers Pending
Review via email: mp+68331@code.launchpad.net

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).

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

Looks good.

I can't believe I didn't request David do it when he proposed merging the recent spud changes ... !

Revision history for this message
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?

Revision history for this message
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/fluidity/adjoint/python/libspud.pyc

Yes, I think someone should package the python bindings. There's a nice example in the libadjoint source tree, if anyone cares ;-)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'python/libspud.py.in'
--- python/libspud.py.in 2011-07-06 05:47:12 +0000
+++ python/libspud.py.in 2011-07-19 05:52:17 +0000
@@ -59,106 +59,116 @@
5959
60libspud = cdll.LoadLibrary('@prefix@' + '/lib/libspud.so')60libspud = cdll.LoadLibrary('@prefix@' + '/lib/libspud.so')
6161
62cclear_options = libspud.cspud_clear_options_62cclear_options = libspud.spud_clear_options
63cclear_options.argtypes = []63cclear_options.argtypes = []
64cclear_options.restype = None64cclear_options.restype = None
6565
66def clear_options():66def clear_options():
67 cclear_options()67 cclear_options()
6868
69cload_options = libspud.cspud_load_options_69cload_options = libspud.spud_load_options
70cload_options.argtypes = [c_char_p, POINTER(c_int)]70cload_options.argtypes = [c_char_p, c_int]
71cload_options.restype = None71cload_options.restype = c_int
7272
73def load_options(s):73def load_options(s):
74 cload_options(s, byref(c_int(len(s))))74 out = cload_options(s, c_int(len(s)))
75 if out != SPUD_NO_ERROR:
76 raise spud_exceptions[out]
7577
76cwrite_options = libspud.cspud_write_options_78cwrite_options = libspud.spud_write_options
77cwrite_options.argtypes = [c_char_p, POINTER(c_int)]79cwrite_options.argtypes = [c_char_p, c_int]
78cwrite_options.restype = None80cwrite_options.restype = c_int
7981
80def write_options(filename):82def write_options(filename):
81 cwrite_options(filename, byref(c_int(len(filename))))83 out = cwrite_options(filename, c_int(len(filename)))
84 if out != SPUD_NO_ERROR:
85 raise spud_exceptions[out]
8286
83cget_child_name = libspud.cspud_get_child_name_87cget_child_name = libspud.spud_get_child_name
84cget_child_name.argtypes = [c_char_p, POINTER(c_int), POINTER(c_int), c_char_p, POINTER(c_int)]88cget_child_name.argtypes = [c_char_p, c_int, c_int, c_char_p, c_int]
85cget_child_name.restype = c_int89cget_child_name.restype = c_int
8690
87def get_child_name(s, idx):91def get_child_name(s, idx):
88 val = create_string_buffer(OPTION_PATH_LEN)92 val = create_string_buffer(OPTION_PATH_LEN)
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)))
90 94
91 if out != SPUD_NO_ERROR:95 if out != SPUD_NO_ERROR:
92 raise spud_exceptions[out]96 raise spud_exceptions[out]
93 97
94 return val.value.strip()98 return val.value.strip()
95 99
96cnumber_of_children = libspud.cspud_number_of_children_100cget_number_of_children = libspud.spud_get_number_of_children
97cnumber_of_children.argtypes = [c_char_p, POINTER(c_int)]101cget_number_of_children.argtypes = [c_char_p, c_int, POINTER(c_int)]
98cnumber_of_children.restype = c_int102cget_number_of_children.restype = c_int
99103
100def number_of_children(s):104def get_number_of_children(s):
101 return cnumber_of_children(s, byref(c_int(len(s))))105 val = c_int()
106 out = cget_number_of_children(s, c_int(len(s)), byref(val))
107
108 if out != SPUD_NO_ERROR:
109 raise spud_exceptions[out]
110
111 return val.value
102 112
103coption_count = libspud.cspud_option_count_113coption_count = libspud.spud_option_count
104coption_count.argtypes = [c_char_p, POINTER(c_int)]114coption_count.argtypes = [c_char_p, c_int]
105coption_count.restype = c_int115coption_count.restype = c_int
106116
107def option_count(s):117def option_count(s):
108 return coption_count(s, byref(c_int(len(s))))118 return coption_count(s, c_int(len(s)))
109 119
110chave_option = libspud.cspud_have_option_120chave_option = libspud.spud_have_option
111chave_option.argtypes = [c_char_p, POINTER(c_int)]121chave_option.argtypes = [c_char_p, c_int]
112chave_option.restype = c_int122chave_option.restype = c_int
113123
114def have_option(s):124def have_option(s):
115 out = chave_option(s, byref(c_int(len(s))))125 out = chave_option(s, c_int(len(s)))
116 126
117 if out == 1:127 if out == 1:
118 return True128 return True
119 else:129 else:
120 return False130 return False
121131
122cget_option_type = libspud.cspud_get_option_type_132cget_option_type = libspud.spud_get_option_type
123cget_option_type.argtypes = [c_char_p, POINTER(c_int), POINTER(c_int)]133cget_option_type.argtypes = [c_char_p, c_int, POINTER(c_int)]
124cget_option_type.restype = c_int134cget_option_type.restype = c_int
125135
126def get_option_type(s):136def get_option_type(s):
127 val = c_int()137 val = c_int()
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))
129139
130 if out != SPUD_NO_ERROR:140 if out != SPUD_NO_ERROR:
131 raise spud_exceptions[out]141 raise spud_exceptions[out]
132142
133 return pytype_map[val.value]143 return pytype_map[val.value]
134 144
135cget_option_rank = libspud.cspud_get_option_rank_145cget_option_rank = libspud.spud_get_option_rank
136cget_option_rank.argtypes = [c_char_p, POINTER(c_int), POINTER(c_int)]146cget_option_rank.argtypes = [c_char_p, c_int, POINTER(c_int)]
137cget_option_rank.restype = c_int147cget_option_rank.restype = c_int
138148
139def get_option_rank(s):149def get_option_rank(s):
140 rank = c_int()150 rank = c_int()
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))
142 if out != SPUD_NO_ERROR:152 if out != SPUD_NO_ERROR:
143 raise spud_exceptions[out]153 raise spud_exceptions[out]
144 return rank.value154 return rank.value
145 155
146cget_option_shape = libspud.cspud_get_option_shape_156cget_option_shape = libspud.spud_get_option_shape
147cget_option_shape.argtypes = [c_char_p, POINTER(c_int), POINTER(c_int)]157cget_option_shape.argtypes = [c_char_p, c_int, POINTER(c_int)]
148cget_option_shape.restype = int158cget_option_shape.restype = int
149159
150def get_option_shape(s):160def get_option_shape(s):
151 shape_type = c_int * 2161 shape_type = c_int * 2
152 shape = shape_type()162 shape = shape_type()
153 out = cget_option_shape(s, byref(c_int(len(s))), shape)163 out = cget_option_shape(s, c_int(len(s)), shape)
154164
155 if out != SPUD_NO_ERROR:165 if out != SPUD_NO_ERROR:
156 raise spud_exceptions[out]166 raise spud_exceptions[out]
157167
158 return tuple(shape)168 return tuple(shape)
159169
160cget_option = libspud.cspud_get_option_170cget_option = libspud.spud_get_option
161cget_option.argtypes = [c_char_p, POINTER(c_int), c_void_p]171cget_option.argtypes = [c_char_p, c_int, c_void_p]
162cget_option.restype = c_int172cget_option.restype = c_int
163173
164def get_option(s):174def get_option(s):
@@ -177,7 +187,7 @@
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]
178 val = val_type()188 val = val_type()
179189
180 out = cget_option(s, byref(c_int(len(s))), byref(val))190 out = cget_option(s, c_int(len(s)), byref(val))
181 if out != SPUD_NO_ERROR:191 if out != SPUD_NO_ERROR:
182 raise spud_exceptions[out]192 raise spud_exceptions[out]
183193
@@ -189,20 +199,20 @@
189 val_out = val.value199 val_out = val.value
190 return val_out200 return val_out
191201
192cadd_option = libspud.cspud_add_option_202cadd_option = libspud.spud_add_option
193cadd_option.argtypes = [c_char_p, POINTER(c_int)]203cadd_option.argtypes = [c_char_p, c_int]
194cadd_option.restype = c_int204cadd_option.restype = c_int
195205
196def add_option(s):206def add_option(s):
197 out = cadd_option(s, byref(c_int(len(s))))207 out = cadd_option(s, c_int(len(s)))
198208
199 if out != SPUD_NO_ERROR:209 if out != SPUD_NO_ERROR:
200 raise spud_exceptions[out]210 raise spud_exceptions[out]
201211
202 return212 return
203213
204cset_option = libspud.cspud_set_option_214cset_option = libspud.spud_set_option
205cset_option.argtypes = [c_char_p, POINTER(c_int), c_void_p, POINTER(c_int), POINTER(c_int), POINTER(c_int)]215cset_option.argtypes = [c_char_p, c_int, c_void_p, c_int, c_int, POINTER(c_int)]
206cset_option.restype = c_int216cset_option.restype = c_int
207217
208def set_option(s, val):218def set_option(s, val):
@@ -246,50 +256,50 @@
246 if py_type is str:256 if py_type is str:
247 shape = shape_type(len(val), -1)257 shape = shape_type(len(val), -1)
248 rank = 1258 rank = 1
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)
250 else:260 else:
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)
252262
253 if out != SPUD_NO_ERROR:263 if out != SPUD_NO_ERROR:
254 raise spud_exceptions[out]264 raise spud_exceptions[out]
255 265
256cset_option_attribute = libspud.cspud_set_option_attribute_266cset_option_attribute = libspud.spud_set_option_attribute
257cset_option_attribute.argtypes = [c_char_p, POINTER(c_int), c_char_p, POINTER(c_int)]267cset_option_attribute.argtypes = [c_char_p, c_int, c_char_p, c_int]
258cset_option_attribute.restype = c_int268cset_option_attribute.restype = c_int
259269
260def set_option_attribute(s, val):270def set_option_attribute(s, val):
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)))
262 272
263 if out != SPUD_NO_ERROR:273 if out != SPUD_NO_ERROR:
264 raise spud_exceptions[out]274 raise spud_exceptions[out]
265275
266 return276 return
267 277
268cdelete_option = libspud.cspud_delete_option_278cdelete_option = libspud.spud_delete_option
269cdelete_option.argtypes = [c_char_p, POINTER(c_int)]279cdelete_option.argtypes = [c_char_p, c_int]
270cdelete_option.restype = c_int280cdelete_option.restype = c_int
271281
272def delete_option(s):282def delete_option(s):
273 out = cdelete_option(s, byref(c_int(len(s))))283 out = cdelete_option(s, c_int(len(s)))
274 284
275 if out != SPUD_NO_ERROR:285 if out != SPUD_NO_ERROR:
276 raise spud_exceptions[out]286 raise spud_exceptions[out]
277287
278 return288 return
279289
280cmove_option = libspud.cspud_move_option_290cmove_option = libspud.spud_move_option
281cmove_option.argtypes = [c_char_p, POINTER(c_int), c_char_p, POINTER(c_int)]291cmove_option.argtypes = [c_char_p, c_int, c_char_p, c_int]
282cmove_option.restype = c_int292cmove_option.restype = c_int
283293
284def move_option(s1, s2):294def move_option(s1, s2):
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)))
286 296
287 if out != SPUD_NO_ERROR:297 if out != SPUD_NO_ERROR:
288 raise spud_exceptions[out]298 raise spud_exceptions[out]
289299
290 return300 return
291301
292cprint_options = libspud.cspud_print_options_302cprint_options = libspud.spud_print_options
293cprint_options.restype = None303cprint_options.restype = None
294cprint_options.argtypes = []304cprint_options.argtypes = []
295305
296306
=== modified file 'python/test_ctypes.py'
--- python/test_ctypes.py 2011-07-06 05:35:39 +0000
+++ python/test_ctypes.py 2011-07-19 05:52:17 +0000
@@ -5,7 +5,7 @@
55
6libspud.print_options()6libspud.print_options()
77
8print libspud.number_of_children('/geometry')8print libspud.get_number_of_children('/geometry')
9print libspud.get_child_name('geometry', 0)9print libspud.get_child_name('geometry', 0)
1010
11print libspud.option_count('/problem_type')11print libspud.option_count('/problem_type')

Subscribers

People subscribed via source and target branches