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
12 changes: 12 additions & 0 deletions graalpython/com.oracle.graal.python.test/src/tests/test_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ def test_ascii(self):
self.assertEqual(ascii(1), "1")
self.assertEqual(ascii("錦蛇 \t \0 a \x03"), "'\\u9326\\u86c7 \\t \\x00 a \\x03'")

def test_ascii_preserves_str_subclass_for_ascii_repr(self):
class MyStr(str):
pass

class Foo:
def __repr__(self):
return MyStr("hello")

result = ascii(Foo())
self.assertEqual(result, "hello")
self.assertIs(type(result), MyStr)

def test_input(self):
import sys
class AlwaysLine:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@
import com.oracle.graal.python.lib.PyNumberDivmodNode;
import com.oracle.graal.python.lib.PyNumberIndexNode;
import com.oracle.graal.python.lib.PyNumberPowerNode;
import com.oracle.graal.python.lib.PyObjectAsciiNode;
import com.oracle.graal.python.lib.PyObjectAsciiAsObjectNode;
import com.oracle.graal.python.lib.PyObjectCallMethodObjArgs;
import com.oracle.graal.python.lib.PyObjectDir;
import com.oracle.graal.python.lib.PyObjectGetAttr;
Expand Down Expand Up @@ -1918,9 +1918,9 @@ public static FormatNode create() {
abstract static class AsciiNode extends PythonUnaryBuiltinNode {

@Specialization
public static TruffleString ascii(VirtualFrame frame, Object obj,
public static Object ascii(VirtualFrame frame, Object obj,
@Bind Node inliningTarget,
@Cached PyObjectAsciiNode asciiNode) {
@Cached PyObjectAsciiAsObjectNode asciiNode) {
return asciiNode.execute(frame, inliningTarget, obj);
}
}
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 @@ -110,7 +110,7 @@
import com.oracle.graal.python.lib.PyCallableCheckNode;
import com.oracle.graal.python.lib.PyLongCheckNode;
import com.oracle.graal.python.lib.PyObjectAsFileDescriptor;
import com.oracle.graal.python.lib.PyObjectAsciiNode;
import com.oracle.graal.python.lib.PyObjectAsciiAsObjectNode;
import com.oracle.graal.python.lib.PyObjectCallMethodObjArgs;
import com.oracle.graal.python.lib.PyObjectDelItem;
import com.oracle.graal.python.lib.PyObjectDir;
Expand Down Expand Up @@ -669,9 +669,9 @@ int doGeneric(Object ptrObject,
@CApiBuiltin(ret = PyObjectTransfer, args = {PyObject}, call = Direct)
abstract static class PyObject_ASCII extends CApiUnaryBuiltinNode {
@Specialization(guards = "!isNoValue(obj)")
static TruffleString ascii(Object obj,
static Object ascii(Object obj,
@Bind Node inliningTarget,
@Cached PyObjectAsciiNode asciiNode) {
@Cached PyObjectAsciiAsObjectNode asciiNode) {
return asciiNode.execute(null, inliningTarget, obj);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
import com.oracle.graal.python.builtins.objects.type.slots.TpSlotHashFun;
import com.oracle.graal.python.builtins.objects.type.slots.TpSlotRichCompare;
import com.oracle.graal.python.lib.PyCallableCheckNode;
import com.oracle.graal.python.lib.PyObjectAsciiNode;
import com.oracle.graal.python.lib.PyObjectAsciiAsTruffleStringNode;
import com.oracle.graal.python.lib.PyObjectGetAttr;
import com.oracle.graal.python.lib.PyObjectHashNode;
import com.oracle.graal.python.lib.PyObjectReprAsTruffleStringNode;
Expand Down Expand Up @@ -1408,7 +1408,7 @@ static ParsedReplacement parseReplacement(Node inliningTarget, VirtualFrame fram
if (!isIdentifierNode.execute(inliningTarget, name) || binary && !ascii) {
errorProfile.enter(inliningTarget);
throw raiseRegexErrorNode.executeFormatted(frame, BAD_CHAR_IN_GROUP_NAME, replacement, toCodepointIndex(nameStartPos, binary),
binary ? PyObjectAsciiNode.executeUncached(name) : PyObjectReprAsTruffleStringNode.executeUncached(name));
binary ? PyObjectAsciiAsTruffleStringNode.executeUncached(name) : PyObjectReprAsTruffleStringNode.executeUncached(name));
}
Object namedCaptureGroups = TRegexUtil.TRegexCompiledRegexAccessor.namedCaptureGroups(tregexCompiledRegex, inliningTarget, readNamedGroupsNode);
if (!TRegexUtil.TRegexNamedCaptureGroupsAccessor.hasGroup(namedCaptureGroups, name, genericInteropLib)) {
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 @@ -69,11 +69,11 @@
import com.oracle.graal.python.builtins.modules.SysModuleBuiltins;
import com.oracle.graal.python.builtins.objects.PNone;
import com.oracle.graal.python.builtins.objects.function.PKeyword;
import com.oracle.graal.python.lib.PyObjectAsciiNode;
import com.oracle.graal.python.lib.PyObjectAsciiAsObjectNode;
import com.oracle.graal.python.lib.PyObjectGetItem;
import com.oracle.graal.python.lib.PyObjectLookupAttr;
import com.oracle.graal.python.lib.PyObjectReprAsObjectNode;
import com.oracle.graal.python.lib.PyObjectStrAsTruffleStringNode;
import com.oracle.graal.python.lib.PyObjectStrAsObjectNode;
import com.oracle.graal.python.nodes.PRaiseNode;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.nodes.Node;
Expand Down Expand Up @@ -458,9 +458,9 @@ private static Object convert(Node node, Object obj, char conversion) {
case 'r':
return PyObjectReprAsObjectNode.executeUncached(obj);
case 's':
return PyObjectStrAsTruffleStringNode.executeUncached(obj);
return PyObjectStrAsObjectNode.executeUncached(obj);
case 'a':
return PyObjectAsciiNode.executeUncached(obj);
return PyObjectAsciiAsObjectNode.executeUncached(obj);
default:
throw PRaiseNode.raiseStatic(node, ValueError, INVALID_CONVERSION);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* 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
*
* Subject to the condition set forth below, permission is hereby granted to any
* person obtaining a copy of this software, associated documentation and/or
* data (collectively the "Software"), free of charge and under any and all
* copyright rights in the Software, and any and all patent rights owned or
* freely licensable by each licensor hereunder covering either (i) the
* unmodified Software as contributed to or provided by such licensor, or (ii)
* the Larger Works (as defined below), to deal in both
*
* (a) the Software, and
*
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
* one is included with the Software each a "Larger Work" to which the Software
* is contributed by such licensors),
*
* without restriction, including without limitation the rights to copy, create
* derivative works of, display, perform, and distribute the Software and make,
* use, sell, offer for sale, import, export, have made, and have sold the
* Software and the Larger Work(s), and to sublicense the foregoing rights on
* either these or other terms.
*
* This license is subject to the following condition:
*
* The above copyright notice and either this complete permission notice or at a
* minimum a reference to the UPL must be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.oracle.graal.python.lib;

import static com.oracle.graal.python.builtins.objects.bytes.BytesUtils.unicodeNonAsciiEscape;
import static com.oracle.graal.python.util.PythonUtils.TS_ENCODING;

import com.oracle.graal.python.nodes.PNodeWithContext;
import com.oracle.graal.python.nodes.util.CastToTruffleStringNode;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.GenerateCached;
import com.oracle.truffle.api.dsl.GenerateInline;
import com.oracle.truffle.api.dsl.GenerateUncached;
import com.oracle.truffle.api.dsl.NeverDefault;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.Frame;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.strings.TruffleString;
import com.oracle.truffle.api.strings.TruffleStringIterator;

/**
* Equivalent of CPython's PyObject_ASCII.
*/
@GenerateUncached
@GenerateInline(inlineByDefault = true)
@GenerateCached
public abstract class PyObjectAsciiAsObjectNode extends PNodeWithContext {
public static Object executeUncached(Object object) {
return PyObjectAsciiAsObjectNodeGen.getUncached().execute(null, null, object);
}

public final Object executeCached(Frame frame, Object object) {
return execute(frame, this, object);
}

public abstract Object execute(Frame frame, Node inliningTarget, Object object);

@Specialization
public static Object ascii(VirtualFrame frame, Node inliningTarget, Object obj,
@Cached PyObjectReprAsObjectNode reprNode,
@Cached CastToTruffleStringNode castToTruffleStringNode,
@Cached TruffleString.GetCodeRangeNode getCodeRangeNode,
@Cached TruffleString.CreateCodePointIteratorNode createCodePointIteratorNode,
@Cached TruffleStringIterator.NextNode nextNode,
@Cached TruffleString.CodePointLengthNode codePointLengthNode,
@Cached TruffleString.FromByteArrayWithCompactionUTF32Node fromByteArrayNode) {
Object reprObj = reprNode.execute(frame, inliningTarget, obj);
TruffleString repr = castToTruffleStringNode.castKnownString(inliningTarget, reprObj);
if (getCodeRangeNode.execute(repr, TS_ENCODING) == TruffleString.CodeRange.ASCII) {
return reprObj;
}
return convertToAscii(repr, createCodePointIteratorNode, nextNode, codePointLengthNode, fromByteArrayNode);
}

public static TruffleString convertToAscii(TruffleString repr, TruffleString.CreateCodePointIteratorNode createCodePointIteratorNode, TruffleStringIterator.NextNode nextNode,
TruffleString.CodePointLengthNode codePointLengthNode, TruffleString.FromByteArrayWithCompactionUTF32Node fromByteArrayNode) {
// TODO GR-37220: rewrite using TruffleStringBuilder?
byte[] bytes = new byte[codePointLengthNode.execute(repr, TS_ENCODING) * 10];
TruffleStringIterator it = createCodePointIteratorNode.execute(repr, TS_ENCODING);
int j = 0;
while (it.hasNext()) {
int ch = nextNode.execute(it, TS_ENCODING);
j = unicodeNonAsciiEscape(ch, j, bytes);
}
return fromByteArrayNode.execute(bytes, 0, j, TruffleString.CompactionLevel.S1, true);
}

@NeverDefault
public static PyObjectAsciiAsObjectNode create() {
return PyObjectAsciiAsObjectNodeGen.create();
}

public static PyObjectAsciiAsObjectNode getUncached() {
return PyObjectAsciiAsObjectNodeGen.getUncached();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
*/
package com.oracle.graal.python.lib;

import static com.oracle.graal.python.builtins.objects.bytes.BytesUtils.unicodeNonAsciiEscape;
import static com.oracle.graal.python.util.PythonUtils.TS_ENCODING;

import com.oracle.graal.python.nodes.PNodeWithContext;
Expand All @@ -62,9 +61,9 @@
@GenerateUncached
@GenerateInline(inlineByDefault = true)
@GenerateCached
public abstract class PyObjectAsciiNode extends PNodeWithContext {
public abstract class PyObjectAsciiAsTruffleStringNode extends PNodeWithContext {
public static TruffleString executeUncached(Object object) {
return PyObjectAsciiNodeGen.getUncached().execute(null, null, object);
return PyObjectAsciiAsTruffleStringNodeGen.getUncached().execute(null, null, object);
}

public final TruffleString executeCached(Frame frame, Object object) {
Expand All @@ -81,27 +80,19 @@ public static TruffleString ascii(VirtualFrame frame, Node inliningTarget, Objec
@Cached TruffleStringIterator.NextNode nextNode,
@Cached TruffleString.CodePointLengthNode codePointLengthNode,
@Cached TruffleString.FromByteArrayWithCompactionUTF32Node fromByteArrayNode) {
// TODO GR-37220: rewrite using TruffleStringBuilder?
TruffleString repr = reprNode.execute(frame, inliningTarget, obj);
if (getCodeRangeNode.execute(repr, TS_ENCODING) == TruffleString.CodeRange.ASCII) {
return repr;
}
byte[] bytes = new byte[codePointLengthNode.execute(repr, TS_ENCODING) * 10];
TruffleStringIterator it = createCodePointIteratorNode.execute(repr, TS_ENCODING);
int j = 0;
while (it.hasNext()) {
int ch = nextNode.execute(it, TS_ENCODING);
j = unicodeNonAsciiEscape(ch, j, bytes);
}
return fromByteArrayNode.execute(bytes, 0, j, TruffleString.CompactionLevel.S1, true);
return PyObjectAsciiAsObjectNode.convertToAscii(repr, createCodePointIteratorNode, nextNode, codePointLengthNode, fromByteArrayNode);
}

@NeverDefault
public static PyObjectAsciiNode create() {
return PyObjectAsciiNodeGen.create();
public static PyObjectAsciiAsTruffleStringNode create() {
return PyObjectAsciiAsTruffleStringNodeGen.create();
}

public static PyObjectAsciiNode getUncached() {
return PyObjectAsciiNodeGen.getUncached();
public static PyObjectAsciiAsTruffleStringNode getUncached() {
return PyObjectAsciiAsTruffleStringNodeGen.getUncached();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@
import com.oracle.graal.python.PythonLanguage;
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
import com.oracle.graal.python.builtins.modules.BuiltinFunctions.FormatNode;
import com.oracle.graal.python.builtins.modules.BuiltinFunctionsFactory.FormatNodeFactory.FormatNodeGen;
import com.oracle.graal.python.builtins.modules.MarshalModuleBuiltins;
import com.oracle.graal.python.builtins.modules.BuiltinFunctionsFactory.FormatNodeFactory.FormatNodeGen;
import com.oracle.graal.python.builtins.objects.PNone;
import com.oracle.graal.python.builtins.objects.asyncio.GetAwaitableNode;
import com.oracle.graal.python.builtins.objects.asyncio.GetAwaitableNodeGen;
Expand Down Expand Up @@ -143,8 +143,8 @@
import com.oracle.graal.python.lib.PyNumberSubtractNode;
import com.oracle.graal.python.lib.PyNumberTrueDivideNode;
import com.oracle.graal.python.lib.PyNumberXorNode;
import com.oracle.graal.python.lib.PyObjectAsciiNode;
import com.oracle.graal.python.lib.PyObjectAsciiNodeGen;
import com.oracle.graal.python.lib.PyObjectAsciiAsObjectNode;
import com.oracle.graal.python.lib.PyObjectAsciiAsObjectNodeGen;
import com.oracle.graal.python.lib.PyObjectDelItem;
import com.oracle.graal.python.lib.PyObjectDelItemNodeGen;
import com.oracle.graal.python.lib.PyObjectGetAttr;
Expand Down Expand Up @@ -401,8 +401,8 @@ public final class PBytecodeRootNode extends PRootNode implements BytecodeOSRNod
private static final NodeSupplier<PyObjectStrAsObjectNode> NODE_STR = PyObjectStrAsObjectNode::create;
private static final PyObjectReprAsObjectNode UNCACHED_REPR = PyObjectReprAsObjectNode.getUncached();
private static final NodeSupplier<PyObjectReprAsObjectNode> NODE_REPR = PyObjectReprAsObjectNode::create;
private static final PyObjectAsciiNode UNCACHED_ASCII = PyObjectAsciiNode.getUncached();
private static final NodeSupplier<PyObjectAsciiNode> NODE_ASCII = PyObjectAsciiNode::create;
private static final PyObjectAsciiAsObjectNode UNCACHED_ASCII = PyObjectAsciiAsObjectNode.getUncached();
private static final NodeSupplier<PyObjectAsciiAsObjectNode> NODE_ASCII = PyObjectAsciiAsObjectNode::create;
private static final NodeSupplier<FormatNode> NODE_FORMAT = FormatNode::create;
private static final NodeSupplier<SendNode> NODE_SEND = SendNode::create;
private static final NodeSupplier<ThrowNode> NODE_THROW = ThrowNode::create;
Expand Down Expand Up @@ -4989,7 +4989,7 @@ private int bytecodeFormatValue(VirtualFrame virtualFrame, int initialStackTop,
value = insertChildNode(localNodes, bci, UNCACHED_REPR, PyObjectReprAsObjectNodeGen.class, NODE_REPR, useCachedNodes).executeCached(virtualFrame, value);
break;
case FormatOptions.FVC_ASCII:
value = insertChildNode(localNodes, bci, UNCACHED_ASCII, PyObjectAsciiNodeGen.class, NODE_ASCII, useCachedNodes).executeCached(virtualFrame, value);
value = insertChildNode(localNodes, bci, UNCACHED_ASCII, PyObjectAsciiAsObjectNodeGen.class, NODE_ASCII, useCachedNodes).executeCached(virtualFrame, value);
break;
default:
assert type == FormatOptions.FVC_NONE;
Expand Down
Loading
Loading