Skip to content

Commit

Permalink
Use Tuple and Dict for args/kwargs instead of Java containers.
Browse files Browse the repository at this point in the history
  • Loading branch information
katharosada committed Aug 16, 2016
1 parent 878d02b commit e42befd
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 14 deletions.
26 changes: 23 additions & 3 deletions docs/internals/signatures.rst
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ but accept as many as you can get. For instance, the ``min`` function.
args = {"first", "others"},
varargs = "others"
)
public function org.python.Object min(org.python.Object first, org.python.Object [] others) {
public function org.python.Object min(org.python.Object first, org.python.types.Tuple others) {
org.python.Object val = first;
for (other: others) {
if (other.__lt__(val)) {
Expand All @@ -149,5 +149,25 @@ but accept as many as you can get. For instance, the ``min`` function.
A function accepting keyword arguments
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Of course, sometimes you want to use keyword arguments. Unfortunately, that is
currently not used. Be the first to contribute an example!
.. code-block:: python
def loop_kwargs(**kwargs):
"""Loop over the kwargs to this function."""
for k in kwargs:
pass
.. code-block:: java
@org.python.Method(
__doc__ = "Loop over the kwargs to this function.",
args = {},
kwargs = "kwargs"
)
public function org.python.Object loop_kwargs(org.python.types.Dict kwargs) {
Map<org.python.Object, org.python.Object> kwargValues = kwargs.value;
for(org.python.Object key : kwargValues.keySet()) {
// The keys will always be python Str objects
org.python.types.Str keyStr = (org.python.types.Str) key;
}
return org.python.types.NoneType.NONE;
}
12 changes: 7 additions & 5 deletions python/common/org/Python.java
Original file line number Diff line number Diff line change
Expand Up @@ -1014,7 +1014,7 @@ public static org.python.types.Dict locals() {
args={"func"},
varargs="iterable"
)
public static org.python.Object map(org.python.Object func, org.python.Object [] iterables) {
public static org.python.Object map(org.python.Object func, org.python.types.Tuple iterables) {
return new org.python.types.Map(func, iterables);
}

Expand Down Expand Up @@ -1297,17 +1297,19 @@ public static org.python.Object pow(org.python.Object x, org.python.Object y, or
varargs="value",
kwonlyargs={"file", "sep", "end", "flush"}
)
public static void print(org.python.Object [] value, org.python.Object file, org.python.Object sep, org.python.Object end, org.python.Object flush) {
public static void print(org.python.types.Tuple value, org.python.Object file, org.python.Object sep, org.python.Object end, org.python.Object flush) {
if (file == null) {
// file = System.out;
}

java.util.List<org.python.Object> valueArgs = value.value;

StringBuilder buffer = new StringBuilder();

for (int i = 0; i < value.length; i++) {
buffer.append(value[i].__str__());
for (int i = 0; i < valueArgs.size(); i++) {
buffer.append(valueArgs.get(i).__str__());

if (i != value.length - 1) {
if (i != valueArgs.size() - 1) {
if (sep == null) {
buffer.append(" ");
} else {
Expand Down
14 changes: 13 additions & 1 deletion python/common/org/python/types/Function.java
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,19 @@ public org.python.Object __get__(org.python.Object instance, org.python.Object k
}

if ((flags & CO_VARARGS) != 0) {
adjusted[pos_count] = java.util.Arrays.copyOfRange(args, pos_count, args.length);
// Construct Python tuple object
org.python.types.Tuple tuple = new org.python.types.Tuple(
java.util.Arrays.asList(java.util.Arrays.copyOfRange(args, pos_count, args.length)));

adjusted[pos_count] = tuple;
}
// Add remaining kwargs to kwargs argument if we have one.
if ((flags & CO_VARKEYWORDS) != 0) {
org.python.types.Dict kwargDict = new org.python.types.Dict();
for (java.util.Map.Entry<String, org.python.Object> entry : kwargs.entrySet()) {
kwargDict.__setitem__(new org.python.types.Str(entry.getKey()), entry.getValue());
}
adjusted[adjusted.length - 1] = kwargDict;
}

return adjusted;
Expand Down
8 changes: 4 additions & 4 deletions python/common/org/python/types/Map.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ public class Map extends org.python.types.Object implements org.python.Iterable
private org.python.Object callable;
private org.python.Iterable[] iterators;

public Map(org.python.Object callable, org.python.Object [] iterables) {
public Map(org.python.Object callable, org.python.types.Tuple iterables) {
this.callable = callable;
this.iterators = new org.python.Iterable[iterables.length];
for (int i = 0; i < iterables.length; i++) {
this.iterators[i] = org.Python.iter(iterables[i]);
this.iterators = new org.python.Iterable[iterables.value.size()];
for (int i = 0; i < iterables.value.size(); i++) {
this.iterators[i] = org.Python.iter(iterables.value.get(i));
}
}

Expand Down
22 changes: 21 additions & 1 deletion tests/structures/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,26 @@ def myfunc(x, y=1, z=2):
print('Done.')
""", run_in_function=False)

def test_call_function_with_var_args(self):
self.assertCodeExecution("""
def myfunc(*args):
print(args)
print('Done.')
""", run_in_function=False)

def test_call_function_with_kw(self):
self.assertCodeExecution("""
def myfunc(**kwargs):
print(kwargs['first'] * 3)
print(kwargs['second'] * 3)
return kwargs['first'] + kwargs['second']
print("values sum =", myfunc(first=1, second=2))
print('Done.')
""", run_in_function=False)

@expectedFailure
def test_call_function_kw(self):
self.assertCodeExecution("""
Expand Down Expand Up @@ -180,4 +200,4 @@ def myfunc(*args):
values_tuple = (1, 2, 3, 4)
print("values count =", myfunc(*values_tuple))
print('Done.')
""", run_in_function=False)
""", run_in_function=False)

0 comments on commit e42befd

Please sign in to comment.