Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
apply petr's comment
  • Loading branch information
shihai1991 committed Aug 7, 2021
commit 4cfebb2078d9349d6627fcc6606587529c93259f
11 changes: 9 additions & 2 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1174,19 +1174,26 @@ test_get_type_qualname(PyObject *self, PyObject *Py_UNUSED(ignored))
if (HeapTypeNameType == NULL) {
Py_RETURN_NONE;
}
tp_qualname = PyType_GetQualName((PyTypeObject *)HeapTypeNameType);
assert(strcmp(PyUnicode_AsUTF8(tp_qualname), "HeapTypeNameType") == 0);
Py_DECREF(tp_qualname);

PyObject *spec_name = PyUnicode_FromString(HeapTypeNameType_Spec.name);
if (spec_name == NULL) {
goto done;
}
if (PyObject_SetAttrString(HeapTypeNameType,
"__qualname__", spec_name) < 0) {
Comment on lines +1185 to +1186

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an interesting test.
Would it make sense to test the original qualname first, then assign a different one and test that PyType_GetQualName returns the replacement?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha. I try to get some qual names in _testcapimodule. But I can get the qual name with prefix.
The reason is res->ht_qualname = res->ht_name; in https://github.com/python/cpython/blob/main/Objects/typeobject.c#L3439.
I don't know the reason why the qual name need to reomve the prefix too.
So I set a qual name with prefix to test this C API, somthing like a mock behavior~

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, C-API classes don't have distinct qualnames.
IMO, the best thing to do here would be:

  • Test the original qualname (same as the short name)
  • Change the qualname
  • Then test the new qualname.

Ideally, the same thing would be tested for PyType_GetName as well – it's not that different from this function.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, C-API classes don't have distinct qualnames.
IMO, the best thing to do here would be:

  • Test the original qualname (same as the short name)
  • Change the qualname
  • Then test the new qualname.

Make sense. How about this updated PR :)

Ideally, the same thing would be tested for PyType_GetName as well – it's not that different from this function.

Got it. I will update enhance this test case later.

Py_DECREF(spec_name);
Py_DECREF(HeapTypeNameType);
Py_RETURN_NONE;
goto done;
}
tp_qualname = PyType_GetQualName((PyTypeObject *)HeapTypeNameType);
assert(strcmp(PyUnicode_AsUTF8(tp_qualname),
"_testcapi.HeapTypeNameType") == 0);
Py_DECREF(spec_name);
Py_DECREF(tp_qualname);

done:
Py_DECREF(HeapTypeNameType);
Py_RETURN_NONE;
}
Expand Down