-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtemplate.cpp
1049 lines (915 loc) · 29.5 KB
/
template.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
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
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <Python.h>
#include "structmember.h"
#include <stdbool.h>
#define PY3K (PY_VERSION_HEX >= 0x03000000)
double pi; // later we import this value from the math module
#if PY3K
#define Py_TPFLAGS_CHECKTYPES 0
#endif
#if !PY3K
#define Py_RETURN_NOTIMPLEMENTED return Py_INCREF(Py_NotImplemented), Py_NotImplemented
#define PyLong_AS_LONG(op) PyLong_AsLong(op)
#define PyExtNumber_Check(op) (PyLong_Check(op) || PyInt_Check(op) || PyFloat_Check(op) || PyBool_Check(op))
#endif
bool PyExtNumber_Check(PyObject* arg) {
#if PY3K
if (PyFloat_Check(arg) || PyLong_Check(arg) || PyBool_Check(arg)) {
#else
if (PyFloat_Check(arg) || PyInt_Check(arg) || PyLong_Check(arg) || PyBool_Check(arg)) {
#endif
return true;
}
if (arg->ob_type->tp_as_number != NULL && arg->ob_type->tp_as_number->nb_float != NULL) {
PyObject* temp = PyNumber_Float(arg);
if (temp != NULL) {
Py_DECREF(temp);
return true;
}
PyErr_Clear();
}
return false;
}
double PyExtNumber_AsDouble(PyObject* arg) {
if (PyFloat_Check(arg)) {
return PyFloat_AS_DOUBLE(arg);
}
#if !PY3K
if (PyInt_Check(arg)) {
return (double)PyInt_AS_LONG(arg);
}
#endif
if (PyLong_Check(arg)) {
return (double)PyLong_AsDouble(arg);
}
if (PyBool_Check(arg)) {
return (arg == Py_True) ? 1.0 : 0.0;
}
PyObject* arg_as_float = PyNumber_Float(arg);
double out = PyFloat_AS_DOUBLE(arg_as_float);
Py_DECREF(arg_as_float);
return out;
}
long PyExtNumber_AsLong(PyObject* arg) {
#if !PY3K
if (PyInt_Check(arg)) {
return PyInt_AS_LONG(arg);
}
#endif
if (PyLong_Check(arg)) {
return PyLong_AS_LONG(arg);
}
if (PyFloat_Check(arg)) {
return (long)PyFloat_AS_DOUBLE(arg);
}
if (PyBool_Check(arg)) {
return (arg == Py_True) ? 1 : 0;
}
PyObject* arg_as_long = PyNumber_Long(arg);
long out = PyLong_AS_LONG(arg_as_long);
Py_DECREF(arg_as_long);
return out;
}
float PyExtNumber_AsFloat(PyObject* arg) {
if (PyFloat_Check(arg)) {
return (float)PyFloat_AS_DOUBLE(arg);
}
#if !PY3K
if (PyInt_Check(arg)) {
return (float)PyInt_AS_LONG(arg);
}
#endif
if (PyLong_Check(arg)) {
return (float)PyLong_AS_LONG(arg);
}
if (PyBool_Check(arg)) {
return (arg == Py_True) ? 1.f : 0.f;
}
PyObject* arg_as_float = PyNumber_Float(arg);
float out = (float)PyFloat_AS_DOUBLE(arg_as_float);
Py_DECREF(arg_as_float);
return out;
}
#define Py_IS_NOTIMPLEMENTED(op) (op == NULL || (PyObject*)op == Py_NotImplemented) // find out if op is NULL or NotImplemented
#define PyType_AS_CSTRING(op) op->ob_type->tp_name
#define Py_RAISE_TYPEERROR_O(str, obj) PyErr_Format(PyExc_TypeError, "%s'%s'", str, PyType_AS_CSTRING(obj))
#define Py_RAISE_TYPEERROR_2O(str, obj1, obj2) PyErr_Format(PyExc_TypeError, "%s'%s' and '%s'", str, PyType_AS_CSTRING(obj1), PyType_AS_CSTRING(obj2))
static char * attr_name_to_cstr(PyObject * name) {
#if PY3K
return PyBytes_AsString(PyUnicode_AsASCIIString(name));
#else
return PyString_AsString(name);
#endif
}
typedef struct {
PyObject_HEAD
double value;
} example_class;
typedef struct {
PyObject_HEAD
Py_ssize_t seq_index;
example_class *sequence;
} example_classIter;
typedef struct {
double value;
} internal_example_class;
static Py_ssize_t example_class_len(example_class * self);
static PyObject* example_class_sq_item(example_class * self, Py_ssize_t index);
static int example_class_sq_setitem(example_class * self, Py_ssize_t index, PyObject * value);
static int example_class_contains(example_class * self, PyObject * value);
static PyObject * example_class_add(PyObject *obj1, PyObject *obj2);
static PyObject * example_class_sub(PyObject *obj1, PyObject *obj2);
static PyObject * example_class_mul(PyObject *obj1, PyObject *obj2);
static PyObject * example_class_mod(PyObject *obj1, PyObject *obj2);
static PyObject * example_class_divmod(PyObject *obj1, PyObject *obj2);
static PyObject * example_class_pow(PyObject * obj1, PyObject * obj2, PyObject * obj3);
static PyObject * example_class_neg(example_class *obj);
static PyObject * example_class_pos(example_class *obj);
static PyObject * example_class_abs(example_class *obj);
static PyObject * example_class_iadd(example_class* self, PyObject *obj);
static PyObject * example_class_isub(example_class* self, PyObject *obj);
static PyObject * example_class_imul(example_class* self, PyObject *obj);
static PyObject * example_class_imod(example_class* self, PyObject *obj);
static PyObject * example_class_ipow(example_class* self, PyObject * obj2, PyObject * obj3);
static PyObject * example_class_floordiv(PyObject *obj1, PyObject *obj2);
static PyObject * example_class_truediv(PyObject *obj1, PyObject *obj2);
static PyObject * example_class_ifloordiv(example_class* self, PyObject *obj);
static PyObject * example_class_itruediv(example_class* self, PyObject *obj);
static void example_class_dealloc(example_class* self);
static PyObject* example_class_str(example_class* self);
static PyObject* example_class_getattr(PyObject* obj, PyObject* name);
static PyObject* example_class_richcompare(example_class* self, PyObject* other, int comp_type);
static PyObject* example_class_geniter(example_class* self);
static int example_class_init(example_class *self, PyObject *args, PyObject *kwds);
static PyObject* example_class_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
static void example_classIter_dealloc(example_classIter *rgstate);
static PyObject* example_classIter_next(example_classIter *rgstate);
static PyObject* example_classIter_new(PyTypeObject *type, PyObject *args, PyObject *kwargs);
static PySequenceMethods example_classSeqMethods = {
/* PySequenceMethods, implementing the sequence protocol
* references:
* https://docs.python.org/3/c-api/typeobj.html#c.PySequenceMethods
* https://docs.python.org/3/c-api/sequence.html
*/
(lenfunc)example_class_len, // sq_length
0, // sq_concat
0, // sq_repeat
(ssizeargfunc)example_class_sq_item, // sq_item
0,
(ssizeobjargproc)example_class_sq_setitem, // sq_ass_item
0,
(objobjproc)example_class_contains, // sq_contains
0, // sq_inplace_concat
0, // sq_inplace_repeat
};
#if PY3K
static PyNumberMethods example_classNumMethods = {
/* PyNumberMethods, implementing the number protocol
* references:
* https://docs.python.org/3/c-api/typeobj.html#c.PyNumberMethods
* https://docs.python.org/3/c-api/number.html
*/
(binaryfunc)example_class_add,
(binaryfunc)example_class_sub,
(binaryfunc)example_class_mul,
(binaryfunc)example_class_mod, //nb_remainder
(binaryfunc)example_class_divmod, //nb_divmod
(ternaryfunc)example_class_pow, //nb_power
(unaryfunc)example_class_neg, //nb_negative
(unaryfunc)example_class_pos, //nb_positive
(unaryfunc)example_class_abs, //nb_absolute
0, //nb_bool
0, //nb_invert
0, //nb_lshift
0, //nb_rshift
0, //nb_and
0, //nb_xor
0, //nb_or
0, //nb_int
0, //nb_reserved
0, //nb_float
(binaryfunc)example_class_iadd, //nb_inplace_add
(binaryfunc)example_class_isub, //nb_inplace_subtract
(binaryfunc)example_class_imul, //nb_inplace_multiply
(binaryfunc)example_class_imod, //nb_inplace_remainder
(ternaryfunc)example_class_ipow, //nb_inplace_power
0, //nb_inplace_lshift
0, //nb_inplace_rshift
0, //nb_inplace_and
0, //nb_inplace_xor
0, //nb_inplace_or
(binaryfunc)example_class_floordiv, //nb_floor_divide
(binaryfunc)example_class_truediv,
(binaryfunc)example_class_ifloordiv, //nb_inplace_floor_divide
(binaryfunc)example_class_itruediv, //nb_inplace_true_divide
0, //nb_index
};
#else
static PyNumberMethods example_classNumMethods = {
/* PyNumberMethods, implementing the number protocol
* references:
* https://docs.python.org/3/c-api/typeobj.html#c.PyNumberMethods
* https://docs.python.org/3/c-api/number.html
*/
(binaryfunc)example_class_add, //nb_add;
(binaryfunc)example_class_sub, //nb_subtract;
(binaryfunc)example_class_mul, //nb_multiply;
(binaryfunc)example_class_truediv, //nb_divide;
(binaryfunc)example_class_mod, //nb_remainder;
(binaryfunc)example_class_divmod, //nb_divmod;
(ternaryfunc)example_class_pow, //nb_power;
(unaryfunc)example_class_neg, //nb_negative;
(unaryfunc)example_class_pos, //nb_positive;
(unaryfunc)example_class_abs, //nb_absolute;
0, //nb_nonzero; /* Used by PyObject_IsTrue */
0, //nb_invert;
0, //nb_lshift;
0, //nb_rshift;
0, //nb_and;
0, //nb_xor;
0, //nb_or;
0, //nb_coerce; /* Used by the coerce() function */
0, //nb_int;
0, //nb_long;
0, //nb_float;
0, //nb_oct;
0, //nb_hex;
/* Added in release 2.0 */
(binaryfunc)example_class_iadd, //nb_inplace_add;
(binaryfunc)example_class_isub, //nb_inplace_subtract;
(binaryfunc)example_class_imul, //nb_inplace_multiply;
(binaryfunc)example_class_itruediv, //nb_inplace_divide;
(binaryfunc)example_class_imod, //nb_inplace_remainder;
(ternaryfunc)example_class_ipow, //nb_inplace_power;
0, //nb_inplace_lshift;
0, //nb_inplace_rshift;
0, //nb_inplace_and;
0, //nb_inplace_xor;
0, //nb_inplace_or;
/* Added in release 2.2 */
(binaryfunc)example_class_floordiv, //nb_floor_divide;
(binaryfunc)example_class_truediv, //nb_true_divide;
(binaryfunc)example_class_ifloordiv, //nb_inplace_floor_divide;
(binaryfunc)example_class_itruediv, //nb_inplace_true_divide;
};
#endif
// example_class
static PyMemberDef example_class_members[] = {
/* PyMemberDef, a structure which describes an attribute of a type which corresponds to a C struct member.
* reference:
* https://docs.python.org/3/c-api/structures.html#c.PyMemberDef
*/
{ "value", T_DOUBLE, offsetof(example_class, value), 0, "value of example_class" },
{ NULL } /* Sentinel */
};
static PyTypeObject example_classType = {
/* PyTypeObject, a structure that defines a new type.
* reference:
* https://docs.python.org/3/c-api/typeobj.html#type-objects
*/
PyVarObject_HEAD_INIT(NULL, 0)
"template.example_class", /* tp_name */
sizeof(example_class), /* tp_basicsize */
0, /* tp_itemsize */
(destructor)example_class_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_reserved */
(reprfunc)example_class_str, /* tp_repr */
&example_classNumMethods, /* tp_as_number */
&example_classSeqMethods, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
(reprfunc)example_class_str, /* tp_str */
(getattrofunc)example_class_getattr, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT |
Py_TPFLAGS_BASETYPE, /* tp_flags */
"example_class( <example_class compatible type> )\nA simple example class holding a double value.", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
(richcmpfunc)example_class_richcompare, /* tp_richcompare */
0, /* tp_weaklistoffset */
(getiterfunc)example_class_geniter, /* tp_iter */
0, /* tp_iternext */
0, /* tp_methods */
example_class_members, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
(initproc)example_class_init, /* tp_init */
0, /* tp_alloc */
(newfunc)example_class_new, /* tp_new */
};
static PyTypeObject example_classIterType = {
PyVarObject_HEAD_INIT(NULL, 0)
"example_classIter", /* tp_name */
sizeof(example_classIter), /* tp_basicsize */
0, /* tp_itemsize */
(destructor)example_classIter_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_reserved */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT, /* tp_flags */
"example_class iterator", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
(iternextfunc)example_classIter_next, /* tp_iternext */
0, /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
(newfunc)example_classIter_new, /* tp_new */
};
static PyObject* pack_example_class(double value) {
example_class* out = (example_class*)example_classType.tp_alloc(&example_classType, 0);
if (out != NULL) {
out->value = value;
}
return (PyObject*)out;
}
static bool unpack_example_class(PyObject * op, internal_example_class* out) {
if (PyObject_TypeCheck(op, &example_classType)) {
out->value = ((example_class*)op)->value;
return true;
}
if (PyExtNumber_Check(op)) {
out->value = PyExtNumber_AsDouble(op);
return true;
}
return false;
}
static void
example_class_dealloc(example_class* self)
{
Py_TYPE(self)->tp_free((PyObject*)self);
}
static PyObject *
example_class_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
example_class *self;
self = (example_class *)type->tp_alloc(type, 0);
if (self != NULL) {
self->value = 0.0;
}
return (PyObject *)self;
}
static int
example_class_init(example_class *self, PyObject *args, PyObject *kwargs)
{
static char *kwlist[] = { "value", NULL };
PyObject * arg1 = NULL;
if (PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist,
&arg1)) {
if (arg1 == NULL) {
return 0;
}
if (PyExtNumber_Check(arg1)) {
self->value = PyExtNumber_AsDouble(arg1);
return 0;
}
}
PyErr_SetString(PyExc_TypeError, "invalid argument type(s) for example_class()");
return -1;
}
// unaryfunc
static PyObject *
example_class_neg(example_class *obj)
{
/* Returns the negation of <obj> on success,
* or NULL on failure. This is the
* equivalent of the Python expression '-obj'.
* (quoted from https://docs.python.org/3/c-api/number.html)
*/
return pack_example_class(-obj->value);
}
static PyObject *
example_class_pos(example_class *obj)
{
/* Returns <obj> on success,
* or NULL on failure. This is the
* equivalent of the Python expression '+obj'.
* (quoted from https://docs.python.org/3/c-api/number.html)
*/
return pack_example_class(obj->value);
}
static PyObject *
example_class_abs(example_class *obj)
{
/* Returns the absolute value of obj,
* or NULL on failure. This is the
* equivalent of the Python expression 'abs(obj)'.
* (quoted from https://docs.python.org/3/c-api/number.html)
*/
return pack_example_class(fabs(obj->value));
}
// binaryfunc
static PyObject *
example_class_add(PyObject *obj1, PyObject *obj2)
{
/* Returns the result of adding obj1 to obj2,
* or NULL on failure. This is the
* equivalent of the Python expression 'obj1 + obj2'.
* (quoted from https://docs.python.org/3/c-api/number.html)
*/
internal_example_class o1, o2;
if (unpack_example_class(obj1, &o1) && unpack_example_class(obj2, &o2)) {
return pack_example_class(
o1.value + o2.value
);
}
Py_RETURN_NOTIMPLEMENTED;
}
static PyObject *
example_class_sub(PyObject *obj1, PyObject *obj2)
{
/* Returns the result of subtracting obj2 from obj1,
* or NULL on failure. This is the
* equivalent of the Python expression 'obj1 - obj2'.
* (quoted from https://docs.python.org/3/c-api/number.html)
*/
internal_example_class o1, o2;
if (unpack_example_class(obj1, &o1) && unpack_example_class(obj2, &o2)) {
return pack_example_class(
o1.value - o2.value
);
}
Py_RETURN_NOTIMPLEMENTED;
}
static PyObject *
example_class_mul(PyObject *obj1, PyObject *obj2)
{
/* Returns the result of multiplying obj1 and obj2,
* or NULL on failure. This is the
* equivalent of the Python expression 'obj1 * obj2'.
* (quoted from https://docs.python.org/3/c-api/number.html)
*/
internal_example_class o1, o2;
if (unpack_example_class(obj1, &o1) && unpack_example_class(obj2, &o2)) {
return pack_example_class(
o1.value * o2.value
);
}
Py_RETURN_NOTIMPLEMENTED;
}
static PyObject *
example_class_truediv(PyObject *obj1, PyObject *obj2)
{
/* Returns the result of dividing obj1 by obj2,
* or NULL on failure. This is the
* equivalent of the Python 3 expression 'obj1 / obj2',
* and the Python 2 expression 'obj1.__truediv__(obj2)'.
*/
internal_example_class o1, o2;
if (unpack_example_class(obj1, &o1) && unpack_example_class(obj2, &o2)) {
return pack_example_class(
o1.value / o2.value
);
}
Py_RETURN_NOTIMPLEMENTED;
}
static PyObject *
example_class_mod(PyObject *obj1, PyObject *obj2)
{
/* Returns the remainder of dividing obj1 by obj2,
* or NULL on failure. This is the
* equivalent of the Python expression 'obj1 % obj2'.
* (quoted from https://docs.python.org/3/c-api/number.html)
*/
internal_example_class o1, o2;
if (unpack_example_class(obj1, &o1) && unpack_example_class(obj2, &o2)) {
return pack_example_class(
fmod(o1.value, o2.value)
);
}
Py_RETURN_NOTIMPLEMENTED;
}
static PyObject *
example_class_floordiv(PyObject *obj1, PyObject *obj2)
{
/* Returns the floor of obj1 divided by obj2,
* or NULL on failure. This is the
* equivalent of the Python expression 'obj1 // obj2'
* and the Python 2 expression 'obj1 / obj2'.
*/
internal_example_class o1, o2;
if (unpack_example_class(obj1, &o1) && unpack_example_class(obj2, &o2)) {
return pack_example_class(
floor(o1.value / o2.value)
);
}
Py_RETURN_NOTIMPLEMENTED;
}
static PyObject *
example_class_divmod(PyObject * obj1, PyObject * obj2) {
/* See the built-in function divmod().
* Returns NULL on failure. This is the
* equivalent of the Python expression 'divmod(obj1, obj2)'.
* (quoted from https://docs.python.org/3/c-api/number.html)
*/
return Py_BuildValue("(OO)", example_class_floordiv(obj1, obj2), example_class_mod(obj1, obj2));
}
// ternaryfunc
static PyObject *
example_class_pow(PyObject * obj1, PyObject * obj2, PyObject * obj3) {
/* See the built-in function pow().
* Returns NULL on failure. This is the
* equivalent of the Python expression 'pow(obj1, obj2, obj3)',
* where obj3 is optional.
* (quoted from https://docs.python.org/3/c-api/number.html)
*/
internal_example_class o1, o2;
if (!unpack_example_class(obj1, &o1) || !unpack_example_class(obj2, &o2)) {
Py_RETURN_NOTIMPLEMENTED;
}
if (obj3 == Py_None) {
return pack_example_class(
pow(o1.value, o2.value)
);
}
internal_example_class o3;
if (unpack_example_class(obj1, &o3)) {
return pack_example_class(
fmod(pow(o1.value, o2.value), o3.value)
);
}
Py_RETURN_NOTIMPLEMENTED;
}
// inplace
// binaryfunc
static PyObject *
example_class_iadd(example_class *self, PyObject *obj)
{
/* Returns the result of adding obj to self,
* or NULL on failure. The operation is done in-place.
* This is the equivalent of the
* Python statement 'self += obj'.
* (quoted from https://docs.python.org/3/c-api/number.html)
*/
example_class * temp = (example_class*)example_class_add((PyObject*)self, obj);
if (Py_IS_NOTIMPLEMENTED(temp)) return (PyObject*)temp;
self->value = temp->value;
Py_DECREF(temp);
Py_INCREF(self);
return (PyObject*)self;
}
static PyObject *
example_class_isub(example_class *self, PyObject *obj)
{
/* Returns the result of subtracting obj from self,
* or NULL on failure. The operation is done in-place.
* This is the equivalent of the
* Python statement 'self -= obj'.
* (quoted from https://docs.python.org/3/c-api/number.html)
*/
example_class * temp = (example_class*)example_class_sub((PyObject*)self, obj);
if (Py_IS_NOTIMPLEMENTED(temp)) return (PyObject*)temp;
self->value = temp->value;
Py_DECREF(temp);
Py_INCREF(self);
return (PyObject*)self;
}
static PyObject *
example_class_imul(example_class *self, PyObject *obj)
{
/* Returns the result of multiplying self by obj,
* or NULL on failure. The operation is done in-place.
* This is the equivalent of the
* Python statement 'self *= obj'.
* (quoted from https://docs.python.org/3/c-api/number.html)
*/
example_class * temp = (example_class*)example_class_mul((PyObject*)self, obj);
if (Py_IS_NOTIMPLEMENTED(temp)) return (PyObject*)temp;
self->value = temp->value;
Py_DECREF(temp);
Py_INCREF(self);
return (PyObject*)self;
}
static PyObject *
example_class_itruediv(example_class *self, PyObject *obj)
{
/* Returns the result of dividing self by obj,
* or NULL on failure. The operation is done in-place.
* This is the equivalent of the
* Python 3 statement 'self /= obj'
* and the Python 2 expression 'self.__itruediv__(obj).
*/
example_class * temp = (example_class*)example_class_truediv((PyObject*)self, obj);
if (Py_IS_NOTIMPLEMENTED(temp)) return (PyObject*)temp;
self->value = temp->value;
Py_DECREF(temp);
Py_INCREF(self);
return (PyObject*)self;
}
static PyObject *
example_class_imod(example_class *self, PyObject *obj)
{
/* Returns the remainder of self divided by obj,
* or NULL on failure. The operation is done in-place.
* This is the equivalent of the
* Python statement 'self %= obj'.
* (quoted from https://docs.python.org/3/c-api/number.html)
*/
example_class * temp = (example_class*)example_class_mod((PyObject*)self, obj);
if (Py_IS_NOTIMPLEMENTED(temp)) return (PyObject*)temp;
self->value = temp->value;
Py_DECREF(temp);
Py_INCREF(self);
return (PyObject*)self;
}
static PyObject *
example_class_ifloordiv(example_class *self, PyObject *obj)
{
/* Returns the floor of self divided by obj,
* or NULL on failure. The operation is done in-place.
* This is the equivalent of the
* Python statement 'self //= obj'.
* and the Python 2 statement 'self /= obj'
* (quoted from https://docs.python.org/3/c-api/number.html)
*/
example_class * temp = (example_class*)example_class_floordiv((PyObject*)self, obj);
if (Py_IS_NOTIMPLEMENTED(temp)) return (PyObject*)temp;
self->value = temp->value;
Py_DECREF(temp);
Py_INCREF(self);
return (PyObject*)self;
}
// ternaryfunc
static PyObject *
example_class_ipow(example_class *self, PyObject *obj1, PyObject * obj2) // obj2 is unused. It points to an invalid address!
{
/* Returns the result of self to the power of obj1,
* or NULL on failure. The operation is done in-place.
* This is the equivalent of the
* Python statement 'self **= obj'.
* (quoted from https://docs.python.org/3/c-api/number.html)
*/
example_class * temp = (example_class*)example_class_pow((PyObject*)self, obj1, Py_None);
if (Py_IS_NOTIMPLEMENTED(temp)) return (PyObject*)temp;
self->value = temp->value;
Py_DECREF(temp);
Py_INCREF(self);
return (PyObject*)self;
}
static PyObject *
example_class_str(example_class* self)
{
char * str_as_cstr = (char*)malloc((30) * sizeof(char));
snprintf(str_as_cstr, 30, "example_class( %12.6g )", self->value);
#if PY3K
PyObject* out = PyUnicode_FromString(str_as_cstr);
#else
PyObject* out = PyString_FromString(str_as_cstr);
#endif
free(str_as_cstr);
return out;
}
static Py_ssize_t example_class_len(example_class * self) {
return (Py_ssize_t)1;
}
static PyObject* example_class_sq_item(example_class * self, Py_ssize_t index) {
switch (index) {
case 0:
return PyFloat_FromDouble(self->value);
default:
PyErr_SetString(PyExc_IndexError, "index out of range");
return NULL;
}
}
static int example_class_sq_setitem(example_class * self, Py_ssize_t index, PyObject * value) {
double value_as_double;
if (PyExtNumber_Check(value)) {
value_as_double = PyExtNumber_AsDouble(value);
}
else {
Py_RAISE_TYPEERROR_O("must be a real number, not ", value);
return -1;
}
switch (index) {
case 0:
self->value = value_as_double;
return 0;
default:
PyErr_SetString(PyExc_IndexError, "index out of range");
return -1;
}
}
static int example_class_contains(example_class * self, PyObject * value) {
double d;
if (PyExtNumber_Check(value)) {
d = PyExtNumber_AsDouble(value);
return (int)(d == self->value);
}
return 0;
}
static PyObject * example_class_richcompare(example_class * self, PyObject * other, int comp_type) {
internal_example_class o2;
if (!unpack_example_class(other, &o2)) {
if (comp_type == Py_EQ || comp_type == Py_NE) {
Py_RETURN_FALSE;
}
Py_RETURN_NOTIMPLEMENTED;
}
switch (comp_type) {
case Py_EQ :
if (self->value == o2.value) Py_RETURN_TRUE;
else Py_RETURN_FALSE;
break;
case Py_NE :
if (self->value != o2.value) Py_RETURN_TRUE;
else Py_RETURN_FALSE;
break;
case Py_LT :
if (self->value < o2.value) Py_RETURN_TRUE;
else Py_RETURN_FALSE;
break;
case Py_LE :
if (self->value <= o2.value) Py_RETURN_TRUE;
else Py_RETURN_FALSE;
break;
case Py_GT :
if (self->value > o2.value) Py_RETURN_TRUE;
else Py_RETURN_FALSE;
break;
case Py_GE :
if (self->value >= o2.value) Py_RETURN_TRUE;
else Py_RETURN_FALSE;
break;
default :
Py_RETURN_NOTIMPLEMENTED;
}
}
static PyObject * example_class_getattr(PyObject * obj, PyObject * name) {
char * name_as_cstr = attr_name_to_cstr(name);
if (strcmp(name_as_cstr, "secret") == 0) {
return PyFloat_FromDouble(pi);
}
return PyObject_GenericGetAttr(obj, name);
}
// iterator
static PyObject *
example_classIter_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
example_class *sequence;
if (!PyArg_UnpackTuple(args, "__iter__", 1, 1, &sequence))
return NULL;
/* Create a new example_classIter and initialize its state - pointing to the last
* index in the sequence.
*/
example_classIter *rgstate = (example_classIter *)type->tp_alloc(type, 0);
if (!rgstate)
return NULL;
rgstate->sequence = sequence;
Py_INCREF(sequence);
rgstate->seq_index = 0;
return (PyObject *)rgstate;
}
static void
example_classIter_dealloc(example_classIter *rgstate)
{
Py_XDECREF(rgstate->sequence);
Py_TYPE(rgstate)->tp_free(rgstate);
}
static PyObject *
example_classIter_next(example_classIter *rgstate)
{
if (rgstate->seq_index < 1) {
if (rgstate->seq_index == 0) {
rgstate->seq_index++;
return PyFloat_FromDouble(rgstate->sequence->value);
}
}
rgstate->seq_index = 1;
Py_CLEAR(rgstate->sequence);
return NULL;
}
static PyObject * example_class_geniter(example_class * self) {
example_classIter *rgstate = (example_classIter *)((&example_classIterType)->tp_alloc(&example_classIterType, 0));
if (!rgstate)
return NULL;
rgstate->sequence = self;
Py_INCREF(self);
rgstate->seq_index = 0;
return (PyObject *)rgstate;
}
static PyObject*
testNO(PyObject* self, PyObject* obj) {
Py_RETURN_NONE;
}
static PyObject*
testO(PyObject* self, PyObject* obj) {
return obj;
}
static PyObject*
testVA(PyObject* self, PyObject* args) {
PyObject* obj1;
PyObject* obj2 = NULL;
if (PyArg_ParseTuple(args, "O|O", &obj1, &obj2)) {
if (obj2 == NULL) {
return obj1;
}
return PyNumber_Add(obj1, obj2);
}
if (PyArg_UnpackTuple(args, "testVA", 1, 2, &obj1, &obj2)) {
if (obj2 == NULL) {
return obj1;
}
return PyNumber_Add(obj1, obj2);
}
Py_RETURN_NONE;
}
static PyObject*
testVK(PyObject* self, PyObject* args, PyObject* kwargs) {
static char* kwlist[] = { "o1", "o2", NULL };
PyObject* obj1;
PyObject* obj2 = NULL;
if (PyArg_ParseTupleAndKeywords(args, kwargs, "O|O", kwlist, &obj1, &obj2)) {
if (obj2 == NULL) {
return obj1;
}
return PyNumber_Add(obj1, obj2);
}
Py_RETURN_NONE;
}
extern "C"
{
static PyMethodDef templatemethods[] = {
/* PyMethodDef, a structure used to describe a method of an extension type.
* reference:
* https://docs.python.org/3/c-api/structures.html#c.PyMethodDef
*/
{ "testNO", (PyCFunction)testNO, METH_NOARGS, "A test function expecting no arguments" },
{ "testO", (PyCFunction)testO, METH_O, "A test function expecting a single argument"},
{ "testVA", (PyCFunction)testVA, METH_VARARGS, "A test function expecting a list of arguments" },
{ "testVK", (PyCFunction)testVK, METH_VARARGS | METH_KEYWORDS, "A test function expecting a list of arguments and keywords" },
{ NULL, NULL, 0, NULL }
};
#if PY3K
static PyModuleDef templatemodule = {