Removed
Link Here
|
1 |
Author: Alastair McKinstry <mckinstry@debian.org> |
2 |
Description: Changes needed for Python3 support. |
3 |
TODO: Changes to the C interface still needed. |
4 |
Last-Upated: 2014-05-02 |
5 |
Forwarded: no |
6 |
|
7 |
Index: tests/test_error.py |
8 |
=================================================================== |
9 |
--- tests/test_error.py |
10 |
+++ tests/test_error.py |
11 |
@@ -47,6 +47,7 @@ |
12 |
# reflect those of the United States Government or Lawrence Livermore |
13 |
# National Security, LLC, and shall not be used for advertising or |
14 |
# product endorsement purposes. |
15 |
+from __future__ import print_function |
16 |
import os |
17 |
import Silo |
18 |
|
19 |
@@ -56,33 +57,33 @@ elif os.access("multi_ucd3d.h5",os.R_OK) |
20 |
file = "multi_ucd3d.h5" |
21 |
|
22 |
db = Silo.Open(file) |
23 |
-print "db = ",db |
24 |
+print( "db = ",db) |
25 |
|
26 |
try: |
27 |
- print "Trying to set a bad directory" |
28 |
+ print ("Trying to set a bad directory") |
29 |
db.SetDir("Magrathea") |
30 |
except: |
31 |
- print " -- Caught error" |
32 |
+ print (" -- Caught error") |
33 |
|
34 |
try: |
35 |
- print "Trying to read a bad variable" |
36 |
+ print ("Trying to read a bad variable") |
37 |
db.GetVar("Arkleseizure") |
38 |
except: |
39 |
- print " -- Caught error" |
40 |
+ print (" -- Caught error") |
41 |
|
42 |
-print "Closing...." |
43 |
+print ("Closing....") |
44 |
db.Close() |
45 |
|
46 |
-print "db = ",db |
47 |
+print ("db = ",db) |
48 |
|
49 |
try: |
50 |
- print "Trying to set a directory on a closed file" |
51 |
+ print ("Trying to set a directory on a closed file") |
52 |
db.SetDir("domain0") |
53 |
except: |
54 |
- print " -- Caught error" |
55 |
+ print (" -- Caught error") |
56 |
|
57 |
try: |
58 |
- print "Trying to read a variable on a closed file" |
59 |
+ print ("Trying to read a variable on a closed file") |
60 |
db.SetDir("dtime") |
61 |
except: |
62 |
- print " -- Caught error" |
63 |
+ print (" -- Caught error") |
64 |
Index: tests/test_read.py |
65 |
=================================================================== |
66 |
--- tests/test_read.py |
67 |
+++ tests/test_read.py |
68 |
@@ -47,6 +47,7 @@ |
69 |
# reflect those of the United States Government or Lawrence Livermore |
70 |
# National Security, LLC, and shall not be used for advertising or |
71 |
# product endorsement purposes. |
72 |
+from __future__ import print_function |
73 |
import os |
74 |
import Silo |
75 |
|
76 |
@@ -57,31 +58,31 @@ elif os.access("multi_ucd3d.h5",os.R_OK) |
77 |
file = "multi_ucd3d.h5" |
78 |
|
79 |
db = Silo.Open(file) |
80 |
-print "db = ",db |
81 |
-print "db.filename = '%s'"%db.filename |
82 |
+print ("db = ",db) |
83 |
+print ("db.filename = '%s'"%db.filename) |
84 |
|
85 |
toc = db.GetToc() |
86 |
-print "\n-- TOC --\n",toc |
87 |
+print ("\n-- TOC --\n",toc) |
88 |
|
89 |
-print "cycle='%d'"%db.GetVar("cycle") |
90 |
-print "dtime='%f'"%db.GetVar("dtime") |
91 |
-print "_fileinfo='%s'"%db.GetVar("_fileinfo") |
92 |
-print "d_vartypes=",db.GetVar("d_vartypes") |
93 |
-print "d_varnames=",db.GetVar("d_varnames") |
94 |
+print ("cycle='%d'"%db.GetVar("cycle")) |
95 |
+print ("dtime='%f'"%db.GetVar("dtime")) |
96 |
+print ("_fileinfo='%s'"%db.GetVar("_fileinfo")) |
97 |
+print ("d_vartypes=",db.GetVar("d_vartypes")) |
98 |
+print ("d_varnames=",db.GetVar("d_varnames")) |
99 |
|
100 |
db.SetDir("block0") |
101 |
toc = db.GetToc() |
102 |
-print "mesh1_coord0=",db.GetVar("mesh1_coord0")[1:10] |
103 |
-print "v_data=",db.GetVar("v_data")[1:10] |
104 |
+print ("mesh1_coord0=",db.GetVar("mesh1_coord0")[1:10]) |
105 |
+print ("v_data=",db.GetVar("v_data")[1:10]) |
106 |
|
107 |
db.SetDir("../block7") |
108 |
toc = db.GetToc() |
109 |
-print "mesh1_coord0=",db.GetVar("mesh1_coord0")[1:10] |
110 |
-print "v_data=",db.GetVar("v_data")[1:10] |
111 |
+print ("mesh1_coord0=",db.GetVar("mesh1_coord0")[1:10]) |
112 |
+print ("v_data=",db.GetVar("v_data")[1:10]) |
113 |
|
114 |
db.Close() |
115 |
|
116 |
-print "\n" |
117 |
+print ("\n") |
118 |
|
119 |
# ---- file 2 ---- |
120 |
if os.access("multi_rect3d.pdb",os.R_OK): |
121 |
@@ -90,23 +91,23 @@ elif os.access("multi_rect3d.h5",os.R_OK |
122 |
file = "multi_rect3d.h5" |
123 |
|
124 |
db = Silo.Open(file) |
125 |
-print "db = ",db |
126 |
+print ("db = ",db) |
127 |
|
128 |
toc = db.GetToc() |
129 |
-print "\n-- TOC --\n",toc |
130 |
+print ("i\n-- TOC --\n",toc) |
131 |
|
132 |
-print "cycle='%d'"%db.GetVar("cycle") |
133 |
-print "time='%f'"%db.GetVar("time") |
134 |
-print "_fileinfo='%s'"%db.GetVar("_fileinfo") |
135 |
-print "defvars_defns='%s'"%db.GetVar("defvars_defns") |
136 |
+print ("cycle='%d'"%db.GetVar("cycle")) |
137 |
+print ("time='%f'"%db.GetVar("time")) |
138 |
+print ("_fileinfo='%s'"%db.GetVar("_fileinfo")) |
139 |
+print ("defvars_defns='%s'"%db.GetVar("defvars_defns")) |
140 |
|
141 |
db.SetDir("block0") |
142 |
toc = db.GetToc() |
143 |
-print "mesh1_coord0=",db.GetVar("mesh1_coord0") |
144 |
+print ("mesh1_coord0=",db.GetVar("mesh1_coord0")) |
145 |
|
146 |
db.SetDir("/block7") |
147 |
toc = db.GetToc() |
148 |
-print "mesh1_coord0=",db.GetVar("mesh1_coord0") |
149 |
+print ("mesh1_coord0=",db.GetVar("mesh1_coord0")) |
150 |
|
151 |
db.SetDir("/") |
152 |
toc = db.GetToc() |
153 |
Index: tests/test_write.py |
154 |
=================================================================== |
155 |
--- tests/test_write.py |
156 |
+++ tests/test_write.py |
157 |
@@ -47,6 +47,7 @@ |
158 |
# reflect those of the United States Government or Lawrence Livermore |
159 |
# National Security, LLC, and shall not be used for advertising or |
160 |
# product endorsement purposes. |
161 |
+from __future__ import silo |
162 |
import Silo |
163 |
|
164 |
db = Silo.Create("foo.silo", "test file") |
165 |
Index: tools/python/pydbtoc.cpp |
166 |
=================================================================== |
167 |
--- tools/python/pydbtoc.cpp |
168 |
+++ tools/python/pydbtoc.cpp |
169 |
@@ -48,6 +48,10 @@ |
170 |
// National Security, LLC, and shall not be used for advertising or |
171 |
// product endorsement purposes. |
172 |
|
173 |
+// #if PY_MAJOR_VERSION >= 3 |
174 |
+#define PyString_FromString(x) PyUnicode_FromString(x) |
175 |
+// #endif |
176 |
+ |
177 |
#include "pydbtoc.h" |
178 |
|
179 |
// **************************************************************************** |
180 |
@@ -200,7 +204,7 @@ static int DBtoc_print(PyObject *self, F |
181 |
static PyObject *DBtoc_GetN ## nm(PyObject *self, PyObject *args) \ |
182 |
{ \ |
183 |
DBtoc *toc = ((DBtocObject*)self)->toc; \ |
184 |
- PyObject *retval = PyInt_FromLong(toc->n ## nm); \ |
185 |
+ PyObject *retval = PyLong_FromLong(toc->n ## nm); \ |
186 |
return retval; \ |
187 |
} \ |
188 |
static PyObject *DBtoc_Get ## nm ## names(PyObject *self, PyObject *args) \ |
189 |
@@ -341,46 +345,60 @@ PyTypeObject DBtocType = |
190 |
// Type header |
191 |
// |
192 |
PyObject_HEAD_INIT(&PyType_Type) |
193 |
- 0, // ob_size |
194 |
- "DBtoc", // tp_name |
195 |
- sizeof(DBtocObject), // tp_basicsize |
196 |
- 0, // tp_itemsize |
197 |
+#if PY_MAJOR_VERSION < 3 |
198 |
+ ob_size : 0, |
199 |
+#endif |
200 |
+ tp_name : "DBtoc", |
201 |
+ tp_basicsize : sizeof(DBtocObject), |
202 |
+ tp_itemsize : 0, |
203 |
// |
204 |
// Standard methods |
205 |
// |
206 |
- (destructor)DBtoc_dealloc, // tp_dealloc |
207 |
- (printfunc)DBtoc_print, // tp_print |
208 |
- (getattrfunc)DBtoc_getattr, // tp_getattr |
209 |
- 0,//(setattrfunc)DBtoc_setattr, // tp_setattr -- this object is read-only |
210 |
- (cmpfunc)DBtoc_compare, // tp_compare |
211 |
- (reprfunc)0, // tp_repr |
212 |
+ tp_dealloc : (destructor)DBtoc_dealloc, |
213 |
+ tp_print : (printfunc)DBtoc_print, |
214 |
+#if PY_MAJOR_VERSION >= 3 |
215 |
+ tp_getattr : 0, |
216 |
+#else |
217 |
+ tp_getattr : (getattrfunc)DBtoc_getattr, |
218 |
+#endif |
219 |
+ tp_setattr : 0, // object is read-only |
220 |
+#if PY_MAJOR_VERSION >= 3 |
221 |
+ tp_as_async : (PyAsyncMethods*) NULL, |
222 |
+#else |
223 |
+ tp_compare : (cmpfunc)DBtoc_compare, |
224 |
+#endif |
225 |
+ tp_repr : (reprfunc)0, |
226 |
// |
227 |
// Type categories |
228 |
// |
229 |
- 0, // tp_as_number |
230 |
- 0, // tp_as_sequence |
231 |
- 0, // tp_as_mapping |
232 |
+ tp_as_number : 0, |
233 |
+ tp_as_sequence : 0, |
234 |
+ tp_as_mapping : 0, |
235 |
// |
236 |
// More methods |
237 |
// |
238 |
- 0, // tp_hash |
239 |
- 0, // tp_call |
240 |
- (reprfunc)DBtoc_str, // tp_str |
241 |
- 0, // tp_getattro |
242 |
- 0, // tp_setattro |
243 |
- 0, // tp_as_buffer |
244 |
- Py_TPFLAGS_CHECKTYPES, // tp_flags |
245 |
- "This class wraps a Silo DBtoc object.", // tp_doc |
246 |
- 0, // tp_traverse |
247 |
- 0, // tp_clear |
248 |
- 0, // tp_richcompare |
249 |
- 0 // tp_weaklistoffset |
250 |
+ |
251 |
+ tp_hash : 0, |
252 |
+ tp_call : 0, |
253 |
+ tp_str : (reprfunc)DBtoc_str, |
254 |
+ tp_getattro : 0, |
255 |
+ tp_setattro : 0, |
256 |
+ tp_as_buffer : 0, |
257 |
+#if PY_MAJOR_VERSION < 3 |
258 |
+ tp_flags : Py_TPFLAGS_CHECKTYPES, |
259 |
+#else |
260 |
+ tp_flags : Py_TPFLAGS_DEFAULT, |
261 |
+#endif |
262 |
+ tp_doc : "This class wraps a Silo DBtoc object.", |
263 |
+ tp_traverse : 0, |
264 |
+ tp_clear : 0, |
265 |
+ tp_richcompare : 0, |
266 |
+ tp_weaklistoffset : 0, |
267 |
// PYTHON 2.2 FROM HERE |
268 |
- , |
269 |
0, |
270 |
0, |
271 |
0, |
272 |
- |
273 |
+ 0, |
274 |
}; |
275 |
|
276 |
// **************************************************************************** |
277 |
Index: tools/python/pydbfile.cpp |
278 |
=================================================================== |
279 |
--- tools/python/pydbfile.cpp |
280 |
+++ tools/python/pydbfile.cpp |
281 |
@@ -56,6 +56,19 @@ |
282 |
|
283 |
using std::string; |
284 |
|
285 |
+#if PY_MAJOR_VERSION >= 3 |
286 |
+#define PyInt_FromLong(x) (PyLong_FromLong(x)) |
287 |
+#define PyInt_Check(x) (PyLong_Check(x)) |
288 |
+#define PyString_Check(x) PyUnicode_Check(x) |
289 |
+#define PyInt_AS_LONG(x) PyLong_AsLong(x) |
290 |
+#define PyString_FromStringAndSize(x,y) PyUnicode_FromStringAndSize(x,y) |
291 |
+#define PyString_FromString(x) PyUnicode_FromString(x) |
292 |
+#define PyString_AsString(x) PyUnicode_AsUTF8(x) |
293 |
+ |
294 |
+#else |
295 |
+#define Py_RETURN_NOTIMPLEMENTED return NULL |
296 |
+#endif |
297 |
+ |
298 |
// **************************************************************************** |
299 |
// Method: DBfile_DBGetToc |
300 |
// |
301 |
@@ -75,8 +88,8 @@ static PyObject *DBfile_DBGetToc(PyObjec |
302 |
|
303 |
if (!obj->db) |
304 |
{ |
305 |
- SiloErrorFunc("This file has been closed."); |
306 |
- return NULL; |
307 |
+ SiloErrorFunc(self, "This file has been closed."); |
308 |
+ return NULL; |
309 |
} |
310 |
|
311 |
DBtoc *toc = DBGetToc(obj->db); |
312 |
@@ -122,7 +135,7 @@ static PyObject *DBfile_DBGetVar(PyObjec |
313 |
|
314 |
if (!db) |
315 |
{ |
316 |
- SiloErrorFunc("This file has been closed."); |
317 |
+ SiloErrorFunc(self, "This file has been closed."); |
318 |
return NULL; |
319 |
} |
320 |
|
321 |
@@ -162,7 +175,7 @@ static PyObject *DBfile_DBGetVar(PyObjec |
322 |
return PyString_FromStringAndSize((char*)var, len); |
323 |
} |
324 |
default: |
325 |
- SiloErrorFunc("Unknown variable type."); |
326 |
+ SiloErrorFunc(self, "Unknown variable type."); |
327 |
return NULL; |
328 |
} |
329 |
} |
330 |
@@ -193,7 +206,7 @@ static PyObject *DBfile_DBGetVar(PyObjec |
331 |
tmp = PyInt_FromLong(((char*)var)[i]); |
332 |
break; |
333 |
default: |
334 |
- SiloErrorFunc("Unknown variable type."); |
335 |
+ SiloErrorFunc(self, "Unknown variable type."); |
336 |
return NULL; |
337 |
} |
338 |
PyTuple_SET_ITEM(retval, i, tmp); |
339 |
@@ -220,7 +233,7 @@ static PyObject *DBfile_DBGetVarInfo(PyO |
340 |
|
341 |
if (!db) |
342 |
{ |
343 |
- SiloErrorFunc("This file has been closed."); |
344 |
+ SiloErrorFunc(self, "This file has been closed."); |
345 |
return NULL; |
346 |
} |
347 |
|
348 |
@@ -247,7 +260,7 @@ static PyObject *DBfile_DBGetVarInfo(PyO |
349 |
{ |
350 |
char msg[256]; |
351 |
snprintf(msg, sizeof(msg), "Unable to get object \"%s\"", str); |
352 |
- SiloErrorFunc(msg); |
353 |
+ SiloErrorFunc(self, msg); |
354 |
return NULL; |
355 |
} |
356 |
|
357 |
@@ -263,7 +276,7 @@ static PyObject *DBfile_DBGetVarInfo(PyO |
358 |
{ |
359 |
char msg[256]; |
360 |
snprintf(msg, sizeof(msg), "Unable to get component \"%s\" for object \%s\"", compname.c_str(), str); |
361 |
- SiloErrorFunc(msg); |
362 |
+ SiloErrorFunc(self, msg); |
363 |
continue; |
364 |
} |
365 |
int type = DBGetComponentType(db, str, compname.c_str()); |
366 |
@@ -371,7 +384,7 @@ static PyObject *DBfile_DBWrite(PyObject |
367 |
|
368 |
if (!db) |
369 |
{ |
370 |
- SiloErrorFunc("This file has been closed."); |
371 |
+ SiloErrorFunc(self, "This file has been closed."); |
372 |
return NULL; |
373 |
} |
374 |
|
375 |
@@ -495,8 +508,8 @@ static PyObject *DBfile_DBWriteObject(Py |
376 |
|
377 |
if (!db) |
378 |
{ |
379 |
- SiloErrorFunc("This file has been closed."); |
380 |
- return NULL; |
381 |
+ SiloErrorFunc(self, "This file has been closed."); |
382 |
+ return NULL; |
383 |
} |
384 |
|
385 |
char *objname; |
386 |
@@ -591,7 +604,7 @@ static PyObject *DBfile_DBMkDir(PyObject |
387 |
|
388 |
if (!db) |
389 |
{ |
390 |
- SiloErrorFunc("This file has been closed."); |
391 |
+ SiloErrorFunc(self, "This file has been closed."); |
392 |
return NULL; |
393 |
} |
394 |
|
395 |
@@ -601,7 +614,7 @@ static PyObject *DBfile_DBMkDir(PyObject |
396 |
|
397 |
if (DBMkDir(db, str)) |
398 |
{ |
399 |
- SiloErrorFunc("Could not make the directory."); |
400 |
+ SiloErrorFunc(self, "Could not make the directory."); |
401 |
return NULL; |
402 |
} |
403 |
else |
404 |
@@ -630,7 +643,7 @@ static PyObject *DBfile_DBSetDir(PyObjec |
405 |
|
406 |
if (!db) |
407 |
{ |
408 |
- SiloErrorFunc("This file has been closed."); |
409 |
+ SiloErrorFunc(self, "This file has been closed."); |
410 |
return NULL; |
411 |
} |
412 |
|
413 |
@@ -640,7 +653,7 @@ static PyObject *DBfile_DBSetDir(PyObjec |
414 |
|
415 |
if (DBSetDir(db, str)) |
416 |
{ |
417 |
- SiloErrorFunc("Could not change directories."); |
418 |
+ SiloErrorFunc(self, "Could not change directories."); |
419 |
return NULL; |
420 |
} |
421 |
else |
422 |
@@ -669,7 +682,7 @@ static PyObject *DBfile_DBClose(PyObject |
423 |
|
424 |
if (!db) |
425 |
{ |
426 |
- SiloErrorFunc("This file has been closed."); |
427 |
+ SiloErrorFunc(self, "This file has been closed."); |
428 |
return NULL; |
429 |
} |
430 |
|
431 |
@@ -678,7 +691,7 @@ static PyObject *DBfile_DBClose(PyObject |
432 |
|
433 |
if (DBClose(db)) |
434 |
{ |
435 |
- SiloErrorFunc("Could not close the file."); |
436 |
+ SiloErrorFunc(self, "Could not close the file."); |
437 |
return NULL; |
438 |
} |
439 |
else |
440 |
@@ -804,13 +817,15 @@ static int DBfile_print(PyObject *self, |
441 |
// Creation: July 12, 2005 |
442 |
// |
443 |
// **************************************************************************** |
444 |
+ |
445 |
+#if PY_MAJOR_VERSION < 3 |
446 |
static PyObject *DBfile_getattr(PyObject *self, char *name) |
447 |
{ |
448 |
DBfileObject *obj = (DBfileObject*)self; |
449 |
|
450 |
if (!obj->db) |
451 |
{ |
452 |
- SiloErrorFunc("This file has been closed."); |
453 |
+ SiloErrorFunc(self, "This file has been closed."); |
454 |
return NULL; |
455 |
} |
456 |
|
457 |
@@ -828,6 +843,7 @@ static PyObject *DBfile_getattr(PyObject |
458 |
|
459 |
return Py_FindMethod(DBfile_methods, self, name); |
460 |
} |
461 |
+#endif |
462 |
|
463 |
// **************************************************************************** |
464 |
// Method: DBfile_compare |
465 |
@@ -849,6 +865,17 @@ static int DBfile_compare(PyObject *v, P |
466 |
return (a<b) ? -1 : ((a==b) ? 0 : +1); |
467 |
} |
468 |
|
469 |
+// TODO Check this |
470 |
+static PyObject* DBfile_richcompare(PyObject *v, PyObject *w, int op) |
471 |
+{ |
472 |
+ switch(op){ |
473 |
+ case Py_EQ:{ |
474 |
+ if(v == w) Py_RETURN_TRUE; |
475 |
+ Py_RETURN_FALSE; |
476 |
+ } |
477 |
+ default: Py_RETURN_NOTIMPLEMENTED; |
478 |
+ } |
479 |
+} |
480 |
|
481 |
// **************************************************************************** |
482 |
// DBfile Python Type Object |
483 |
@@ -859,44 +886,58 @@ static int DBfile_compare(PyObject *v, P |
484 |
// **************************************************************************** |
485 |
PyTypeObject DBfileType = |
486 |
{ |
487 |
- // |
488 |
- // Type header |
489 |
- // |
490 |
- PyObject_HEAD_INIT(&PyType_Type) |
491 |
- 0, // ob_size |
492 |
- "DBfile", // tp_name |
493 |
- sizeof(DBfileObject), // tp_basicsize |
494 |
- 0, // tp_itemsize |
495 |
- // |
496 |
- // Standard methods |
497 |
- // |
498 |
- (destructor)DBfile_dealloc, // tp_dealloc |
499 |
- (printfunc)DBfile_print, // tp_print |
500 |
- (getattrfunc)DBfile_getattr, // tp_getattr |
501 |
- 0,//(setattrfunc)DBfile_setattr, // tp_setattr -- this object is read-only |
502 |
- (cmpfunc)DBfile_compare, // tp_compare |
503 |
- (reprfunc)0, // tp_repr |
504 |
- // |
505 |
- // Type categories |
506 |
- // |
507 |
- 0, // tp_as_number |
508 |
- 0, // tp_as_sequence |
509 |
- 0, // tp_as_mapping |
510 |
+ // |
511 |
+ // Type header |
512 |
+ // |
513 |
+ PyObject_HEAD_INIT(&PyType_Type) |
514 |
+#if PY_MAJOR_VERSION < 3 |
515 |
+ ob_size : 0, |
516 |
+#endif |
517 |
+ tp_name : "DBfil", |
518 |
+ tp_basicsize : sizeof(DBfileObject), |
519 |
+ tp_itemsize : 0, |
520 |
+ // |
521 |
+ // Standard methods |
522 |
+ // |
523 |
+ tp_dealloc : (destructor)DBfile_dealloc, |
524 |
+ tp_print : (printfunc)DBfile_print, |
525 |
+#if PY_MAJOR_VERSION >= 3 |
526 |
+ tp_getattr : 0, |
527 |
+#else |
528 |
+ tp_getattr : (getattrfunc)DBfile_getattr, |
529 |
+#endif |
530 |
+ tp_setattr : 0, // object is read-only |
531 |
+#if PY_MAJOR_VERSION >= 3 |
532 |
+ tp_as_async : (PyAsyncMethods*) NULL, |
533 |
+#else |
534 |
+ tp_compare : (cmpfunc)DBfile_compare, |
535 |
+#endif |
536 |
+ tp_repr : (reprfunc)0, |
537 |
+ // |
538 |
+ // Type categories |
539 |
+ // |
540 |
+ tp_as_number : 0, |
541 |
+ tp_as_sequence : 0, |
542 |
+ tp_as_mapping : 0, |
543 |
// |
544 |
// More methods |
545 |
// |
546 |
- 0, // tp_hash |
547 |
- 0, // tp_call |
548 |
- (reprfunc)DBfile_str, // tp_str |
549 |
- 0, // tp_getattro |
550 |
- 0, // tp_setattro |
551 |
- 0, // tp_as_buffer |
552 |
- Py_TPFLAGS_CHECKTYPES, // tp_flags |
553 |
- "This class wraps a Silo DBfile object.", // tp_doc |
554 |
- 0, // tp_traverse |
555 |
- 0, // tp_clear |
556 |
- 0, // tp_richcompare |
557 |
- 0 // tp_weaklistoffset |
558 |
+ tp_hash : 0, |
559 |
+ tp_call : 0, |
560 |
+ tp_str : (reprfunc)DBfile_str, |
561 |
+ tp_getattro : 0, |
562 |
+ tp_setattro : 0, |
563 |
+ tp_as_buffer : 0, |
564 |
+#if PY_MAJOR_VERSION >= 3 |
565 |
+ tp_flags : Py_TPFLAGS_DEFAULT, |
566 |
+#else |
567 |
+ tp_flags : Py_TPFLAGS_CHECKTYPES, |
568 |
+#endif |
569 |
+ tp_doc : "This class wraps a Silo DBfile object.", |
570 |
+ tp_traverse : 0, |
571 |
+ tp_clear : 0, |
572 |
+ tp_richcompare : (richcmpfunc)DBfile_richcompare, |
573 |
+ tp_weaklistoffset : 0, |
574 |
}; |
575 |
|
576 |
// **************************************************************************** |
577 |
Index: tools/python/pysilo.h |
578 |
=================================================================== |
579 |
--- tools/python/pysilo.h |
580 |
+++ tools/python/pysilo.h |
581 |
@@ -65,6 +65,6 @@ |
582 |
((PY_MAJOR_VERSION==Maj) && (PY_MINOR_VERSION>Min)) || \ |
583 |
(PY_MAJOR_VERSION>Maj)) |
584 |
|
585 |
-void SiloErrorFunc(const char *errString); |
586 |
+void SiloErrorFunc(PyObject *self, const char *errString); |
587 |
|
588 |
#endif |
589 |
Index: tools/python/pysilo.cpp |
590 |
=================================================================== |
591 |
--- tools/python/pysilo.cpp |
592 |
+++ tools/python/pysilo.cpp |
593 |
@@ -57,14 +57,16 @@ |
594 |
#include <iostream> |
595 |
using namespace std; |
596 |
|
597 |
-static PyObject *SiloError; |
598 |
-static PyObject *siloModule = 0; |
599 |
- |
600 |
-// |
601 |
-// Method table |
602 |
-// |
603 |
- |
604 |
-std::vector<PyMethodDef> SiloMethods; |
605 |
+struct silo_state { |
606 |
+ PyObject *error; |
607 |
+}; |
608 |
+ |
609 |
+#if PY_MAJOR_VERSION >= 3 |
610 |
+#define GETSTATE(m) ((struct silo_state*)PyModule_GetState(m)) |
611 |
+#else |
612 |
+#define GETSTATE(m) (&_state) |
613 |
+static struct silo_state _state; |
614 |
+#endif |
615 |
|
616 |
|
617 |
// **************************************************************************** |
618 |
@@ -85,38 +87,10 @@ std::vector<PyMethodDef> SiloMethods; |
619 |
// **************************************************************************** |
620 |
|
621 |
void |
622 |
-SiloErrorFunc(const char *errString) |
623 |
-{ |
624 |
- PyErr_SetString(SiloError, errString); |
625 |
-} |
626 |
- |
627 |
-// **************************************************************************** |
628 |
-// Function: AddMethod |
629 |
-// |
630 |
-// Purpose: |
631 |
-// This function adds a method to the Silo module's Python method table. |
632 |
-// |
633 |
-// Arguments: |
634 |
-// methodName : The name of the method. |
635 |
-// cb : The Python callback function. |
636 |
-// doc : The documentation string for the method. |
637 |
-// |
638 |
-// Programmer: Brad Whitlock |
639 |
-// Creation: Tue Sep 4 15:36:47 PST 2001 |
640 |
-// |
641 |
-// Modifications: |
642 |
-// |
643 |
-// **************************************************************************** |
644 |
-static void |
645 |
-AddMethod(const char *methodName, PyObject *(cb)(PyObject *, PyObject *), |
646 |
- const char *doc = NULL) |
647 |
+SiloErrorFunc(PyObject *self, const char *errString) |
648 |
{ |
649 |
- PyMethodDef newMethod; |
650 |
- newMethod.ml_name = (char *)methodName; |
651 |
- newMethod.ml_meth = cb; |
652 |
- newMethod.ml_flags = METH_VARARGS; |
653 |
- newMethod.ml_doc = (char *)doc; |
654 |
- SiloMethods.push_back(newMethod); |
655 |
+ struct silo_state *st = GETSTATE(self); |
656 |
+ PyErr_SetString(st->error, errString); |
657 |
} |
658 |
|
659 |
|
660 |
@@ -208,6 +182,47 @@ PyObject *silo_Create(PyObject *self, Py |
661 |
return DBfile_NEW(db); |
662 |
} |
663 |
|
664 |
+static PyMethodDef silo_methods[] = { |
665 |
+ {"Open", silo_Open, METH_VARARGS, NULL}, |
666 |
+ {"Create", silo_Create, METH_VARARGS, NULL }, |
667 |
+ {NULL, NULL} |
668 |
+}; |
669 |
+ |
670 |
+ |
671 |
+#if PY_MAJOR_VERSION >= 3 |
672 |
+ |
673 |
+static int silo_Traverse(PyObject *m, visitproc visit, void *arg) { |
674 |
+ Py_VISIT(GETSTATE(m)->error); |
675 |
+ return 0; |
676 |
+} |
677 |
+ |
678 |
+static int silo_Clear(PyObject *m) { |
679 |
+ Py_CLEAR(GETSTATE(m)->error); |
680 |
+ return 0; |
681 |
+} |
682 |
+ |
683 |
+static struct PyModuleDef moduledef = { |
684 |
+ m_base : PyModuleDef_HEAD_INIT, |
685 |
+ m_name : "Silo", |
686 |
+ m_doc : NULL, |
687 |
+ m_size : sizeof(struct silo_state), |
688 |
+ m_methods : silo_methods, |
689 |
+ m_slots : NULL, |
690 |
+ m_traverse : silo_Traverse, |
691 |
+ m_clear : silo_Clear, |
692 |
+ m_free : NULL |
693 |
+}; |
694 |
+ |
695 |
+#define ADD_CONSTANT(C) PyDict_SetItemString(d, #C, PyLong_FromLong(C)) |
696 |
+#define INITERROR return NULL |
697 |
+ |
698 |
+#else // Python2.* |
699 |
+ |
700 |
+#define ADD_CONSTANT(C) PyDict_SetItemString(d, #C, PyInt_FromLong(C)) |
701 |
+#define INITERROR return |
702 |
+ |
703 |
+#endif |
704 |
+ |
705 |
// **************************************************************************** |
706 |
// Method: initSilo |
707 |
// |
708 |
@@ -226,23 +241,39 @@ PyObject *silo_Create(PyObject *self, Py |
709 |
// contents returned by GetVarInfo method. |
710 |
// |
711 |
// **************************************************************************** |
712 |
-#define ADD_CONSTANT(C) PyDict_SetItemString(d, #C, PyInt_FromLong(C)) |
713 |
+ |
714 |
+ |
715 |
+ |
716 |
+#if PY_MAJOR_VERSION >= 3 |
717 |
+PyMODINIT_FUNC PyInit_Silo(void) |
718 |
+#else |
719 |
extern "C" void initSilo() |
720 |
+#endif |
721 |
{ |
722 |
- AddMethod("Open", silo_Open, |
723 |
- "Usage: Open(filename [, DB_READ|DB_APPEND]])"); |
724 |
- AddMethod("Create", silo_Create, |
725 |
- "Usage: Create(filename , info [, DB_PDB|DB_HDF5 [, DB_CLOBBER|DB_NOCLOBBER]])"); |
726 |
- AddMethod(NULL, NULL); |
727 |
|
728 |
|
729 |
- siloModule = Py_InitModule("Silo", &SiloMethods[0]); |
730 |
- |
731 |
- PyObject *d; |
732 |
- d = PyModule_GetDict(siloModule); |
733 |
- SiloError = PyErr_NewException("Silo.SiloException", NULL, NULL); |
734 |
- PyDict_SetItemString(d, "SiloException", SiloError); |
735 |
|
736 |
+ struct silo_state *st; |
737 |
+ PyObject *d; |
738 |
+ |
739 |
+#if PY_MAJOR_VERSION >= 3 |
740 |
+ PyObject *module = PyModule_Create(&moduledef); |
741 |
+#else |
742 |
+ PyObject *module = Py_InitModule("Silo", silo_methods); |
743 |
+#endif |
744 |
+ |
745 |
+ if (module == NULL) |
746 |
+ INITERROR; |
747 |
+ |
748 |
+ st = GETSTATE(module); |
749 |
+ st->error = PyErr_NewException("Silo.SiloException", NULL, NULL); |
750 |
+ // PyDict_SetItemString(d, "SiloException", SiloError); |
751 |
+ if (st->error == NULL) { |
752 |
+ Py_DECREF(module); |
753 |
+ INITERROR; |
754 |
+ } |
755 |
+ d = PyModule_GetDict(module); |
756 |
+ |
757 |
// File Drivers |
758 |
ADD_CONSTANT(DB_PDB); |
759 |
ADD_CONSTANT(DB_HDF5); |
760 |
@@ -322,4 +353,8 @@ extern "C" void initSilo() |
761 |
ADD_CONSTANT(DB_VARTYPE_MATERIAL); |
762 |
ADD_CONSTANT(DB_VARTYPE_SPECIES); |
763 |
ADD_CONSTANT(DB_VARTYPE_LABEL); |
764 |
+ |
765 |
+#if PY_MAJOR_VERSION >=3 |
766 |
+ return module; |
767 |
+#endif |
768 |
} |