@@ -308,8 +308,8 @@ def generate_forward_declarations(self, code_sink, module):
308
308
''' % (self .pystruct , self .full_name , self .iter_pystruct ))
309
309
310
310
code_sink .writeln ()
311
- code_sink .writeln ('extern PyTypeObject %s;' % (self .pytypestruct ,))
312
- code_sink .writeln ('extern PyTypeObject %s;' % (self .iter_pytypestruct ,))
311
+ code_sink .writeln ('extern PyTypeObject _TYPEDEC %s;' % (self .pytypestruct ,))
312
+ code_sink .writeln ('extern PyTypeObject _TYPEDEC %s;' % (self .iter_pytypestruct ,))
313
313
code_sink .writeln ()
314
314
315
315
this_type_converter = self .module .get_root ().get_python_to_c_type_converter_function_name (
@@ -346,18 +346,25 @@ def generate(self, code_sink, module, docstring=None):
346
346
347
347
## --- register the class type in the module ---
348
348
module .after_init .write_code ("/* Register the '%s' class */" % self .full_name )
349
+ #import traceback; module.after_init.write_code(repr(traceback.format_stack()))
349
350
350
- module .after_init .write_error_check ('PyType_Ready(&%s)' % (self .pytypestruct ,))
351
- module .after_init .write_error_check ('PyType_Ready(&%s)' % (self .iter_pytypestruct ,))
351
+ module .after_init .write_code ('#ifdef Py_LIMITED_API\n '
352
+ '%s = (PyTypeObject*)PyType_FromSpec(&%s_spec);\n '
353
+ '%s = (PyTypeObject*)PyType_FromSpec(&%s_spec);\n '
354
+ '#endif' % (self .pytypestruct , self .pytypestruct ,
355
+ self .iter_pytypestruct , self .iter_pytypestruct ))
356
+
357
+ module .after_init .write_error_check ('PyType_Ready(_TYPEREF %s)' % (self .pytypestruct ,))
358
+ module .after_init .write_error_check ('PyType_Ready(_TYPEREF %s)' % (self .iter_pytypestruct ,))
352
359
353
360
class_python_name = self .python_name
354
361
355
362
if self .outer_class is None :
356
363
module .after_init .write_code (
357
- 'PyModule_AddObject(m, (char *) \" %s\" , (PyObject *) & %s);' % (
364
+ 'PyModule_AddObject(m, (char *) \" %s\" , (PyObject *) _TYPEREF %s);' % (
358
365
class_python_name , self .pytypestruct ))
359
366
module .after_init .write_code (
360
- 'PyModule_AddObject(m, (char *) \" %s\" , (PyObject *) & %s);' % (
367
+ 'PyModule_AddObject(m, (char *) \" %s\" , (PyObject *) _TYPEREF %s);' % (
361
368
class_python_name + 'Iter' , self .iter_pytypestruct ))
362
369
else :
363
370
module .after_init .write_code (
@@ -438,10 +445,13 @@ def _generate_destructor(self, code_sink):
438
445
%s(%s *self)
439
446
{
440
447
%s
448
+ #ifdef Py_LIMITED_API
449
+ PyObject_DEL(self);
450
+ #else
441
451
Py_TYPE(self)->tp_free((PyObject*)self);
452
+ #endif
442
453
}
443
- ''' % (container_tp_dealloc_function_name , self .pystruct ,
444
- self ._get_container_delete_code ()))
454
+ ''' % (container_tp_dealloc_function_name , self .pystruct , self ._get_container_delete_code ()))
445
455
446
456
self .pytype .slots .setdefault ("tp_dealloc" , container_tp_dealloc_function_name )
447
457
@@ -454,7 +464,11 @@ def _generate_destructor(self, code_sink):
454
464
{
455
465
Py_CLEAR(self->container);
456
466
%s
467
+ #ifdef Py_LIMITED_API
468
+ PyObject_DEL(self);
469
+ #else
457
470
Py_TYPE(self)->tp_free((PyObject*)self);
471
+ #endif
458
472
}
459
473
''' % (iter_tp_dealloc_function_name , self .iter_pystruct , self ._get_iter_delete_code ()))
460
474
@@ -478,7 +492,7 @@ def _generate_iter_methods(self, code_sink):
478
492
static PyObject*
479
493
%(CONTAINER_ITER_FUNC)s(%(PYSTRUCT)s *self)
480
494
{
481
- %(ITER_PYSTRUCT)s *iter = PyObject_GC_New(%(ITER_PYSTRUCT)s, & %(ITER_PYTYPESTRUCT)s);
495
+ %(ITER_PYSTRUCT)s *iter = PyObject_GC_New(%(ITER_PYSTRUCT)s, _TYPEREF %(ITER_PYTYPESTRUCT)s);
482
496
Py_INCREF(self);
483
497
iter->container = self;
484
498
iter->iterator = new %(CTYPE)s::iterator(self->obj->begin());
@@ -542,7 +556,7 @@ def _generate_container_constructor(self, code_sink):
542
556
Py_ssize_t size = PyList_Size(arg);
543
557
for (Py_ssize_t i = 0; i < size; i++) {
544
558
%(ITEM_CTYPE)s item;
545
- if (!%(ITEM_CONVERTER)s(PyList_GET_ITEM (arg, i), &item)) {
559
+ if (!%(ITEM_CONVERTER)s(PyList_GetItem (arg, i), &item)) {
546
560
return 0;
547
561
}
548
562
container->%(ADD_VALUE)s(item);
@@ -573,16 +587,16 @@ def _generate_container_constructor(self, code_sink):
573
587
container->clear();
574
588
Py_ssize_t size = PyList_Size(arg);
575
589
for (Py_ssize_t i = 0; i < size; i++) {
576
- PyObject *tup = PyList_GET_ITEM (arg, i);
590
+ PyObject *tup = PyList_GetItem (arg, i);
577
591
if (!PyTuple_Check(tup) || PyTuple_Size(tup) != 2) {
578
592
PyErr_SetString(PyExc_TypeError, "items must be tuples with two elements");
579
593
return 0;
580
594
}
581
595
std::pair< %(KEY_CTYPE)s, %(ITEM_CTYPE)s > item;
582
- if (!%(KEY_CONVERTER)s(PyTuple_GET_ITEM (tup, 0), &item.first)) {
596
+ if (!%(KEY_CONVERTER)s(PyTuple_GetItem (tup, 0), &item.first)) {
583
597
return 0;
584
598
}
585
- if (!%(ITEM_CONVERTER)s(PyTuple_GET_ITEM (tup, 1), &item.second)) {
599
+ if (!%(ITEM_CONVERTER)s(PyTuple_GetItem (tup, 1), &item.second)) {
586
600
return 0;
587
601
}
588
602
container->%(ADD_VALUE)s(item);
@@ -693,7 +707,7 @@ def convert_c_to_python(self, wrapper):
693
707
self .container_type .pystruct + '*' , 'py_' + self .container_type .name )
694
708
wrapper .before_call .write_code (
695
709
"%s = PyObject_New(%s, %s);" %
696
- (self .py_name , self .container_type .pystruct , '& ' + self .container_type .pytypestruct ))
710
+ (self .py_name , self .container_type .pystruct , '_TYPEREF ' + self .container_type .pytypestruct ))
697
711
698
712
wrapper .before_call .write_code ("%s->obj = new %s(%s);" % (self .py_name , self .container_type .full_name , self .value ))
699
713
@@ -728,7 +742,7 @@ def convert_python_to_c(self, wrapper):
728
742
self .container_type .pystruct + '*' , 'py_' + self .container_type .name )
729
743
wrapper .after_call .write_code (
730
744
"%s = PyObject_New(%s, %s);" %
731
- (py_name , self .container_type .pystruct , '& ' + self .container_type .pytypestruct ))
745
+ (py_name , self .container_type .pystruct , '_TYPEREF ' + self .container_type .pytypestruct ))
732
746
wrapper .after_call .write_code ("%s->obj = new %s(%s);" % (py_name , self .container_type .full_name , container_tmp_var ))
733
747
wrapper .build_params .add_parameter ("N" , [py_name ])
734
748
@@ -740,7 +754,7 @@ def convert_c_to_python(self, wrapper):
740
754
self .container_type .pystruct + '*' , 'py_' + self .container_type .name )
741
755
wrapper .before_call .write_code (
742
756
"%s = PyObject_New(%s, %s);" %
743
- (self .py_name , self .container_type .pystruct , '& ' + self .container_type .pytypestruct ))
757
+ (self .py_name , self .container_type .pystruct , '_TYPEREF ' + self .container_type .pytypestruct ))
744
758
745
759
if self .direction & Parameter .DIRECTION_IN :
746
760
wrapper .before_call .write_code ("%s->obj = new %s(%s);" % (self .py_name , self .container_type .full_name , self .name ))
@@ -800,7 +814,7 @@ def convert_python_to_c(self, wrapper):
800
814
801
815
wrapper .after_call .write_code (
802
816
"%s = PyObject_New(%s, %s);" %
803
- (py_name , self .container_type .pystruct , '& ' + self .container_type .pytypestruct ))
817
+ (py_name , self .container_type .pystruct , '_TYPEREF ' + self .container_type .pytypestruct ))
804
818
805
819
wrapper .after_call .write_code ("%s->obj = %s;" % (py_name , container_tmp_var ))
806
820
@@ -855,7 +869,7 @@ def convert_c_to_python(self, wrapper):
855
869
856
870
wrapper .after_call .write_code (
857
871
"%s = PyObject_New(%s, %s);" %
858
- (py_name , self .container_type .pystruct , '& ' + self .container_type .pytypestruct ))
872
+ (py_name , self .container_type .pystruct , '_TYPEREF ' + self .container_type .pytypestruct ))
859
873
wrapper .after_call .write_code ("%s->obj = new %s(%s);" % (self .py_name , self .container_type .full_name , self .value ))
860
874
wrapper .build_params .add_parameter ("N" , [py_name ], prepend = True )
861
875
0 commit comments