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

Skip to content

Commit 104a7bc

Browse files
committed
Support for augmented assignment in the UserList, UserDict, UserString and
rfc822 (Addresslist) modules. Also a preliminary testcase for augmented assignment, which should actually be merged with the test_class testcase I added last week.
1 parent 434d082 commit 104a7bc

6 files changed

Lines changed: 327 additions & 1 deletion

File tree

Lib/UserList.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,20 @@ def __radd__(self, other):
5151
return self.__class__(other + self.data)
5252
else:
5353
return self.__class__(list(other) + self.data)
54+
def __iadd__(self, other):
55+
if isinstance(other, UserList):
56+
self.data += other.data
57+
elif isinstance(other, type(self.data)):
58+
self.data += other
59+
else:
60+
self.data += list(other)
61+
return self
5462
def __mul__(self, n):
5563
return self.__class__(self.data*n)
5664
__rmul__ = __mul__
65+
def __imul__(self, n):
66+
self.data *= n
67+
return self
5768
def append(self, item): self.data.append(item)
5869
def insert(self, i, item): self.data.insert(i, item)
5970
def pop(self, i=-1): return self.data.pop(i)

Lib/UserString.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,20 @@ def __radd__(self, other):
5050
return self.__class__(other + self.data)
5151
else:
5252
return self.__class__(str(other) + self.data)
53+
def __iadd__(self, other):
54+
if isinstance(other, UserString):
55+
self.data += other.data
56+
elif isinstance(other, StringType) or isinstance(other, UnicodeType):
57+
self.data += other
58+
else
59+
self.data += str(other)
60+
return self
5361
def __mul__(self, n):
5462
return self.__class__(self.data*n)
5563
__rmul__ = __mul__
64+
def __imull__(self, n):
65+
self.data += n
66+
return self
5667

5768
# the following methods are defined in alphabetical order:
5869
def capitalize(self): return self.__class__(self.data.capitalize())

Lib/dis.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,11 @@ def jabs_op(name, op):
182182
def_op('DELETE_SLICE+2', 52)
183183
def_op('DELETE_SLICE+3', 53)
184184

185+
def_op('INPLACE_ADD', 55)
186+
def_op('INPLACE_SUBTRACT', 56)
187+
def_op('INPLACE_MULTIPLY', 57)
188+
def_op('INPLACE_DIVIDE', 58)
189+
def_op('INPLACE_MODULO', 59)
185190
def_op('STORE_SUBSCR', 60)
186191
def_op('DELETE_SUBSCR', 61)
187192

@@ -190,13 +195,18 @@ def jabs_op(name, op):
190195
def_op('BINARY_AND', 64)
191196
def_op('BINARY_XOR', 65)
192197
def_op('BINARY_OR', 66)
198+
def_op('INPLACE_POWER', 67)
193199

194200
def_op('PRINT_EXPR', 70)
195201
def_op('PRINT_ITEM', 71)
196202
def_op('PRINT_NEWLINE', 72)
197203
def_op('PRINT_ITEM_TO', 73)
198204
def_op('PRINT_NEWLINE_TO', 74)
199-
205+
def_op('INPLACE_LSHIFT', 75)
206+
def_op('INPLACE_RSHIFT', 76)
207+
def_op('INPLACE_AND', 77)
208+
def_op('INPLACE_XOR', 78)
209+
def_op('INPLACE_OR', 79)
200210
def_op('BREAK_LOOP', 80)
201211

202212
def_op('LOAD_LOCALS', 82)

Lib/rfc822.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,13 @@ def __add__(self, other):
763763
newaddr.addresslist.append(x)
764764
return newaddr
765765

766+
def __iadd__(self, other):
767+
# Set union, in-place
768+
for x in other.addresslist:
769+
if not x in self.addresslist:
770+
self.addresslist.append(x)
771+
return self
772+
766773
def __sub__(self, other):
767774
# Set difference
768775
newaddr = AddressList(None)
@@ -771,6 +778,13 @@ def __sub__(self, other):
771778
newaddr.addresslist.append(x)
772779
return newaddr
773780

781+
def __isub__(self, other):
782+
# Set difference, in-place
783+
for x in other.addresslist:
784+
if x in self.addresslist:
785+
self.addresslist.remove(x)
786+
return self
787+
774788
def __getitem__(self, index):
775789
# Make indexing, slices, and 'in' work
776790
return self.addresslist[index]

Lib/test/output/test_augassign

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
test_augassign
2+
6
3+
[6]
4+
6
5+
[1, 2, 3, 4, 1, 2, 3, 4]
6+
[1, 2, 1, 2, 3]
7+
1
8+
1
9+
1
10+
11
11+
1
12+
12
13+
1
14+
1
15+
13
16+
__add__ called
17+
__radd__ called
18+
__iadd__ called
19+
__sub__ called
20+
__rsub__ called
21+
__isub__ called
22+
__mul__ called
23+
__rmul__ called
24+
__imul__ called
25+
__div__ called
26+
__rdiv__ called
27+
__idiv__ called
28+
__mod__ called
29+
__rmod__ called
30+
__imod__ called
31+
__pow__ called
32+
__rpow__ called
33+
__ipow__ called
34+
__or__ called
35+
__ror__ called
36+
__ior__ called
37+
__and__ called
38+
__rand__ called
39+
__iand__ called
40+
__xor__ called
41+
__rxor__ called
42+
__ixor__ called
43+
__rshift__ called
44+
__rrshift__ called
45+
__irshift__ called
46+
__lshift__ called
47+
__rlshift__ called
48+
__ilshift__ called

Lib/test/test_augassign.py

Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
2+
# Augmented assignment test.
3+
4+
x = 2
5+
x += 1
6+
x *= 2
7+
x **= 2
8+
x -= 8
9+
x /= 2
10+
x %= 12
11+
x &= 2
12+
x |= 5
13+
x ^= 1
14+
15+
print x
16+
17+
x = [2]
18+
x[0] += 1
19+
x[0] *= 2
20+
x[0] **= 2
21+
x[0] -= 8
22+
x[0] /= 2
23+
x[0] %= 12
24+
x[0] &= 2
25+
x[0] |= 5
26+
x[0] ^= 1
27+
28+
print x
29+
30+
x = {0: 2}
31+
x[0] += 1
32+
x[0] *= 2
33+
x[0] **= 2
34+
x[0] -= 8
35+
x[0] /= 2
36+
x[0] %= 12
37+
x[0] &= 2
38+
x[0] |= 5
39+
x[0] ^= 1
40+
41+
print x[0]
42+
43+
x = [1,2]
44+
x += [3,4]
45+
x *= 2
46+
47+
print x
48+
49+
x = [1, 2, 3]
50+
y = x
51+
x[1:2] *= 2
52+
y[1:2] += [1]
53+
54+
print x
55+
print x is y
56+
57+
class aug_test:
58+
def __init__(self, value):
59+
self.val = value
60+
def __radd__(self, val):
61+
return self.val + val
62+
def __add__(self, val):
63+
return aug_test(self.val + val)
64+
65+
66+
class aug_test2(aug_test):
67+
def __iadd__(self, val):
68+
self.val = self.val + val
69+
return self
70+
71+
class aug_test3(aug_test):
72+
def __iadd__(self, val):
73+
return aug_test3(self.val + val)
74+
75+
x = aug_test(1)
76+
y = x
77+
x += 10
78+
79+
print isinstance(x, aug_test)
80+
print y is not x
81+
print x.val
82+
83+
x = aug_test2(2)
84+
y = x
85+
x += 10
86+
87+
print y is x
88+
print x.val
89+
90+
x = aug_test3(3)
91+
y = x
92+
x += 10
93+
94+
print isinstance(x, aug_test3)
95+
print y is not x
96+
print x.val
97+
98+
class testall:
99+
100+
def __add__(self, val):
101+
print "__add__ called"
102+
def __radd__(self, val):
103+
print "__radd__ called"
104+
def __iadd__(self, val):
105+
print "__iadd__ called"
106+
return self
107+
108+
def __sub__(self, val):
109+
print "__sub__ called"
110+
def __rsub__(self, val):
111+
print "__rsub__ called"
112+
def __isub__(self, val):
113+
print "__isub__ called"
114+
return self
115+
116+
def __mul__(self, val):
117+
print "__mul__ called"
118+
def __rmul__(self, val):
119+
print "__rmul__ called"
120+
def __imul__(self, val):
121+
print "__imul__ called"
122+
return self
123+
124+
def __div__(self, val):
125+
print "__div__ called"
126+
def __rdiv__(self, val):
127+
print "__rdiv__ called"
128+
def __idiv__(self, val):
129+
print "__idiv__ called"
130+
return self
131+
132+
def __mod__(self, val):
133+
print "__mod__ called"
134+
def __rmod__(self, val):
135+
print "__rmod__ called"
136+
def __imod__(self, val):
137+
print "__imod__ called"
138+
return self
139+
140+
def __pow__(self, val):
141+
print "__pow__ called"
142+
def __rpow__(self, val):
143+
print "__rpow__ called"
144+
def __ipow__(self, val):
145+
print "__ipow__ called"
146+
return self
147+
148+
def __or__(self, val):
149+
print "__or__ called"
150+
def __ror__(self, val):
151+
print "__ror__ called"
152+
def __ior__(self, val):
153+
print "__ior__ called"
154+
return self
155+
156+
def __and__(self, val):
157+
print "__and__ called"
158+
def __rand__(self, val):
159+
print "__rand__ called"
160+
def __iand__(self, val):
161+
print "__iand__ called"
162+
return self
163+
164+
def __xor__(self, val):
165+
print "__xor__ called"
166+
def __rxor__(self, val):
167+
print "__rxor__ called"
168+
def __ixor__(self, val):
169+
print "__ixor__ called"
170+
return self
171+
172+
def __rshift__(self, val):
173+
print "__rshift__ called"
174+
def __rrshift__(self, val):
175+
print "__rrshift__ called"
176+
def __irshift__(self, val):
177+
print "__irshift__ called"
178+
return self
179+
180+
def __lshift__(self, val):
181+
print "__lshift__ called"
182+
def __rlshift__(self, val):
183+
print "__rlshift__ called"
184+
def __ilshift__(self, val):
185+
print "__ilshift__ called"
186+
return self
187+
188+
x = testall()
189+
x + 1
190+
1 + x
191+
x += 1
192+
193+
x - 1
194+
1 - x
195+
x -= 1
196+
197+
x * 1
198+
1 * x
199+
x *= 1
200+
201+
x / 1
202+
1 / x
203+
x /= 1
204+
205+
x % 1
206+
1 % x
207+
x %= 1
208+
209+
x ** 1
210+
1 ** x
211+
x **= 1
212+
213+
x | 1
214+
1 | x
215+
x |= 1
216+
217+
x & 1
218+
1 & x
219+
x &= 1
220+
221+
x ^ 1
222+
1 ^ x
223+
x ^= 1
224+
225+
x >> 1
226+
1 >> x
227+
x >>= 1
228+
229+
x << 1
230+
1 << x
231+
x <<= 1
232+

0 commit comments

Comments
 (0)