Skip to content

Commit

Permalink
Reworked closure and constructor handling to support kwargs.
Browse files Browse the repository at this point in the history
  • Loading branch information
freakboy3742 committed Nov 27, 2015
1 parent c853d9d commit 937a314
Show file tree
Hide file tree
Showing 27 changed files with 437 additions and 356 deletions.
10 changes: 7 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ What it does:

* Some mathematical operations

* Some operations and methods on primitive types

* Exception handling

* for/while/if constructs
Expand All @@ -36,14 +38,16 @@ What it does:

* List comprehensions

* Keyword arguments

* `import` statements

It *doesn't* currently support:

* Keyword arguments
* `from X import Y` statements

* Generators

* Import statements

* ``exec()``/``eval()``

These things should all be *possible* - it's just a matter of time
Expand Down
75 changes: 72 additions & 3 deletions python/common/org/Python.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class Python {
}
}

public static java.lang.String pythonTypeName(java.lang.Class cls) {
public static java.lang.String typeName(java.lang.Class cls) {
try {
java.lang.String class_name = cls.getName();
if (class_name == "org.python.types.NoneType") {
Expand All @@ -38,10 +38,79 @@ public static java.lang.String pythonTypeName(java.lang.Class cls) {
}
}

public static java.lang.String pythonTypeName(org.python.Object obj) {
return pythonTypeName(obj.getClass());
public static java.lang.String typeName(org.python.Object obj) {
return typeName(obj.getClass());
}

public static void debug(java.lang.Object msg) {
System.out.println("DEBUG: " + msg);
}

public static void adjustArguments(
java.lang.String func_name,
java.util.List<org.python.Object> args,
java.util.Map<java.lang.String, org.python.Object> kwargs,
java.lang.String [] arg_names,
java.util.List<org.python.Object> default_args,
java.util.Map<java.lang.String, org.python.Object> default_kwargs,
int pos_count,
int flags) {

// System.out.println("ADJUST " + func_name);
// System.out.println(" args: " + args);
// System.out.println(" kwargs: " + kwargs);
// System.out.print(" arg_names: [");
// for (java.lang.String arg: arg_names) {
// System.out.print(arg + ", ");
// }
// System.out.println("]");
// System.out.println(" default_args: " + default_args);
// System.out.println(" default_kwargs: " + default_kwargs);
// System.out.println(" pos_count: " + pos_count);

// Iterate over all the positional arguments provided; check that
// we have enough of them. If we don't, populate them from kwargs,
// or if there's no kwargs, from defaults.
for (int a = 0; a < pos_count; a++) {
// System.out.println("arg " + a + " = " + arg_names[a]);
if (a >= args.size()) {
// This argument wasn't provided as a positional; check to
// see if it was provided as a keyword.
org.python.Object value = kwargs.remove(arg_names[a]);

// If it wasn't provided as a kwarg, use the
// defaults list.
if (value == null) {
value = default_args.get(a - (pos_count - default_args.size()));
}

// Add the value to the full args list.
args.add(value);
} else {
// We've been given a position argument at this index; check that
// it isn't also provided as a kwarg.
if (kwargs.containsKey(arg_names[a])) {
throw new org.python.exceptions.TypeError(func_name + "() for multiple values for argument '" + arg_names[a] + "'");
}
}
}

// If this function provides VARARGS, extract them and add them as
// a Python list as the last positional argument.
if ((flags & org.python.types.Function.CO_VARARGS) != 0) {
java.util.List<org.python.Object> var_args;
if (args.size() > pos_count) {
var_args = args.subList(pos_count, args.size());
args = new java.util.ArrayList<org.python.Object>(args.subList(0, pos_count));
} else {
// No positional arguments - add an empty list.
var_args = new java.util.ArrayList<org.python.Object>();
}
args.add(new org.python.types.List(var_args));
}
}


@org.python.Method(
__doc__ = "__import__(name, globals=None, locals=None, fromlist=(), level=0) -> module" +
"\n" +
Expand Down
6 changes: 6 additions & 0 deletions python/common/org/python/Argument.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.python;

@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
public @interface Argument {
java.lang.String name();
}
2 changes: 1 addition & 1 deletion python/common/org/python/Method.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
public @interface Method {
java.lang.String __doc__() default "";
java.lang.String __doc__() default "";
}
2 changes: 1 addition & 1 deletion python/common/org/python/exceptions/AttributeError.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

public class AttributeError extends org.python.exceptions.Exception {
public AttributeError(org.python.Object obj, String attr) {
super(org.Python.pythonTypeName(obj) + " has no attribute '" + attr + "'");
super(org.Python.typeName(obj) + " has no attribute '" + attr + "'");
}
}
26 changes: 13 additions & 13 deletions python/common/org/python/exceptions/BaseException.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,11 @@ public org.python.types.Bytes __bytes__() {
}

public org.python.types.Str __format__(org.python.Object value) {
throw new org.python.exceptions.NotImplementedError("'" + org.Python.pythonTypeName(this) + ".__format__' has not been implemented");
throw new org.python.exceptions.NotImplementedError("'" + org.Python.typeName(this) + ".__format__' has not been implemented");
}

public org.python.types.Str __format__(org.python.Object value, org.python.Object format_spec) {
throw new org.python.exceptions.NotImplementedError("'" + org.Python.pythonTypeName(this) + ".__format__' has not been implemented");
throw new org.python.exceptions.NotImplementedError("'" + org.Python.typeName(this) + ".__format__' has not been implemented");
}

@org.python.Method(
Expand All @@ -144,7 +144,7 @@ public org.python.Object __lt__(java.util.List<org.python.Object> args, java.uti
}

public org.python.Object __lt__(org.python.Object other) {
throw new org.python.exceptions.NotImplementedError("'" + org.Python.pythonTypeName(this) + ".__lt__' has not been implemented");
throw new org.python.exceptions.NotImplementedError("'" + org.Python.typeName(this) + ".__lt__' has not been implemented");
}


Expand All @@ -164,7 +164,7 @@ public org.python.Object __le__(java.util.List<org.python.Object> args, java.uti
}

public org.python.Object __le__(org.python.Object other) {
throw new org.python.exceptions.NotImplementedError("'" + org.Python.pythonTypeName(this) + ".__le__' has not been implemented");
throw new org.python.exceptions.NotImplementedError("'" + org.Python.typeName(this) + ".__le__' has not been implemented");
}

@org.python.Method(
Expand All @@ -183,7 +183,7 @@ public org.python.Object __eq__(java.util.List<org.python.Object> args, java.uti
}

public org.python.Object __eq__(org.python.Object other) {
throw new org.python.exceptions.NotImplementedError("'" + org.Python.pythonTypeName(this) + ".__eq__' has not been implemented");
throw new org.python.exceptions.NotImplementedError("'" + org.Python.typeName(this) + ".__eq__' has not been implemented");
}

@org.python.Method(
Expand All @@ -202,7 +202,7 @@ public org.python.Object __ne__(java.util.List<org.python.Object> args, java.uti
}

public org.python.Object __ne__(org.python.Object other) {
throw new org.python.exceptions.NotImplementedError("'" + org.Python.pythonTypeName(this) + ".__ne__' has not been implemented");
throw new org.python.exceptions.NotImplementedError("'" + org.Python.typeName(this) + ".__ne__' has not been implemented");
}

@org.python.Method(
Expand All @@ -221,7 +221,7 @@ public org.python.Object __gt__(java.util.List<org.python.Object> args, java.uti
}

public org.python.Object __gt__(org.python.Object other) {
throw new org.python.exceptions.NotImplementedError("'" + org.Python.pythonTypeName(this) + ".__gt__' has not been implemented");
throw new org.python.exceptions.NotImplementedError("'" + org.Python.typeName(this) + ".__gt__' has not been implemented");
}

@org.python.Method(
Expand All @@ -240,7 +240,7 @@ public org.python.Object __ge__(java.util.List<org.python.Object> args, java.uti
}

public org.python.Object __ge__(org.python.Object other) {
throw new org.python.exceptions.NotImplementedError("'" + org.Python.pythonTypeName(this) + ".__ge__' has not been implemented");
throw new org.python.exceptions.NotImplementedError("'" + org.Python.typeName(this) + ".__ge__' has not been implemented");
}

@org.python.Method(
Expand Down Expand Up @@ -294,7 +294,7 @@ public org.python.Object __getattribute__(java.util.List<org.python.Object> args
}

public org.python.Object __getattribute__(java.lang.String name) {
throw new org.python.exceptions.NotImplementedError("'" + org.Python.pythonTypeName(this) + ".__getattribute__' has not been implemented");
throw new org.python.exceptions.NotImplementedError("'" + org.Python.typeName(this) + ".__getattribute__' has not been implemented");
}

public org.python.Object __get__(org.python.Object instance, org.python.types.Type klass) {
Expand All @@ -321,7 +321,7 @@ public void __setattr__(java.util.List<org.python.Object> args, java.util.Map<ja
}

public void __setattr__(java.lang.String name, org.python.Object value) {
throw new org.python.exceptions.NotImplementedError("'" + org.Python.pythonTypeName(this) + ".__setattr__' has not been implemented");
throw new org.python.exceptions.NotImplementedError("'" + org.Python.typeName(this) + ".__setattr__' has not been implemented");
}

@org.python.Method(
Expand All @@ -344,11 +344,11 @@ public void __delattr__(java.util.List<org.python.Object> args, java.util.Map<ja
}

public void __delattr__(java.lang.String name) {
throw new org.python.exceptions.NotImplementedError("'" + org.Python.pythonTypeName(this) + ".__delattr__' has not been implemented");
throw new org.python.exceptions.NotImplementedError("'" + org.Python.typeName(this) + ".__delattr__' has not been implemented");
}

public org.python.types.List __dir__() {
throw new org.python.exceptions.NotImplementedError("'" + org.Python.pythonTypeName(this) + ".__dir__' has not been implemented");
throw new org.python.exceptions.NotImplementedError("'" + org.Python.typeName(this) + ".__dir__' has not been implemented");
}

/**
Expand Down Expand Up @@ -436,7 +436,7 @@ public org.python.Object __pow__(org.python.Object other) {
}

public org.python.Object __pow__(org.python.Object other, org.python.Object modulus) {
throw new org.python.exceptions.TypeError("unsupported operand type(s) for ** or pow(): '" + org.Python.pythonTypeName(this) + "', '" + org.Python.pythonTypeName(other) + "', '" + org.Python.pythonTypeName(modulus) + "'");
throw new org.python.exceptions.TypeError("unsupported operand type(s) for ** or pow(): '" + org.Python.typeName(this) + "', '" + org.Python.typeName(other) + "', '" + org.Python.typeName(modulus) + "'");
}

public org.python.Object __lshift__(org.python.Object other) {
Expand Down
4 changes: 2 additions & 2 deletions python/common/org/python/types/Bytes.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.python.types;

public class Bytes extends org.python.types.Object {
public byte[] value;
public byte [] value;

/**
* A utility method to update the internal value of this object.
Expand All @@ -13,7 +13,7 @@ void setValue(org.python.Object obj) {
this.value = ((org.python.types.Bytes) obj).value;
}

public Bytes(byte[] value) {
public Bytes(byte [] value) {
this.value = value;
}

Expand Down
11 changes: 10 additions & 1 deletion python/common/org/python/types/Closure.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package org.python.types;

public class Closure extends org.python.types.Object {

public java.util.List<org.python.Object> default_args;
public java.util.Map<java.lang.String, org.python.Object> default_kwargs;

/**
* A utility method to update the internal value of this object.
*
Expand All @@ -10,7 +14,12 @@ public class Closure extends org.python.types.Object {
void setValue(org.python.Object obj) {
}

public Closure() {
public Closure(
java.util.List<org.python.Object> default_args,
java.util.Map<java.lang.String, org.python.Object> default_kwargs
) {
super();
this.default_args = default_args;
this.default_kwargs = default_kwargs;
}
}
2 changes: 1 addition & 1 deletion python/common/org/python/types/Code.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public Code(
public org.python.types.Str __repr__() {
return new org.python.types.Str(
String.format("<%s object at %x, file \"%s\", line %s>",
org.Python.pythonTypeName(this),
org.Python.typeName(this),
this.hashCode(),
this.attrs.get("co_filename"),
this.attrs.get("co_firstlineno")
Expand Down
19 changes: 1 addition & 18 deletions python/common/org/python/types/Constructor.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,10 @@

public class Constructor extends org.python.types.Object implements org.python.Callable {
java.lang.reflect.Constructor constructor;
java.util.List<org.python.Object> default_args;
java.util.Map<java.lang.String, org.python.Object> default_kwargs;

public Constructor(
java.lang.reflect.Constructor constructor,
java.util.List<org.python.Object> default_args,
java.util.Map<java.lang.String, org.python.Object> default_kwargs
) {
public Constructor(java.lang.reflect.Constructor constructor) {
super();
this.constructor = constructor;
this.default_args = default_args;
this.default_kwargs = default_kwargs;
}

public org.python.Object invoke(java.util.List<org.python.Object> args, java.util.Map<java.lang.String, org.python.Object> kwargs) {
Expand All @@ -30,15 +22,6 @@ public org.python.Object invoke(java.util.List<org.python.Object> args, java.uti
// System.out.println(" " + argname + " = " + kwargs.get(argname));
// }

// System.out.println("DEFAULTS:");
// for (org.python.Object arg: this.default_args) {
// System.out.println(" " + arg);
// }

// for (java.lang.String argname: this.default_kwargs.keySet()) {
// System.out.println(" " + argname + " = " + this.default_kwargs.get(argname));
// }

return (org.python.Object) this.constructor.newInstance(args, kwargs);
} catch (java.lang.IllegalAccessException e) {
throw new org.python.exceptions.RuntimeError("Illegal access to Java constructor " + this.constructor);
Expand Down
8 changes: 4 additions & 4 deletions python/common/org/python/types/Float.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public org.python.Object __lt__(org.python.Object other) {
return new org.python.types.Bool(this.value < 0.0);
}
}
throw new org.python.exceptions.TypeError("unorderable types: float() < " + org.Python.pythonTypeName(other) + "()");
throw new org.python.exceptions.TypeError("unorderable types: float() < " + org.Python.typeName(other) + "()");
}

public org.python.Object __le__(org.python.Object other) {
Expand All @@ -72,7 +72,7 @@ public org.python.Object __eq__(org.python.Object other) {
|| (this.value != 0.0 && ((org.python.types.Bool) other).value)
);
}
throw new org.python.exceptions.TypeError("unorderable types: float() == " + org.Python.pythonTypeName(other) + "()");
throw new org.python.exceptions.TypeError("unorderable types: float() == " + org.Python.typeName(other) + "()");
}

public org.python.Object __ne__(org.python.Object other) {
Expand Down Expand Up @@ -124,7 +124,7 @@ public org.python.Object __sub__(org.python.Object other) {
}
return new org.python.types.Float(this.value);
}
throw new org.python.exceptions.TypeError("unsupported operand type(s) for /: 'float' and '" + org.Python.pythonTypeName(other) + "'");
throw new org.python.exceptions.TypeError("unsupported operand type(s) for /: 'float' and '" + org.Python.typeName(other) + "'");
}

public org.python.Object __mul__(org.python.Object other) {
Expand Down Expand Up @@ -152,7 +152,7 @@ public org.python.Object __truediv__(org.python.Object other) {
throw new org.python.exceptions.ZeroDivisionError("float division by zero");
}
}
throw new org.python.exceptions.TypeError("unsupported operand type(s) for /: 'float' and '" + org.Python.pythonTypeName(other) + "'");
throw new org.python.exceptions.TypeError("unsupported operand type(s) for /: 'float' and '" + org.Python.typeName(other) + "'");
}

public org.python.Object __floordiv__(org.python.Object other) {
Expand Down
Loading

0 comments on commit 937a314

Please sign in to comment.