Skip to content

Commit d9fd329

Browse files
steve-stimfel
authored andcommitted
HPy: fix HPyType_GetName for types with module in the name
1 parent 6c8bd16 commit d9fd329

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

graalpython/com.oracle.graal.python.test/src/tests/cpyext/test_object.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,22 @@ def test_slots(self):
289289
assert tester.year == 1, "year was %s "% tester.year
290290
assert tester.is_binary_compatible()
291291

292+
def test_tp_name(self):
293+
TestTpName = CPyExtType("TestTpName",
294+
'''
295+
static PyObject* testslots_tp_name(PyObject* self, PyObject *cls) {
296+
return PyUnicode_FromString(((PyTypeObject*)cls)->tp_name);
297+
}
298+
''',
299+
tp_methods='{"get_tp_name", (PyCFunction)testslots_tp_name, METH_O, ""}',
300+
)
301+
tester = TestTpName()
302+
class MyClass:
303+
pass
304+
assert tester.get_tp_name(MyClass) == 'MyClass'
305+
assert tester.get_tp_name(int) == 'int'
306+
assert tester.get_tp_name(type(tester)) == 'TestTpName.TestTpName'
307+
292308
def test_slots_initialized(self):
293309
TestSlotsInitialized = CPyExtType("TestSlotsInitialized",
294310
'''

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/hpy/GraalHPyContextFunctions.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3336,6 +3336,8 @@ Object execute(Object[] arguments,
33363336
@Cached HPyAsContextNode asContextNode,
33373337
@Cached HPyAsPythonObjectNode asType,
33383338
@Cached GetNameNode getName,
3339+
@Cached ReadAttributeFromObjectNode readHPyFlagsNode,
3340+
@Cached ReadAttributeFromObjectNode readModuleNameNode,
33393341
@Cached EncodeNativeStringNode encodeNativeStringNode,
33403342
@Cached GilNode gil) throws ArityException {
33413343
checkArity(arguments, 2);
@@ -3344,6 +3346,15 @@ Object execute(Object[] arguments,
33443346
GraalHPyContext context = asContextNode.execute(arguments[0]);
33453347
Object type = asType.execute(context, arguments[1]);
33463348
String name = getName.execute(type);
3349+
if (readHPyFlagsNode.execute(type, GraalHPyDef.TYPE_HPY_FLAGS) != PNone.NO_VALUE) {
3350+
// Types that originated from HPy: although they are ordinary managed
3351+
// PythonClasses, the name should have "cext semantics", i.e., contain the
3352+
// module if it was specified in the HPyType_Spec
3353+
Object moduleName = readModuleNameNode.execute(type, SpecialAttributeNames.__MODULE__);
3354+
if (moduleName instanceof String) {
3355+
name = moduleName + "." + name;
3356+
}
3357+
}
33473358
byte[] result = encodeNativeStringNode.execute(StandardCharsets.UTF_8, name, CodecsModuleBuiltins.STRICT);
33483359
return new CByteArrayWrapper(result);
33493360
} finally {

0 commit comments

Comments
 (0)