Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2019, 2025, Oracle and/or its affiliates.
# Copyright (c) 2019, 2026, Oracle and/or its affiliates.
# Copyright (C) 1996-2017 Python Software Foundation
#
# Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
Expand Down Expand Up @@ -76,6 +76,23 @@ def test_encode():
assert codecs.encode('[]', 'ascii') == b'[]'


def test_unicode_error_large_positions():
large = 18977273910
negative = -58

translate = UnicodeTranslateError('worl', large, negative, 'worldworldworldworldworldworldworldworldworldworldworld')
assert translate.args == ('worl', large, negative, 'worldworldworldworldworldworldworldworldworldworldworld')
assert str(translate) == "can't translate characters in position 18977273910--59: worldworldworldworldworldworldworldworldworldworldworld"

encode = UnicodeEncodeError('utf-8', 'worl', large, negative, 'boom')
assert encode.args == ('utf-8', 'worl', large, negative, 'boom')
assert str(encode) == "'utf-8' codec can't encode characters in position 18977273910--59: boom"

decode = UnicodeDecodeError('utf-8', b'worl', large, negative, 'boom')
assert decode.args == ('utf-8', b'worl', large, negative, 'boom')
assert str(decode) == "'utf-8' codec can't decode bytes in position 18977273910--59: boom"


import codecs
import unittest

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
Expand Down Expand Up @@ -75,6 +75,12 @@ public final int getInt(PBaseException self, int index, StorageFactory factory)
return (int) val;
}

public final long getLong(PBaseException self, int index, StorageFactory factory) {
final Object val = execute(self, PNone.NO_VALUE, index, factory);
assert val instanceof Integer || val instanceof Long : "expected PBaseException attribute to be an integer";
return val instanceof Integer ? (int) val : (long) val;
}

public final Object set(PBaseException self, Object value, int index, StorageFactory factory) {
return execute(self, value, index, factory);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
Expand Down Expand Up @@ -47,7 +47,7 @@
import static com.oracle.graal.python.builtins.objects.exception.UnicodeErrorBuiltins.IDX_START;
import static com.oracle.graal.python.builtins.objects.exception.UnicodeErrorBuiltins.UNICODE_ERROR_ATTR_FACTORY;
import static com.oracle.graal.python.builtins.objects.exception.UnicodeErrorBuiltins.getArgAsBytes;
import static com.oracle.graal.python.builtins.objects.exception.UnicodeErrorBuiltins.getArgAsInt;
import static com.oracle.graal.python.builtins.objects.exception.UnicodeErrorBuiltins.getArgAsLong;
import static com.oracle.graal.python.builtins.objects.exception.UnicodeErrorBuiltins.getArgAsString;
import static com.oracle.graal.python.nodes.StringLiterals.T_EMPTY_STRING;
import static com.oracle.graal.python.runtime.exception.PythonErrorType.TypeError;
Expand Down Expand Up @@ -78,7 +78,7 @@
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
import com.oracle.graal.python.nodes.object.GetClassNode;
import com.oracle.graal.python.nodes.util.CastToJavaIntExactNode;
import com.oracle.graal.python.nodes.util.CastToJavaLongExactNode;
import com.oracle.graal.python.nodes.util.CastToTruffleStringNode;
import com.oracle.graal.python.util.PythonUtils;
import com.oracle.truffle.api.dsl.Bind;
Expand Down Expand Up @@ -114,16 +114,16 @@ static Object initNoArgs(VirtualFrame frame, PBaseException self, Object[] args,
@Bind Node inliningTarget,
@Cached UnicodeErrorBuiltins.GetArgAsBytesNode getArgAsBytesNode,
@Cached CastToTruffleStringNode toStringNode,
@Cached CastToJavaIntExactNode toJavaIntExactNode,
@Cached CastToJavaLongExactNode toJavaLongExactNode,
@Cached BaseExceptionBuiltins.BaseExceptionInitNode baseInitNode,
@Cached PRaiseNode raiseNode) {
baseInitNode.execute(frame, self, args, keywords);
// PyArg_ParseTuple(args, "UOnnU"), TODO: add proper error messages
self.setExceptionAttributes(new Object[]{
getArgAsString(inliningTarget, args, 0, raiseNode, toStringNode),
getArgAsBytes(frame, inliningTarget, args, 1, raiseNode, getArgAsBytesNode),
getArgAsInt(inliningTarget, args, 2, raiseNode, toJavaIntExactNode),
getArgAsInt(inliningTarget, args, 3, raiseNode, toJavaIntExactNode),
getArgAsLong(inliningTarget, args, 2, raiseNode, toJavaLongExactNode),
getArgAsLong(inliningTarget, args, 3, raiseNode, toJavaLongExactNode),
getArgAsString(inliningTarget, args, 4, raiseNode, toStringNode)
});
return PNone.NONE;
Expand All @@ -148,12 +148,12 @@ TruffleString str(VirtualFrame frame, PBaseException self,
// Get reason and encoding as strings, which they might not be if they've been
// modified after we were constructed.
PBytesLike object = (PBytesLike) attrNode.get(self, IDX_OBJECT, UNICODE_ERROR_ATTR_FACTORY);
final int start = attrNode.getInt(self, IDX_START, UNICODE_ERROR_ATTR_FACTORY);
final int end = attrNode.getInt(self, IDX_END, UNICODE_ERROR_ATTR_FACTORY);
final long start = attrNode.getLong(self, IDX_START, UNICODE_ERROR_ATTR_FACTORY);
final long end = attrNode.getLong(self, IDX_END, UNICODE_ERROR_ATTR_FACTORY);
final TruffleString encoding = strNode.execute(frame, inliningTarget, attrNode.get(self, IDX_ENCODING, UNICODE_ERROR_ATTR_FACTORY));
final TruffleString reason = strNode.execute(frame, inliningTarget, attrNode.get(self, IDX_REASON, UNICODE_ERROR_ATTR_FACTORY));
if (start < object.getSequenceStorage().length() && end == start + 1) {
final int b = getitemNode.executeKnownInt(object.getSequenceStorage(), start);
final int b = getitemNode.executeKnownInt(object.getSequenceStorage(), (int) start);
String bStr = PythonUtils.formatJString("%02x", b);
return simpleTruffleStringFormatNode.format("'%s' codec can't decode byte 0x%s in position %d: %s", encoding, bStr, start, reason);
} else {
Expand Down Expand Up @@ -252,14 +252,14 @@ static int doIt(VirtualFrame frame, Node inliningTarget, PBaseException exceptio
@Cached PyObjectSizeNode sizeNode) {
Object obj = getObjectNode.execute(inliningTarget, exceptionObject);
int size = sizeNode.execute(frame, inliningTarget, obj);
int start = attrNode.getInt(exceptionObject, IDX_START, UNICODE_ERROR_ATTR_FACTORY);
long start = attrNode.getLong(exceptionObject, IDX_START, UNICODE_ERROR_ATTR_FACTORY);
if (start < 0) {
start = 0;
}
if (start >= size) {
start = size - 1;
}
return start;
return (int) start;
}
}

Expand All @@ -281,14 +281,14 @@ static int doIt(VirtualFrame frame, Node inliningTarget, PBaseException exceptio
@Cached PyObjectSizeNode sizeNode) {
Object obj = getObjectNode.execute(inliningTarget, exceptionObject);
int size = sizeNode.execute(frame, inliningTarget, obj);
int end = attrNode.getInt(exceptionObject, IDX_END, UNICODE_ERROR_ATTR_FACTORY);
long end = attrNode.getLong(exceptionObject, IDX_END, UNICODE_ERROR_ATTR_FACTORY);
if (end < 1) {
end = 1;
}
if (end > size) {
end = size;
}
return end;
return (int) end;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
Expand Down Expand Up @@ -47,7 +47,7 @@
import static com.oracle.graal.python.builtins.objects.exception.UnicodeErrorBuiltins.IDX_REASON;
import static com.oracle.graal.python.builtins.objects.exception.UnicodeErrorBuiltins.IDX_START;
import static com.oracle.graal.python.builtins.objects.exception.UnicodeErrorBuiltins.UNICODE_ERROR_ATTR_FACTORY;
import static com.oracle.graal.python.builtins.objects.exception.UnicodeErrorBuiltins.getArgAsInt;
import static com.oracle.graal.python.builtins.objects.exception.UnicodeErrorBuiltins.getArgAsLong;
import static com.oracle.graal.python.builtins.objects.exception.UnicodeErrorBuiltins.getArgAsString;
import static com.oracle.graal.python.nodes.StringLiterals.T_EMPTY_STRING;
import static com.oracle.graal.python.runtime.exception.PythonErrorType.TypeError;
Expand All @@ -72,7 +72,7 @@
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
import com.oracle.graal.python.nodes.util.CastToJavaIntExactNode;
import com.oracle.graal.python.nodes.util.CastToJavaLongExactNode;
import com.oracle.graal.python.nodes.util.CastToTruffleStringNode;
import com.oracle.graal.python.util.PythonUtils;
import com.oracle.truffle.api.dsl.Bind;
Expand Down Expand Up @@ -106,16 +106,16 @@ public abstract static class UnicodeEncodeErrorInitNode extends PythonBuiltinNod
static Object initNoArgs(VirtualFrame frame, PBaseException self, Object[] args, PKeyword[] keywords,
@Bind Node inliningTarget,
@Cached CastToTruffleStringNode toStringNode,
@Cached CastToJavaIntExactNode toJavaIntExactNode,
@Cached CastToJavaLongExactNode toJavaLongExactNode,
@Cached BaseExceptionBuiltins.BaseExceptionInitNode baseInitNode,
@Cached PRaiseNode raiseNode) {
baseInitNode.execute(frame, self, args, keywords);
// PyArg_ParseTuple(args, "UUnnU"), TODO: add proper error messages
self.setExceptionAttributes(new Object[]{
getArgAsString(inliningTarget, args, 0, raiseNode, toStringNode),
getArgAsString(inliningTarget, args, 1, raiseNode, toStringNode),
getArgAsInt(inliningTarget, args, 2, raiseNode, toJavaIntExactNode),
getArgAsInt(inliningTarget, args, 3, raiseNode, toJavaIntExactNode),
getArgAsLong(inliningTarget, args, 2, raiseNode, toJavaLongExactNode),
getArgAsLong(inliningTarget, args, 3, raiseNode, toJavaLongExactNode),
getArgAsString(inliningTarget, args, 4, raiseNode, toStringNode)
});
return PNone.NONE;
Expand All @@ -142,12 +142,12 @@ TruffleString str(VirtualFrame frame, PBaseException self,
// Get reason and encoding as strings, which they might not be if they've been
// modified after we were constructed.
final TruffleString object = toTruffleStringNode.execute(inliningTarget, attrNode.get(self, IDX_OBJECT, UNICODE_ERROR_ATTR_FACTORY));
final int start = attrNode.getInt(self, IDX_START, UNICODE_ERROR_ATTR_FACTORY);
final int end = attrNode.getInt(self, IDX_END, UNICODE_ERROR_ATTR_FACTORY);
final long start = attrNode.getLong(self, IDX_START, UNICODE_ERROR_ATTR_FACTORY);
final long end = attrNode.getLong(self, IDX_END, UNICODE_ERROR_ATTR_FACTORY);
final TruffleString encoding = strNode.execute(frame, inliningTarget, attrNode.get(self, IDX_ENCODING, UNICODE_ERROR_ATTR_FACTORY));
final TruffleString reason = strNode.execute(frame, inliningTarget, attrNode.get(self, IDX_REASON, UNICODE_ERROR_ATTR_FACTORY));
if (start < codePointLengthNode.execute(object, TS_ENCODING) && end == start + 1) {
final int badChar = codePointAtIndexNode.execute(object, start);
final int badChar = codePointAtIndexNode.execute(object, (int) start);
String badCharStr;
if (badChar <= 0xFF) {
badCharStr = PythonUtils.formatJString("\\x%02x", badChar);
Expand Down Expand Up @@ -247,14 +247,14 @@ static int doIt(Node inliningTarget, PBaseException exceptionObject,
@Cached TruffleString.CodePointLengthNode codePointLengthNode) {
TruffleString ts = getObjectNode.execute(inliningTarget, exceptionObject);
int size = codePointLengthNode.execute(ts, TS_ENCODING);
int start = attrNode.getInt(exceptionObject, IDX_START, UNICODE_ERROR_ATTR_FACTORY);
long start = attrNode.getLong(exceptionObject, IDX_START, UNICODE_ERROR_ATTR_FACTORY);
if (start < 0) {
start = 0;
}
if (start >= size) {
start = size - 1;
}
return start;
return (int) start;
}
}

Expand All @@ -275,14 +275,14 @@ static int doIt(Node inliningTarget, PBaseException exceptionObject,
@Cached TruffleString.CodePointLengthNode codePointLengthNode) {
TruffleString ts = getObjectNode.execute(inliningTarget, exceptionObject);
int size = codePointLengthNode.execute(ts, TS_ENCODING);
int end = attrNode.getInt(exceptionObject, IDX_END, UNICODE_ERROR_ATTR_FACTORY);
long end = attrNode.getLong(exceptionObject, IDX_END, UNICODE_ERROR_ATTR_FACTORY);
if (end < 1) {
end = 1;
}
if (end > size) {
end = size;
}
return end;
return (int) end;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
Expand Down Expand Up @@ -60,6 +60,7 @@
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
import com.oracle.graal.python.nodes.util.CastToJavaIntExactNode;
import com.oracle.graal.python.nodes.util.CastToJavaLongExactNode;
import com.oracle.graal.python.nodes.util.CastToTruffleStringNode;
import com.oracle.graal.python.runtime.IndirectCallData.InteropCallData;
import com.oracle.graal.python.runtime.object.PFactory;
Expand Down Expand Up @@ -109,6 +110,14 @@ public static int getArgAsInt(Node inliningTarget, Object[] args, int index, PRa
}
}

public static long getArgAsLong(Node inliningTarget, Object[] args, int index, PRaiseNode raiseNode, CastToJavaLongExactNode castNode) {
if (args.length < index + 1 || !(PGuards.isInteger(args[index]) || PGuards.isPInt(args[index]))) {
throw raiseNode.raise(inliningTarget, PythonBuiltinClassType.TypeError);
} else {
return castNode.execute(inliningTarget, args[index]);
}
}

@GenerateInline
@GenerateCached(false)
public abstract static class GetArgAsBytesNode extends PNodeWithContext {
Expand Down Expand Up @@ -194,17 +203,17 @@ static Object setInt(PBaseException self, int value,
}

@Specialization
static Object setInt(PBaseException self, long value,
static Object setLong(PBaseException self, long value,
@Shared @Cached BaseExceptionAttrNode attrNode) {
return attrNode.execute(self, (int) value, IDX_START, UNICODE_ERROR_ATTR_FACTORY);
return attrNode.execute(self, value, IDX_START, UNICODE_ERROR_ATTR_FACTORY);
}

@Specialization
static Object setPInt(PBaseException self, PInt value,
@Bind Node inliningTarget,
@Cached CastToJavaIntExactNode castToJavaIntExactNode,
@Cached CastToJavaLongExactNode castToJavaLongExactNode,
@Shared @Cached BaseExceptionAttrNode attrNode) {
return attrNode.execute(self, castToJavaIntExactNode.execute(inliningTarget, value), IDX_START, UNICODE_ERROR_ATTR_FACTORY);
return attrNode.execute(self, castToJavaLongExactNode.execute(inliningTarget, value), IDX_START, UNICODE_ERROR_ATTR_FACTORY);
}

@Specialization(guards = {"!isNoValue(value)", "!canBeInteger(value)"})
Expand Down Expand Up @@ -236,12 +245,18 @@ static Object setInt(PBaseException self, int value,
return attrNode.execute(self, value, IDX_END, UNICODE_ERROR_ATTR_FACTORY);
}

@Specialization
static Object setLong(PBaseException self, long value,
@Shared @Cached BaseExceptionAttrNode attrNode) {
return attrNode.execute(self, value, IDX_END, UNICODE_ERROR_ATTR_FACTORY);
}

@Specialization
static Object setPInt(PBaseException self, PInt value,
@Bind Node inliningTarget,
@Cached CastToJavaIntExactNode castToJavaIntExactNode,
@Cached CastToJavaLongExactNode castToJavaLongExactNode,
@Shared @Cached BaseExceptionAttrNode attrNode) {
return attrNode.execute(self, castToJavaIntExactNode.execute(inliningTarget, value), IDX_END, UNICODE_ERROR_ATTR_FACTORY);
return attrNode.execute(self, castToJavaLongExactNode.execute(inliningTarget, value), IDX_END, UNICODE_ERROR_ATTR_FACTORY);
}

@Specialization(guards = {"!isNoValue(value)", "!canBeInteger(value)"})
Expand Down
Loading
Loading