Skip to content

Commit

Permalink
Factor out __iadd__ from most classes to Object.__iadd__.
Browse files Browse the repository at this point in the history
Most cases of __iadd__ implementations follow the same pattern,
so just factor it out to Object.__iadd__.
  • Loading branch information
tbeadle committed Aug 24, 2017
1 parent b7ee5a3 commit 709ba3a
Show file tree
Hide file tree
Showing 18 changed files with 45 additions and 159 deletions.
17 changes: 0 additions & 17 deletions python/common/org/python/types/Bool.java
Original file line number Diff line number Diff line change
Expand Up @@ -527,23 +527,6 @@ public org.python.Object __ror__(org.python.Object other) {

}

@org.python.Method(
__doc__ = ""
)
public org.python.Object __iadd__(org.python.Object other) {
int this_val = (((org.python.types.Bool) this).value ? 1 : 0);
if (other instanceof org.python.types.Bool) {
this_val += (((org.python.types.Bool) other).value ? 1 : 0);
return new org.python.types.Int(this_val);
} else if (other instanceof org.python.types.Int) {
this_val += ((org.python.types.Int) other).value;
return new org.python.types.Int(this_val);
} else if (other instanceof org.python.types.Float) {
return new org.python.types.Float(this_val + ((org.python.types.Float) other).value);
}
throw new org.python.exceptions.TypeError("unsupported operand type(s) for +=: 'bool' and '" + other.typeName() + "'");
}

@org.python.Method(
__doc__ = ""
)
Expand Down
11 changes: 8 additions & 3 deletions python/common/org/python/types/ByteArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,15 @@ public org.python.Object __add__(org.python.Object other) {
__doc__ = "Implement self+=value."
)
public org.python.Object __iadd__(org.python.Object other) {
if (other instanceof org.python.types.Bytes || other instanceof org.python.types.ByteArray) {
return this.__add__(other);
try {
return super.__iadd__(other);
} catch (org.python.exceptions.TypeError ae) {
if (org.Python.VERSION < 0x03060200) {
throw new org.python.exceptions.TypeError("can't concat " + other.typeName() + " to " + this.typeName());
} else {
throw ae;
}
}
throw new org.python.exceptions.TypeError("can't concat " + other.typeName() + " to " + this.typeName());
}

@org.python.Method(
Expand Down
7 changes: 0 additions & 7 deletions python/common/org/python/types/Bytes.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,6 @@ public org.python.Object __add__(org.python.Object other) {
}
}

@org.python.Method(
__doc__ = ""
)
public org.python.Object __iadd__(org.python.Object other) {
return this.__add__(other);
}

@org.python.Method(
__doc__ = ""
)
Expand Down
7 changes: 0 additions & 7 deletions python/common/org/python/types/Dict.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,6 @@ public org.python.Object __pos__() {
throw new org.python.exceptions.TypeError("bad operand type for unary +: 'dict'");
}

@org.python.Method(
__doc__ = ""
)
public org.python.Object __iadd__(org.python.Object other) {
throw new org.python.exceptions.TypeError("unsupported operand type(s) for +=: 'dict' and '" + other.typeName() + "'");
}

@org.python.Method(
__doc__ = ""
)
Expand Down
20 changes: 0 additions & 20 deletions python/common/org/python/types/Float.java
Original file line number Diff line number Diff line change
Expand Up @@ -528,26 +528,6 @@ public org.python.Object __rpow__(org.python.Object other) {
throw new org.python.exceptions.NotImplementedError("float.__rpow__() has not been implemented.");
}

@org.python.Method(
__doc__ = ""
)
public org.python.Object __iadd__(org.python.Object other) {
if (other instanceof org.python.types.Int) {
long other_val = ((org.python.types.Int) other).value;
this.value += ((double) other_val);
return new org.python.types.Float(this.value);
} else if (other instanceof org.python.types.Bool) {
if (((org.python.types.Bool) other).value) {
this.value += 1.0;
}
return new org.python.types.Float(this.value);
} else if (other instanceof org.python.types.Float) {
double other_val = ((org.python.types.Float) other).value;
return new org.python.types.Float(this.value + other_val);
}
throw new org.python.exceptions.TypeError("unsupported operand type(s) for +=: 'float' and '" + other.typeName() + "'");
}

@org.python.Method(
__doc__ = ""
)
Expand Down
21 changes: 0 additions & 21 deletions python/common/org/python/types/Int.java
Original file line number Diff line number Diff line change
Expand Up @@ -657,27 +657,6 @@ public org.python.Object __ror__(org.python.Object other) {
throw new org.python.exceptions.NotImplementedError("int.__ror__() has not been implemented");
}

@org.python.Method(
__doc__ = ""
)
public org.python.Object __iadd__(org.python.Object other) {
if (other instanceof org.python.types.Int) {
this.value += ((org.python.types.Int) other).value;
return new org.python.types.Int(this.value);
} else if (other instanceof org.python.types.Float) {
double this_val = this.value;
this_val += ((org.python.types.Float) other).value;
return new org.python.types.Float(this_val);
} else if (other instanceof org.python.types.Bool) {
this.value += (((org.python.types.Bool) other).value ? 1 : 0);
return new org.python.types.Int(this.value);
} else if (other instanceof org.python.types.Complex) {
org.python.types.Complex other_cmplx_obj = (org.python.types.Complex) other;
return new org.python.types.Complex(this.value + other_cmplx_obj.real.value, other_cmplx_obj.imag.value);
}
throw new org.python.exceptions.TypeError("unsupported operand type(s) for +=: 'int' and '" + other.typeName() + "'");
}

@org.python.Method(
__doc__ = ""
)
Expand Down
32 changes: 30 additions & 2 deletions python/common/org/python/types/List.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,42 @@ public org.python.Object __neg__() {
public org.python.Object __iadd__(org.python.Object other) {
if (other instanceof org.python.types.List) {
this.value.addAll(((org.python.types.List) other).value);
return this;
} else if (other instanceof org.python.types.Tuple) {
this.value.addAll(((org.python.types.Tuple) other).value);
return this;
} else if (other instanceof org.python.types.Set) {
this.value.addAll(((org.python.types.Set) other).value);
} else if (other instanceof org.python.types.FrozenSet) {
this.value.addAll(((org.python.types.FrozenSet) other).value);
} else if (
(other instanceof org.python.types.Str) ||
(other instanceof org.python.types.Dict) ||
(other instanceof org.python.types.Range) ||
(other instanceof org.python.types.Bytes) ||
(other instanceof org.python.types.ByteArray)) {
org.python.Object iter = null;
if (other instanceof org.python.types.Str) {
iter = ((org.python.types.Str)other).__iter__();
} else if (other instanceof org.python.types.Dict) {
iter = ((org.python.types.Dict)other).__iter__();
} else if (other instanceof org.python.types.Range) {
iter = ((org.python.types.Range)other).__iter__();
} else if (other instanceof org.python.types.Bytes) {
iter = ((org.python.types.Bytes)other).__iter__();
} else if (other instanceof org.python.types.ByteArray) {
iter = ((org.python.types.ByteArray)other).__iter__();
}
while (true) {
try {
this.value.add(iter.__next__());
} catch (org.python.exceptions.StopIteration ae) {
break;
}
}
} else {
throw new org.python.exceptions.TypeError(
String.format("'%s' object is not iterable", Python.typeName(other.getClass())));
}
return this;
}

@org.python.Method(
Expand Down
7 changes: 0 additions & 7 deletions python/common/org/python/types/NoneType.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@ public org.python.Object __pos__() {
throw new org.python.exceptions.TypeError("bad operand type for unary +: 'NoneType'");
}

@org.python.Method(
__doc__ = ""
)
public org.python.Object __iadd__(org.python.Object other) {
throw new org.python.exceptions.TypeError("unsupported operand type(s) for +=: 'NoneType' and '" + other.typeName() + "'");
}

@org.python.Method(
__doc__ = ""
)
Expand Down
7 changes: 0 additions & 7 deletions python/common/org/python/types/NotImplementedType.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,6 @@ public org.python.Object __pos__() {
throw new org.python.exceptions.TypeError("bad operand type for unary +: 'NotImplementedType'");
}

@org.python.Method(
__doc__ = ""
)
public org.python.Object __iadd__(org.python.Object other) {
throw new org.python.exceptions.TypeError("unsupported operand type(s) for +=: 'NotImplementedType' and '" + other.typeName() + "'");
}

@org.python.Method(
__doc__ = ""
)
Expand Down
11 changes: 7 additions & 4 deletions python/common/org/python/types/Object.java
Original file line number Diff line number Diff line change
Expand Up @@ -785,10 +785,13 @@ public org.python.Object __ror__(org.python.Object other) {
)
public org.python.Object __iadd__(org.python.Object other) {
try {
this.setValue(this.__add__(other));
return this;
} catch (org.python.exceptions.TypeError e) {
throw new org.python.exceptions.TypeError("unsupported operand type(s) for +=: '" + this.typeName() + "' and '" + other.typeName() + "'");
return this.__add__(other);
} catch (org.python.exceptions.TypeError ae) {
if (ae.getMessage().startsWith("unsupported operand")) {
throw new org.python.exceptions.TypeError("unsupported operand type(s) for +=: '" + this.typeName() + "' and '" + other.typeName() + "'");
} else {
throw ae;
}
}
}

Expand Down
14 changes: 0 additions & 14 deletions python/common/org/python/types/Range.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,6 @@ public org.python.Object __repr__() {
}
}

@org.python.Method(
__doc__ = ""
)
public org.python.Object __iadd__(org.python.Object other) {
throw new org.python.exceptions.TypeError("unsupported operand type(s) for +=: 'range' and '" + other.typeName() + "'");
}

@org.python.Method(
__doc__ = "Implement iter(self)."
)
Expand Down Expand Up @@ -257,13 +250,6 @@ public RangeIterator(long start, long stop, long step) {
index = this.start;
}

@org.python.Method(
__doc__ = ""
)
public org.python.Object __iadd__(org.python.Object other) {
throw new org.python.exceptions.TypeError("unsupported operand type(s) for +=: 'range_iterator' and '" + other.typeName() + "'");
}

@org.python.Method(
__doc__ = "Implement iter(self)."
)
Expand Down
7 changes: 0 additions & 7 deletions python/common/org/python/types/Set.java
Original file line number Diff line number Diff line change
Expand Up @@ -466,13 +466,6 @@ public org.python.Object discard(org.python.Object item) {
return org.python.types.NoneType.NONE;
}

@org.python.Method(
__doc__ = ""
)
public org.python.Object __iadd__(org.python.Object other) {
throw new org.python.exceptions.TypeError("unsupported operand type(s) for +=: 'set' and '" + other.typeName() + "'");
}

@org.python.Method(
__doc__ = "Return the intersection of two sets as a new set.\n\n(i.e. all elements that are in both sets.)",
args = {"other"}
Expand Down
17 changes: 0 additions & 17 deletions python/common/org/python/types/Str.java
Original file line number Diff line number Diff line change
Expand Up @@ -579,23 +579,6 @@ public org.python.Object __imod__(org.python.Object other) {
}
}

@org.python.Method(
__doc__ = "",
args = {"other"}
)
public org.python.Object __iadd__(org.python.Object other) {
try {
this.setValue(this.__add__(other));
return this;
} catch (org.python.exceptions.TypeError e) {
if (org.Python.VERSION < 0x03060000) {
throw new org.python.exceptions.TypeError("Can't convert '" + other.typeName() + "' object to str implicitly");
} else {
throw new org.python.exceptions.TypeError("must be str, not " + other.typeName());
}
}
}

@org.python.Method(
__doc__ = ""
)
Expand Down
13 changes: 0 additions & 13 deletions python/common/org/python/types/Tuple.java
Original file line number Diff line number Diff line change
Expand Up @@ -437,19 +437,6 @@ public org.python.Object count() {
return new org.python.types.Int(this.value.size());
}

@org.python.Method(
__doc__ = ""
)
public org.python.Object __iadd__(org.python.Object other) {
if (other instanceof org.python.types.Tuple) {
this.value.addAll(((org.python.types.Tuple) other).value);
return this;
} else {
throw new org.python.exceptions.TypeError(
String.format("can only concatenate tuple (not \"%s\") to tuple", org.Python.typeName(other.getClass())));
}
}

@org.python.Method(
__doc__ = "index of the first occurrence of x in s (at or after index i and before index j)",
default_args = {"item", "start", "end"}
Expand Down
2 changes: 0 additions & 2 deletions tests/datatypes/test_bool.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ class InplaceBoolOperationTests(InplaceOperationTestCase, TranspileTestCase):
data_type = 'bool'

not_implemented = [
'test_add_complex',

'test_and_int',

'test_floor_divide_bool',
Expand Down
5 changes: 0 additions & 5 deletions tests/datatypes/test_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,6 @@ class InplaceComplexOperationTests(InplaceOperationTestCase, TranspileTestCase):
data_type = 'complex'

not_implemented = [
'test_add_bool',
'test_add_complex',
'test_add_float',
'test_add_int',

'test_and_none',

'test_eq_None',
Expand Down
2 changes: 0 additions & 2 deletions tests/datatypes/test_float.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,6 @@ class InplaceFloatOperationTests(InplaceOperationTestCase, TranspileTestCase):
data_type = 'float'

not_implemented = [
'test_add_complex',

'test_multiply_bytearray',
'test_multiply_bytes',
'test_multiply_class',
Expand Down
4 changes: 0 additions & 4 deletions tests/datatypes/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,11 +661,7 @@ class InplaceListOperationTests(InplaceOperationTestCase, TranspileTestCase):
data_type = 'list'

not_implemented = [
'test_add_bytearray',
'test_add_bytes',
'test_add_dict',
'test_add_frozenset',
'test_add_range',
'test_add_set',
'test_add_str',
]

0 comments on commit 709ba3a

Please sign in to comment.