Skip to content

Commit

Permalink
Merge pull request beeware#711 from onyb/fix-exception-handling
Browse files Browse the repository at this point in the history
Fix bugs in args attribute of exception objects
eliasdorneles authored Dec 20, 2017
2 parents 917e54a + f7d58b8 commit a8810f9
Showing 3 changed files with 21 additions and 31 deletions.
21 changes: 10 additions & 11 deletions python/common/org/python/exceptions/BaseException.java
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@
)
public class BaseException extends org.python.types.Object {
@org.python.Attribute()
org.python.types.Tuple args = new org.python.types.Tuple();
public org.python.types.Tuple args = new org.python.types.Tuple();

public BaseException() {
super();
@@ -17,17 +17,10 @@ public BaseException(String msg) {
}

public BaseException(org.python.Object[] args, java.util.Map<java.lang.String, org.python.Object> kwargs) {
super(buildMessage(buildTuple(args)));
super();
this.args = buildTuple(args);
}

private static String buildMessage(org.python.types.Tuple tupArgs) {
if (tupArgs.value.size() == 0) {
return "";
}
return tupArgs.toString();
}

private static org.python.types.Tuple buildTuple(org.python.Object[] args) {
return new org.python.types.Tuple(java.util.Arrays.asList(args));
}
@@ -36,14 +29,16 @@ private static org.python.types.Tuple buildTuple(org.python.Object[] args) {
__doc__ = "Return repr(self)."
)
public org.python.Object __repr__() {
return new org.python.types.Str(this.getClass().getSimpleName() + "(\"" + this.getMessage() + "\",)");
return new org.python.types.Str(this.getClass().getSimpleName() + this.args.toString());
}

@org.python.Method(
__doc__ = "Return str(self)."
)
public org.python.Object __str__() {
if (this.args.value.size() == 1) {
if (this.args.value.size() == 0) {
return new org.python.types.Str("");
} else if (this.args.value.size() == 1) {
return this.args.value.get(0).__str__();
}
return this.args.__str__();
@@ -52,4 +47,8 @@ public org.python.Object __str__() {
public java.lang.String toString() {
return this.getClass().getSimpleName() + ": " + this.getMessage();
}

public java.lang.String getMessage() {
return this.__str__().toString();
}
}
27 changes: 9 additions & 18 deletions python/common/org/python/exceptions/KeyError.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,21 @@
package org.python.exceptions;

public class KeyError extends org.python.exceptions.LookupError {
private String customMessage = null;
public KeyError(org.python.Object[] args, java.util.Map<java.lang.String, org.python.Object> kwargs) {
super(convertToReprIfOneArgument(args), kwargs);
if (args.length == 1) {
customMessage = args[0].__repr__().toString();
}
}

@Override
public String getMessage() {
if (customMessage != null) {
return customMessage;
}
return super.getMessage();
super(args, kwargs);
}

private static org.python.Object[] convertToReprIfOneArgument(org.python.Object[] args) {
if (args.length == 1) {
return new org.python.Object[] { args[0].__repr__() };
@org.python.Method(
__doc__ = "Return str(self)."
)
public org.python.Object __str__() {
if (this.args.value.size() == 1) {
return this.args.value.get(0).__repr__();
}
return args;
return this.args.__str__();
}

public KeyError(org.python.Object key) {
super(key.__repr__().toString());
super(new org.python.Object[] {key}, null);
}
}
4 changes: 2 additions & 2 deletions tests/structures/test_exception.py
Original file line number Diff line number Diff line change
@@ -85,11 +85,11 @@ def test_raising_exceptions_multiple_args(self):
try:
raise exc("one")
except Exception as e:
print(e)
print(e, e.args, str(e), repr(e))
try:
raise exc("one", 2)
except Exception as e:
print(e)
print(e, e.args)
""")

@expectedFailure

0 comments on commit a8810f9

Please sign in to comment.