Skip to content

Commit

Permalink
Merge pull request beeware#178 from freakboy3742/master
Browse files Browse the repository at this point in the history
Make sys.modules a Python dict.
  • Loading branch information
freakboy3742 authored Jun 20, 2016
2 parents 47ec1b0 + ec2765b commit 73e5c6d
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 50 deletions.
3 changes: 2 additions & 1 deletion build-java.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@
</javac>
</target>

<target name="compile-testdaemon" description="Compile the testdaemon source">
<target name="compile-testdaemon" depends="compile" description="Compile the testdaemon source">
<mkdir dir="${build}/java-testdaemon"/>
<javac
debug="true"
debuglevel="lines,vars,source"
includeantruntime="false"
destdir="${build}/java-testdaemon">
<src path="${src}/testdaemon" />
<classpath path="${build}/java" />
</javac>
</target>

Expand Down
29 changes: 16 additions & 13 deletions python/common/org/python/ImportLib.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public static org.python.types.Module __import__(java.lang.String python_name, j
|| path[0].equals("android")) {
native_import = true;
} else {
import_name.append("python");
native_import = false;
}

Expand All @@ -35,15 +34,18 @@ public static org.python.types.Module __import__(java.lang.String python_name, j
import_name.append(".");
}
import_name.append(name);
java.lang.String mod_name = import_name.toString();

python_module = python.sys.__init__.modules.get(import_name.toString());
if (python_module == null) {
try {
// System.out.println("IMPORT " + mod_name);
python_module = (org.python.types.Module) python.sys.__init__.modules.__getitem__(new org.python.types.Str(mod_name));
} catch (org.python.exceptions.KeyError ke) {
try {
// System.out.println("IMPORT " + import_name.toString());
// System.out.println("handle IMPORT " + mod_name);
if (native_import) {
python_module = importNativeModule(import_name.toString());
python_module = importNativeModule(mod_name);
} else {
python_module = importPythonModule(import_name.toString());
python_module = importPythonModule(mod_name);
}
} catch (java.lang.ClassNotFoundException e) {
throw new org.python.exceptions.ImportError("No module named '" + python_name + "'");
Expand Down Expand Up @@ -76,12 +78,13 @@ public static org.python.types.Module __import__(java.lang.String python_name, j
for (java.lang.String name: from_list) {
// System.out.println("IMPORT NAME " + name);
if (!name.equals("*")) {
java.lang.String mod_name = import_name.toString() + name;
try {
if (native_import) {
java.lang.Class java_class = java.lang.Thread.currentThread().getContextClassLoader().loadClass(import_name.toString() + name);
java.lang.Class java_class = java.lang.Thread.currentThread().getContextClassLoader().loadClass(mod_name);
parent_module.__setattr__(name, org.python.java.Type.pythonType(java_class));
} else {
python_module = importPythonModule(import_name.toString() + name);
python_module = importPythonModule(mod_name);
parent_module.__setattr__(name, python_module);
}
} catch (java.lang.ClassNotFoundException e) {
Expand All @@ -90,9 +93,9 @@ public static org.python.types.Module __import__(java.lang.String python_name, j
try {
parent_module.__getattribute__(name);
} catch (org.python.exceptions.NameError ne) {
python_module = new org.python.java.Module(import_name.toString() + name);
python_module = new org.python.java.Module(mod_name);
parent_module.__setattr__(name, python_module);
python.sys.__init__.modules.put(name.toString() + name, python_module);
python.sys.__init__.modules.__setitem__(new org.python.types.Str(mod_name), python_module);
}
}
}
Expand All @@ -109,7 +112,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.put(import_name, python_module);
python.sys.__init__.modules.__setitem__(new org.python.types.Str(import_name), python_module);
} finally {
// System.out.println("CONSTRUCTOR DONE");
}
Expand All @@ -121,7 +124,7 @@ 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(import_name + ".__init__");
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();

Expand All @@ -130,7 +133,7 @@ private static org.python.types.Module importPythonModule(java.lang.String impor
if (import_name.startsWith("python.")) {
python_name = import_name.substring(7);
}
python.sys.__init__.modules.put(python_name, python_module);
python.sys.__init__.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
3 changes: 1 addition & 2 deletions python/common/python/sys/__init__.java
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,7 @@ public static org.python.Object intern(java.util.List<org.python.Object> args, j

public static org.python.types.List meta_path;

// FIXME: This should be an org.python.types.Dict
public static java.util.Map<java.lang.String, org.python.types.Module> modules = new java.util.HashMap<java.lang.String, org.python.types.Module>();
public static org.python.types.Dict modules = new org.python.types.Dict();

public static org.python.types.List path;

Expand Down
5 changes: 3 additions & 2 deletions python/testdaemon/python/testdaemon/TestDaemon.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.Arrays;
import java.util.Scanner;


public class TestDaemon {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Expand Down Expand Up @@ -89,9 +90,9 @@ public static void main(String[] args) {
} finally {
// always cleanup the module cache in ImportLib
try {
Class<?> importlib = joinedClassLoader.loadClass("org.python.ImportLib");
Class<?> importlib = joinedClassLoader.loadClass("python.sys.__init__");
Field importlib_modules = importlib.getDeclaredField("modules");
importlib_modules.set(null, new java.util.HashMap());
importlib_modules.set(null, new org.python.types.Dict());
} catch (ReflectiveOperationException e) {
// ClassNotFound, NoSuchMethod, IllegalAccess Exceptions
}
Expand Down
4 changes: 2 additions & 2 deletions tests/stdlib/test_sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,13 @@ def test_argv(self):
import sys
print('ARGS =', sys.argv)
print('Done.')
""", run_in_global=False)
""")

self.assertCodeExecution("""
import sys
print('ARGS =', sys.argv)
print('Done.')
""", run_in_global=False, args=['1', 'asdf', '3'])
""", args=['1', 'asdf', '3'])

############################################################
# base_exec_prefix
Expand Down
8 changes: 6 additions & 2 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,10 @@ def setUpClass(cls):
global _output_dir
setUpSuite()
cls.temp_dir = os.path.join(_output_dir, 'temp')
classpath = os.path.join('dist', 'python-java-testdaemon.jar')
classpath = ':'.join([
os.path.join('dist', 'python-java-testdaemon.jar'),
os.path.join('dist', 'python-java.jar'),
])
cls.jvm = subprocess.Popen(
["java", "-classpath", classpath, "python.testdaemon.TestDaemon"],
stdin=subprocess.PIPE,
Expand Down Expand Up @@ -495,6 +498,7 @@ def runAsJava(self, main_code, extra_code=None, run_in_function=False, args=None
# encode to turn str into bytes-like object
self.jvm.stdin.write(("python.test.__init__\n").encode("utf-8"))
self.jvm.stdin.flush()

out = ""
while True:
try:
Expand All @@ -503,7 +507,7 @@ def runAsJava(self, main_code, extra_code=None, run_in_function=False, args=None
break
else:
out += line
except IOError:
except IOError as e:
continue
else:
classpath = os.pathsep.join([
Expand Down
68 changes: 51 additions & 17 deletions voc/python/methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,14 @@ def store_name(self, name, use_locals):
self.add_opcodes(
ASTORE_name(self, '#value'),

JavaOpcodes.GETSTATIC('python/sys/__init__', 'modules', 'Ljava/util/Map;'),
JavaOpcodes.GETSTATIC('python/sys/__init__', 'modules', 'Lorg/python/types/Dict;'),

JavaOpcodes.NEW('org/python/types/Str'),
JavaOpcodes.DUP(),
JavaOpcodes.LDC_W(self.globals_module.full_name),
JavaOpcodes.INVOKEINTERFACE('java/util/Map', 'get', '(Ljava/lang/Object;)Ljava/lang/Object;'),
JavaOpcodes.INVOKESPECIAL('org/python/types/Str', '<init>', '(Ljava/lang/String;)V'),

JavaOpcodes.INVOKEINTERFACE('org/python/Object', '__getitem__', '(Lorg/python/Object;)Lorg/python/Object;'),
JavaOpcodes.CHECKCAST('org/python/types/Module'),

JavaOpcodes.LDC_W(name),
Expand All @@ -124,10 +128,14 @@ def load_name(self, name, use_locals):
pass

self.add_opcodes(
JavaOpcodes.GETSTATIC('python/sys/__init__', 'modules', 'Ljava/util/Map;'),
JavaOpcodes.GETSTATIC('python/sys/__init__', 'modules', 'Lorg/python/types/Dict;'),

JavaOpcodes.NEW('org/python/types/Str'),
JavaOpcodes.DUP(),
JavaOpcodes.LDC_W(self.globals_module.full_name),
JavaOpcodes.INVOKESPECIAL('org/python/types/Str', '<init>', '(Ljava/lang/String;)V'),

JavaOpcodes.INVOKEINTERFACE('java/util/Map', 'get', '(Ljava/lang/Object;)Ljava/lang/Object;'),
JavaOpcodes.INVOKEINTERFACE('org/python/Object', '__getitem__', '(Lorg/python/Object;)Lorg/python/Object;'),
JavaOpcodes.CHECKCAST('org/python/types/Module'),

JavaOpcodes.LDC_W(name),
Expand All @@ -140,9 +148,14 @@ def delete_name(self, name, use_locals):
free_name(self, name)
except KeyError:
self.add_opcodes(
JavaOpcodes.GETSTATIC('python/sys/__init__', 'modules', 'Ljava/util/Map;'),
JavaOpcodes.GETSTATIC('python/sys/__init__', 'modules', 'Lorg/python/types/Dict;'),

JavaOpcodes.NEW('org/python/types/Str'),
JavaOpcodes.DUP(),
JavaOpcodes.LDC_W(self.globals_module.full_name),
JavaOpcodes.INVOKEINTERFACE('java/util/Map', 'get', '(Ljava/lang/Object;)Ljava/lang/Object;'),
JavaOpcodes.INVOKESPECIAL('org/python/types/Str', '<init>', '(Ljava/lang/String;)V'),

JavaOpcodes.INVOKEINTERFACE('org/python/Object', '__getitem__', '(Lorg/python/Object;)Lorg/python/Object;'),
JavaOpcodes.CHECKCAST('org/python/types/Module'),

JavaOpcodes.LDC_W(name),
Expand Down Expand Up @@ -704,9 +717,14 @@ def globals_module(self):
def store_name(self, name, use_locals):
self.add_opcodes(
ASTORE_name(self, '#value'),
JavaOpcodes.GETSTATIC('python/sys/__init__', 'modules', 'Ljava/util/Map;'),
JavaOpcodes.GETSTATIC('python/sys/__init__', 'modules', 'Lorg/python/types/Dict;'),

JavaOpcodes.NEW('org/python/types/Str'),
JavaOpcodes.DUP(),
JavaOpcodes.LDC_W(self.module.full_name),
JavaOpcodes.INVOKEINTERFACE('java/util/Map', 'get', '(Ljava/lang/Object;)Ljava/lang/Object;'),
JavaOpcodes.INVOKESPECIAL('org/python/types/Str', '<init>', '(Ljava/lang/String;)V'),

JavaOpcodes.INVOKEINTERFACE('org/python/Object', '__getitem__', '(Lorg/python/Object;)Lorg/python/Object;'),
JavaOpcodes.CHECKCAST('org/python/types/Module'),

JavaOpcodes.LDC_W(name),
Expand All @@ -731,19 +749,29 @@ def store_dynamic(self):

def load_name(self, name, use_locals):
self.add_opcodes(
JavaOpcodes.GETSTATIC('python/sys/__init__', 'modules', 'Ljava/util/Map;'),
JavaOpcodes.GETSTATIC('python/sys/__init__', 'modules', 'Lorg/python/types/Dict;'),

JavaOpcodes.NEW('org/python/types/Str'),
JavaOpcodes.DUP(),
JavaOpcodes.LDC_W(self.module.full_name),
JavaOpcodes.INVOKEINTERFACE('java/util/Map', 'get', '(Ljava/lang/Object;)Ljava/lang/Object;'),
JavaOpcodes.INVOKESPECIAL('org/python/types/Str', '<init>', '(Ljava/lang/String;)V'),

JavaOpcodes.INVOKEINTERFACE('org/python/Object', '__getitem__', '(Lorg/python/Object;)Lorg/python/Object;'),
JavaOpcodes.CHECKCAST('org/python/types/Module'),
JavaOpcodes.LDC_W(name),
JavaOpcodes.INVOKEINTERFACE('org/python/Object', '__getattribute__', '(Ljava/lang/String;)Lorg/python/Object;'),
)

def delete_name(self, name, use_locals):
self.add_opcodes(
JavaOpcodes.GETSTATIC('python/sys/__init__', 'modules', 'Ljava/util/Map;'),
JavaOpcodes.GETSTATIC('python/sys/__init__', 'modules', 'Lorg/python/types/Dict;'),

JavaOpcodes.NEW('org/python/types/Str'),
JavaOpcodes.DUP(),
JavaOpcodes.LDC_W(self.module.full_name),
JavaOpcodes.INVOKEINTERFACE('java/util/Map', 'get', '(Ljava/lang/Object;)Ljava/lang/Object;'),
JavaOpcodes.INVOKESPECIAL('org/python/types/Str', '<init>', '(Ljava/lang/String;)V'),

JavaOpcodes.INVOKEINTERFACE('org/python/Object', '__getitem__', '(Lorg/python/Object;)Lorg/python/Object;'),
JavaOpcodes.CHECKCAST('org/python/types/Module'),
JavaOpcodes.LDC_W(name),
JavaOpcodes.INVOKEVIRTUAL('org/python/types/Module', '__delattr__', '(Ljava/lang/String;)V'),
Expand All @@ -755,24 +783,30 @@ def transpile_setup(self):
TRY(),

# Initialize and register this module
JavaOpcodes.GETSTATIC('python/sys/__init__', 'modules', 'Ljava/util/Map;'),
JavaOpcodes.GETSTATIC('python/sys/__init__', 'modules', 'Lorg/python/types/Dict;'),
JavaOpcodes.DUP(),

JavaOpcodes.NEW('org/python/types/Str'),
JavaOpcodes.DUP(),
JavaOpcodes.LDC_W(self.module.full_name),
JavaOpcodes.INVOKESPECIAL('org/python/types/Str', '<init>', '(Ljava/lang/String;)V'),

JavaOpcodes.NEW(self.module.class_descriptor),
JavaOpcodes.DUP(),
JavaOpcodes.DUP(),
JavaOpcodes.INVOKESPECIAL(self.module.class_descriptor, '<init>', '()V'),
ASTORE_name(self, '#module'),

JavaOpcodes.INVOKEINTERFACE('java/util/Map', 'put', '(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;'),
JavaOpcodes.POP(),
JavaOpcodes.INVOKEINTERFACE('org/python/Object', '__setitem__', '(Lorg/python/Object;Lorg/python/Object;)V'),

# Register the same instances as __main__
JavaOpcodes.NEW('org/python/types/Str'),
JavaOpcodes.DUP(),
JavaOpcodes.LDC_W('__main__'),
JavaOpcodes.INVOKESPECIAL('org/python/types/Str', '<init>', '(Ljava/lang/String;)V'),

ALOAD_name(self, '#module'),
JavaOpcodes.INVOKEINTERFACE('java/util/Map', 'put', '(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;'),
JavaOpcodes.POP(),
JavaOpcodes.INVOKEINTERFACE('org/python/Object', '__setitem__', '(Lorg/python/Object;Lorg/python/Object;)V'),

# Run the module block.
ALOAD_name(self, '#module'),
Expand Down
Loading

0 comments on commit 73e5c6d

Please sign in to comment.