Skip to content

Commit 81cabf1

Browse files
committed
Ensure formatting errors get correct tracebacks
1 parent ed96030 commit 81cabf1

File tree

15 files changed

+148
-147
lines changed

15 files changed

+148
-147
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/bytes/BytesBuiltins.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -626,12 +626,12 @@ Object mod(PBytesLike self, Object right,
626626
@Cached("create(__GETITEM__)") LookupAndCallBinaryNode getItemNode,
627627
@Cached TupleBuiltins.GetItemNode getTupleItemNode,
628628
@CachedContext(PythonLanguage.class) PythonContext context) {
629-
byte[] data = format(getBytes(selfLib, self), right, getItemNode, getTupleItemNode, context);
629+
byte[] data = format(getBytes(selfLib, self), right, getItemNode, getTupleItemNode, context, getRaiseNode());
630630
return create.execute(factory(), self, data);
631631
}
632632

633-
protected static byte[] format(byte[] self, Object right, LookupAndCallBinaryNode getItemNode, TupleBuiltins.GetItemNode getTupleItemNode, PythonContext context) {
634-
BytesFormatProcessor formatter = new BytesFormatProcessor(context.getCore(), getItemNode, getTupleItemNode, self);
633+
protected static byte[] format(byte[] self, Object right, LookupAndCallBinaryNode getItemNode, TupleBuiltins.GetItemNode getTupleItemNode, PythonContext context, PRaiseNode raiseNode) {
634+
BytesFormatProcessor formatter = new BytesFormatProcessor(context.getCore(), raiseNode, getItemNode, getTupleItemNode, self);
635635
return formatter.format(right);
636636
}
637637
}

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
9494
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
9595
import com.oracle.graal.python.nodes.ErrorMessages;
96+
import com.oracle.graal.python.nodes.PRaiseNode;
9697
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
9798
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
9899
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
@@ -101,7 +102,6 @@
101102
import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentClinicProvider;
102103
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
103104
import com.oracle.graal.python.nodes.util.CoerceToComplexNode;
104-
import com.oracle.graal.python.runtime.PythonCore;
105105
import com.oracle.graal.python.runtime.exception.PythonErrorType;
106106
import com.oracle.graal.python.runtime.formatting.ComplexFormatter;
107107
import com.oracle.graal.python.runtime.formatting.InternalFormat;
@@ -722,12 +722,12 @@ PNotImplemented doGeneric(Object left, Object right) {
722722
abstract static class ReprNode extends PythonUnaryBuiltinNode {
723723
@Specialization
724724
String repr(PComplex self) {
725-
return repr(self, getCore());
725+
return repr(self, getRaiseNode());
726726
}
727727

728728
@TruffleBoundary
729-
private static String repr(PComplex self, PythonCore core) {
730-
ComplexFormatter formatter = new ComplexFormatter(core, new Spec(-1, Spec.NONE));
729+
private static String repr(PComplex self, PRaiseNode raiseNode) {
730+
ComplexFormatter formatter = new ComplexFormatter(raiseNode, new Spec(-1, Spec.NONE));
731731
formatter.format(self);
732732
return formatter.pad().getResult();
733733
}
@@ -750,15 +750,14 @@ protected ArgumentClinicProvider getArgumentClinic() {
750750

751751
@Specialization(guards = "!formatString.isEmpty()")
752752
Object format(PComplex self, String formatString) {
753-
PythonCore core = getCore();
754-
InternalFormat.Spec spec = InternalFormat.fromText(core, formatString, __FORMAT__);
753+
InternalFormat.Spec spec = InternalFormat.fromText(getRaiseNode(), formatString, __FORMAT__);
755754
validateSpec(spec);
756-
return doFormat(self, spec, core);
755+
return doFormat(getRaiseNode(), self, spec);
757756
}
758757

759758
@TruffleBoundary
760-
private static Object doFormat(PComplex self, Spec spec, PythonCore core) {
761-
ComplexFormatter formatter = new ComplexFormatter(core, validateAndPrepareForFloat(spec, core, "complex"));
759+
private static Object doFormat(PRaiseNode raiseNode, PComplex self, Spec spec) {
760+
ComplexFormatter formatter = new ComplexFormatter(raiseNode, validateAndPrepareForFloat(raiseNode, spec, "complex"));
762761
formatter.format(self);
763762
return formatter.pad().getResult();
764763
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/floats/FloatBuiltins.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@
101101
import com.oracle.graal.python.nodes.object.GetClassNode;
102102
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
103103
import com.oracle.graal.python.runtime.PythonContext;
104-
import com.oracle.graal.python.runtime.PythonCore;
105104
import com.oracle.graal.python.runtime.exception.PythonErrorType;
106105
import com.oracle.graal.python.runtime.formatting.FloatFormatter;
107106
import com.oracle.graal.python.runtime.formatting.InternalFormat;
@@ -142,7 +141,7 @@ abstract static class StrNode extends PythonUnaryBuiltinNode {
142141
@Specialization
143142
String str(double self) {
144143
Spec spec = new Spec(' ', '>', Spec.NONE, false, Spec.UNSPECIFIED, Spec.NONE, 0, 'r');
145-
FloatFormatter f = new FloatFormatter(getCore(), spec);
144+
FloatFormatter f = new FloatFormatter(getRaiseNode(), spec);
146145
f.setMinFracDigits(1);
147146
return doFormat(self, f);
148147
}
@@ -183,13 +182,13 @@ protected ArgumentClinicProvider getArgumentClinic() {
183182

184183
@Specialization(guards = "!formatString.isEmpty()")
185184
Object formatPF(double self, String formatString) {
186-
return doFormat(self, formatString, getCore());
185+
return doFormat(self, formatString);
187186
}
188187

189188
@TruffleBoundary
190-
private String doFormat(double self, String formatString, PythonCore core) {
191-
InternalFormat.Spec spec = InternalFormat.fromText(core, formatString, __FORMAT__);
192-
FloatFormatter formatter = new FloatFormatter(core, validateAndPrepareForFloat(spec, getCore(), "float"));
189+
private String doFormat(double self, String formatString) {
190+
InternalFormat.Spec spec = InternalFormat.fromText(getRaiseNode(), formatString, __FORMAT__);
191+
FloatFormatter formatter = new FloatFormatter(getRaiseNode(), validateAndPrepareForFloat(getRaiseNode(), spec, "float"));
193192
formatter.format(self);
194193
return formatter.pad().getResult();
195194
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/ints/IntBuiltins.java

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@
9595
import com.oracle.graal.python.nodes.object.GetClassNode;
9696
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
9797
import com.oracle.graal.python.runtime.PythonContext;
98-
import com.oracle.graal.python.runtime.PythonCore;
9998
import com.oracle.graal.python.runtime.exception.PythonErrorType;
10099
import com.oracle.graal.python.runtime.formatting.FloatFormatter;
101100
import com.oracle.graal.python.runtime.formatting.IntegerFormatter;
@@ -2565,13 +2564,13 @@ Object formatB(boolean self, String formatString) {
25652564

25662565
@Specialization(guards = "!formatString.isEmpty()")
25672566
Object formatI(int self, String formatString) {
2568-
PythonCore core = getCore();
2569-
Spec spec = getSpec(formatString, core);
2567+
PRaiseNode raiseNode = getRaiseNode();
2568+
Spec spec = getSpec(formatString, raiseNode);
25702569
if (isDoubleSpec(spec)) {
2571-
return formatDouble(core, spec, self);
2570+
return formatDouble(raiseNode, spec, self);
25722571
}
2573-
validateIntegerSpec(core, spec);
2574-
return formatInt(self, core, spec);
2572+
validateIntegerSpec(raiseNode, spec);
2573+
return formatInt(self, raiseNode, spec);
25752574
}
25762575

25772576
@Specialization(guards = "!formatString.isEmpty()")
@@ -2581,15 +2580,15 @@ Object formatL(VirtualFrame frame, long self, String formatString) {
25812580

25822581
@Specialization(guards = "!formatString.isEmpty()")
25832582
Object formatPI(VirtualFrame frame, PInt self, String formatString) {
2584-
PythonCore core = getCore();
2585-
Spec spec = getSpec(formatString, core);
2583+
PRaiseNode raiseNode = getRaiseNode();
2584+
Spec spec = getSpec(formatString, raiseNode);
25862585
if (isDoubleSpec(spec)) {
25872586
// lazy init of floatNode serves as branch profile
25882587
double doubleVal = asDouble(frame, self);
2589-
return formatDouble(core, spec, doubleVal);
2588+
return formatDouble(raiseNode, spec, doubleVal);
25902589
}
2591-
validateIntegerSpec(core, spec);
2592-
return formatPInt(self, core, spec);
2590+
validateIntegerSpec(raiseNode, spec);
2591+
return formatPInt(self, raiseNode, spec);
25932592
}
25942593

25952594
private double asDouble(VirtualFrame frame, Object self) {
@@ -2601,8 +2600,8 @@ private double asDouble(VirtualFrame frame, Object self) {
26012600
return (double) floatNode.executeWith(frame, PythonBuiltinClassType.PFloat, self);
26022601
}
26032602

2604-
private static Spec getSpec(String formatString, PythonCore core) {
2605-
Spec spec = InternalFormat.fromText(core, formatString, __FORMAT__);
2603+
private static Spec getSpec(String formatString, PRaiseNode raiseNode) {
2604+
Spec spec = InternalFormat.fromText(raiseNode, formatString, __FORMAT__);
26062605
return spec.withDefaults(Spec.NUMERIC);
26072606
}
26082607

@@ -2613,35 +2612,35 @@ private static boolean isDoubleSpec(Spec spec) {
26132612
}
26142613

26152614
@TruffleBoundary
2616-
private static String formatDouble(PythonCore core, Spec spec, double value) {
2617-
FloatFormatter formatter = new FloatFormatter(core, spec);
2615+
private static String formatDouble(PRaiseNode raiseNode, Spec spec, double value) {
2616+
FloatFormatter formatter = new FloatFormatter(raiseNode, spec);
26182617
formatter.format(value);
26192618
return formatter.pad().getResult();
26202619
}
26212620

26222621
@TruffleBoundary
2623-
private static String formatInt(int self, PythonCore core, Spec spec) {
2624-
IntegerFormatter formatter = new IntegerFormatter(core, spec);
2622+
private static String formatInt(int self, PRaiseNode raiseNode, Spec spec) {
2623+
IntegerFormatter formatter = new IntegerFormatter(raiseNode, spec);
26252624
formatter.format(self);
26262625
return formatter.pad().getResult();
26272626
}
26282627

26292628
@TruffleBoundary
2630-
private static String formatPInt(PInt self, PythonCore core, Spec spec) {
2631-
IntegerFormatter formatter = new IntegerFormatter(core, spec);
2629+
private static String formatPInt(PInt self, PRaiseNode raiseNode, Spec spec) {
2630+
IntegerFormatter formatter = new IntegerFormatter(raiseNode, spec);
26322631
formatter.format(self.getValue());
26332632
return formatter.pad().getResult();
26342633
}
26352634

2636-
private static void validateIntegerSpec(PythonCore core, Spec spec) {
2635+
private static void validateIntegerSpec(PRaiseNode raiseNode, Spec spec) {
26372636
if (Spec.specified(spec.precision)) {
2638-
throw core.raise(ValueError, ErrorMessages.PRECISION_NOT_ALLOWED_FOR_INT);
2637+
throw raiseNode.raise(ValueError, ErrorMessages.PRECISION_NOT_ALLOWED_FOR_INT);
26392638
}
26402639
if (spec.type == 'c') {
26412640
if (Spec.specified(spec.sign)) {
2642-
throw core.raise(ValueError, ErrorMessages.SIGN_NOT_ALLOWED_WITH_C_FOR_INT);
2641+
throw raiseNode.raise(ValueError, ErrorMessages.SIGN_NOT_ALLOWED_WITH_C_FOR_INT);
26432642
} else if (spec.alternate) {
2644-
throw core.raise(ValueError, ErrorMessages.ALTERNATE_NOT_ALLOWED_WITH_C_FOR_INT);
2643+
throw raiseNode.raise(ValueError, ErrorMessages.ALTERNATE_NOT_ALLOWED_WITH_C_FOR_INT);
26452644
}
26462645
}
26472646
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/str/StringBuiltins.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
import com.oracle.graal.python.builtins.objects.type.TypeNodes.IsSameTypeNode;
109109
import com.oracle.graal.python.nodes.ErrorMessages;
110110
import com.oracle.graal.python.nodes.PGuards;
111+
import com.oracle.graal.python.nodes.PRaiseNode;
111112
import com.oracle.graal.python.nodes.SpecialMethodNames;
112113
import com.oracle.graal.python.nodes.builtins.ListNodes.AppendNode;
113114
import com.oracle.graal.python.nodes.call.special.LookupAndCallBinaryNode;
@@ -132,7 +133,6 @@
132133
import com.oracle.graal.python.nodes.util.CastToJavaStringNodeGen;
133134
import com.oracle.graal.python.runtime.ExecutionContext.IndirectCallContext;
134135
import com.oracle.graal.python.runtime.PythonContext;
135-
import com.oracle.graal.python.runtime.PythonCore;
136136
import com.oracle.graal.python.runtime.PythonOptions;
137137
import com.oracle.graal.python.runtime.exception.PException;
138138
import com.oracle.graal.python.runtime.formatting.InternalFormat;
@@ -194,18 +194,18 @@ Object format(Object self, String formatString,
194194
// We cannot cast self via argument clinic, because we need to keep it as-is for the
195195
// empty format string case, which should call __str__, which may be overridden
196196
String str = castToJavaStringNode.cast(self, ErrorMessages.REQUIRES_STR_OBJECT_BUT_RECEIVED_P, __STR__, self);
197-
return formatString(getCore(), getAndValidateSpec(formatString), str);
197+
return formatString(getRaiseNode(), getAndValidateSpec(formatString), str);
198198
}
199199

200200
@TruffleBoundary
201-
private static Object formatString(PythonCore core, Spec spec, String str) {
202-
TextFormatter formatter = new TextFormatter(core, spec.withDefaults(Spec.STRING));
201+
private static Object formatString(PRaiseNode raiseNode, Spec spec, String str) {
202+
TextFormatter formatter = new TextFormatter(raiseNode, spec.withDefaults(Spec.STRING));
203203
formatter.format(str);
204204
return formatter.pad().getResult();
205205
}
206206

207207
private Spec getAndValidateSpec(String formatString) {
208-
Spec spec = InternalFormat.fromText(getCore(), formatString, __FORMAT__);
208+
Spec spec = InternalFormat.fromText(getRaiseNode(), formatString, __FORMAT__);
209209
if (Spec.specified(spec.type) && spec.type != 's') {
210210
throw raise(PythonBuiltinClassType.TypeError, ErrorMessages.UNKNOWN_FORMAT_CODE, spec.type, "str");
211211
}
@@ -1809,7 +1809,7 @@ Object doStringObject(VirtualFrame frame, String self, Object right,
18091809
@Shared("context") @CachedContext(PythonLanguage.class) PythonContext context) {
18101810
Object state = IndirectCallContext.enter(frame, context, this);
18111811
try {
1812-
return new StringFormatProcessor(context.getCore(), getItemNode, getTupleItemNode, self).format(right);
1812+
return new StringFormatProcessor(context.getCore(), getRaiseNode(), getItemNode, getTupleItemNode, self).format(right);
18131813
} finally {
18141814
IndirectCallContext.exit(frame, context, state);
18151815
}

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import com.oracle.graal.python.builtins.objects.tuple.TupleBuiltins;
6161
import com.oracle.graal.python.nodes.ErrorMessages;
6262
import com.oracle.graal.python.nodes.PGuards;
63+
import com.oracle.graal.python.nodes.PRaiseNode;
6364
import com.oracle.graal.python.nodes.call.special.LookupAndCallBinaryNode;
6465
import com.oracle.graal.python.runtime.PythonCore;
6566
import com.oracle.graal.python.runtime.exception.PException;
@@ -71,8 +72,8 @@
7172
public class BytesFormatProcessor extends FormatProcessor<byte[]> {
7273
private final byte[] formatBytes;
7374

74-
public BytesFormatProcessor(PythonCore core, LookupAndCallBinaryNode getItemNode, TupleBuiltins.GetItemNode getTupleItemNode, byte[] formatBytes) {
75-
super(core, getItemNode, getTupleItemNode, new BytesFormattingBuffer());
75+
public BytesFormatProcessor(PythonCore core, PRaiseNode raiseNode, LookupAndCallBinaryNode getItemNode, TupleBuiltins.GetItemNode getTupleItemNode, byte[] formatBytes) {
76+
super(core, raiseNode, getItemNode, getTupleItemNode, new BytesFormattingBuffer());
7677
this.formatBytes = formatBytes;
7778
}
7879

@@ -92,7 +93,7 @@ char pop() {
9293
try {
9394
return (char) formatBytes[index++];
9495
} catch (ArrayIndexOutOfBoundsException e) {
95-
throw core.raise(ValueError, ErrorMessages.INCOMPLETE_FORMAT);
96+
throw raiseNode.raise(ValueError, ErrorMessages.INCOMPLETE_FORMAT);
9697
}
9798
}
9899

@@ -118,7 +119,7 @@ protected double asFloat(Object arg) {
118119
return super.asFloat(arg);
119120
} catch (PException ex) {
120121
// exactly like in CPython, all errors are translated to this
121-
throw core.raise(TypeError, ErrorMessages.FLOAT_ARG_REQUIRED, arg);
122+
throw raiseNode.raise(TypeError, ErrorMessages.FLOAT_ARG_REQUIRED, arg);
122123
}
123124
}
124125

@@ -140,7 +141,7 @@ protected Formatter handleSingleCharacterFormat(Spec spec) {
140141
if (pyLib.isBuffer(arg)) {
141142
try {
142143
if (pyLib.getBufferLength(arg) == 1) {
143-
BytesFormatter f = new BytesFormatter(core, buffer, spec);
144+
BytesFormatter f = new BytesFormatter(raiseNode, buffer, spec);
144145
f.format(pyLib.getBufferBytes(arg));
145146
return f;
146147
}
@@ -179,16 +180,16 @@ protected Formatter handleSingleCharacterFormat(Spec spec) {
179180
}
180181

181182
if (!foundByte) {
182-
throw core.raise(TypeError, ErrorMessages.C_REQUIRES_INT_IN_BYTE_RANGE_OR_SINGLE_BYTE);
183+
throw raiseNode.raise(TypeError, ErrorMessages.C_REQUIRES_INT_IN_BYTE_RANGE_OR_SINGLE_BYTE);
183184
}
184185

185-
BytesFormatter f = new BytesFormatter(core, buffer, spec);
186+
BytesFormatter f = new BytesFormatter(raiseNode, buffer, spec);
186187
f.format(value);
187188
return f;
188189
}
189190

190191
private PException raiseOverflow() {
191-
throw core.raise(OverflowError, ErrorMessages.C_ARG_NOT_IN_RANGE256_DECIMAL);
192+
throw raiseNode.raise(OverflowError, ErrorMessages.C_ARG_NOT_IN_RANGE256_DECIMAL);
192193
}
193194

194195
@Override
@@ -202,7 +203,7 @@ protected InternalFormat.Formatter handleRemainingFormats(InternalFormat.Spec sp
202203
// call __bytes__
203204
arg = getArg();
204205
bytes = asBytes(arg);
205-
BytesFormatter fb = new BytesFormatter(core, buffer, spec);
206+
BytesFormatter fb = new BytesFormatter(raiseNode, buffer, spec);
206207
fb.format(bytes);
207208
return fb;
208209

@@ -215,12 +216,12 @@ protected InternalFormat.Formatter handleRemainingFormats(InternalFormat.Spec sp
215216
Object result = call(attribute, arg);
216217
if (PGuards.isString(result)) {
217218
bytes = BytesUtils.unicodeNonAsciiEscape(result.toString());
218-
fb = new BytesFormatter(core, buffer, spec);
219+
fb = new BytesFormatter(raiseNode, buffer, spec);
219220
fb.format(bytes);
220221
return fb;
221222
}
222223
}
223-
throw core.raise(TypeError, ErrorMessages.REQUIRES_OBJ_THAT_IMPLEMENTS_S, __REPR__);
224+
throw raiseNode.raise(TypeError, ErrorMessages.REQUIRES_OBJ_THAT_IMPLEMENTS_S, __REPR__);
224225

225226
default:
226227
return null;
@@ -237,14 +238,14 @@ private byte[] asBytes(Object arg) {
237238
if (attribute != PNone.NO_VALUE) {
238239
Object bytesResult = call(attribute, arg);
239240
if (!(bytesResult instanceof PBytes)) {
240-
throw core.raise(TypeError, ErrorMessages.RETURNED_NONBYTES, __BYTES__, arg);
241+
throw raiseNode.raise(TypeError, ErrorMessages.RETURNED_NONBYTES, __BYTES__, arg);
241242
}
242243
return toBytes((PBytes) bytesResult);
243244
}
244245
// otherwise: use the buffer protocol
245246
byte[] result = byteBufferAsBytesOrNull(arg);
246247
if (result == null) {
247-
throw core.raise(TypeError, ErrorMessages.B_REQUIRES_BYTES_OR_OBJ_THAT_IMPLEMENTS_S_NOT_P, __BYTES__, arg);
248+
throw raiseNode.raise(TypeError, ErrorMessages.B_REQUIRES_BYTES_OR_OBJ_THAT_IMPLEMENTS_S_NOT_P, __BYTES__, arg);
248249
}
249250
return result;
250251
}

0 commit comments

Comments
 (0)