From 427902480aa8ca8f74e32d084a4b9ea31944a176 Mon Sep 17 00:00:00 2001 From: Joran van Apeldoorn Date: Thu, 16 May 2019 15:52:52 +0200 Subject: [PATCH 1/5] Added check for android.jar to android build --- build-android.xml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/build-android.xml b/build-android.xml index 027f2dad6c..e3f4c452fb 100644 --- a/build-android.xml +++ b/build-android.xml @@ -17,6 +17,14 @@ + + + + + + + + Date: Thu, 16 May 2019 17:02:34 +0200 Subject: [PATCH 2/5] Added __format__ and fstring support --- python/common/org/Python.java | 2 +- python/common/org/python/Object.java | 2 +- python/common/org/python/types/Bool.java | 7 - python/common/org/python/types/ByteArray.java | 6 - python/common/org/python/types/Bytes.java | 6 - python/common/org/python/types/Complex.java | 72 +++++----- python/common/org/python/types/Dict.java | 7 - python/common/org/python/types/DictKeys.java | 6 - .../common/org/python/types/DictValues.java | 7 - python/common/org/python/types/Float.java | 123 +++++++++++------- python/common/org/python/types/Int.java | 22 +++- python/common/org/python/types/List.java | 7 - python/common/org/python/types/Object.java | 14 +- python/common/org/python/types/Set.java | 6 - python/common/org/python/types/Str.java | 12 +- python/common/org/python/types/Super.java | 10 +- python/common/org/python/types/Tuple.java | 6 - voc/python/ast.py | 96 +++++++++++++- 18 files changed, 250 insertions(+), 161 deletions(-) diff --git a/python/common/org/Python.java b/python/common/org/Python.java index a693f7e5b5..f08b6397f1 100644 --- a/python/common/org/Python.java +++ b/python/common/org/Python.java @@ -786,7 +786,7 @@ public static org.python.Object filter(org.python.Object function, org.python.Ob ) public static org.python.types.Str format(org.python.Object value, org.python.Object format_spec) { if (format_spec == null) { - return (org.python.types.Str) value.__str__(); + return (org.python.types.Str) value.__format__(new org.python.types.Str("")); } else { return (org.python.types.Str) value.__format__(format_spec); } diff --git a/python/common/org/python/Object.java b/python/common/org/python/Object.java index 9c840d5bf7..20089ca381 100644 --- a/python/common/org/python/Object.java +++ b/python/common/org/python/Object.java @@ -48,7 +48,7 @@ public interface Object extends Comparable { public org.python.Object __str__(); public org.python.Object __bytes__(); - public org.python.Object __format__(org.python.Object format); + public org.python.Object __format__(org.python.Object format_spec); public org.python.Object __lt__(org.python.Object other); public org.python.Object __le__(org.python.Object other); diff --git a/python/common/org/python/types/Bool.java b/python/common/org/python/types/Bool.java index fb31ec0287..876a2d295c 100644 --- a/python/common/org/python/types/Bool.java +++ b/python/common/org/python/types/Bool.java @@ -68,13 +68,6 @@ public org.python.types.Str __repr__() { } } - @org.python.Method( - __doc__ = "" - ) - public org.python.types.Str __format__(org.python.Object format_string) { - throw new org.python.exceptions.NotImplementedError("'bool'.__format__ has not been implemented."); - } - @org.python.Method( __doc__ = "", args = {"index", "value"} diff --git a/python/common/org/python/types/ByteArray.java b/python/common/org/python/types/ByteArray.java index b90023ea3c..516d5da03f 100644 --- a/python/common/org/python/types/ByteArray.java +++ b/python/common/org/python/types/ByteArray.java @@ -587,12 +587,6 @@ public org.python.Object __contains__(org.python.Object slice) { return new Bytes(this.value).__contains__(slice); } - @org.python.Method( - __doc__ = "default object formatter" - ) - public org.python.types.Str __format__(java.util.List args, java.util.Map kwargs, java.util.List default_args, java.util.Map default_kwargs) { - throw new org.python.exceptions.NotImplementedError("bytearray.__format__ has not been implemented."); - } @org.python.Method( __doc__ = "" diff --git a/python/common/org/python/types/Bytes.java b/python/common/org/python/types/Bytes.java index e2e3722067..3ef5644b33 100644 --- a/python/common/org/python/types/Bytes.java +++ b/python/common/org/python/types/Bytes.java @@ -251,12 +251,6 @@ public org.python.Object __contains__(org.python.Object slice) { return org.python.types.Bool.FALSE; } - @org.python.Method( - __doc__ = "default object formatter" - ) - public org.python.types.Str __format__(java.util.List args, java.util.Map kwargs, java.util.List default_args, java.util.Map default_kwargs) { - throw new org.python.exceptions.NotImplementedError("bytes.__format__ has not been implemented."); - } @org.python.Method( __doc__ = "Return self>=value.", diff --git a/python/common/org/python/types/Complex.java b/python/common/org/python/types/Complex.java index afda805bad..2680de77a9 100644 --- a/python/common/org/python/types/Complex.java +++ b/python/common/org/python/types/Complex.java @@ -161,39 +161,49 @@ private String partToStr(org.python.types.Float x) { __doc__ = "Return repr(self)." ) public org.python.Object __repr__() { - java.lang.StringBuilder buffer = new java.lang.StringBuilder(); - boolean real_present = true; - if (this.real.value != 0) { - buffer.append("("); - if (((org.python.types.Bool) ((this.real).__int__().__eq__(this.real))).value) { - buffer.append(((org.python.types.Str) this.real.__int__().__repr__()).value); - } else { - buffer.append(((org.python.types.Str) this.real.__repr__()).value); - } + + if (this.real.value != 0.0 || this.real.isNegativeZero()) { + return new org.python.types.Str("(" + partToStr(this.real) + ((this.imag.value >= 0.0 && !this.imag.isNegativeZero()) ? "+" : "-") + partToStr(new org.python.types.Float(Math.abs(this.imag.value))) + "j)"); } else { - real_present = false; + return new org.python.types.Str(partToStr(this.imag) + "j"); } - if (this.real.value != 0 && this.imag.value >= 0) { + + } + + + @org.python.Method( + __doc__ = "complex.__format__() -> str\n\nConvert to a string according to format_spec.", + args={"format_spec"} + ) + public org.python.Object __format__(org.python.Object format_spec) { + String fs = ((org.python.types.Str) format_spec).value; + + + if(!(fs.endsWith("f") + ||fs.endsWith("F") + ||fs.endsWith("g") + ||fs.endsWith("G") + ||fs.endsWith("e") + ||fs.endsWith("E") + ||fs.endsWith("n") + )){ + //format specs are ignored by python for complex numbers if no floatingpoit spec is present + return this.__repr__(); + } + // use the floating point spec + + java.lang.StringBuilder buffer = new java.lang.StringBuilder(); + + buffer.append(((org.python.types.Str) this.real.__format__(format_spec)).value); + + if (this.imag.value >= 0) { buffer.append("+"); } - if (((org.python.types.Bool) ((this.imag).__int__().__eq__(this.imag))).value) { - buffer.append(((org.python.types.Str) (this.imag).__int__().__repr__()).value); - } else { - buffer.append(((org.python.types.Str) (this.imag).__repr__()).value); - } + buffer.append(((org.python.types.Str) (this.imag).__format__(format_spec)).value); buffer.append("j"); - if (real_present) { - buffer.append(")"); - } return new org.python.types.Str(buffer.toString()); - } - @org.python.Method( - __doc__ = "complex.__format__() -> str\n\nConvert to a string according to format_spec." - ) - public org.python.Object __format__(org.python.Object format_string) { - throw new org.python.exceptions.NotImplementedError("complex.__format__ has not been implemented."); - } + } @org.python.Method( __doc__ = "Return self= 0.0 && !this.imag.isNegativeZero()) ? "+" : "-") + partToStr(new org.python.types.Float(Math.abs(this.imag.value))) + "j)"); - } else { - return new org.python.types.Str(partToStr(this.imag) + "j"); - } - } @org.python.Method( __doc__ = "Return self*value.", diff --git a/python/common/org/python/types/Dict.java b/python/common/org/python/types/Dict.java index d82ba1d244..0b2037bd73 100644 --- a/python/common/org/python/types/Dict.java +++ b/python/common/org/python/types/Dict.java @@ -135,13 +135,6 @@ public org.python.types.Str __repr__() { return new org.python.types.Str(buffer.toString()); } - @org.python.Method( - __doc__ = "default object formatter" - ) - public org.python.types.Str __format__(org.python.Object other) { - throw new org.python.exceptions.NotImplementedError("dict.__format__() has not been implemented."); - } - @org.python.Method( __doc__ = "" ) diff --git a/python/common/org/python/types/DictKeys.java b/python/common/org/python/types/DictKeys.java index 08c16200b3..8c41c3e491 100644 --- a/python/common/org/python/types/DictKeys.java +++ b/python/common/org/python/types/DictKeys.java @@ -56,12 +56,6 @@ public org.python.types.Str __repr__() { return new org.python.types.Str(buffer.toString()); } - @org.python.Method( - __doc__ = "default object formatter" - ) - public org.python.types.Str __format__(org.python.Object other) { - throw new org.python.exceptions.NotImplementedError(this.typeName() + ".__format__() has not been implemented."); - } @org.python.Method( __doc__ = "__dir__() -> list\ndefault dir() implementation" diff --git a/python/common/org/python/types/DictValues.java b/python/common/org/python/types/DictValues.java index 86c4da18a9..05cfa681d2 100644 --- a/python/common/org/python/types/DictValues.java +++ b/python/common/org/python/types/DictValues.java @@ -47,13 +47,6 @@ public org.python.types.Str __repr__() { return new org.python.types.Str(buffer.toString()); } - @org.python.Method( - __doc__ = "default object formatter" - ) - public org.python.types.Str __format__(org.python.Object other) { - throw new org.python.exceptions.NotImplementedError(this.typeName() + ".__format__() has not been implemented."); - } - @org.python.Method( __doc__ = "__dir__() -> list\ndefault dir() implementation" ) diff --git a/python/common/org/python/types/Float.java b/python/common/org/python/types/Float.java index 9f7ee8b3a7..fe2c9cd556 100644 --- a/python/common/org/python/types/Float.java +++ b/python/common/org/python/types/Float.java @@ -59,6 +59,41 @@ public Float(org.python.Object[] args, java.util.Map Date: Thu, 16 May 2019 17:12:42 +0200 Subject: [PATCH 3/5] coding style --- voc/python/ast.py | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/voc/python/ast.py b/voc/python/ast.py index 25f61dc43b..3386a29fe4 100644 --- a/voc/python/ast.py +++ b/voc/python/ast.py @@ -99,7 +99,7 @@ def visit_Index(self, node): class LocalsVisitor(ast.NodeVisitor): def __init__(self, context): self.context = context - + def visit_Name(self, node): if type(node.ctx) == ast.Store: if node.id not in self.context.local_vars: @@ -189,18 +189,15 @@ def convert_to_Name(_node): return index += 1 - - def generic_visit(self,node): + def generic_visit(self, node): # Catches node types that are not implemented # Expr calls this function directly and is meant to do so # Other node types mean that we are skipping something that we should not if type(node) != _ast.Expr: raise TypeError("Not translating node of type "+str(type(node))) super().generic_visit(node) - - - def visit(self, node): + def visit(self, node): try: self.parse_yield(node) if id(node) in self.resolved_yield_expression.keys(): @@ -1808,7 +1805,7 @@ def visit_Yield(self, node): 'Lorg/python/Object;' ) ) - + yield_point = len(self.context.yield_points) + 1 # Save the current stack and yield index @@ -2267,8 +2264,6 @@ def visit_Num(self, node): def visit_Str(self, node): self.context.add_str(node.s) - - @node_visitor def visit_Bytes(self, node): self.context.add_opcodes( @@ -2488,7 +2483,6 @@ def visit_ExtSlice(self, node): def visit_JoinedStr(self, node): # Joined strs are new in python 3.6+ and are used in fstrings. # The easyiest way to handle them seems to be to transform them to "".join(*values) - transformed_node = _ast.Call( lineno=node.lineno, col_offset=node.col_offset, @@ -2509,7 +2503,7 @@ def visit_JoinedStr(self, node): ], keywords=[], ) - # pass on the handeling + # pass on the handling self.visit(transformed_node) @node_visitor @@ -2548,7 +2542,7 @@ def visit_FormattedValue(self, node): else: raise ValueError("Conversion type %i not implemented for f-strings" % node.conversion) - formatspec = node.format_spec or _ast.Str(lineno=node.lineno, col_offset=node.col_offset, s="") + formatspec = node.format_spec or _ast.Str(lineno=node.lineno, col_offset=node.col_offset, s="") transformed_node = _ast.Call(lineno=node.lineno, col_offset=node.col_offset, func=_ast.Attribute( @@ -2560,10 +2554,9 @@ def visit_FormattedValue(self, node): ), args=[formatspec], keywords=[], - ) + ) self.visit(transformed_node) - @node_visitor def visit_Index(self, node): self.visit(node.value) From 40c0bdc40c67e4220344b3e89cf915f7952115dc Mon Sep 17 00:00:00 2001 From: Joran van Apeldoorn Date: Fri, 17 May 2019 13:16:12 +0200 Subject: [PATCH 4/5] removed unused import --- voc/python/ast.py | 1 - 1 file changed, 1 deletion(-) diff --git a/voc/python/ast.py b/voc/python/ast.py index 3386a29fe4..fd5a87e27d 100644 --- a/voc/python/ast.py +++ b/voc/python/ast.py @@ -2,7 +2,6 @@ import _ast import copy import sys -import traceback from ..java import opcodes as JavaOpcodes from .modules import Module From 0f76b6743c5c4539c4fcc5fee26a106d8054a5bb Mon Sep 17 00:00:00 2001 From: Joran van Apeldoorn Date: Sat, 18 May 2019 14:17:33 +0200 Subject: [PATCH 5/5] removed unimplemented for tests with complex repr in it --- tests/builtins/test_repr.py | 1 - tests/builtins/test_slice.py | 1 - tests/datatypes/test_dict.py | 1 - 3 files changed, 3 deletions(-) diff --git a/tests/builtins/test_repr.py b/tests/builtins/test_repr.py index 9670d923bf..05bd924d01 100644 --- a/tests/builtins/test_repr.py +++ b/tests/builtins/test_repr.py @@ -10,5 +10,4 @@ class BuiltinReprFunctionTests(BuiltinFunctionTestCase, TranspileTestCase): not_implemented = [ 'test_class', - 'test_complex', ] diff --git a/tests/builtins/test_slice.py b/tests/builtins/test_slice.py index cdee0295b0..dab153c4ca 100644 --- a/tests/builtins/test_slice.py +++ b/tests/builtins/test_slice.py @@ -26,5 +26,4 @@ class BuiltinSliceFunctionTests(BuiltinFunctionTestCase, TranspileTestCase): not_implemented = [ 'test_class', - 'test_complex', ] diff --git a/tests/datatypes/test_dict.py b/tests/datatypes/test_dict.py index e4cb60318a..7213d3673f 100644 --- a/tests/datatypes/test_dict.py +++ b/tests/datatypes/test_dict.py @@ -490,7 +490,6 @@ class BinaryDictOperationTests(BinaryOperationTestCase, TranspileTestCase): not_implemented = [ 'test_subscr_bytearray', 'test_subscr_class', - 'test_subscr_complex', ]