@@ -227,6 +227,55 @@ unicode_GET_CACHED_HASH(PyObject *self, PyObject *arg)
227227}
228228
229229
230+ // Test the deprecated _Py_Identifier C API:
231+ // - _Py_IDENTIFIER()
232+ // - _Py_static_string()
233+ // - _Py_static_string_init()
234+ // - _PyObject_CallMethodId()
235+ // - _PyObject_GetAttrId()
236+ // - _PyUnicode_FromId()
237+ //
238+ // _testembed also has tests on _PyUnicode_FromId().
239+ static PyObject *
240+ test_py_identifier (PyObject * self , PyObject * Py_UNUSED (args ))
241+ {
242+ // Ignore deprecation warnings
243+ _Py_COMP_DIAG_PUSH
244+ _Py_COMP_DIAG_IGNORE_DEPR_DECLS
245+
246+ _Py_IDENTIFIER (hello );
247+ PyObject * str = _PyUnicode_FromId (& PyId_hello ); // borrowed ref
248+ if (str == NULL ) {
249+ return NULL ;
250+ }
251+ assert (PyUnicode_EqualToUTF8 (str , "hello" ) == 1 );
252+
253+ // Calling twice return the same object
254+ PyObject * str2 = _PyUnicode_FromId (& PyId_hello ); // borrowed ref
255+ assert (str2 == str );
256+
257+ PyObject * number = Py_GetConstant (Py_CONSTANT_ONE ); // immortal
258+ _Py_static_string (to_bytes_id , "to_bytes" );
259+ PyObject * res = _PyObject_CallMethodId (number , & to_bytes_id , NULL );
260+ if (res == NULL ) {
261+ return NULL ;
262+ }
263+ Py_DECREF (res );
264+
265+ static _Py_Identifier real_id = _Py_static_string_init ("real" );
266+ res = _PyObject_GetAttrId (number , & real_id );
267+ if (res == NULL ) {
268+ return NULL ;
269+ }
270+ assert (res == number );
271+ Py_DECREF (res );
272+
273+ Py_RETURN_NONE ;
274+
275+ _Py_COMP_DIAG_POP
276+ }
277+
278+
230279// --- PyUnicodeWriter type -------------------------------------------------
231280
232281typedef struct {
@@ -572,6 +621,7 @@ static PyMethodDef TestMethods[] = {
572621 {"unicode_asutf8" , unicode_asutf8 , METH_VARARGS },
573622 {"unicode_copycharacters" , unicode_copycharacters , METH_VARARGS },
574623 {"unicode_GET_CACHED_HASH" , unicode_GET_CACHED_HASH , METH_O },
624+ {"test_py_identifier" , test_py_identifier , METH_NOARGS },
575625 {NULL },
576626};
577627
0 commit comments