Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dd8dbdb

Browse files
committed
The real suport for augmented assignment: new opcodes, new PyNumber and
PySequence methods and functions, new tokens.
1 parent e289e0b commit dd8dbdb

6 files changed

Lines changed: 223 additions & 58 deletions

File tree

Include/abstract.h

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,108 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
664664
float(o).
665665
*/
666666

667+
/* In-place variants of (some of) the above number protocol functions */
668+
669+
DL_IMPORT(PyObject *) PyNumber_InPlaceAdd(PyObject *o1, PyObject *o2);
670+
671+
/*
672+
Returns the result of adding o2 to o1, possibly in-place, or null
673+
on failure. This is the equivalent of the Python expression:
674+
o1 += o2.
675+
676+
*/
677+
678+
DL_IMPORT(PyObject *) PyNumber_InPlaceSubtract(PyObject *o1, PyObject *o2);
679+
680+
/*
681+
Returns the result of subtracting o2 from o1, possibly in-place or
682+
null on failure. This is the equivalent of the Python expression:
683+
o1 -= o2.
684+
685+
*/
686+
687+
DL_IMPORT(PyObject *) PyNumber_InPlaceMultiply(PyObject *o1, PyObject *o2);
688+
689+
/*
690+
Returns the result of multiplying o1 by o2, possibly in-place, or
691+
null on failure. This is the equivalent of the Python expression:
692+
o1 *= o2.
693+
694+
*/
695+
696+
DL_IMPORT(PyObject *) PyNumber_InPlaceDivide(PyObject *o1, PyObject *o2);
697+
698+
/*
699+
Returns the result of dividing o1 by o2, possibly in-place, or null
700+
on failure. This is the equivalent of the Python expression:
701+
o1 /= o2.
702+
703+
*/
704+
705+
DL_IMPORT(PyObject *) PyNumber_InPlaceRemainder(PyObject *o1, PyObject *o2);
706+
707+
/*
708+
Returns the remainder of dividing o1 by o2, possibly in-place, or
709+
null on failure. This is the equivalent of the Python expression:
710+
o1 %= o2.
711+
712+
*/
713+
714+
DL_IMPORT(PyObject *) PyNumber_InPlacePower(PyObject *o1, PyObject *o2,
715+
PyObject *o3);
716+
717+
/*
718+
Returns the result of raising o1 to the power of o2, possibly
719+
in-place, or null on failure. This is the equivalent of the Python
720+
expression: o1 **= o2, or pow(o1, o2, o3) if o3 is present.
721+
722+
*/
723+
724+
DL_IMPORT(PyObject *) PyNumber_InPlaceLshift(PyObject *o1, PyObject *o2);
725+
726+
/*
727+
Returns the result of left shifting o1 by o2, possibly in-place, or
728+
null on failure. This is the equivalent of the Python expression:
729+
o1 <<= o2.
730+
731+
*/
732+
733+
DL_IMPORT(PyObject *) PyNumber_InPlaceRshift(PyObject *o1, PyObject *o2);
734+
735+
/*
736+
Returns the result of right shifting o1 by o2, possibly in-place or
737+
null on failure. This is the equivalent of the Python expression:
738+
o1 >>= o2.
739+
740+
*/
741+
742+
DL_IMPORT(PyObject *) PyNumber_InPlaceAnd(PyObject *o1, PyObject *o2);
743+
744+
/*
745+
Returns the result of bitwise and of o1 and o2, possibly in-place,
746+
or null on failure. This is the equivalent of the Python
747+
expression: o1 &= o2.
748+
749+
*/
750+
751+
DL_IMPORT(PyObject *) PyNumber_InPlaceXor(PyObject *o1, PyObject *o2);
752+
753+
/*
754+
Returns the bitwise exclusive or of o1 by o2, possibly in-place, or
755+
null on failure. This is the equivalent of the Python expression:
756+
o1 ^= o2.
757+
758+
*/
759+
760+
DL_IMPORT(PyObject *) PyNumber_InPlaceOr(PyObject *o1, PyObject *o2);
761+
762+
/*
763+
Returns the result of bitwise or or o1 and o2, possibly in-place,
764+
or null on failure. This is the equivalent of the Python
765+
expression: o1 |= o2.
766+
767+
*/
768+
667769

668770
/* Sequence protocol:*/
669771

@@ -824,6 +926,26 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
824926
expression: o.index(value).
825927
*/
826928

929+
/* In-place versions of some of the above Sequence functions. */
930+
931+
DL_IMPORT(PyObject *) PySequence_InPlaceConcat(PyObject *o1, PyObject *o2);
932+
933+
/*
934+
Append o2 to o1, in-place when possible. Return the resulting
935+
object, which could be o1, or NULL on failure. This is the
936+
equivalent of the Python expression: o1 += o2.
937+
938+
*/
939+
940+
DL_IMPORT(PyObject *) PySequence_InPlaceRepeat(PyObject *o, int count);
941+
942+
/*
943+
Repeat o1 by count, in-place when possible. Return the resulting
944+
object, which could be o1, or NULL on failure. This is the
945+
equivalent of the Python expression: o1 *= count.
946+
947+
*/
948+
827949
/* Mapping protocol:*/
828950

829951
DL_IMPORT(int) PyMapping_Check(PyObject *o);

Include/classobject.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ extern DL_IMPORT(PyObject *) PyInstance_DoBinOp(PyObject *, PyObject *,
7373
PyObject * (*)(PyObject *,
7474
PyObject *));
7575

76+
extern DL_IMPORT(int)
77+
PyInstance_HalfBinOp(PyObject *, PyObject *, char *, PyObject **,
78+
PyObject * (*)(PyObject *, PyObject *), int);
79+
7680
#ifdef __cplusplus
7781
}
7882
#endif

Include/graminit.h

Lines changed: 52 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -10,54 +10,55 @@
1010
#define simple_stmt 265
1111
#define small_stmt 266
1212
#define expr_stmt 267
13-
#define print_stmt 268
14-
#define del_stmt 269
15-
#define pass_stmt 270
16-
#define flow_stmt 271
17-
#define break_stmt 272
18-
#define continue_stmt 273
19-
#define return_stmt 274
20-
#define raise_stmt 275
21-
#define import_stmt 276
22-
#define import_as_name 277
23-
#define dotted_as_name 278
24-
#define dotted_name 279
25-
#define global_stmt 280
26-
#define exec_stmt 281
27-
#define assert_stmt 282
28-
#define compound_stmt 283
29-
#define if_stmt 284
30-
#define while_stmt 285
31-
#define for_stmt 286
32-
#define try_stmt 287
33-
#define except_clause 288
34-
#define suite 289
35-
#define test 290
36-
#define and_test 291
37-
#define not_test 292
38-
#define comparison 293
39-
#define comp_op 294
40-
#define expr 295
41-
#define xor_expr 296
42-
#define and_expr 297
43-
#define shift_expr 298
44-
#define arith_expr 299
45-
#define term 300
46-
#define factor 301
47-
#define power 302
48-
#define atom 303
49-
#define listmaker 304
50-
#define lambdef 305
51-
#define trailer 306
52-
#define subscriptlist 307
53-
#define subscript 308
54-
#define sliceop 309
55-
#define exprlist 310
56-
#define testlist 311
57-
#define dictmaker 312
58-
#define classdef 313
59-
#define arglist 314
60-
#define argument 315
61-
#define list_iter 316
62-
#define list_for 317
63-
#define list_if 318
13+
#define augassign 268
14+
#define print_stmt 269
15+
#define del_stmt 270
16+
#define pass_stmt 271
17+
#define flow_stmt 272
18+
#define break_stmt 273
19+
#define continue_stmt 274
20+
#define return_stmt 275
21+
#define raise_stmt 276
22+
#define import_stmt 277
23+
#define import_as_name 278
24+
#define dotted_as_name 279
25+
#define dotted_name 280
26+
#define global_stmt 281
27+
#define exec_stmt 282
28+
#define assert_stmt 283
29+
#define compound_stmt 284
30+
#define if_stmt 285
31+
#define while_stmt 286
32+
#define for_stmt 287
33+
#define try_stmt 288
34+
#define except_clause 289
35+
#define suite 290
36+
#define test 291
37+
#define and_test 292
38+
#define not_test 293
39+
#define comparison 294
40+
#define comp_op 295
41+
#define expr 296
42+
#define xor_expr 297
43+
#define and_expr 298
44+
#define shift_expr 299
45+
#define arith_expr 300
46+
#define term 301
47+
#define factor 302
48+
#define power 303
49+
#define atom 304
50+
#define listmaker 305
51+
#define lambdef 306
52+
#define trailer 307
53+
#define subscriptlist 308
54+
#define subscript 309
55+
#define sliceop 310
56+
#define exprlist 311
57+
#define testlist 312
58+
#define dictmaker 313
59+
#define classdef 314
60+
#define arglist 315
61+
#define argument 316
62+
#define list_iter 317
63+
#define list_for 318
64+
#define list_if 319

Include/object.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,17 @@ typedef struct {
151151
unaryfunc nb_float;
152152
unaryfunc nb_oct;
153153
unaryfunc nb_hex;
154+
binaryfunc nb_inplace_add;
155+
binaryfunc nb_inplace_subtract;
156+
binaryfunc nb_inplace_multiply;
157+
binaryfunc nb_inplace_divide;
158+
binaryfunc nb_inplace_remainder;
159+
ternaryfunc nb_inplace_power;
160+
binaryfunc nb_inplace_lshift;
161+
binaryfunc nb_inplace_rshift;
162+
binaryfunc nb_inplace_and;
163+
binaryfunc nb_inplace_xor;
164+
binaryfunc nb_inplace_or;
154165
} PyNumberMethods;
155166

156167
typedef struct {
@@ -162,6 +173,8 @@ typedef struct {
162173
intobjargproc sq_ass_item;
163174
intintobjargproc sq_ass_slice;
164175
objobjproc sq_contains;
176+
binaryfunc sq_inplace_concat;
177+
intargfunc sq_inplace_repeat;
165178
} PySequenceMethods;
166179

167180
typedef struct {
@@ -315,8 +328,12 @@ given type object has a specified feature.
315328
#define Py_TPFLAGS_GC 0
316329
#endif
317330

331+
/* PySequenceMethods and PyNumberMethods contain in-place operators */
332+
#define Py_TPFLAGS_HAVE_INPLACEOPS (1L<<3)
333+
318334
#define Py_TPFLAGS_DEFAULT (Py_TPFLAGS_HAVE_GETCHARBUFFER | \
319-
Py_TPFLAGS_HAVE_SEQUENCE_IN)
335+
Py_TPFLAGS_HAVE_SEQUENCE_IN | \
336+
Py_TPFLAGS_HAVE_INPLACEOPS)
320337

321338
#define PyType_HasFeature(t,f) (((t)->tp_flags & (f)) != 0)
322339

Include/opcode.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
2121
#define ROT_TWO 2
2222
#define ROT_THREE 3
2323
#define DUP_TOP 4
24+
#define ROT_FOUR 5
2425

2526
#define UNARY_POSITIVE 10
2627
#define UNARY_NEGATIVE 11
@@ -47,6 +48,11 @@ redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
4748
#define DELETE_SLICE 50
4849
/* Also uses 51-53 */
4950

51+
#define INPLACE_ADD 55
52+
#define INPLACE_SUBTRACT 56
53+
#define INPLACE_MULTIPLY 57
54+
#define INPLACE_DIVIDE 58
55+
#define INPLACE_MODULO 59
5056
#define STORE_SUBSCR 60
5157
#define DELETE_SUBSCR 61
5258

@@ -55,14 +61,18 @@ redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
5561
#define BINARY_AND 64
5662
#define BINARY_XOR 65
5763
#define BINARY_OR 66
58-
64+
#define INPLACE_POWER 67
5965

6066
#define PRINT_EXPR 70
6167
#define PRINT_ITEM 71
6268
#define PRINT_NEWLINE 72
6369
#define PRINT_ITEM_TO 73
6470
#define PRINT_NEWLINE_TO 74
65-
71+
#define INPLACE_LSHIFT 75
72+
#define INPLACE_RSHIFT 76
73+
#define INPLACE_AND 77
74+
#define INPLACE_XOR 78
75+
#define INPLACE_OR 79
6676
#define BREAK_LOOP 80
6777

6878
#define LOAD_LOCALS 82
@@ -84,7 +94,7 @@ redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
8494
#define DELETE_ATTR 96 /* "" */
8595
#define STORE_GLOBAL 97 /* "" */
8696
#define DELETE_GLOBAL 98 /* "" */
87-
97+
#define DUP_TOPX 99 /* number of items to duplicate */
8898
#define LOAD_CONST 100 /* Index in const list */
8999
#define LOAD_NAME 101 /* Index in name list */
90100
#define BUILD_TUPLE 102 /* Number of tuple items */

Include/token.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,21 @@ extern "C" {
5353
#define LEFTSHIFT 34
5454
#define RIGHTSHIFT 35
5555
#define DOUBLESTAR 36
56+
#define PLUSEQUAL 37
57+
#define MINEQUAL 38
58+
#define STAREQUAL 39
59+
#define SLASHEQUAL 40
60+
#define PERCENTEQUAL 41
61+
#define AMPEREQUAL 42
62+
#define VBAREQUAL 43
63+
#define CIRCUMFLEXEQUAL 44
64+
#define LEFTSHIFTEQUAL 45
65+
#define RIGHTSHIFTEQUAL 46
66+
#define DOUBLESTAREQUAL 47
5667
/* Don't forget to update the table _PyParser_TokenNames in tokenizer.c! */
57-
#define OP 37
58-
#define ERRORTOKEN 38
59-
#define N_TOKENS 39
68+
#define OP 48
69+
#define ERRORTOKEN 49
70+
#define N_TOKENS 50
6071

6172
/* Special definitions for cooperation with parser */
6273

0 commit comments

Comments
 (0)