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