Skip to content
This repository has been archived by the owner on May 31, 2020. It is now read-only.

Commit

Permalink
Merge pull request #517 from freakboy3742/neat_class_names
Browse files Browse the repository at this point in the history
Remove the need for the __init__ class name when there's no submodules
  • Loading branch information
glasnt authored Apr 22, 2017
2 parents e45e6c2 + 2414f03 commit dd2a56f
Show file tree
Hide file tree
Showing 22 changed files with 137 additions and 86 deletions.
8 changes: 4 additions & 4 deletions docs/tutorials/tutorial-0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,23 @@ You will see output like the following:
Compiling example.py ...
Adding default main method...
Writing python/example/__init__.class ...
Writing python/example.class ...
This will produce an ``__init__.class`` in the ``python/example`` namespace.
This will produce an ``example.class`` in the ``python`` namespace.
This classfile can run on any Java 6 (or higher) VM. To run the project, type:

* On Linux / OS X

.. code-block:: bash
$ java -classpath ../voc/dist/python-java-support.jar:. python.example.__init__
$ java -classpath ../voc/dist/python-java-support.jar:. python.example
Hello World!
* On Windows

.. code-block:: bash
> java -classpath ../voc/dist/python-java-support.jar;. python.example.__init__
> java -classpath ../voc/dist/python-java-support.jar;. python.example
Hello World!
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package python.platform;
package python._platform;

import android.os.Debug;

public class AndroidPlatform implements python.Platform {
public class AndroidPlatform implements python.PlatformInterface {
private org.python.stdlib._io.TextIOWrapper _stderr;
private org.python.stdlib._io.TextIOWrapper _stdout;
private org.python.stdlib._io.TextIOWrapper _stdin;
Expand All @@ -18,11 +18,11 @@ public long clock() {
}

public void debug(java.lang.String msg) {
android.util.Log.d("Python", msg);
android.util.Log.i("Python", msg);
}

public void debug(java.lang.String msg, java.lang.Object obj) {
android.util.Log.d("Python", msg + " " + obj);
android.util.Log.i("Python", msg + " " + obj);
}

public org.python.Object getPlatform() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package python.platform;
package python._platform;

public class LogStream extends java.io.OutputStream {
protected java.lang.String tag;
Expand Down
8 changes: 4 additions & 4 deletions python/common/org/Python.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,14 @@ public class Python {
}

public static void debug(java.lang.String msg) {
python.platform.__init__.impl.debug(msg);
python.platform.impl.debug(msg);
}

public static void debug(java.lang.String msg, java.lang.Object obj) {
if (obj == null) {
python.platform.__init__.impl.debug(msg, "NULL");
python.platform.impl.debug(msg, "NULL");
} else {
python.platform.__init__.impl.debug(msg, obj);
python.platform.impl.debug(msg, obj);
}
}

Expand Down Expand Up @@ -1405,7 +1405,7 @@ public static void print(org.python.types.Tuple value, org.python.Object file, o
}

if (file == null) {
file = python.sys.__init__.stdout;
file = python.sys.stdout;
}

org.python.Object content = new org.python.types.Str(buffer.toString());
Expand Down
29 changes: 20 additions & 9 deletions python/common/org/python/ImportLib.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public static org.python.types.Module __import__(

java.lang.Class java_class = java.lang.Thread.currentThread().getContextClassLoader().loadClass("org.Python");
python_module = new org.python.types.Module(java_class);
python.sys.__init__.modules.__setitem__(new org.python.types.Str("builtins"), python_module);
python.sys.modules.__setitem__(new org.python.types.Str("builtins"), python_module);

parent_module = python_module;
return_module = python_module;
Expand Down Expand Up @@ -111,7 +111,7 @@ public static org.python.types.Module __import__(

try {
// org.Python.debug("IMPORT", mod_name);
python_module = (org.python.types.Module) python.sys.__init__.modules.__getitem__(new org.python.types.Str(mod_name));
python_module = (org.python.types.Module) python.sys.modules.__getitem__(new org.python.types.Str(mod_name));
// org.Python.debug("Already imported", mod_name);
} catch (org.python.exceptions.KeyError ke) {
try {
Expand Down Expand Up @@ -149,7 +149,7 @@ public static org.python.types.Module __import__(
return_module = python_module;
}

// org.Python.debug("MODULES", python.sys.__init__.modules);
// org.Python.debug("MODULES", python.sys.modules);
// The module just imported will be the parent of the next import
// in the chain.
parent_module = python_module;
Expand Down Expand Up @@ -186,7 +186,7 @@ public static org.python.types.Module __import__(
// org.Python.debug("must be a java module");
python_module = new org.python.java.Module(mod_name);
parent_module.__setattr__(name, python_module);
python.sys.__init__.modules.__setitem__(new org.python.types.Str(mod_name), python_module);
python.sys.modules.__setitem__(new org.python.types.Str(mod_name), python_module);
}
}
}
Expand All @@ -204,7 +204,7 @@ private static org.python.types.Module importNativeModule(java.lang.String impor
python_module = null;
} catch (java.lang.ClassNotFoundException e) {
python_module = new org.python.java.Module(import_name);
python.sys.__init__.modules.__setitem__(new org.python.types.Str(import_name), python_module);
python.sys.modules.__setitem__(new org.python.types.Str(import_name), python_module);
// } finally {
// org.Python.debug("CONSTRUCTOR DONE");
}
Expand All @@ -216,16 +216,27 @@ private static org.python.types.Module importPythonModule(java.lang.String impor
org.python.types.Module python_module;
try {
// Load and construct an instance of the module class.
java.lang.Class java_class = java.lang.Thread.currentThread().getContextClassLoader().loadClass("python." + import_name + ".__init__");
java.lang.reflect.Constructor constructor = java_class.getConstructor();
python_module = (org.python.types.Module) constructor.newInstance();
java.lang.Class java_class;
try {
java_class = java.lang.Thread.currentThread().getContextClassLoader().loadClass("python." + import_name + ".__init__");
} catch (ClassNotFoundException cnfe) {
java_class = java.lang.Thread.currentThread().getContextClassLoader().loadClass("python." + import_name);
}

// Create an instance of the class; if it isn't a module, we haven't been able to import.
try {
java.lang.reflect.Constructor constructor = java_class.getConstructor();
python_module = (org.python.types.Module) constructor.newInstance();
} catch (ClassCastException cce) {
throw new java.lang.ClassNotFoundException("Found " + import_name + " class, but class is not a module");
}

// Store module instance at imported name
java.lang.String python_name = import_name;
if (import_name.startsWith("python.")) {
python_name = import_name.substring(7);
}
python.sys.__init__.modules.__setitem__(new org.python.types.Str(python_name), python_module);
python.sys.modules.__setitem__(new org.python.types.Str(python_name), python_module);

// Initialize module
java.lang.reflect.Method init = java_class.getMethod("module$import");
Expand Down
6 changes: 5 additions & 1 deletion python/common/org/python/types/Type.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,20 @@ public static org.python.types.Type declarePythonType(
if (org.python.Object.class.isAssignableFrom(java_class)) {
if (java_class.getName().startsWith("org.python.types")
|| java_class.getName().startsWith("org.python.stdlib")) {
// System.out.println(" BUILTIN");
python_type = new org.python.types.Type(Origin.BUILTIN, java_class);
org.Python.initializeModule(java_class, python_type.__dict__);
} else {
// System.out.println(" PYTHON");
python_type = new org.python.types.Type(Origin.PYTHON, java_class);
}
} else {
try {
// System.out.println(" EXTENSION");
java_class.getDeclaredField("__VOC__");
python_type = new org.python.java.Type(Origin.EXTENSION, java_class);
} catch (NoSuchFieldException e) {
// System.out.println(" JAVA");
python_type = new org.python.java.Type(Origin.JAVA, java_class);
}
}
Expand Down Expand Up @@ -326,7 +330,7 @@ public org.python.Object __getattribute_null(java.lang.String name) {
// System.out.println("__dict__ " + this.__dict__);
org.python.Object module_name = this.__dict__.get("__module__");
if (module_name != null) {
org.python.Object module = python.sys.__init__.modules.__getitem__(module_name);
org.python.Object module = python.sys.modules.__getitem__(module_name);
value = module.__getattribute_null(name);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package python;

public interface Platform {
public interface PlatformInterface {
/**
* Return the number of CPU nanoseconds that this thread has consumed.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package python._io;
package python;

@org.python.Module(
__doc__ =
Expand Down Expand Up @@ -38,7 +38,7 @@
"\n" +
"\n"
)
public class __init__ extends org.python.types.Module {
public class _io extends org.python.types.Module {
@org.python.Method(
__doc__ = "Create and return a new object. See help(type) for accurate signature."
)
Expand Down Expand Up @@ -67,6 +67,8 @@ public org.python.Object __new__(org.python.Object klass) {
public static org.python.Object __loader__ = org.python.types.NoneType.NONE; // TODO
@org.python.Attribute
public static org.python.Object __name__ = new org.python.types.Str("_io");
@org.python.Attribute()
public static org.python.Object __file__ = new org.python.types.Str("python/common/python/_io.java");
@org.python.Attribute
public static org.python.Object __package__ = new org.python.types.Str("");
@org.python.Attribute
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package python.importlib;
package python;

@org.python.Module(
__doc__ = "A pure Python implementation of import."
)
public class __init__ extends org.python.types.Module {
public class importlib extends org.python.types.Module {
@org.python.Method(
__doc__ = "Create and return a new object. See help(type) for accurate signature."
)
Expand All @@ -22,8 +22,6 @@ public org.python.Object __new__(org.python.Object klass) {
// public static org.python.Object __builtins__;
@org.python.Attribute()
public static org.python.Object __cached__;
@org.python.Attribute
public static org.python.Object __file__ = new org.python.types.Str("python/common/python/importlib/__init__.java");

@org.python.Method(
__doc__ = "",
Expand All @@ -39,6 +37,8 @@ public static org.python.Object __import__(org.python.Object python_name, org.py
public static org.python.Object __loader__ = org.python.types.NoneType.NONE; // TODO
@org.python.Attribute
public static org.python.Object __name__ = new org.python.types.Str("importlib");
@org.python.Attribute()
public static org.python.Object __file__ = new org.python.types.Str("python/common/python/importlib.java");
@org.python.Attribute
public static org.python.Object __package__ = new org.python.types.Str("");
@org.python.Attribute()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package python.os;
package python;

@org.python.Module(
__doc__ =
Expand All @@ -25,7 +25,7 @@
"(e.g., split and join).\n" +
"\n"
)
public class __init__ extends org.python.types.Module {
public class os extends org.python.types.Module {
static {
environ = new org.python.types.Dict();
environb = new org.python.types.Dict();
Expand Down Expand Up @@ -279,7 +279,7 @@ public static org.python.Object _Environ() {
@org.python.Attribute()
public static org.python.Object __cached__ = org.python.types.NoneType.NONE; // TODO;
@org.python.Attribute()
public static org.python.Object __file__ = new org.python.types.Str("python/common/python/os/__init__.java");
public static org.python.Object __file__ = new org.python.types.Str("python/common/python/os.java");
@org.python.Attribute()
public static org.python.Object __loader__ = org.python.types.NoneType.NONE; // TODO
@org.python.Attribute()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package python.platform;
package python;

@org.python.Module(
__doc__ =
Expand All @@ -12,8 +12,8 @@
"\n" +
"\n"
)
public class __init__ extends org.python.types.Module {
public static python.Platform impl;
public class platform extends org.python.types.Module {
public static python.PlatformInterface impl;

static {
java.util.Properties prop = System.getProperties();
Expand All @@ -22,16 +22,16 @@ public class __init__ extends org.python.types.Module {
java.lang.Class platform_class;

if (vendor.equals("Oracle Corporation")) {
platform_class_name = "python.platform.JavaPlatform";
platform_class_name = "python._platform.JavaPlatform";
} else if (vendor.equals("The Android Project")) {
platform_class_name = "python.platform.AndroidPlatform";
platform_class_name = "python._platform.AndroidPlatform";
} else {
throw new org.python.exceptions.RuntimeError("Unknown platform vendor '" + vendor + "'");
}

try {
platform_class = java.lang.Class.forName(platform_class_name);
impl = (python.Platform) platform_class.getConstructor().newInstance();
impl = (python.PlatformInterface) platform_class.getConstructor().newInstance();
} catch (ClassNotFoundException e) {
throw new org.python.exceptions.RuntimeError("Unable to find platform '" + platform_class_name + "'");
} catch (NoSuchMethodException e) {
Expand All @@ -53,7 +53,7 @@ public class __init__ extends org.python.types.Module {

// __copyright__',
@org.python.Attribute()
public static org.python.Object __file__ = new org.python.types.Str("python/common/python/platform/__init__.java");
public static org.python.Object __file__ = new org.python.types.Str("python/common/python/platform.java");
@org.python.Attribute()
public static org.python.Object __loader__ = org.python.types.NoneType.NONE; // TODO
@org.python.Attribute()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package python.sys;
package python;

@org.python.Module(
__doc__ =
Expand Down Expand Up @@ -71,17 +71,17 @@
"setrecursionlimit() -- set the max recursion depth for the interpreter\n" +
"settrace() -- set the global debug tracing function\n"
)
public class __init__ extends org.python.types.Module {
public class sys extends org.python.types.Module {
static {
stdout = python.platform.__init__.impl.stdout();
stderr = python.platform.__init__.impl.stderr();
stdin = python.platform.__init__.impl.stdin();
stdout = python.platform.impl.stdout();
stderr = python.platform.impl.stderr();
stdin = python.platform.impl.stdin();

__stdout__ = python.platform.__init__.impl.stdout();
__stderr__ = python.platform.__init__.impl.stderr();
__stdin__ = python.platform.__init__.impl.stdin();
__stdout__ = python.platform.impl.stdout();
__stderr__ = python.platform.impl.stderr();
__stdin__ = python.platform.impl.stdin();

platform = python.platform.__init__.impl.getPlatform();
platform = python.platform.impl.getPlatform();

hexversion = new org.python.types.Int(org.Python.VERSION);

Expand Down Expand Up @@ -109,7 +109,7 @@ public org.python.Object __new__(org.python.Object klass) {
// cls.__dict__.put("__spec__", new org.python.types...);
// cls.__dict__.put("__loader__", new org.python.types...);

cls.__dict__.put("argv", python.platform.__init__.impl.args());
cls.__dict__.put("argv", python.platform.impl.args());
return cls;
}

Expand All @@ -133,11 +133,15 @@ public static org.python.Object __excepthook__(java.util.List<org.python.Object>
public static org.python.Object __loader__ = org.python.types.NoneType.NONE; // TODO
@org.python.Attribute()
public static org.python.Object __name__ = new org.python.types.Str("sys");
@org.python.Attribute
public static org.python.Object __file__ = new org.python.types.Str("python/common/python/sys.java");
@org.python.Attribute()
public static org.python.Object __package__ = new org.python.types.Str("");
@org.python.Attribute()
public static org.python.Object __spec__ = org.python.types.NoneType.NONE; // TODO

public static org.python.types.Int __plen;

@org.python.Attribute()
public static org.python.Object __stderr__;
@org.python.Attribute()
Expand Down
Loading

0 comments on commit dd2a56f

Please sign in to comment.