@@ -286,7 +286,7 @@ def _print_ModuleHeader(self, expr):
286
286
sig_methods = c .methods + (c .new_func ,) + tuple (f for i in c .interfaces for f in i .functions ) + \
287
287
tuple (i .interface_func for i in c .interfaces ) + \
288
288
tuple (getset for p in c .properties for getset in (p .getter , p .setter ) if getset ) + \
289
- c .number_magic_methods
289
+ c .magic_methods
290
290
function_signatures += '\n ' + '' .join (self .function_signature (f )+ ';\n ' for f in sig_methods )
291
291
macro_defs += f'#define { type_name } (*(PyTypeObject*){ API_var .name } [{ i } ])\n '
292
292
@@ -396,7 +396,7 @@ def _print_PyClassDef(self, expr):
396
396
getters = tuple (p .getter for p in expr .properties )
397
397
setters = tuple (p .setter for p in expr .properties if p .setter )
398
398
print_methods = expr .methods + (expr .new_func ,) + expr .interfaces + \
399
- expr .number_magic_methods + getters + setters
399
+ expr .magic_methods + getters + setters
400
400
functions = '\n ' .join (self ._print (f ) for f in print_methods )
401
401
init_string = ''
402
402
del_string = ''
@@ -435,44 +435,58 @@ def _print_PyClassDef(self, expr):
435
435
'},\n ' )
436
436
for name , (wrapper_name , doc_string ) in funcs .items ())
437
437
438
+ magic_methods = {self .get_python_name (original_scope , f .original_function ): f for f in expr .magic_methods }
439
+
438
440
number_magic_method_name = self .scope .get_new_name (f'{ expr .name } _number_methods' )
439
- number_magic_methods = {self .get_python_name (original_scope , f .original_function ): f for f in expr .number_magic_methods }
440
441
441
442
number_magic_methods_def = f"static PyNumberMethods { number_magic_method_name } = {{\n "
442
- if '__add__' in number_magic_methods :
443
- number_magic_methods_def += f" .nb_add = (binaryfunc){ number_magic_methods ['__add__' ].name } ,\n "
444
- if '__sub__' in number_magic_methods :
445
- number_magic_methods_def += f" .nb_subtract = (binaryfunc){ number_magic_methods ['__sub__' ].name } ,\n "
446
- if '__mul__' in number_magic_methods :
447
- number_magic_methods_def += f" .nb_multiply = (binaryfunc){ number_magic_methods ['__mul__' ].name } ,\n "
448
- if '__truediv__' in number_magic_methods :
449
- number_magic_methods_def += f" .nb_true_divide = (binaryfunc){ number_magic_methods ['__truediv__' ].name } ,\n "
450
- if '__lshift__' in number_magic_methods :
451
- number_magic_methods_def += f" .nb_lshift = (binaryfunc){ number_magic_methods ['__lshift__' ].name } ,\n "
452
- if '__rshift__' in number_magic_methods :
453
- number_magic_methods_def += f" .nb_rshift = (binaryfunc){ number_magic_methods ['__rshift__' ].name } ,\n "
454
- if '__and__' in number_magic_methods :
455
- number_magic_methods_def += f" .nb_and = (binaryfunc){ number_magic_methods ['__and__' ].name } ,\n "
456
- if '__or__' in number_magic_methods :
457
- number_magic_methods_def += f" .nb_or = (binaryfunc){ number_magic_methods ['__or__' ].name } ,\n "
458
- if '__iadd__' in number_magic_methods :
459
- number_magic_methods_def += f" .nb_inplace_add = (binaryfunc){ number_magic_methods ['__iadd__' ].name } ,\n "
460
- if '__isub__' in number_magic_methods :
461
- number_magic_methods_def += f" .nb_inplace_subtract = (binaryfunc){ number_magic_methods ['__isub__' ].name } ,\n "
462
- if '__imul__' in number_magic_methods :
463
- number_magic_methods_def += f" .nb_inplace_multiply = (binaryfunc){ number_magic_methods ['__imul__' ].name } ,\n "
464
- if '__itruediv__' in number_magic_methods :
465
- number_magic_methods_def += f" .nb_inplace_true_divide = (binaryfunc){ number_magic_methods ['__itruediv__' ].name } ,\n "
466
- if '__ilshift__' in number_magic_methods :
467
- number_magic_methods_def += f" .nb_inplace_lshift = (binaryfunc){ number_magic_methods ['__ilshift__' ].name } ,\n "
468
- if '__irshift__' in number_magic_methods :
469
- number_magic_methods_def += f" .nb_inplace_rshift = (binaryfunc){ number_magic_methods ['__irshift__' ].name } ,\n "
470
- if '__iand__' in number_magic_methods :
471
- number_magic_methods_def += f" .nb_inplace_and = (binaryfunc){ number_magic_methods ['__iand__' ].name } ,\n "
472
- if '__ior__' in number_magic_methods :
473
- number_magic_methods_def += f" .nb_inplace_or = (binaryfunc){ number_magic_methods ['__ior__' ].name } ,\n "
443
+ if '__add__' in magic_methods :
444
+ number_magic_methods_def += f" .nb_add = (binaryfunc){ magic_methods ['__add__' ].name } ,\n "
445
+ if '__sub__' in magic_methods :
446
+ number_magic_methods_def += f" .nb_subtract = (binaryfunc){ magic_methods ['__sub__' ].name } ,\n "
447
+ if '__mul__' in magic_methods :
448
+ number_magic_methods_def += f" .nb_multiply = (binaryfunc){ magic_methods ['__mul__' ].name } ,\n "
449
+ if '__truediv__' in magic_methods :
450
+ number_magic_methods_def += f" .nb_true_divide = (binaryfunc){ magic_methods ['__truediv__' ].name } ,\n "
451
+ if '__lshift__' in magic_methods :
452
+ number_magic_methods_def += f" .nb_lshift = (binaryfunc){ magic_methods ['__lshift__' ].name } ,\n "
453
+ if '__rshift__' in magic_methods :
454
+ number_magic_methods_def += f" .nb_rshift = (binaryfunc){ magic_methods ['__rshift__' ].name } ,\n "
455
+ if '__and__' in magic_methods :
456
+ number_magic_methods_def += f" .nb_and = (binaryfunc){ magic_methods ['__and__' ].name } ,\n "
457
+ if '__or__' in magic_methods :
458
+ number_magic_methods_def += f" .nb_or = (binaryfunc){ magic_methods ['__or__' ].name } ,\n "
459
+ if '__iadd__' in magic_methods :
460
+ number_magic_methods_def += f" .nb_inplace_add = (binaryfunc){ magic_methods ['__iadd__' ].name } ,\n "
461
+ if '__isub__' in magic_methods :
462
+ number_magic_methods_def += f" .nb_inplace_subtract = (binaryfunc){ magic_methods ['__isub__' ].name } ,\n "
463
+ if '__imul__' in magic_methods :
464
+ number_magic_methods_def += f" .nb_inplace_multiply = (binaryfunc){ magic_methods ['__imul__' ].name } ,\n "
465
+ if '__itruediv__' in magic_methods :
466
+ number_magic_methods_def += f" .nb_inplace_true_divide = (binaryfunc){ magic_methods ['__itruediv__' ].name } ,\n "
467
+ if '__ilshift__' in magic_methods :
468
+ number_magic_methods_def += f" .nb_inplace_lshift = (binaryfunc){ magic_methods ['__ilshift__' ].name } ,\n "
469
+ if '__irshift__' in magic_methods :
470
+ number_magic_methods_def += f" .nb_inplace_rshift = (binaryfunc){ magic_methods ['__irshift__' ].name } ,\n "
471
+ if '__iand__' in magic_methods :
472
+ number_magic_methods_def += f" .nb_inplace_and = (binaryfunc){ magic_methods ['__iand__' ].name } ,\n "
473
+ if '__ior__' in magic_methods :
474
+ number_magic_methods_def += f" .nb_inplace_or = (binaryfunc){ magic_methods ['__ior__' ].name } ,\n "
474
475
number_magic_methods_def += '};\n '
475
476
477
+ seq_magic_method_name = self .scope .get_new_name (f'{ expr .name } _sequence_methods' )
478
+
479
+ seq_magic_methods_def = f"static PySequenceMethods { seq_magic_method_name } = {{\n "
480
+ if '__len__' in magic_methods :
481
+ seq_magic_methods_def += f" .sq_length = { magic_methods ['__len__' ].name } ,\n "
482
+ seq_magic_methods_def += '};\n '
483
+
484
+ map_magic_method_name = self .scope .get_new_name (f'{ expr .name } _mapping_methods' )
485
+ map_magic_methods_def = f"static PyMappingMethods { map_magic_method_name } = {{\n "
486
+ if '__len__' in magic_methods :
487
+ map_magic_methods_def += f" .mp_length = { magic_methods ['__len__' ].name } ,\n "
488
+ map_magic_methods_def += '};\n '
489
+
476
490
method_def_name = self .scope .get_new_name (f'{ expr .name } _methods' )
477
491
method_def = (f'static PyMethodDef { method_def_name } [] = {{\n '
478
492
f'{ method_def_funcs } '
@@ -488,6 +502,8 @@ def _print_PyClassDef(self, expr):
488
502
" PyVarObject_HEAD_INIT(NULL, 0)\n "
489
503
f" .tp_name = \" { self ._module_name } .{ name } \" ,\n "
490
504
f" .tp_as_number = &{ number_magic_method_name } ,\n "
505
+ f" .tp_as_sequence = &{ seq_magic_method_name } ,\n "
506
+ f" .tp_as_mapping = &{ map_magic_method_name } ,\n "
491
507
f" .tp_doc = PyDoc_STR({ docstring } ),\n "
492
508
f" .tp_basicsize = sizeof(struct { struct_name } ),\n "
493
509
" .tp_itemsize = 0,\n "
@@ -498,7 +514,8 @@ def _print_PyClassDef(self, expr):
498
514
f" .tp_getset = { property_def_name } ,\n "
499
515
"};\n " )
500
516
501
- return '\n ' .join ((method_def , number_magic_methods_def , property_def , type_code , functions ))
517
+ return '\n ' .join ((method_def , number_magic_methods_def , seq_magic_methods_def ,
518
+ map_magic_methods_def , property_def , type_code , functions ))
502
519
503
520
def _print_PyModInitFunc (self , expr ):
504
521
decs = '' .join (self ._print (d ) for d in expr .declarations )
0 commit comments