/
HelpersBuiltin.c
946 lines (729 loc) · 23.5 KB
/
HelpersBuiltin.c
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
// Copyright 2019, Kay Hayen, mailto:kay.hayen@gmail.com
//
// Part of "Nuitka", an optimizing Python compiler that is compatible and
// integrates with CPython, but also works on its own.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
/** For calling built-ins, calls it and uses keyword dictionary if necessary.
*
* This helper simplifies calling built-ins with optional arguments that can
* be given as keyword arguments. We basically re-construct the minimal call
* using keywords here. This obviously is for inefficient calls to the original
* built-in and should be avoided.
*
**/
// This file is included from another C file, help IDEs to still parse it on
// its own.
#ifdef __IDE_ONLY__
#include "nuitka/prelude.h"
#endif
static PyObject *CALL_BUILTIN_KW_ARGS(PyObject *callable, PyObject **args, char const **arg_names, int max_args) {
int i = 0;
while (i < max_args) {
if (args[i] == NULL)
break;
CHECK_OBJECT(args[i]);
i++;
}
int usable_args = i;
PyObject *kw_dict = NULL;
while (i < max_args) {
if (args[i] != NULL) {
CHECK_OBJECT(args[i]);
if (kw_dict == NULL) {
kw_dict = PyDict_New();
}
int res = PyDict_SetItemString(kw_dict, arg_names[i], args[i]);
assert(res == 0);
}
i++;
}
PyObject *args_tuple = PyTuple_New(usable_args);
for (i = 0; i < usable_args; i++) {
PyTuple_SET_ITEM(args_tuple, i, args[i]);
Py_INCREF(args[i]);
}
PyObject *result = CALL_FUNCTION(callable, args_tuple, kw_dict);
Py_XDECREF(kw_dict);
Py_DECREF(args_tuple);
return result;
}
/** The "compile" built-in.
*
*/
NUITKA_DEFINE_BUILTIN(compile)
#if PYTHON_VERSION < 300
PyObject *COMPILE_CODE(PyObject *source_code, PyObject *file_name, PyObject *mode, PyObject *flags,
PyObject *dont_inherit)
#else
PyObject *COMPILE_CODE(PyObject *source_code, PyObject *file_name, PyObject *mode, PyObject *flags,
PyObject *dont_inherit, PyObject *optimize)
#endif
{
// May be a source, but also could already be a compiled object, in which
// case this should just return it.
if (PyCode_Check(source_code)) {
Py_INCREF(source_code);
return source_code;
}
PyObject *pos_args = PyTuple_New(3);
PyTuple_SET_ITEM(pos_args, 0, source_code);
Py_INCREF(source_code);
PyTuple_SET_ITEM(pos_args, 1, file_name);
Py_INCREF(file_name);
PyTuple_SET_ITEM(pos_args, 2, mode);
Py_INCREF(mode);
PyObject *kw_args = NULL;
if (flags != NULL) {
if (kw_args == NULL)
kw_args = PyDict_New();
PyDict_SetItemString(kw_args, "flags", flags);
}
if (dont_inherit != NULL) {
if (kw_args == NULL)
kw_args = PyDict_New();
PyDict_SetItemString(kw_args, "dont_inherit", dont_inherit);
}
#if PYTHON_VERSION >= 300
if (optimize != NULL) {
if (kw_args == NULL)
kw_args = PyDict_New();
PyDict_SetItemString(kw_args, "optimize", optimize);
}
#endif
NUITKA_ASSIGN_BUILTIN(compile);
PyObject *result = CALL_FUNCTION(NUITKA_ACCESS_BUILTIN(compile), pos_args, kw_args);
Py_DECREF(pos_args);
Py_XDECREF(kw_args);
return result;
}
/**
* Helper used to deal with exec statement
*/
#if PYTHON_VERSION < 300
extern PyObject *const_str_plain_read;
bool EXEC_FILE_ARG_HANDLING(PyObject **prog, PyObject **name) {
CHECK_OBJECT(*prog);
CHECK_OBJECT(*name);
if (PyFile_Check(*prog)) {
PyObject *old = *name;
*name = PyFile_Name(*prog);
Py_INCREF(*name);
Py_DECREF(old);
if (unlikely(*name == NULL)) {
return false;
}
old = *prog;
*prog = CALL_METHOD_NO_ARGS(*prog, const_str_plain_read);
Py_DECREF(old);
if (unlikely(*prog == NULL)) {
return false;
}
}
return true;
}
#endif
/**
* The "eval" implementation, used for "exec" too.
*/
PyObject *EVAL_CODE(PyObject *code, PyObject *globals, PyObject *locals) {
CHECK_OBJECT(code);
CHECK_OBJECT(globals);
CHECK_OBJECT(locals);
if (PyDict_Check(globals) == 0) {
PyErr_Format(PyExc_TypeError, "exec: arg 2 must be a dictionary or None");
return NULL;
}
// TODO: Our re-formulation prevents this externally, doesn't it.
if (locals == Py_None) {
locals = globals;
}
if (PyMapping_Check(locals) == 0) {
PyErr_Format(PyExc_TypeError, "exec: arg 3 must be a mapping or None");
return NULL;
}
// Set the __builtins__ in globals, it is expected to be present.
if (PyDict_GetItem(globals, const_str_plain___builtins__) == NULL) {
if (PyDict_SetItem(globals, const_str_plain___builtins__, (PyObject *)builtin_module) != 0) {
return NULL;
}
}
#if PYTHON_VERSION < 300
PyObject *result = PyEval_EvalCode((PyCodeObject *)code, globals, locals);
#else
PyObject *result = PyEval_EvalCode(code, globals, locals);
#endif
if (unlikely(result == NULL)) {
return NULL;
}
return result;
}
/** The "open" built-in.
*
* Different for Python2 and Python3, the later has more arguments and
* both accept keyword arguments.
*
**/
NUITKA_DEFINE_BUILTIN(open);
#if PYTHON_VERSION < 300
PyObject *BUILTIN_OPEN(PyObject *file_name, PyObject *mode, PyObject *buffering) {
NUITKA_ASSIGN_BUILTIN(open);
PyObject *args[] = {file_name, mode, buffering};
char const *arg_names[] = {"file_name", "mode", "buffering"};
return CALL_BUILTIN_KW_ARGS(NUITKA_ACCESS_BUILTIN(open), args, arg_names, 3);
}
#else
PyObject *BUILTIN_OPEN(PyObject *file_name, PyObject *mode, PyObject *buffering, PyObject *encoding, PyObject *errors,
PyObject *newline, PyObject *closefd, PyObject *opener) {
NUITKA_ASSIGN_BUILTIN(open);
PyObject *args[] = {file_name, mode, buffering, encoding, errors, newline, closefd, opener};
char const *arg_names[] = {"file_name", "mode", "buffering", "encoding", "errors", "newline", "closefd", "opener"};
return CALL_BUILTIN_KW_ARGS(NUITKA_ACCESS_BUILTIN(open), args, arg_names, 8);
}
#endif
/** The "staticmethod" built-in.
*
**/
NUITKA_DEFINE_BUILTIN(staticmethod)
PyObject *BUILTIN_STATICMETHOD(PyObject *value) {
NUITKA_ASSIGN_BUILTIN(staticmethod);
return CALL_FUNCTION_WITH_SINGLE_ARG(NUITKA_ACCESS_BUILTIN(staticmethod), value);
}
/** The "zip" built-in.
*
**/
NUITKA_DEFINE_BUILTIN(zip)
PyObject *BUILTIN_ZIP(PyObject *value) {
NUITKA_ASSIGN_BUILTIN(zip);
return CALL_FUNCTION_WITH_POSARGS(NUITKA_ACCESS_BUILTIN(zip), value);
}
/** The "classmethod" built-in.
*
**/
NUITKA_DEFINE_BUILTIN(classmethod)
PyObject *BUILTIN_CLASSMETHOD(PyObject *value) {
NUITKA_ASSIGN_BUILTIN(classmethod);
return CALL_FUNCTION_WITH_SINGLE_ARG(NUITKA_ACCESS_BUILTIN(classmethod), value);
}
#if PYTHON_VERSION >= 300
/** The "bytes" built-in.
*
* Only for Python3. There is not BYTES_BUILTIN1 yet, this only delegates to
* the actual built-in which is wasteful. TODO: Have dedicated implementation
* for this.
*
**/
NUITKA_DEFINE_BUILTIN(bytes);
PyObject *BUILTIN_BYTES1(PyObject *value) {
NUITKA_ASSIGN_BUILTIN(bytes);
return CALL_FUNCTION_WITH_SINGLE_ARG(NUITKA_ACCESS_BUILTIN(bytes), value);
}
PyObject *BUILTIN_BYTES3(PyObject *value, PyObject *encoding, PyObject *errors) {
NUITKA_ASSIGN_BUILTIN(bytes);
PyObject *args[] = {value, encoding, errors};
char const *arg_names[] = {"value", "encoding", "errors"};
return CALL_BUILTIN_KW_ARGS(NUITKA_ACCESS_BUILTIN(bytes), args, arg_names, 3);
}
#endif
/** The "bin" built-in.
*
**/
PyObject *BUILTIN_BIN(PyObject *value) {
// Note: I don't really know why "oct" and "hex" don't use this as well.
PyObject *result = PyNumber_ToBase(value, 2);
if (unlikely(result == NULL)) {
return NULL;
}
return result;
}
/** The "oct" built-in.
*
**/
PyObject *BUILTIN_OCT(PyObject *value) {
#if PYTHON_VERSION >= 300
PyObject *result = PyNumber_ToBase(value, 8);
if (unlikely(result == NULL)) {
return NULL;
}
return result;
#else
if (unlikely(value == NULL)) {
PyErr_Format(PyExc_TypeError, "oct() argument can't be converted to oct");
return NULL;
}
PyNumberMethods *nb = Py_TYPE(value)->tp_as_number;
if (unlikely(nb == NULL || nb->nb_oct == NULL)) {
PyErr_Format(PyExc_TypeError, "oct() argument can't be converted to oct");
return NULL;
}
PyObject *result = (*nb->nb_oct)(value);
if (result) {
if (unlikely(!PyString_Check(result))) {
PyErr_Format(PyExc_TypeError, "__oct__ returned non-string (type %s)", Py_TYPE(result)->tp_name);
Py_DECREF(result);
return NULL;
}
}
return result;
#endif
}
/** The "hex" built-in.
*
**/
PyObject *BUILTIN_HEX(PyObject *value) {
#if PYTHON_VERSION >= 300
PyObject *result = PyNumber_ToBase(value, 16);
if (unlikely(result == NULL)) {
return NULL;
}
return result;
#else
if (unlikely(value == NULL)) {
PyErr_Format(PyExc_TypeError, "hex() argument can't be converted to hex");
return NULL;
}
PyNumberMethods *nb = Py_TYPE(value)->tp_as_number;
if (unlikely(nb == NULL || nb->nb_hex == NULL)) {
PyErr_Format(PyExc_TypeError, "hex() argument can't be converted to hex");
return NULL;
}
PyObject *result = (*nb->nb_hex)(value);
if (likely(result)) {
if (unlikely(!PyString_Check(result))) {
PyErr_Format(PyExc_TypeError, "__hex__ returned non-string (type %s)", Py_TYPE(result)->tp_name);
Py_DECREF(result);
return NULL;
}
}
return result;
#endif
}
/** The "hash" built-in.
*
**/
PyObject *BUILTIN_HASH(PyObject *value) {
Py_hash_t hash = PyObject_Hash(value);
if (unlikely(hash == -1)) {
return NULL;
}
#if PYTHON_VERSION < 300
return PyInt_FromLong(hash);
#else
return PyLong_FromSsize_t(hash);
#endif
}
/** The "bytearray" built-in.
*
* These should be more in-lined maybe, as a lot of checks are not necessary
* and the error checking for the 3 arguments variant may even not be enough,
* as it could be keyword arguments.
*
**/
PyObject *BUILTIN_BYTEARRAY1(PyObject *value) {
PyObject *result = PyByteArray_FromObject(value);
if (unlikely(result == NULL)) {
return NULL;
}
return result;
}
NUITKA_DEFINE_BUILTIN(bytearray)
PyObject *BUILTIN_BYTEARRAY3(PyObject *string, PyObject *encoding, PyObject *errors) {
CHECK_OBJECT(string);
CHECK_OBJECT(encoding);
NUITKA_ASSIGN_BUILTIN(bytearray);
if (errors == NULL) {
PyObject *args[] = {string, encoding};
PyObject *result = CALL_FUNCTION_WITH_ARGS2(NUITKA_ACCESS_BUILTIN(bytearray), args);
return result;
} else {
PyObject *args[] = {string, encoding, errors};
PyObject *result = CALL_FUNCTION_WITH_ARGS3(NUITKA_ACCESS_BUILTIN(bytearray), args);
return result;
}
}
/** The "iter" built-in.
*
* This comes in two flavors, with one or two arguments. The second one
* creates a "calliterobject" that is private to CPython. We define it here
* for ourselves. The one argument version is in headers for in-lining of
* the code.
*
**/
// From CPython:
typedef struct {
PyObject_HEAD PyObject *it_callable;
PyObject *it_sentinel;
} calliterobject;
PyObject *BUILTIN_ITER2(PyObject *callable, PyObject *sentinel) {
calliterobject *result = PyObject_GC_New(calliterobject, &PyCallIter_Type);
if (unlikely(result == NULL)) {
return NULL;
}
// Note: References were taken at call site already.
result->it_callable = callable;
Py_INCREF(callable);
result->it_sentinel = sentinel;
Py_INCREF(sentinel);
Nuitka_GC_Track(result);
return (PyObject *)result;
}
/** The "type" built-in.
*
* This comes in two flavors, one being the detection of a values type,
* and 3 argument variant creates a new type.
*
**/
PyObject *BUILTIN_TYPE1(PyObject *arg) {
PyObject *result = (PyObject *)Py_TYPE(arg);
Py_INCREF(result);
return result;
}
extern PyObject *const_str_plain___module__;
PyObject *BUILTIN_TYPE3(PyObject *module_name, PyObject *name, PyObject *bases, PyObject *dict) {
PyObject *pos_args = PyTuple_New(3);
PyTuple_SET_ITEM(pos_args, 0, name);
Py_INCREF(name);
PyTuple_SET_ITEM(pos_args, 1, bases);
Py_INCREF(bases);
PyTuple_SET_ITEM(pos_args, 2, dict);
Py_INCREF(dict);
PyObject *result = PyType_Type.tp_new(&PyType_Type, pos_args, NULL);
if (unlikely(result == NULL)) {
Py_DECREF(pos_args);
return NULL;
}
PyTypeObject *type = Py_TYPE(result);
if (likely(PyType_IsSubtype(type, &PyType_Type))) {
if (
#if PYTHON_VERSION < 300
PyType_HasFeature(type, Py_TPFLAGS_HAVE_CLASS) &&
#endif
type->tp_init != NULL) {
int res = type->tp_init(result, pos_args, NULL);
if (unlikely(res < 0)) {
Py_DECREF(pos_args);
Py_DECREF(result);
return NULL;
}
}
}
Py_DECREF(pos_args);
int res = PyObject_SetAttr(result, const_str_plain___module__, module_name);
if (res < 0) {
return NULL;
}
return result;
}
/** The "super" built-in.
*
* This uses a private structure "superobject" that we declare here too.
*
**/
typedef struct {
PyObject_HEAD PyTypeObject *type;
PyObject *obj;
PyTypeObject *obj_type;
} superobject;
extern PyObject *const_str_plain___class__;
PyObject *BUILTIN_SUPER(PyObject *type, PyObject *object) {
CHECK_OBJECT(type);
superobject *result = PyObject_GC_New(superobject, &PySuper_Type);
assert(result);
if (object == Py_None) {
object = NULL;
}
if (unlikely(PyType_Check(type) == false)) {
PyErr_Format(PyExc_RuntimeError, "super(): __class__ is not a type (%s)", Py_TYPE(type)->tp_name);
return NULL;
}
result->type = (PyTypeObject *)type;
Py_INCREF(type);
if (object) {
result->obj = object;
Py_INCREF(object);
if (PyType_Check(object) && PyType_IsSubtype((PyTypeObject *)object, (PyTypeObject *)type)) {
result->obj_type = (PyTypeObject *)object;
Py_INCREF(object);
} else if (PyType_IsSubtype(Py_TYPE(object), (PyTypeObject *)type)) {
result->obj_type = Py_TYPE(object);
Py_INCREF(result->obj_type);
} else {
PyObject *class_attr = PyObject_GetAttr(object, const_str_plain___class__);
if (likely(class_attr != NULL && PyType_Check(class_attr) &&
(PyTypeObject *)class_attr != Py_TYPE(object))) {
result->obj_type = (PyTypeObject *)class_attr;
} else {
if (class_attr == NULL) {
CLEAR_ERROR_OCCURRED();
} else {
Py_DECREF(class_attr);
}
PyErr_Format(PyExc_TypeError, "super(type, obj): obj must be an instance or subtype of type");
return NULL;
}
}
} else {
result->obj = NULL;
result->obj_type = NULL;
}
Nuitka_GC_Track(result);
CHECK_OBJECT((PyObject *)result);
assert(Py_TYPE(result) == &PySuper_Type);
return (PyObject *)result;
}
/** The "callable" built-in.
*
**/
PyObject *BUILTIN_CALLABLE(PyObject *value) {
int res = PyCallable_Check(value);
PyObject *result = BOOL_FROM(res != 0);
Py_INCREF(result);
return result;
}
/* The "getattr" built-in with default value.
*
* We might want to split it off for a variant without default value.
*
**/
PyObject *BUILTIN_GETATTR(PyObject *object, PyObject *attribute, PyObject *default_value) {
#if PYTHON_VERSION < 300
if (PyUnicode_Check(attribute)) {
attribute = _PyUnicode_AsDefaultEncodedString(attribute, NULL);
if (unlikely(attribute == NULL)) {
return NULL;
}
}
if (unlikely(!PyString_Check(attribute))) {
PyErr_Format(PyExc_TypeError, "getattr(): attribute name must be string");
return NULL;
}
#else
if (!PyUnicode_Check(attribute)) {
PyErr_Format(PyExc_TypeError, "getattr(): attribute name must be string");
return NULL;
}
#endif
PyObject *result = PyObject_GetAttr(object, attribute);
if (result == NULL) {
if (default_value != NULL && EXCEPTION_MATCH_BOOL_SINGLE(GET_ERROR_OCCURRED(), PyExc_AttributeError)) {
CLEAR_ERROR_OCCURRED();
Py_INCREF(default_value);
return default_value;
} else {
return NULL;
}
} else {
return result;
}
}
/** The "setattr" built-in.
*
**/
PyObject *BUILTIN_SETATTR(PyObject *object, PyObject *attribute, PyObject *value) {
int res = PyObject_SetAttr(object, attribute, value);
if (res < 0) {
return NULL;
}
Py_INCREF(Py_None);
return Py_None;
}
/** The "divmod" built-in.
*
* This is really a binary number operation and probably should live
* where the others are.
*
**/
PyObject *BUILTIN_DIVMOD(PyObject *operand1, PyObject *operand2) {
CHECK_OBJECT(operand1);
CHECK_OBJECT(operand2);
binaryfunc slot1 = NULL;
binaryfunc slot2 = NULL;
PyTypeObject *type1 = Py_TYPE(operand1);
PyTypeObject *type2 = Py_TYPE(operand2);
if (type1->tp_as_number != NULL && NEW_STYLE_NUMBER(operand1)) {
slot1 = type1->tp_as_number->nb_divmod;
}
if (type1 != type2) {
if (type2->tp_as_number != NULL && NEW_STYLE_NUMBER(operand2)) {
slot2 = type2->tp_as_number->nb_divmod;
if (slot1 == slot2) {
slot2 = NULL;
}
}
}
if (slot1 != NULL) {
if (slot2 && PyType_IsSubtype(type2, type1)) {
PyObject *x = slot2(operand1, operand2);
if (x != Py_NotImplemented) {
if (unlikely(x == NULL)) {
return NULL;
}
return x;
}
Py_DECREF(x);
slot2 = NULL;
}
PyObject *x = slot1(operand1, operand2);
if (x != Py_NotImplemented) {
if (unlikely(x == NULL)) {
return NULL;
}
return x;
}
Py_DECREF(x);
}
if (slot2 != NULL) {
PyObject *x = slot2(operand1, operand2);
if (x != Py_NotImplemented) {
if (unlikely(x == NULL)) {
return NULL;
}
return x;
}
Py_DECREF(x);
}
#if PYTHON_VERSION < 300
if (!NEW_STYLE_NUMBER(operand1) || !NEW_STYLE_NUMBER(operand2)) {
int err = PyNumber_CoerceEx(&operand1, &operand2);
if (err < 0) {
return NULL;
}
if (err == 0) {
PyNumberMethods *mv = Py_TYPE(operand1)->tp_as_number;
if (mv) {
binaryfunc slot = mv->nb_divmod;
if (slot != NULL) {
PyObject *x = slot(operand1, operand2);
Py_DECREF(operand1);
Py_DECREF(operand2);
if (unlikely(x == NULL)) {
return NULL;
}
return x;
}
}
// CoerceEx did that
Py_DECREF(operand1);
Py_DECREF(operand2);
}
}
#endif
PyErr_Format(PyExc_TypeError, "unsupported operand type(s) for divmod(): '%s' and '%s'", type1->tp_name,
type2->tp_name);
return NULL;
}
PyObject *BUILTIN_INT2(PyObject *value, PyObject *base) {
#if PYTHON_VERSION < 340
long base_int = PyInt_AsLong(base);
#else
Py_ssize_t base_int = PyNumber_AsSsize_t(base, NULL);
#endif
if (unlikely(base_int == -1)) {
PyObject *error = GET_ERROR_OCCURRED();
if (likely(error)) {
#if PYTHON_VERSION >= 300
if (EXCEPTION_MATCH_BOOL_SINGLE(error, PyExc_OverflowError)) {
PyErr_Format(PyExc_ValueError,
#if PYTHON_VERSION < 324
"int() arg 2 must be >= 2 and <= 36"
#elif PYTHON_VERSION < 364
"int() base must be >= 2 and <= 36"
#else
"int() base must be >= 2 and <= 36, or 0"
#endif
);
}
#endif
return NULL;
}
}
#if PYTHON_VERSION >= 300
if (unlikely((base_int != 0 && base_int < 2) || base_int > 36)) {
PyErr_Format(PyExc_ValueError,
#if PYTHON_VERSION < 324
"int() arg 2 must be >= 2 and <= 36"
#elif PYTHON_VERSION < 364
"int() base must be >= 2 and <= 36"
#else
"int() base must be >= 2 and <= 36, or 0"
#endif
);
return NULL;
}
#endif
#if PYTHON_VERSION < 300
if (unlikely(!Nuitka_String_Check(value) && !PyUnicode_Check(value))) {
PyErr_Format(PyExc_TypeError, "int() can't convert non-string with explicit base");
return NULL;
}
char *value_str = Nuitka_String_AsString(value);
if (unlikely(value_str == NULL)) {
return NULL;
}
PyObject *result = PyInt_FromString(value_str, NULL, base_int);
if (unlikely(result == NULL)) {
return NULL;
}
return result;
#else
if (PyUnicode_Check(value)) {
return PyLong_FromUnicodeObject(value, (int)base_int);
} else if (PyBytes_Check(value) || PyByteArray_Check(value)) {
// Check for "NUL" as PyLong_FromString has no length parameter,
Py_ssize_t size = Py_SIZE(value);
char *value_str;
if (PyByteArray_Check(value)) {
value_str = PyByteArray_AS_STRING(value);
} else {
value_str = PyBytes_AS_STRING(value);
}
PyObject *result = NULL;
if (size != 0 && strlen(value_str) == (size_t)size) {
result = PyInt_FromString(value_str, NULL, (int)base_int);
}
if (unlikely(result == NULL)) {
PyErr_Format(PyExc_ValueError, "invalid literal for int() with base %d: %R", base_int, value);
return NULL;
}
return result;
} else {
PyErr_Format(PyExc_TypeError, "int() can't convert non-string with explicit base");
return NULL;
}
#endif
}
#if PYTHON_VERSION < 300
// Note: Python3 uses TO_INT2 function.
PyObject *BUILTIN_LONG2(PyObject *value, PyObject *base) {
long base_int = PyInt_AsLong(base);
if (unlikely(base_int == -1)) {
if (likely(ERROR_OCCURRED())) {
return NULL;
}
}
if (unlikely(!Nuitka_String_Check(value) && !PyUnicode_Check(value))) {
PyErr_Format(PyExc_TypeError, "long() can't convert non-string with explicit base");
return NULL;
}
char *value_str = Nuitka_String_AsString(value);
if (unlikely(value_str == NULL)) {
return NULL;
}
PyObject *result = PyLong_FromString(value_str, NULL, base_int);
if (unlikely(result == NULL)) {
return NULL;
}
return result;
}
#endif