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 #256 from freakboy3742/master
Browse files Browse the repository at this point in the history
Complete implementation of function invocation and print()
  • Loading branch information
freakboy3742 authored Sep 26, 2016
2 parents 72058a3 + 2bbe6c3 commit 76890d9
Show file tree
Hide file tree
Showing 24 changed files with 476 additions and 81 deletions.
3 changes: 2 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.. image:: http://pybee.org/project/projects/bridges/voc/voc-72.png
.. image:: http://pybee.org/project/projects/bridges/voc/voc.png
:height: 72px
:target: https://pybee.org/voc

VOC
Expand Down
28 changes: 24 additions & 4 deletions python/android/python/platform/AndroidPlatform.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,37 @@


public class AndroidPlatform implements python.Platform {
public AndroidPlatform() {}
private org.python.stdlib._io.TextIOWrapper _stderr;
private org.python.stdlib._io.TextIOWrapper _stdout;
private org.python.stdlib._io.TextIOWrapper _stdin;

public AndroidPlatform() {
_stderr = new org.python.stdlib._io.TextIOWrapper(new LogStream(android.util.Log.ERROR, "Python", 1024));
_stdout = new org.python.stdlib._io.TextIOWrapper(new LogStream(android.util.Log.INFO, "Python", 1024));
// _stdin = new org.python.stdlib._io.TextIOWrapper(System.in);
}

public long clock() {
return Debug.threadCpuTimeNanos();
}

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

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

public org.python.Object stderr() {
return _stderr;
}

public org.python.Object stdout() {
return _stdout;
}

public org.python.Object stdin() {
return _stdin;
}
}
}
30 changes: 30 additions & 0 deletions python/android/python/platform/LogStream.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package python.platform;


public class LogStream extends java.io.OutputStream {
protected java.lang.String tag;
protected byte[] buf;
protected int size;
protected int count;
protected int priority;

public LogStream(int priority, java.lang.String tag, int size) {
this.tag = tag;
this.priority = priority;
this.size = size;

buf = new byte[size];
}

public void write(int b) {
if (b != '\n') {
buf[count] = (byte) b;
count++;
}

if (b == '\n' || count == size) {
android.util.Log.println(priority, tag, new java.lang.String(buf, 0, count));
count = 0;
}
}
}
85 changes: 78 additions & 7 deletions python/common/org/Python.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,63 @@ public static java.lang.String typeName(java.lang.Class cls) {
return name;
}

/**
* Add the contents of a list to the varargs to be used in a function call.
*/
public static org.python.Object [] addToArgs(org.python.Object [] args, org.python.Object varargs) {
java.util.List<org.python.Object> temp_list = new java.util.ArrayList<org.python.Object>();
try {
org.python.Iterable iter = varargs.__iter__();
while (true) {
org.python.Object item = iter.__next__();
temp_list.add(item);
}
} catch (org.python.exceptions.StopIteration e) {}

java.lang.Object [] va_list = temp_list.toArray();
org.python.Object [] arg_list = new org.python.Object [args.length + va_list.length];

System.arraycopy(args, 0, arg_list, 0, args.length);
System.arraycopy(va_list, 0, arg_list, args.length, va_list.length);

return arg_list;
}

/**
* Add the contents of a dictionary to the keyword arguments to be used
* in a function call.
*
* Returns the updated kwargs Map.
*
* If any of the keys in the varkwargs dictionary aren't strings,
* a TypeError is raised.
*/
public static java.util.Map<java.lang.String, org.python.Object> addToKwargs(java.util.Map<java.lang.String, org.python.Object> kwargs, org.python.Object kwvarargs, java.lang.String func_name) {
// FIXME: Once we have dictionary iterators, we should use an iteration-based
// rollout, rather than casting to Dict.
// try {
// org.python.Iterable iter = varkwargs.__iter__();
// while (true) {
// org.python.Object key = iter.__next__()
// java.lang.String key_string = ((org.python.types.Str) key).value;
// org.python.Object key
// kwargs.put(key_string, value);
// }
// } catch (ClassCastException e) {
// throw new org.python.exceptions.TypeError(func_name + "() keywords must be strings");
// } catch (org.python.exceptions.StopIteration e) {}

for (java.util.Map.Entry<org.python.Object, org.python.Object> entry : ((org.python.types.Dict) kwvarargs).value.entrySet()) {
try {
kwargs.put(((org.python.types.Str) entry.getKey()).value, entry.getValue());
} catch (ClassCastException e) {
throw new org.python.exceptions.TypeError(func_name + "() keywords must be strings");
}
}
return kwargs;
}


@org.python.Method(
__doc__ = "__import__(name, globals=None, locals=None, fromlist=(), level=0) -> module" +
"\n" +
Expand Down Expand Up @@ -1331,12 +1388,7 @@ public static org.python.Object pow(org.python.Object x, org.python.Object y, or
kwonlyargs={"file", "sep", "end", "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 < valueArgs.size(); i++) {
Expand All @@ -1355,8 +1407,27 @@ public static void print(org.python.types.Tuple value, org.python.Object file, o
} else {
buffer.append(end);
}
System.out.print(buffer.toString());
// file.write(buffer.toString());

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

org.python.Object content = new org.python.types.Str(buffer.toString());
org.python.Object write_method = file.__getattribute__("write");
try {
((org.python.Callable) write_method).invoke(new org.python.Object [] { content }, null);
} catch (java.lang.ClassCastException e) {
throw new org.python.exceptions.TypeError("'" + write_method.typeName() + "' object is not callable");
}

if (flush != null && ((org.python.types.Bool) flush.__bool__()).value) {
org.python.Object flush_method = file.__getattribute__("flush");
try {
((org.python.Callable) flush_method).invoke(null, null);
} catch (java.lang.ClassCastException e) {
throw new org.python.exceptions.TypeError("'" + flush_method.typeName() + "' object is not callable");
}
}
}

@org.python.Method(
Expand Down
112 changes: 112 additions & 0 deletions python/common/org/python/stdlib/_io/TextIOWrapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package org.python.stdlib._io;

public class TextIOWrapper extends org.python.types.Object {
public org.python.Object __VOC__;

java.io.InputStream input;
java.io.OutputStream output;

public TextIOWrapper(java.io.InputStream istream) {
super(org.python.types.Type.Origin.BUILTIN, null);
this.input = istream;
}

public TextIOWrapper(java.io.OutputStream ostream) {
super(org.python.types.Type.Origin.BUILTIN, null);
this.output = ostream;
}

// @org.python.Method(
// __doc__ = "Create and return a new object. See help(type) for accurate signature."
// )
// public org.python.Object __new__(org.python.Object klass) {
// org.python.types.Type cls = (org.python.types.Type) super.__new__(klass);
// System.out.println("__NEW__ TextIOWrapper");
// return cls;
// }

// '_CHUNK_SIZE',
// '__class__',
// '__del__',
// '__delattr__',
// '__dict__',
// '__dir__',
// '__doc__',
// '__enter__',
// '__eq__',
// '__exit__',
// '__format__',
// '__ge__',
// '__getattribute__',
// '__getstate__',
// '__gt__',
// '__hash__',
// '__init__',
// '__iter__',
// '__le__',
// '__lt__',
// '__ne__',
// '__new__',
// '__next__',
// '__reduce__',
// '__reduce_ex__',
// '__repr__',
// '__setattr__',
// '__sizeof__',
// '__str__',
// '__subclasshook__',
// '_checkClosed',
// '_checkReadable',
// '_checkSeekable',
// '_checkWritable',
// '_finalizing',
// 'buffer',
// 'close',
// 'closed',
// 'detach',
// 'encoding',
// 'errors',
// 'fileno',

@org.python.Method(
__doc__ = ""
)
public org.python.Object flush() {
try {
output.flush();
} catch (java.io.IOException e) {
throw new org.python.exceptions.RuntimeError("Unable to flush output: " + e.toString());
}
return org.python.types.NoneType.NONE;
}

// 'isatty',
// 'line_buffering',
// 'name',
// 'newlines',
// 'read',
// 'readable',
// 'readline',
// 'readlines',
// 'seek',
// 'seekable',
// 'tell',
// 'truncate',
// 'writable',

@org.python.Method(
__doc__ = "",
args = {"content"}
)
public org.python.Object write(org.python.Object content) {
try {
output.write(content.toString().getBytes("UTF-8"));
} catch (java.io.IOException e) {
throw new org.python.exceptions.RuntimeError("Unable to write output: " + e.toString());
}
return org.python.types.NoneType.NONE;
}

// 'writelines'

};
2 changes: 1 addition & 1 deletion python/common/org/python/types/ByteArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ public org.python.Object __mod__(org.python.Object other) {
if (other instanceof org.python.types.List || other instanceof org.python.types.Range || other instanceof org.python.types.Dict) {
int i;
for (i=0; i < this.value.length; i++) {
if (this.value[0] == 0) break;
if (this.value[0] == 0) break;
}
byte[] bytes = new byte[i];
System.arraycopy(this.value, 0, bytes, 0, i);
Expand Down
1 change: 0 additions & 1 deletion python/common/org/python/types/Closure.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.python.types;

public class Closure extends org.python.types.Object {

public java.util.List<org.python.Object> default_args;
public java.util.Map<java.lang.String, org.python.Object> default_kwargs;

Expand Down
1 change: 0 additions & 1 deletion python/common/org/python/types/Complex.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.python.types;

public class Complex extends org.python.types.Object {

public org.python.types.Float real;
public org.python.types.Float imag;

Expand Down
1 change: 1 addition & 0 deletions python/common/org/python/types/Dict.java
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ public void __delitem__(org.python.Object item) {
__doc__ = ""
)
public org.python.Iterable __iter__() {
// FIXME: Once this is implemented, update org.Python.addToKwargs()
throw new org.python.exceptions.NotImplementedError("dict.__iter__() has not been implemented.");
}

Expand Down
Loading

0 comments on commit 76890d9

Please sign in to comment.