forked from Normaliz/PyNormaliz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNormalizModule.cpp
732 lines (581 loc) · 20.8 KB
/
NormalizModule.cpp
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
#include <Python.h>
using namespace std;
#include <iostream>
using std::cout;
using std::cerr;
using std::endl;
#include <string>
using std::string;
#include <libnormaliz/cone.h>
#include <libnormaliz/map_operations.h>
using libnormaliz::Cone;
//using libnormaliz::ConeProperty;
using libnormaliz::ConeProperties;
using libnormaliz::Sublattice_Representation;
using libnormaliz::Type::InputType;
#include <vector>
using std::map;
using std::vector;
using std::pair;
// Macros for try catch
#define FUNC_BEGIN try {
#define FUNC_END \
} catch (libnormaliz::NormalizException& e) { \
PyErr_SetString( NormalizError, e.what() ); \
return NULL; \
} catch( ... ) { \
PyErr_SetString( PyNormalizError, "unknown exception" ); \
return NULL; \
}
static PyObject * NormalizError;
static PyObject * PyNormalizError;
static char* cone_name = "Cone";
static string cone_name_str( cone_name );
typedef int py_size_t;
string PyUnicodeToString( PyObject* in ){
string out = "";
int length = PyUnicode_GET_SIZE( in );
for( int i = 0; i < length; i++ ){
out += PyUnicode_READ_CHAR( in, i );
}
return out;
}
// Converting MPZ's to PyLong and back via strings. Worst possible solution ever.
bool PyLongToNmz( PyObject * in, mpz_class& out ){
PyObject * in_as_string = PyObject_Str( in );
const char* in_as_c_string = PyUnicodeToString( in_as_string ).c_str();
out.set_str( in_as_c_string, 10 );
return true;
}
PyObject* NmzToPyLong( mpz_class in ){
string mpz_as_string = in.get_str();
const char* mpz_as_c_string = mpz_as_string.c_str();
char * pend;
PyObject* ret_val = PyLong_FromString( mpz_as_c_string, &pend, 10 );
return ret_val;
}
PyObject* NmzToPyList( mpq_class in ){
PyObject* out_list = PyList_New( 2 );
PyList_SetItem( out_list, 0, NmzToPyLong( in.get_num() ) );
PyList_SetItem( out_list, 1, NmzToPyLong( in.get_den() ) );
return out_list;
}
bool PyLongToNmz( PyObject* in, long long & out ){
int overflow;
out = PyLong_AsLongLongAndOverflow( in, &overflow );
if( overflow == -1 )
return false;
return true;
}
PyObject* NmzToPyLong( long long in ){
return PyLong_FromLongLong( in );
}
PyObject* NmzToPyLong( libnormaliz::key_t in ){
return PyLong_FromLong( in );
}
PyObject* NmzToPyLong( size_t in ){
return PyLong_FromLong( in );
}
PyObject* NmzToPyLong( long in ){
return PyLong_FromLong( in );
}
template<typename Integer>
bool PyLongToNmz(Integer& x, Integer &out){
return Integer::unimplemented_function;
}
template<typename Integer>
PyObject* NmzToPyLong(Integer &in){
return Integer::unimplemented_function;
}
template<typename Integer>
static bool PyListToNmz( vector<Integer>& out, PyObject* in ){
if (!PyList_Check(in))
return false;
const int n = PyList_Size(in);
out.resize(n);
for (int i = 0; i < n; ++i) {
PyObject* tmp = PyList_GetItem(in, i);
if (!PyLongToNmz(tmp, out[i]))
return false;
}
return true;
}
template<typename Integer>
static bool PyIntMatrixToNmz( vector<vector<Integer> >& out, PyObject* in ){
if (!PyList_Check( in ) )
return false;
const int nr = PyList_Size( in );
out.resize(nr);
for (int i = 0; i < nr; ++i) {
bool okay = PyListToNmz(out[i], PyList_GetItem(in, i));
if (!okay)
return false;
}
return true;
}
template<typename Integer>
PyObject* NmzVectorToPyList(const vector<Integer>& in)
{
PyObject* vector;
const size_t n = in.size();
vector = PyList_New(n);
for (size_t i = 0; i < n; ++i) {
PyList_SetItem(vector, i, NmzToPyLong(in[i]));
}
return vector;
}
template<typename Integer>
PyObject* NmzMatrixToPyList(const vector< vector<Integer> >& in)
{
PyObject* matrix;
const size_t n = in.size();
matrix = PyList_New( n );
for (size_t i = 0; i < n; ++i) {
PyList_SetItem(matrix, i, NmzVectorToPyList(in[i]));
}
return matrix;
}
PyObject* NmzHilbertSeriesToPyList(const libnormaliz::HilbertSeries& HS)
{
PyObject* return_list = PyList_New( 3 );
PyList_SetItem(return_list, 0, NmzVectorToPyList(HS.getNum()));
PyList_SetItem(return_list, 1, NmzVectorToPyList(libnormaliz::to_vector(HS.getDenom())));
PyList_SetItem(return_list, 2, NmzToPyLong(HS.getShift()));
return return_list;
}
template<typename Integer>
PyObject* NmzHilbertQuasiPolynomialToPyList(const libnormaliz::HilbertSeries& HS)
{
vector< vector<Integer> > HQ = HS.getHilbertQuasiPolynomial();
const size_t n = HS.getPeriod();
PyObject* return_list = PyList_New(n+1);
for (size_t i = 0; i < n; ++i) {
PyList_SetItem(return_list, i, NmzVectorToPyList(HQ[i]));
}
PyList_SetItem(return_list, n+1, NmzToPyLong(HS.getHilbertQuasiPolynomialDenom()));
return return_list;
}
template<typename Integer>
PyObject* NmzTriangleListToPyList(const vector< pair<vector<libnormaliz::key_t>, Integer> >& in)
{
const size_t n = in.size();
PyObject* M = PyList_New( n );
for (size_t i = 0; i < n; ++i) {
// convert the pair
PyObject* pair = PyList_New(2);
PyList_SetItem(pair, 0, NmzVectorToPyList<libnormaliz::key_t>(in[i].first));
PyList_SetItem(pair, 1, NmzToPyLong(in[i].second));
PyList_SetItem(M, i, pair);
}
return M;
}
template<typename Integer>
void delete_cone( PyObject* cone ){
Cone<Integer> * cone_ptr = reinterpret_cast<Cone<Integer>* >( PyCapsule_GetPointer( cone, cone_name ) );
delete cone_ptr;
}
template<typename Integer>
Cone<Integer>* get_cone( PyObject* cone ){
return reinterpret_cast<Cone<Integer>*>( PyCapsule_GetPointer( cone, cone_name ) );
}
template<typename Integer>
PyObject* pack_cone( Cone<Integer>* C ){
return PyCapsule_New( reinterpret_cast<void*>( C ), cone_name, &delete_cone<Integer> );
}
bool is_cone( PyObject* cone ){
if( PyCapsule_CheckExact( cone ) ){
// compare as string
return cone_name_str == string(PyCapsule_GetName( cone ));
}
return false;
}
template<typename Integer>
static PyObject* _NmzConeIntern(PyObject * input_list)
{
map <InputType, vector< vector<Integer> > > input;
const int n = PyList_Size(input_list);
if (n&1) {
cerr << "Input list must have even number of elements" << endl;
return Py_False;
}
for (int i = 0; i < n; i += 2) {
PyObject* type = PyList_GetItem(input_list, i);
if (!PyUnicode_Check(type)) {
cerr << "Element " << i+1 << " of the input list must be a type string" << endl;
return Py_False;
}
string type_str = PyUnicodeToString( type );
PyObject* M = PyList_GetItem(input_list, i+1);
vector<vector<Integer> > Mat;
bool okay = PyIntMatrixToNmz(Mat, M);
if (!okay) {
cerr << "Element " << i+2 << " of the input list must integer matrix" << endl;
return Py_False;
}
input[libnormaliz::to_type(type_str)] = Mat;
}
Cone<Integer>* C = new Cone<Integer>(input);
PyObject* return_container = pack_cone( C );
return return_container;
}
PyObject* _NmzCone(PyObject* self, PyObject* args)
{
FUNC_BEGIN
PyObject* input_list = PyTuple_GetItem( args, 0 );
if (!PyList_Check( input_list ) )
return Py_False;
return _NmzConeIntern<mpz_class>(input_list);
FUNC_END
}
PyObject* _NmzCompute(PyObject* self, PyObject* args)
{
FUNC_BEGIN
PyObject* cone = PyTuple_GetItem( args, 0 );
PyObject* to_compute = PyTuple_GetItem( args, 1 );
if (!PyList_Check( to_compute ) )
PyErr_SetString( PyNormalizError, "wrong input type" );
ConeProperties propsToCompute;
// we have a list
const int n = PyList_Size(to_compute);
for (int i = 0; i < n; ++i) {
PyObject* prop = PyList_GetItem(to_compute, i);
if (!PyUnicode_Check(prop)) {
cerr << "Element " << i+1 << " of the input list must be a type string";
return Py_False;
}
string prop_str(PyUnicodeToString(prop));
propsToCompute.set( libnormaliz::toConeProperty(prop_str) );
}
Cone<mpz_class>* C = get_cone<mpz_class>( cone );
ConeProperties notComputed = C->compute(propsToCompute);
// Cone.compute returns the not computed properties
// we return a bool, true when everything requested was computed
return notComputed.none() ? Py_True : Py_False;
FUNC_END
}
PyObject* NmzHasConeProperty(PyObject* self, PyObject* args)
{
FUNC_BEGIN
PyObject* cone = PyTuple_GetItem( args, 0 );
PyObject* prop = PyTuple_GetItem( args, 1 );
libnormaliz::ConeProperty::Enum p = libnormaliz::toConeProperty(PyUnicodeToString( prop ) );
Cone<mpz_class>* C = get_cone<mpz_class>( cone );
return C->isComputed(p) ? Py_True : Py_False;
FUNC_END
}
template<typename Integer>
PyObject* _NmzConePropertyImpl(Cone<Integer>* C, PyObject* prop_obj)
{
string prop = PyUnicodeToString( prop_obj );
// there is no ConeProperty HilbertQuasiPolynomial, it is part of the HilbertSeries
// FIXME better way?
// if(prop == string("HilbertQuasiPolynomial")) {
// C->compute(ConeProperties(libnormaliz::ConeProperty::HilbertSeries));
// return NmzHilbertQuasiPolynomialToPyList(C->getHilbertSeries());
// }
libnormaliz::ConeProperty::Enum p = libnormaliz::toConeProperty(prop);
ConeProperties notComputed = C->compute(ConeProperties(p));
if (notComputed.any()) {
return Py_None;
}
switch (p) {
case libnormaliz::ConeProperty::Generators:
return NmzMatrixToPyList(C->getGenerators());
case libnormaliz::ConeProperty::ExtremeRays:
return NmzMatrixToPyList(C->getExtremeRays());
case libnormaliz::ConeProperty::VerticesOfPolyhedron:
return NmzMatrixToPyList(C->getVerticesOfPolyhedron());
case libnormaliz::ConeProperty::SupportHyperplanes:
return NmzMatrixToPyList(C->getSupportHyperplanes());
case libnormaliz::ConeProperty::TriangulationSize:
return NmzToPyLong(C->getTriangulationSize());
case libnormaliz::ConeProperty::TriangulationDetSum:
return NmzToPyLong(C->getTriangulationDetSum());
case libnormaliz::ConeProperty::Triangulation:
return NmzTriangleListToPyList<Integer>(C->getTriangulation());
case libnormaliz::ConeProperty::Multiplicity:
return NmzToPyList(C->getMultiplicity());
case libnormaliz::ConeProperty::RecessionRank:
return NmzToPyLong(C->getRecessionRank());
case libnormaliz::ConeProperty::AffineDim:
return NmzToPyLong(C->getAffineDim());
case libnormaliz::ConeProperty::ModuleRank:
return NmzToPyLong(C->getModuleRank());
case libnormaliz::ConeProperty::HilbertBasis:
return NmzMatrixToPyList(C->getHilbertBasis());
case libnormaliz::ConeProperty::MaximalSubspace:
return NmzMatrixToPyList(C->getMaximalSubspace());
case libnormaliz::ConeProperty::ModuleGenerators:
return NmzMatrixToPyList(C->getModuleGenerators());
case libnormaliz::ConeProperty::Deg1Elements:
return NmzMatrixToPyList(C->getDeg1Elements());
case libnormaliz::ConeProperty::HilbertSeries:
return NmzHilbertSeriesToPyList(C->getHilbertSeries());
case libnormaliz::ConeProperty::Grading:
{
vector<Integer> grad = C->getGrading();
grad.push_back(C->getGradingDenom());
return NmzVectorToPyList(grad);
}
case libnormaliz::ConeProperty::IsPointed:
return C->isPointed() ? Py_True : Py_False;
case libnormaliz::ConeProperty::IsDeg1ExtremeRays:
return C->isDeg1ExtremeRays() ? Py_True : Py_False;
case libnormaliz::ConeProperty::IsDeg1HilbertBasis:
return C->isDeg1HilbertBasis() ? Py_True : Py_False;
case libnormaliz::ConeProperty::IsIntegrallyClosed:
return C->isIntegrallyClosed() ? Py_True : Py_False;
case libnormaliz::ConeProperty::OriginalMonoidGenerators:
return NmzMatrixToPyList(C->getOriginalMonoidGenerators());
case libnormaliz::ConeProperty::IsReesPrimary:
return C->isReesPrimary() ? Py_True : Py_False;
case libnormaliz::ConeProperty::ReesPrimaryMultiplicity:
return NmzToPyLong(C->getReesPrimaryMultiplicity());
// StanleyDec is special and we do not support the required conversion at
// this time. If you really need this, contact the developers.
case libnormaliz::ConeProperty::StanleyDec:
//C->getStanleyDec();
break;
case libnormaliz::ConeProperty::ExcludedFaces:
return NmzMatrixToPyList(C->getExcludedFaces());
case libnormaliz::ConeProperty::Dehomogenization:
return NmzVectorToPyList(C->getDehomogenization());
case libnormaliz::ConeProperty::InclusionExclusionData:
return NmzTriangleListToPyList<long>(C->getInclusionExclusionData());
case libnormaliz::ConeProperty::ClassGroup:
return NmzVectorToPyList(C->getClassGroup());
case libnormaliz::ConeProperty::Sublattice:
C->compute(p);
return C->isComputed(p) ? Py_True : Py_False;
// the following properties are compute options and do not return anything
case libnormaliz::ConeProperty::DualMode:
case libnormaliz::ConeProperty::DefaultMode:
case libnormaliz::ConeProperty::Approximate:
case libnormaliz::ConeProperty::BottomDecomposition:
case libnormaliz::ConeProperty::KeepOrder:
return Py_True; // FIXME: appropriate value?
default:
// Case not handled. Should signal an error
break;
}
return Py_None;
}
PyObject* _NmzConeProperty( PyObject* self, PyObject* args ){
FUNC_BEGIN
PyObject* cone = PyTuple_GetItem( args, 0 );
PyObject* prop = PyTuple_GetItem( args, 1 );
if( !is_cone( cone ) ){
PyErr_SetString( PyNormalizError, "First argument must be a cone" );
return NULL;
}
if( !PyUnicode_Check( prop ) ){
PyErr_SetString( PyNormalizError, "Second argument must be a unicode string" );
return NULL;
}
Cone<mpz_class>* C = get_cone<mpz_class>( cone );
return _NmzConePropertyImpl( C, prop );
FUNC_END
}
PyObject* NmzSetVerboseDefault( PyObject* self, PyObject* args)
{
FUNC_BEGIN
PyObject * value = PyTuple_GetItem( args, 0 );
if (value != Py_True && value != Py_False){
PyErr_SetString( PyNormalizError, "Argument must be true or false" );
return NULL;
}
return libnormaliz::setVerboseDefault(value == Py_True) ? Py_True : Py_False;
FUNC_END
}
PyObject* NmzSetVerbose(PyObject* self, PyObject* args)
{
FUNC_BEGIN
PyObject* cone = PyTuple_GetItem( args, 0 );
if( !is_cone( cone ) ){
PyErr_SetString( PyNormalizError, "First argument must be a cone" );
return NULL;
}
PyObject* value = PyTuple_GetItem( args, 1 );
if (value != Py_True && value != Py_False){
PyErr_SetString( PyNormalizError, "Second argument must be true or false" );
return NULL;
}
bool old_value;
Cone<mpz_class>* C = get_cone<mpz_class>( cone );
old_value = C->setVerbose(value == Py_True);
return old_value ? Py_True : Py_False;
FUNC_END
}
PyObject* NmzEmbeddingDimension(PyObject* self, PyObject* args)
{
FUNC_BEGIN
PyObject* cone = PyTuple_GetItem( args, 0 );
if (!is_cone(cone)){
PyErr_SetString( PyNormalizError, "First argument must be a cone" );
return NULL;
}
Cone<mpz_class>* C = get_cone<mpz_class>(cone);
return NmzToPyLong(C->getEmbeddingDim());
FUNC_END
}
template<typename Integer>
static PyObject* _NmzBasisChangeIntern(PyObject* cone)
{
Cone<Integer>* C = get_cone<Integer>(cone);
Sublattice_Representation<Integer> bc = C->getSublattice();
PyObject* res = PyList_New( 3 );
PyList_SetItem(res, 0, NmzMatrixToPyList(bc.getEmbedding()));
PyList_SetItem(res, 1, NmzMatrixToPyList(bc.getProjection()));
PyList_SetItem(res, 2, NmzToPyLong(bc.getAnnihilator()));
// Dim, Rank, Equations and Congruences are already covered by special functions
// The index is not always computed and not so relevant
return res;
}
PyObject* _NmzBasisChange(PyObject* self, PyObject* args)
{
FUNC_BEGIN
PyObject* cone = PyTuple_GetItem( args, 0 );
if (!is_cone(cone)){
PyErr_SetString( PyNormalizError, "First argument must be a cone" );
return NULL;
}
return _NmzBasisChangeIntern<mpz_class>(cone);
FUNC_END
}
PyObject* NmzRank(PyObject* self, PyObject* args)
{
FUNC_BEGIN
PyObject* cone = PyTuple_GetItem( args, 0 );
if (!is_cone(cone)){
PyErr_SetString( PyNormalizError, "First argument must be a cone" );
return NULL;
}
Cone<mpz_class>* C = get_cone<mpz_class>(cone);
return NmzToPyLong(C->getSublattice().getRank());
FUNC_END
}
PyObject* NmzIsInhomogeneous(PyObject* self, PyObject* args)
{
FUNC_BEGIN
PyObject* cone = PyTuple_GetItem( args, 0 );
if (!is_cone(cone)){
PyErr_SetString( PyNormalizError, "First argument must be a cone" );
return NULL;
}
Cone<mpz_class>* C = get_cone<mpz_class>(cone);
return C->isInhomogeneous() ? Py_True : Py_False;
FUNC_END
}
PyObject* NmzEquations(PyObject* self, PyObject* args)
{
FUNC_BEGIN
PyObject* cone = PyTuple_GetItem( args, 0 );
if (!is_cone(cone)){
PyErr_SetString( PyNormalizError, "First argument must be a cone" );
return NULL;
}
Cone<mpz_class>* C = get_cone<mpz_class>(cone);
C->compute(ConeProperties(libnormaliz::ConeProperty::SupportHyperplanes));
return NmzMatrixToPyList(C->getSublattice().getEquations());
FUNC_END
}
PyObject* NmzCongruences(PyObject* self, PyObject* args)
{
FUNC_BEGIN
PyObject* cone = PyTuple_GetItem( args, 0 );
if (!is_cone(cone)){
PyErr_SetString( PyNormalizError, "First argument must be a cone" );
return NULL;
}
Cone<mpz_class>* C = get_cone<mpz_class>(cone);
C->compute(ConeProperties(libnormaliz::ConeProperty::SupportHyperplanes));
return NmzMatrixToPyList(C->getSublattice().getCongruences());
FUNC_END
}
/*
* Python init stuff
*/
static PyMethodDef PyNormalizMethods[] = {
{"NmzCone", _NmzCone, METH_VARARGS,
"Create a cone"},
{"NmzCompute", _NmzCompute, METH_VARARGS,
"Compute some stuff"},
{"NmzHasConeProperty", NmzHasConeProperty, METH_VARARGS,
"Check if property is computed "},
{"NmzConeProperty", _NmzConeProperty, METH_VARARGS,
"Return cone property" },
{ "NmzSetVerboseDefault", NmzSetVerboseDefault, METH_VARARGS,
"Set verbosity" },
{ "NmzSetVerbose", NmzSetVerbose, METH_VARARGS,
"Set verbosity of cone" },
{ "NmzBasisChange", _NmzBasisChange, METH_VARARGS,
"Get information of basis change" },
{ "NmzIsInhomogeneous", NmzIsInhomogeneous, METH_VARARGS,
"Is inhomogeneous cone" },
{ "NmzEquations", NmzEquations, METH_VARARGS,
"Equations" },
{ "NmzCongruences", NmzCongruences, METH_VARARGS,
"Congruences of cone" },
{ "NmzRank", NmzRank, METH_VARARGS,
"Get rank" },
{NULL, NULL, 0, NULL} /* Sentinel */
};
static struct PyModuleDef PyNormalizmodule = {
PyModuleDef_HEAD_INIT,
"PyNormaliz", /* name of module */
NULL, /* module documentation, may be NULL */
-1, /* size of per-interpreter state of the module,
or -1 if the module keeps state in global variables. */
PyNormalizMethods
};
PyMODINIT_FUNC
PyInit_PyNormaliz(void)
{
PyObject * module;
module = PyModule_Create(&PyNormalizmodule);
if(module == NULL){
return NULL;
}
NormalizError = PyErr_NewException( "Normaliz.error", NULL, NULL );
Py_INCREF( NormalizError );
PyNormalizError = PyErr_NewException( "Normaliz.interface_error", NULL, NULL );
Py_INCREF( PyNormalizError );
PyModule_AddObject( module, "error", NormalizError );
PyModule_AddObject( module, "error", PyNormalizError );
return module;
}
/*
* main
*/
wchar_t *GetWC(const char *c)
{
const size_t cSize = strlen(c)+1;
wchar_t* wc = new wchar_t[cSize];
mbstowcs (wc, c, cSize);
return wc;
}
int main(int argc, char *argv[]){
#ifdef PYTHON_VERSION_OLDER_THREE_FIVE
const size_t cSize = strlen(argv[0])+1;
wchar_t* program = new wchar_t[cSize];
mbstowcs (program, argv[0], cSize);
#else
wchar_t *program = Py_DecodeLocale(argv[0], NULL);
if (program == NULL) {
fprintf(stderr, "Fatal error: cannot decode argv[0]\n");
exit(1);
}
#endif
/* Add a built-in module, before Py_Initialize */
PyImport_AppendInittab("PyNormaliz", PyInit_PyNormaliz);
/* Pass argv[0] to the Python interpreter */
Py_SetProgramName(program);
/* Initialize the Python interpreter. Required. */
Py_Initialize();
/* Optionally import the module; alternatively,
import can be deferred until the embedded script
imports it. */
PyImport_ImportModule("PyNormaliz");
PyMem_RawFree(program);
return 0;
}