Skip to content

Commit

Permalink
Reworked handling of modules, globals, and class attributes.
Browse files Browse the repository at this point in the history
  • Loading branch information
freakboy3742 committed Nov 2, 2015
1 parent 5241e6d commit 59ea3d0
Show file tree
Hide file tree
Showing 42 changed files with 3,466 additions and 2,945 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ local
local.properties
distribute-*
tests/temp
org
/org
1,635 changes: 849 additions & 786 deletions python/common/org/Python.java

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion python/common/org/python/Callable.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@


public interface Callable {
public org.python.Object invoke(org.python.Object[] args, java.util.Hashtable<java.lang.String, org.python.Object> kwargs);
public org.python.Object invoke(org.python.Object[] args, java.util.Map<java.lang.String, org.python.Object> kwargs);
}
6 changes: 6 additions & 0 deletions python/common/org/python/Class.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 Class {
java.lang.String __doc__() default "";
}
62 changes: 62 additions & 0 deletions python/common/org/python/ImportLib.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package org.python;

public class ImportLib {
public static java.util.Map<java.lang.String, org.python.types.Module> modules = new java.util.HashMap<java.lang.String, org.python.types.Module>();

/**
* Factory method to obtain Python classes from their Java counterparts
*/
public static org.python.types.Module __import__(java.lang.String python_name, int level) {
// Create an array containing the module path.
java.lang.String java_name;

// If the package name isn't clearly identifiable as a java package path,
// put it in the python namespace.
if ( python_name.startsWith("java.")
|| python_name.startsWith("org.")
|| python_name.startsWith("com.")
|| python_name.startsWith("edu.")
|| python_name.startsWith("net.")
|| python_name.startsWith("android.")) {
throw new org.python.exceptions.ImportError("Cannot import native Java classes (yet!)");
} else {
java_name = "python." + python_name;
}

org.python.types.Module python_module = modules.get(java_name);
if (python_module == null) {
try {
java.lang.Class java_class = java.lang.Class.forName(java_name);
java.lang.reflect.Constructor constructor = java_class.getConstructor();
python_module = (org.python.types.Module) constructor.newInstance();
modules.put(java_name, python_module);
} catch (java.lang.ClassNotFoundException e) {
throw new org.python.exceptions.ImportError("No module named '" + python_name + "'");
} catch (java.lang.IllegalAccessException e) {
throw new org.python.exceptions.RuntimeError("Illegal access to constructor for module " + python_name);
} catch (java.lang.NoSuchMethodException e) {
throw new org.python.exceptions.RuntimeError("Couldn't find constructor for module " + python_name);
} catch (java.lang.reflect.InvocationTargetException e) {
try {
// If the Java method raised an Python exception, re-raise that
// exception as-is. If it wasn't a Python exception, wrap it
// as one and continue.
throw (org.python.exceptions.BaseException) e.getCause();
} catch (ClassCastException java_e) {
throw new org.python.exceptions.RuntimeError(e.getCause().getMessage());
}
} catch (java.lang.InstantiationException e) {
throw new org.python.exceptions.RuntimeError(e.getCause().toString());
} finally {
// System.out.println("CONSTRUCTOR DONE");
}
}

return python_module;
}

public static org.python.types.Module getModule(java.lang.String java_name) {
return modules.get(java_name);
}

}
6 changes: 6 additions & 0 deletions python/common/org/python/Method.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 Method {
java.lang.String __doc__() default "";
}
160 changes: 16 additions & 144 deletions python/common/org/python/Object.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,26 @@ public interface Object {
* Section 3.3.1 - Basic customization
*/

// public void __new__(); {
// }
public org.python.types.Type __new__(org.python.types.Type cls);

// public void __init__(); {
// }

public void __del__();
// public void __del__();

public org.python.types.Str __repr__();

public org.python.types.Str __str__();

public org.python.types.Bytes __bytes__();

public org.python.types.Str __format__();
public org.python.types.Str __format__(org.python.Object format_spec);

public org.python.Object __lt__(org.python.Object [] args, java.util.Hashtable kwargs);
public org.python.Object __lt__(org.python.Object other);

public org.python.Object __le__(org.python.Object [] args, java.util.Hashtable kwargs);
public org.python.Object __le__(org.python.Object other);

public org.python.Object __eq__(org.python.Object [] args, java.util.Hashtable kwargs);
public org.python.Object __eq__(org.python.Object other);

public org.python.Object __ne__(org.python.Object [] args, java.util.Hashtable kwargs);
public org.python.Object __ne__(org.python.Object other);

public org.python.Object __gt__(org.python.Object [] args, java.util.Hashtable kwargs);
public org.python.Object __gt__(org.python.Object other);

public org.python.Object __ge__(org.python.Object [] args, java.util.Hashtable kwargs);
public org.python.Object __ge__(org.python.Object other);

public org.python.types.Int __hash__();
Expand All @@ -48,200 +36,84 @@ public interface Object {
/**
* Section 3.3.2 - Emulating container types
*/
// The cast back from java Object to Hashtable is, strictly speaking,
// unchecked, but given we know it's an Object, we know value is a Map.
public org.python.Object __getattr__(org.python.Object [] args, java.util.Hashtable kwargs);
public org.python.Object __getattr__(org.python.Object name);
public org.python.Object __getattr__(java.lang.String name);

public org.python.Object __getattribute__(org.python.Object [] args, java.util.Hashtable kwargs);
public org.python.Object __getattribute__(org.python.Object name);
public org.python.Object __getattribute__(java.lang.String name);
public org.python.Object __get__(org.python.Object instance, org.python.types.Type klass);

public void __setattr__(org.python.Object [] args, java.util.Hashtable kwargs);
public void __setattr__(org.python.Object name, org.python.Object value);
public void __setattr__(java.lang.String name, org.python.Object value);

public void __delattr__(org.python.Object [] args, java.util.Hashtable kwargs);
public void __delattr__(org.python.Object name);
public void __delattr__(java.lang.String name);

public org.python.types.List __dir__();

/**
* Section 3.3.4 - Customizing instance and subclass checks
*/
public org.python.Object __instancecheck__(org.python.Object instance);

public org.python.Object __subclasscheck__(org.python.Object subclass);
// public org.python.Object __instancecheck__(org.python.Object instance);
// public org.python.Object __subclasscheck__(org.python.Object subclass);

/**
* Section 3.3.5 - Emulating callable objects
*/
public org.python.Object __call__(org.python.Object... args);
// public org.python.Object __call__(org.python.Object... args);

/**
* Section 3.3.6 - Emulating container types
*/

public org.python.types.Int __len__();

public org.python.types.Int __length_hint__();

public org.python.Object __getitem__(org.python.Object [] args, java.util.Hashtable kwargs);
public org.python.Object __getitem__(org.python.Object index);
public org.python.Object __getitem__(int index);

public org.python.Object __missing__(org.python.Object key);

public void __setitem__(org.python.Object [] args, java.util.Hashtable kwargs);
public void __setitem__(org.python.Object index, org.python.Object value);
public void __setitem__(int index, org.python.Object value);

public void __delitem__(org.python.Object [] args, java.util.Hashtable kwargs);
public void __delitem__(org.python.Object index);
public void __delitem__(int index);

public org.python.Iterable __iter__();

public org.python.Iterable __reversed__();

public org.python.types.Bool __contains__(org.python.Object item);
// public org.python.types.Bool __contains__(org.python.Object item);

/**
* Section 3.3.7 - Emulating numeric types
*/

public org.python.Object __add__(org.python.Object [] args, java.util.Hashtable kwargs);
public org.python.Object __add__(org.python.Object other);

public org.python.Object __sub__(org.python.Object [] args, java.util.Hashtable kwargs);
public org.python.Object __sub__(org.python.Object other);

public org.python.Object __mul__(org.python.Object [] args, java.util.Hashtable kwargs);
public org.python.Object __mul__(org.python.Object other);

public org.python.Object __truediv__(org.python.Object [] args, java.util.Hashtable kwargs);
public org.python.Object __truediv__(org.python.Object other);

public org.python.Object __floordiv__(org.python.Object [] args, java.util.Hashtable kwargs);
public org.python.Object __floordiv__(org.python.Object other);

public org.python.Object __mod__(org.python.Object [] args, java.util.Hashtable kwargs);
public org.python.Object __mod__(org.python.Object other);

public org.python.Object __divmod__(org.python.Object [] args, java.util.Hashtable kwargs);
public org.python.Object __divmod__(org.python.Object other);

public org.python.Object __pow__(org.python.Object [] args, java.util.Hashtable kwargs);
public org.python.Object __pow__(org.python.Object other);

public org.python.Object __lshift__(org.python.Object [] args, java.util.Hashtable kwargs);
public org.python.Object __pow__(org.python.Object other, org.python.Object modulus);
public org.python.Object __lshift__(org.python.Object other);

public org.python.Object __rshift__(org.python.Object [] args, java.util.Hashtable kwargs);
public org.python.Object __rshift__(org.python.Object other);

public org.python.Object __and__(org.python.Object [] args, java.util.Hashtable kwargs);
public org.python.Object __and__(org.python.Object other);

public org.python.Object __xor__(org.python.Object [] args, java.util.Hashtable kwargs);
public org.python.Object __xor__(org.python.Object other);

public org.python.Object __or__(org.python.Object [] args, java.util.Hashtable kwargs);
public org.python.Object __or__(org.python.Object other);

public org.python.Object __radd__(org.python.Object [] args, java.util.Hashtable kwargs);
public org.python.Object __radd__(org.python.Object other);

public org.python.Object __rsub__(org.python.Object [] args, java.util.Hashtable kwargs);
public org.python.Object __rsub__(org.python.Object other);

public org.python.Object __rmul__(org.python.Object [] args, java.util.Hashtable kwargs);
public org.python.Object __rmul__(org.python.Object other);

public org.python.Object __rtruediv__(org.python.Object [] args, java.util.Hashtable kwargs);
public org.python.Object __rtruediv__(org.python.Object other);

public org.python.Object __rfloordiv__(org.python.Object [] args, java.util.Hashtable kwargs);
public org.python.Object __rfloordiv__(org.python.Object other);

public org.python.Object __rmod__(org.python.Object [] args, java.util.Hashtable kwargs);
public org.python.Object __rmod__(org.python.Object other);

public org.python.Object __rdivmod__(org.python.Object [] args, java.util.Hashtable kwargs);
public org.python.Object __rdivmod__(org.python.Object other);

public org.python.Object __rpow__(org.python.Object [] args, java.util.Hashtable kwargs);
public org.python.Object __rpow__(org.python.Object other);

public org.python.Object __rlshift__(org.python.Object [] args, java.util.Hashtable kwargs);
public org.python.Object __rlshift__(org.python.Object other);

public org.python.Object __rrshift__(org.python.Object [] args, java.util.Hashtable kwargs);
public org.python.Object __rrshift__(org.python.Object other);

public org.python.Object __rand__(org.python.Object [] args, java.util.Hashtable kwargs);
public org.python.Object __rand__(org.python.Object other);

public org.python.Object __rxor__(org.python.Object [] args, java.util.Hashtable kwargs);
public org.python.Object __rxor__(org.python.Object other);

public org.python.Object __ror__(org.python.Object [] args, java.util.Hashtable kwargs);
public org.python.Object __ror__(org.python.Object other);

public void __iadd__(org.python.Object [] args, java.util.Hashtable kwargs);
public void __iadd__(org.python.Object other);

public void __isub__(org.python.Object [] args, java.util.Hashtable kwargs);
public void __isub__(org.python.Object other);

public void __imul__(org.python.Object [] args, java.util.Hashtable kwargs);
public void __imul__(org.python.Object other);

public void __itruediv__(org.python.Object [] args, java.util.Hashtable kwargs);
public void __itruediv__(org.python.Object other);

public void __ifloordiv__(org.python.Object [] args, java.util.Hashtable kwargs);
public void __ifloordiv__(org.python.Object other);

public void __imod__(org.python.Object [] args, java.util.Hashtable kwargs);
public void __imod__(org.python.Object other);

public void __ipow__(org.python.Object [] args, java.util.Hashtable kwargs);
public void __ipow__(org.python.Object other);

public void __ilshift__(org.python.Object [] args, java.util.Hashtable kwargs);
public void __ilshift__(org.python.Object other);

public void __irshift__(org.python.Object [] args, java.util.Hashtable kwargs);
public void __irshift__(org.python.Object other);

public void __iand__(org.python.Object [] args, java.util.Hashtable kwargs);
public void __iand__(org.python.Object other);

public void __ixor__(org.python.Object [] args, java.util.Hashtable kwargs);
public void __ixor__(org.python.Object other);

public void __ior__(org.python.Object [] args, java.util.Hashtable kwargs);
public void __ior__(org.python.Object other);

public org.python.Object __neg__();
public org.python.Object __pos__();
public org.python.Object __abs__();
public org.python.Object __invert__();

public org.python.Object __not__();
public org.python.Object __complex__(org.python.Object [] args, java.util.Hashtable kwargs);

public org.python.Object __complex__(org.python.Object real, org.python.Object imag);
public org.python.types.Int __int__();
public org.python.types.Float __float__();
public org.python.Object __round__();
public org.python.Object __round__(org.python.Object ndigits);

/**
* Section 3.3.8 - With statement context
*/
public org.python.Object __enter__();
public org.python.Object __exit__(org.python.Object exc_type, org.python.Object exc_value, org.python.Object traceback);
// /**
// * Section 3.3.8 - With statement context
// */
// public org.python.Object __enter__();
// public org.python.Object __exit__(org.python.Object exc_type, org.python.Object exc_value, org.python.Object traceback);

}
8 changes: 2 additions & 6 deletions python/common/org/python/exceptions/AttributeError.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package org.python.exceptions;

public class AttributeError extends org.python.exceptions.Exception {
public AttributeError() {
super();
}

public AttributeError(String msg) {
super(msg);
public AttributeError(org.python.Object obj, String attr) {
super(org.Python.pythonTypeName(obj) + " has no attribute '" + attr + "'");
}
}
Loading

0 comments on commit 59ea3d0

Please sign in to comment.