Skip to content

[Bug]: ascii() does not preserve str subclass type for all-ASCII repr #686

@jseop-lim

Description

@jseop-lim

Describe the bug

ascii() returns a plain str instead of preserving the str subclass type when __repr__ returns an all-ASCII str subclass instance. This is a CPython compatibility issue.

Operating system

Linux

CPU architecture

ARM64

GraalPy version

25.0.2

JDK version

No response

Context configuration

No response

Steps to reproduce

class MyStr(str):
    pass

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

result = ascii(Foo())
print(result)
print(type(result))

GraalPy (Actual):

hello
<class 'str'>

CPython (Expected):

hello
<class '__main__.MyStr'>

Additional context

Root Cause

PyObjectAsciiNode.java#L77-L87 uses PyObjectReprAsTruffleStringNode, which converts the repr result to a raw TruffleString via CastToTruffleStringNode, discarding the PString subclass wrapper. When the repr is all-ASCII, the node returns that bare TruffleString — a plain str — instead of the original object.

CPython reference: Objects/object.c#L514-L523PyObject_ASCII does if (PyUnicode_IS_ASCII(repr)) return repr, preserving the str subclass type.

Fix Suggestion

Use PyObjectReprAsObjectNode instead of PyObjectReprAsTruffleStringNode and return the original repr object on the all-ASCII fast path to preserve the str subclass type.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions