Skip to content

Commit 6948ea6

Browse files
committed
Fix complex __repr__ formatting
1 parent 1e4421f commit 6948ea6

File tree

3 files changed

+26
-18
lines changed

3 files changed

+26
-18
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/complex/ComplexBuiltins.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -723,11 +723,11 @@ PNotImplemented doGeneric(Object left, Object right) {
723723
@Builtin(name = __REPR__, minNumOfPositionalArgs = 1)
724724
abstract static class ReprNode extends PythonUnaryBuiltinNode {
725725
@Specialization
726-
@TruffleBoundary
727726
String repr(PComplex self) {
728727
return repr(self, getCore());
729728
}
730729

730+
@TruffleBoundary
731731
private static String repr(PComplex self, PythonCore core) {
732732
ComplexFormatter formatter = new ComplexFormatter(core, new Spec(-1, Spec.NONE));
733733
formatter.format(self);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/formatting/ComplexFormatter.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,17 @@ protected ComplexFormatter(PythonCore core, FormattingBuffer result, Spec spec)
5454
Spec imSpec;
5555
if (hasNoSpecType()) {
5656
// no type spec: should be like the default __str__
57-
reSpec = getComponentSpecForNoSpecType(InternalFormat.Spec.NONE);
58-
imSpec = getComponentSpecForNoSpecType('+');
57+
reSpec = getComponentSpecForNoSpecType(spec, InternalFormat.Spec.NONE);
58+
imSpec = getComponentSpecForNoSpecType(spec, '+');
5959
} else {
6060
// Turn off any flags that should apply to the result as a whole and not to the
61-
// individual
62-
// components (re/im). Sign of re is determined by the sign flag, sign of im will be
63-
// always
64-
// printed ('+' flag)
61+
// individual components (re/im). Sign of re is determined by the sign flag, sign of im
62+
// will be always printed ('+' flag)
6563
reSpec = getComponentSpec(spec, spec.sign);
6664
imSpec = getComponentSpec(spec, '+');
6765
}
68-
reFormatter = new FloatFormatter(core, result, reSpec);
69-
imFormatter = new FloatFormatter(core, result, imSpec);
66+
reFormatter = new FloatFormatter(core, result, reSpec, false);
67+
imFormatter = new FloatFormatter(core, result, imSpec, false);
7068
}
7169

7270
public ComplexFormatter(PythonCore core, Spec spec) {
@@ -85,10 +83,17 @@ private static Spec getComponentSpec(Spec spec, char sign) {
8583
spec.type);
8684
}
8785

88-
private static Spec getComponentSpecForNoSpecType(char sign) {
86+
private static Spec getComponentSpecForNoSpecType(Spec spec, char sign) {
8987
// CPython uses "r" type, but also some internal flags that cause that integer values
9088
// are printed without the decimal part, which is mostly what "g" does
91-
return new InternalFormat.Spec(' ', '>', sign, false, InternalFormat.Spec.UNSPECIFIED, Spec.NONE, -1, 'g');
89+
int precision = spec.precision;
90+
char type = 'r';
91+
if (precision < 0) {
92+
precision = 0;
93+
} else {
94+
type = 'g';
95+
}
96+
return new InternalFormat.Spec(' ', '>', sign, false, InternalFormat.Spec.UNSPECIFIED, Spec.NONE, precision, type);
9297
}
9398

9499
private boolean hasNoSpecType() {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/formatting/FloatFormatter.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,14 @@ public class FloatFormatter extends InternalFormat.Formatter {
4646
*
4747
* @param result destination buffer
4848
* @param spec parsed conversion specification
49+
* @param addDot0 reflects flag {@code Py_DTSF_ADD_DOT_0} in CPython, applicable only for 'r'
50+
* specifier
4951
*/
50-
public FloatFormatter(PythonCore core, FormattingBuffer result, Spec spec) {
52+
public FloatFormatter(PythonCore core, FormattingBuffer result, Spec spec, boolean addDot0) {
5153
super(core, result, spec);
52-
if (spec.alternate) {
54+
if (!addDot0 && spec.type == 'r') {
55+
minFracDigits = 0;
56+
} else if (spec.alternate) {
5357
// Alternate form means do not trim the zero fractional digits.
5458
minFracDigits = -1;
5559
} else if (spec.type == 'r' || spec.type == Spec.NONE) {
@@ -64,11 +68,10 @@ public FloatFormatter(PythonCore core, FormattingBuffer result, Spec spec) {
6468
}
6569
}
6670

67-
/**
68-
* Construct the formatter from a specification, allocating a buffer internally for the result.
69-
*
70-
* @param spec parsed conversion specification
71-
*/
71+
public FloatFormatter(PythonCore core, FormattingBuffer result, Spec spec) {
72+
this(core, result, spec, true);
73+
}
74+
7275
public FloatFormatter(PythonCore core, Spec spec) {
7376
this(core, new FormattingBuffer.StringFormattingBuffer(size(spec)), spec);
7477
}

0 commit comments

Comments
 (0)