Skip to content

Commit e3272b9

Browse files
authored
Add checks for non-0-based arrays in addition and multiplication operators (#1898)
* Add checks for non-0-based arrays in addition and multiplication operators * Reorder operator methods
1 parent f874612 commit e3272b9

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

src/core/IronPython/Runtime/Operations/ArrayOps.cs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,6 @@ namespace IronPython.Runtime.Operations {
2121
public static class ArrayOps {
2222
#region Python APIs
2323

24-
[SpecialName]
25-
public static Array Add(Array data1, Array data2) {
26-
if (data1 == null) throw PythonOps.TypeError("expected array for 1st argument, got None");
27-
if (data2 == null) throw PythonOps.TypeError("expected array for 2nd argument, got None");
28-
29-
if (data1.Rank > 1 || data2.Rank > 1) throw new NotImplementedException("can't add multidimensional arrays");
30-
31-
Type type1 = data1.GetType();
32-
Type type2 = data2.GetType();
33-
Type type = (type1 == type2) ? type1.GetElementType()! : typeof(object);
34-
35-
Array ret = Array.CreateInstance(type, data1.Length + data2.Length);
36-
Array.Copy(data1, 0, ret, 0, data1.Length);
37-
Array.Copy(data2, 0, ret, data1.Length, data2.Length);
38-
return ret;
39-
}
40-
4124
[StaticExtensionMethod]
4225
public static object __new__(CodeContext context, PythonType pythonType, int length) {
4326
Type type = pythonType.UnderlyingSystemType.GetElementType()!;
@@ -143,12 +126,31 @@ public static object __ne__(CodeContext context, Array self, [NotNone] Array oth
143126
[return: MaybeNotImplemented]
144127
public static object __ne__(CodeContext context, object self, object? other) => NotImplementedType.Value;
145128

129+
[SpecialName]
130+
public static Array Add(Array data1, Array data2) {
131+
if (data1 == null) throw PythonOps.TypeError("expected array for 1st argument, got None");
132+
if (data2 == null) throw PythonOps.TypeError("expected array for 2nd argument, got None");
133+
134+
if (data1.Rank > 1 || data2.Rank > 1) throw new NotImplementedException("can't add multidimensional arrays");
135+
if (data1.GetLowerBound(0) != 0 || data2.GetLowerBound(0) != 0) throw new NotImplementedException("can't add non-0-based arrays");
136+
137+
Type type1 = data1.GetType();
138+
Type type2 = data2.GetType();
139+
Type type = (type1 == type2) ? type1.GetElementType()! : typeof(object);
140+
141+
Array ret = Array.CreateInstance(type, data1.Length + data2.Length);
142+
Array.Copy(data1, 0, ret, 0, data1.Length);
143+
Array.Copy(data2, 0, ret, data1.Length, data2.Length);
144+
return ret;
145+
}
146+
146147
/// <summary>
147148
/// Multiply two object[] arrays - slow version, we need to get the type, etc...
148149
/// </summary>
149150
[SpecialName]
150151
public static Array Multiply(Array data, int count) {
151152
if (data.Rank > 1) throw new NotImplementedException("can't multiply multidimensional arrays");
153+
if (data.GetLowerBound(0) != 0) throw new NotImplementedException("can't multiply non-0-based arrays");
152154

153155
Type elemType = data.GetType().GetElementType()!;
154156
if (count <= 0) return Array.CreateInstance(elemType, 0);

0 commit comments

Comments
 (0)