Skip to content

Commit

Permalink
Added implementation of import *.
Browse files Browse the repository at this point in the history
  • Loading branch information
freakboy3742 committed Nov 30, 2015
1 parent 1e7a5aa commit 0e71f1b
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 47 deletions.
13 changes: 8 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,25 @@ What it does:

* Keyword arguments

* `import` statements
* `import`, `from X import Y` and `from X import *` statements for Python code

It *doesn't* currently support:

* `from X import Y` statements
* Dict and Set constants

* Importing and using native Java APIs

* Extending native java classes and interfaces.

* Generators

* with statements

* Dict and Set constants

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

These things are all *possible* - it's just a matter of time
and development effort.
and development effort. The order listed here is a rough indicator of
development priorities.

Quickstart
----------
Expand Down
77 changes: 52 additions & 25 deletions python/common/org/Python.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public static org.python.Object abs(
if (args.size() != 1) {
throw new org.python.exceptions.TypeError("abs() takes exactly one argument (" + args.size() + " given)");
}
if (kwargs.size() != 0) {
if (kwargs != null && kwargs.size() != 0) {
throw new org.python.exceptions.TypeError("abs() takes no keyword arguments");
}
return args.get(0).__abs__();
Expand Down Expand Up @@ -211,7 +211,7 @@ public static org.python.types.Str bin(
if (args.size() != 1) {
throw new org.python.exceptions.TypeError("bin() takes exactly one argument (" + args.size() + " given)");
}
if (kwargs.size() != 0) {
if (kwargs != null && kwargs.size() != 0) {
throw new org.python.exceptions.TypeError("bin() takes no keyword arguments");
}
return new org.python.types.Str(java.lang.String.format("0b%b", int_cast(args, kwargs).value));
Expand All @@ -230,7 +230,7 @@ public static org.python.types.Bool bool(
if (args.size() != 1) {
throw new org.python.exceptions.TypeError("bool() takes exactly one argument (" + args.size() + " given)");
}
if (kwargs.size() != 0) {
if (kwargs != null && kwargs.size() != 0) {
throw new org.python.exceptions.TypeError("bool() takes no keyword arguments");
}
return args.get(0).__bool__();
Expand Down Expand Up @@ -288,7 +288,7 @@ public static org.python.types.Bool callable(
if (args.size() != 1) {
throw new org.python.exceptions.TypeError("callable() takes exactly one argument (" + args.size() + " given)");
}
if (kwargs.size() != 0) {
if (kwargs != null && kwargs.size() != 0) {
throw new org.python.exceptions.TypeError("callable() takes no keyword arguments");
}
return new org.python.types.Bool(org.python.Callable.class.isAssignableFrom(args.get(0).getClass()));
Expand All @@ -305,7 +305,7 @@ public static org.python.types.Str chr(
if (args.size() != 1) {
throw new org.python.exceptions.TypeError("chr() takes exactly one argument (" + args.size() + " given)");
}
if (kwargs.size() != 0) {
if (kwargs != null && kwargs.size() != 0) {
throw new org.python.exceptions.TypeError("chr() takes no keyword arguments");
}

Expand Down Expand Up @@ -469,7 +469,7 @@ public static org.python.types.Dict dict(
public static org.python.types.List dir(
java.util.List<org.python.Object> args,
java.util.Map<java.lang.String, org.python.Object> kwargs) {
if (kwargs.size() != 0) {
if (kwargs != null && kwargs.size() != 0) {
throw new org.python.exceptions.TypeError("dir() takes no keyword arguments");
}
if (args.size() == 0) {
Expand Down Expand Up @@ -560,7 +560,7 @@ public static org.python.types.Float float_cast(
if (args.size() != 1) {
throw new org.python.exceptions.TypeError("float() takes exactly one argument (" + args.size() + " given)");
}
if (kwargs.size() != 0) {
if (kwargs != null && kwargs.size() != 0) {
throw new org.python.exceptions.TypeError("float() takes no keyword arguments");
}
return args.get(0).__float__();
Expand All @@ -575,7 +575,7 @@ public static org.python.types.Float float_cast(
public static org.python.types.Str format(
java.util.List<org.python.Object> args,
java.util.Map<java.lang.String, org.python.Object> kwargs) {
if (kwargs.size() != 0) {
if (kwargs != null && kwargs.size() != 0) {
throw new org.python.exceptions.TypeError("format() takes no keyword arguments");
}
if (args.size() < 1) {
Expand All @@ -599,7 +599,7 @@ public static org.python.types.FrozenSet frozenset(
java.util.List<org.python.Object> args,
java.util.Map<java.lang.String, org.python.Object> kwargs) {
throw new org.python.exceptions.NotImplementedError("Builtin function 'frozenset' not implemented");
// if (kwargs.size() != 0) {
// if (kwargs != null && kwargs.size() != 0) {
// throw new org.python.exceptions.TypeError("frozenset() does not take keyword arguments");
// }
// if (args.size() == 0) {
Expand Down Expand Up @@ -660,7 +660,7 @@ public static org.python.types.Int hash(
if (args.size() != 1) {
throw new org.python.exceptions.TypeError("hash() takes exactly one argument (" + args.size() + " given)");
}
if (kwargs.size() != 0) {
if (kwargs != null && kwargs.size() != 0) {
throw new org.python.exceptions.TypeError("hash() takes no keyword arguments");
}
return args.get(0).__hash__();
Expand Down Expand Up @@ -690,7 +690,7 @@ public static org.python.types.Str hex(
if (args.size() != 1) {
throw new org.python.exceptions.TypeError("hex() takes exactly one argument (" + args.size() + " given)");
}
if (kwargs.size() != 0) {
if (kwargs != null && kwargs.size() != 0) {
throw new org.python.exceptions.TypeError("hex() takes no keyword arguments");
}
return new org.python.types.Str(String.format("0x%x", int_cast(args, kwargs)));
Expand All @@ -708,7 +708,7 @@ public static org.python.types.Int id(
if (args.size() != 1) {
throw new org.python.exceptions.TypeError("id() takes exactly one argument (" + args.size() + " given)");
}
if (kwargs.size() != 0) {
if (kwargs != null && kwargs.size() != 0) {
throw new org.python.exceptions.TypeError("id() takes no keyword arguments");
}
return new org.python.types.Int(System.identityHashCode(args.get(0)));
Expand All @@ -728,7 +728,7 @@ public static org.python.types.Str input(
if (args.size() > 1) {
throw new org.python.exceptions.TypeError("input() expected at most one arguments ( got " + args.size() + ")");
}
if (kwargs.size() != 0) {
if (kwargs != null && kwargs.size() != 0) {
throw new org.python.exceptions.TypeError("bin() takes no keyword arguments");
}

Expand Down Expand Up @@ -818,7 +818,7 @@ public static org.python.types.Bool issubclass(
public static org.python.Iterable iter(
java.util.List<org.python.Object> args,
java.util.Map<java.lang.String, org.python.Object> kwargs) {
if (kwargs.size() != 0) {
if (kwargs != null && kwargs.size() != 0) {
throw new org.python.exceptions.TypeError("iter() takes no keyword arguments");
}
if (args.size() < 1) {
Expand All @@ -843,7 +843,7 @@ public static org.python.types.Int len(
if (args.size() != 1) {
throw new org.python.exceptions.TypeError("len() takes exactly one argument (" + args.size() + " given)");
}
if (kwargs.size() != 0) {
if (kwargs != null && kwargs.size() != 0) {
throw new org.python.exceptions.TypeError("len() takes no keyword arguments");
}
return args.get(0).__len__();
Expand All @@ -866,7 +866,34 @@ public static org.python.Object license(
public static org.python.types.List list(
java.util.List<org.python.Object> args,
java.util.Map<java.lang.String, org.python.Object> kwargs) {
throw new org.python.exceptions.NotImplementedError("Builtin function 'list' not implemented");
if (kwargs != null && kwargs.size() != 0) {
throw new org.python.exceptions.TypeError("list() takes no keyword arguments");
}
if (args.size() == 1) {
if (args.get(0) instanceof org.python.types.List) {
return new org.python.types.List(
new java.util.ArrayList(
((org.python.types.List) args.get(0)).value
)
);
} else if (args.get(0) instanceof org.python.types.Tuple) {
return new org.python.types.List(
new java.util.ArrayList(
((org.python.types.Tuple) args.get(0)).value
)
);
} else if (args.get(0) instanceof org.python.Iterable) {
throw new org.python.exceptions.NotImplementedError("Builtin function 'list' with iterator argument not implemented");
// org.python.types.List out = new org.python.types.List();
// return out;
} else {
throw new org.python.exceptions.TypeError("'" + org.python.types.Type.pythonType(args.get(0).getClass()) + "' object is not iterable");
}
} else if (args.size() == 0) {
return new org.python.types.List();
} else {
throw new org.python.exceptions.TypeError("list() takes at most 1 argument (" + args.size() + " given)");
}
}

@org.python.Method(
Expand Down Expand Up @@ -968,7 +995,7 @@ public static org.python.types.Str oct(
if (args.size() != 1) {
throw new org.python.exceptions.TypeError("oct() takes exactly one argument (" + args.size() + " given)");
}
if (kwargs.size() != 0) {
if (kwargs != null && kwargs.size() != 0) {
throw new org.python.exceptions.TypeError("oct() takes no keyword arguments");
}
return new org.python.types.Str(String.format("0o%o", int_cast(args, kwargs).value));
Expand Down Expand Up @@ -1114,7 +1141,7 @@ public static org.python.types.Int ord(
if (args.size() != 1) {
throw new org.python.exceptions.TypeError("org() takes exactly one argument (" + args.size() + " given)");
}
if (kwargs.size() != 0) {
if (kwargs != null && kwargs.size() != 0) {
throw new org.python.exceptions.TypeError("ord() takes no keyword arguments");
}

Expand Down Expand Up @@ -1145,7 +1172,7 @@ public static org.python.Object pow(
if (args.size() > 3) {
throw new org.python.exceptions.TypeError("pow() expected at most 3 arguments, got " + args.size());
}
if (kwargs.size() != 0) {
if (kwargs != null && kwargs.size() != 0) {
throw new org.python.exceptions.TypeError("pow() takes no keyword arguments");
}

Expand Down Expand Up @@ -1267,7 +1294,7 @@ public static org.python.types.Str repr(
if (args.size() != 1) {
throw new org.python.exceptions.TypeError("repr() takes exactly one argument (" + args.size() + " given)");
}
if (kwargs.size() != 0) {
if (kwargs != null && kwargs.size() != 0) {
throw new org.python.exceptions.TypeError("repr() takes no keyword arguments");
}
return args.get(0).__repr__();
Expand All @@ -1284,7 +1311,7 @@ public static org.python.Iterable reversed(
if (args.size() != 1) {
throw new org.python.exceptions.TypeError("reversed() takes exactly one argument (" + args.size() + " given)");
}
if (kwargs.size() != 0) {
if (kwargs != null && kwargs.size() != 0) {
throw new org.python.exceptions.TypeError("reversed() takes no keyword arguments");
}
return args.get(0).__reversed__();
Expand All @@ -1300,7 +1327,7 @@ public static org.python.Iterable reversed(
public static org.python.Object round(
java.util.List<org.python.Object> args,
java.util.Map<java.lang.String, org.python.Object> kwargs) {
if (kwargs.size() != 0) {
if (kwargs != null && kwargs.size() != 0) {
throw new org.python.exceptions.TypeError("round() takes no keyword arguments");
}
if (args.size() == 0) {
Expand All @@ -1323,7 +1350,7 @@ public static org.python.Object round(
public static org.python.types.Set set(
java.util.List<org.python.Object> args,
java.util.Map<java.lang.String, org.python.Object> kwargs) {
if (kwargs.size() != 0) {
if (kwargs != null && kwargs.size() != 0) {
throw new org.python.exceptions.TypeError("set() does not take keyword arguments");
}
if (args.size() == 0) {
Expand Down Expand Up @@ -1411,7 +1438,7 @@ public static org.python.types.Str str(
if (args.size() != 1) {
throw new org.python.exceptions.TypeError("len() takes exactly one argument (" + args.size() + " given)");
}
if (kwargs.size() != 0) {
if (kwargs != null && kwargs.size() != 0) {
throw new org.python.exceptions.TypeError("len() takes no keyword arguments");
}
return args.get(0).__str__();
Expand Down Expand Up @@ -1471,7 +1498,7 @@ public static org.python.types.Tuple tuple(
public static org.python.types.Type type(
java.util.List<org.python.Object> args,
java.util.Map<java.lang.String, org.python.Object> kwargs) {
if (kwargs.size() != 0) {
if (kwargs != null && kwargs.size() != 0) {
throw new org.python.exceptions.TypeError("type() takes 1 or 3 arguments");
}
if (args.size() == 1) {
Expand Down
37 changes: 37 additions & 0 deletions python/common/org/python/ImportLib.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,41 @@ public static org.python.types.Module getModule(java.lang.String java_name) {
return modules.get(java_name);
}

/**
* Develop a map of exported symbols for a module.
*
* Implements the semantics of "from X import *". Honors the "__all__"
* attribute if it exists; otherwise, exports all symbols that don't start
* with an underscore (i.e., all public symbols).
*
* @param module_instance the *instance* of the module to interrogate.
*
* @return A map of attribute names to Python Objects, representing the
* symbols to be exported as part of an "import *" from this
* module.
*/
public static java.util.Map importAll(org.python.types.Module module_instance) {
java.util.Map<java.lang.String, org.python.Object> exports = new java.util.HashMap<java.lang.String, org.python.Object>();
org.python.types.Type module = org.python.types.Type.pythonType(module_instance.getClass());

org.python.Object all_obj = module.attrs.get("__all__");
if (all_obj == null) {
for (java.lang.String name: module.attrs.keySet()) {
if (!name.startsWith("_")) {
exports.put(name, module.attrs.get(name));
}
}
} else {
java.util.List args = new java.util.ArrayList();
args.add(all_obj);
org.python.types.List all = org.Python.list(args, null);

for (org.python.Object name: all.value) {
exports.put(name.toString(), module.attrs.get(name.toString()));
}
}
// System.out.println("exports" + exports);

return exports;
}
}
1 change: 0 additions & 1 deletion python/common/org/python/types/Module.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public org.python.Object __getattribute__(java.lang.String name) {
if (value == null) {
throw new org.python.exceptions.AttributeError(this, name);
}

} catch (org.python.exceptions.AttributeError e) {
// System.out.println("MODULE NO ATTRIBUTE");
value = org.Python.builtins.get(name);
Expand Down
6 changes: 2 additions & 4 deletions tests/structures/test_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ def some_method():
"""
})

@expectedFailure
def test_import_star(self):
self.assertCodeExecution(
"""
Expand Down Expand Up @@ -209,9 +208,8 @@ def third_method():
print("Now we're calling a third module method")
"""
})
}, run_in_function=False)

@expectedFailure
def test_import_star_with_all(self):
self.assertCodeExecution(
"""
Expand Down Expand Up @@ -248,4 +246,4 @@ def third_method():
print("This shouldn't be called")
"""
})
}, run_in_function=False)
3 changes: 3 additions & 0 deletions voc/python/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ def module(self):
def store_name(self, name, use_locals):
raise NotImplementedError('Abstract class `block` cannot be used directly.')

def store_dynamic(self):
raise NotImplementedError('Abstract class `block` cannot be used directly.')

def load_name(self, name, use_locals):
raise NotImplementedError('Abstract class `block` cannot be used directly.')

Expand Down
13 changes: 13 additions & 0 deletions voc/python/klass.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ def store_name(self, name, use_locals):
)
free_name(self, '#value')

def store_dynamic(self):
self.add_opcodes(
ASTORE_name(self, '#value'),
JavaOpcodes.LDC_W(self.klass.descriptor),
JavaOpcodes.INVOKESTATIC('org/python/types/Type', 'pythonType', '(Ljava/lang/String;)Lorg/python/types/Type;'),

JavaOpcodes.GETFIELD('org/python/types/Type', 'attrs', 'Ljava/util/Map;'),
ALOAD_name(self, '#value'),

JavaOpcodes.INVOKEINTERFACE('java/util/Map', 'putAll', '(Ljava/util/Map;)V'),
)
free_name(self, '#value')

def load_name(self, name, use_locals):
self.add_opcodes(
JavaOpcodes.LDC_W(self.klass.descriptor),
Expand Down
Loading

0 comments on commit 0e71f1b

Please sign in to comment.