Skip to content

Commit

Permalink
Let constructors handle too many arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
patiences committed Aug 19, 2018
1 parent 442d23f commit 8a06317
Show file tree
Hide file tree
Showing 16 changed files with 101 additions and 42 deletions.
4 changes: 3 additions & 1 deletion python/common/org/python/types/Bool.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ private Bool(boolean bool) {
public Bool(org.python.Object[] args, java.util.Map<java.lang.String, org.python.Object> kwargs) {
if (args[0] == null) {
this.value = false;
} else {
} else if (args.length == 1) {
this.value = args[0].toBoolean();
} else {
throw new org.python.exceptions.TypeError("bool() takes at most 1 argument (" + args.length + " given)");
}
}
// public org.python.Object __new__() {
Expand Down
4 changes: 4 additions & 0 deletions python/common/org/python/types/Bytes.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ public Bytes(org.python.Object[] args, java.util.Map<java.lang.String, org.pytho
org.python.Object encoding = args[1];
org.python.Object errors = args[2];

if (args.length > 3) {
`throw new org.python.exceptions.TypeError("bytes() takes at most 3 arguments (" + args.length + " given)");
}

if (encoding != null && !(encoding instanceof org.python.types.Str)) {
throw new org.python.exceptions.TypeError("bytes() argument 2 must be str, not " + encoding.typeName());
} else if (errors != null && !(errors instanceof org.python.types.Str)) {
Expand Down
20 changes: 13 additions & 7 deletions python/common/org/python/types/Float.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,21 @@ public Float(double value) {
__doc__ = "float(x) -> floating point number" +
"\n" +
"Convert a string or number to a floating point number, if possible.\n",
args = {"x"}
default_args = {"x"}
)
public Float(org.python.Object[] args, java.util.Map<java.lang.String, org.python.Object> kwargs) {
try {
this.value = ((org.python.types.Float) args[0].__float__()).value;
} catch (org.python.exceptions.AttributeError ae) {
throw new org.python.exceptions.TypeError(
"float() argument must be a string or a number, not '" + args[0].typeName() + "'"
);
if (args[0] == null) {
this.value = 0.0;
} else if (args.length == 1) {
try {
this.value = ((org.python.types.Float) args[0].__float__()).value;
} catch (org.python.exceptions.AttributeError ae) {
throw new org.python.exceptions.TypeError(
"float() argument must be a string or a number, not '" + args[0].typeName() + "'"
);
}
} else {
throw new org.python.exceptions.TypeError("float() takes at most 1 argument (" + args.length + " given)");
}
}

Expand Down
4 changes: 3 additions & 1 deletion python/common/org/python/types/List.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public List(org.python.Object[] args, java.util.Map<java.lang.String, org.python
super();
if (args[0] == null) {
this.value = new java.util.ArrayList<org.python.Object>();
} else {
} else if (args.length == 1) {
if (args[0] instanceof org.python.types.List) {
this.value = new java.util.ArrayList<org.python.Object>(
((org.python.types.List) args[0]).value
Expand All @@ -80,6 +80,8 @@ public List(org.python.Object[] args, java.util.Map<java.lang.String, org.python
}
this.value = generated;
}
} else {
throw new org.python.exceptions.TypeError("list() takes at most 1 argument (" + args.length + " given)");
}
}

Expand Down
4 changes: 3 additions & 1 deletion python/common/org/python/types/Set.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public Set(java.util.Set<org.python.Object> set) {
public Set(org.python.Object[] args, java.util.Map<java.lang.String, org.python.Object> kwargs) {
if (args[0] == null) {
this.value = new java.util.HashSet<org.python.Object>();
} else {
} else if (args.length == 1) {
if (args[0] instanceof org.python.types.Set) {
this.value = new java.util.HashSet<org.python.Object>(
((org.python.types.Set) args[0]).value
Expand All @@ -76,6 +76,8 @@ public Set(org.python.Object[] args, java.util.Map<java.lang.String, org.python.
}
this.value = generated;
}
} else {
throw new org.python.exceptions.TypeError("set expected at most 1 arguments, got " + args.length);
}
}

Expand Down
8 changes: 7 additions & 1 deletion python/common/org/python/types/Str.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,14 @@ public Str(char chr) {
public Str(org.python.Object[] args, java.util.Map<java.lang.String, org.python.Object> kwargs) {
if (args[0] == null) {
this.value = "";
} else {
} else if (args.length == 1) {
this.value = ((org.python.types.Str) args[0].__str__()).value;
} else if (args.length == 2) {
throw new org.python.exceptions.NotImplementedError("Builtin function 'str(object=b'', encoding='utf-8', errors='strict')' not implemented");
} else if (args.length == 3) {
throw new org.python.exceptions.NotImplementedError("Builtin function 'str(object=b'', encoding='utf-8', errors='strict')' not implemented");
} else if (args.length > 3) {
throw new org.python.exceptions.TypeError("str() takes at most 3 arguments (" + args.length + " given)");
}
}

Expand Down
10 changes: 2 additions & 8 deletions python/common/org/python/types/Type.java
Original file line number Diff line number Diff line change
Expand Up @@ -466,14 +466,8 @@ public org.python.Object invoke(org.python.Object[] args, java.util.Map<java.lan
kwargs.put(arg_name, args[a]);
}
} else {
java.lang.String name = org.Python.typeName(this.klass);
if (n_args == 0) {
throw new org.python.exceptions.TypeError(name + "() takes no arguments (" + n_provided_args + " given)");
} else if (n_args == 1) {
throw new org.python.exceptions.TypeError(name + "() takes at most 1 argument (" + n_provided_args + " given)");
} else {
throw new org.python.exceptions.TypeError(name + "() takes at most " + n_args + " arguments (" + n_provided_args + " given)");
}
// More arguments than expected, call the constructor anyway to handle too many arguments
adjusted_args = args;
}
} else {
adjusted_args = args;
Expand Down
15 changes: 1 addition & 14 deletions tests/builtins/test_int.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,7 @@


class IntTests(TranspileTestCase):

def test_bad_int(self):
self.assertCodeExecution("""
try:
print(int(1, 2, 3))
except TypeError as err:
print(err)
try:
print(int([1, 2, 3]))
except TypeError as err:
print(err)
""")

pass

class BuiltinIntFunctionTests(BuiltinFunctionTestCase, TranspileTestCase):
functions = ["int"]
Expand Down
9 changes: 1 addition & 8 deletions tests/builtins/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,7 @@


class ListTests(TranspileTestCase):

def test_bad_list(self):
self.assertCodeExecution("""
try:
print(list(1, 2, 3))
except TypeError as err:
print(err)
""")
pass


class BuiltinListFunctionTests(BuiltinFunctionTestCase, TranspileTestCase):
Expand Down
8 changes: 8 additions & 0 deletions tests/datatypes/test_bool.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ def test_setitem(self):
print(err)
""")

def test_too_many_arguments(self):
self.assertCodeExecution("""
try:
print(bool(1, 2))
except TypeError as err:
print(err)
""")


class UnaryBoolOperationTests(UnaryOperationTestCase, TranspileTestCase):
data_type = 'bool'
Expand Down
8 changes: 8 additions & 0 deletions tests/datatypes/test_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,14 @@ def test_join(self):
print(b.join([b'12']))
""")

def test_too_many_arguments(self):
self.assertCodeExecution("""
try:
print(bytes(1, 2, 3, 4))
except TypeError as err:
print(err)
""")


class UnaryBytesOperationTests(UnaryOperationTestCase, TranspileTestCase):
data_type = 'bytes'
Expand Down
18 changes: 18 additions & 0 deletions tests/datatypes/test_float.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,24 @@ def test_mul_TypeError(self):
print(e)
""")

def test_none(self):
self.assertCodeExecution("""
print(float(None))
""")

def test_no_arguments(self):
self.assertCodeExecution("""
print(float())
""")

def test_too_many_arguments(self):
self.assertCodeExecution("""
try:
print(float(1, 2))
except TypeError as err:
print(err)
""")


class UnaryFloatOperationTests(UnaryOperationTestCase, TranspileTestCase):
data_type = 'float'
Expand Down
1 change: 0 additions & 1 deletion tests/datatypes/test_int.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ def test_no_arguments(self):
print(int())
""")

@expectedFailure
def test_too_many_arguments(self):
self.assertCodeExecution("""
try:
Expand Down
8 changes: 8 additions & 0 deletions tests/datatypes/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,14 @@ def test_reversed(self):
print(list == origList)
""")

def test_too_many_arguments(self):
self.assertCodeExecution("""
try:
print(list(1, 2, 3))
except TypeError as err:
print(err)
""")


class UnaryListOperationTests(UnaryOperationTestCase, TranspileTestCase):
data_type = 'list'
Expand Down
8 changes: 8 additions & 0 deletions tests/datatypes/test_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,14 @@ def test_symmetric_difference_update(self):
print(err)
""")

def test_too_many_arguments(self):
self.assertCodeExecution("""
try:
print(set(1, 2))
except TypeError as err:
print(err)
""")


class UnarySetOperationTests(UnaryOperationTestCase, TranspileTestCase):
data_type = 'set'
Expand Down
14 changes: 14 additions & 0 deletions tests/datatypes/test_str.py
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,20 @@ def test_contains(self):
print('' in 'a')
""")

def test_too_many_arguments(self):
self.assertCodeExecution("""
try:
print(str(1, 2, 3, 4, 5))
except TypeError as err:
print(err)
try:
print(str("1", "2", "3", "4", "5"))
except TypeError as err:
print(err)
""")


class UnaryStrOperationTests(UnaryOperationTestCase, TranspileTestCase):
data_type = 'str'
Expand Down

0 comments on commit 8a06317

Please sign in to comment.