@@ -1686,6 +1686,42 @@ static int test_get_argc_argv(void)
1686
1686
}
1687
1687
1688
1688
1689
+ static int test_unicode_id_init (void )
1690
+ {
1691
+ // bpo-42882: Test that _PyUnicode_FromId() works
1692
+ // when Python is initialized multiples times.
1693
+ _Py_IDENTIFIER (test_unicode_id_init );
1694
+
1695
+ // Initialize Python once without using the identifier
1696
+ _testembed_Py_Initialize ();
1697
+ Py_Finalize ();
1698
+
1699
+ // Now initialize Python multiple times and use the identifier.
1700
+ // The first _PyUnicode_FromId() call initializes the identifier index.
1701
+ for (int i = 0 ; i < 3 ; i ++ ) {
1702
+ _testembed_Py_Initialize ();
1703
+
1704
+ PyObject * str1 , * str2 ;
1705
+
1706
+ str1 = _PyUnicode_FromId (& PyId_test_unicode_id_init );
1707
+ assert (str1 != NULL );
1708
+ assert (Py_REFCNT (str1 ) == 1 );
1709
+
1710
+ str2 = PyUnicode_FromString ("test_unicode_id_init" );
1711
+ assert (str2 != NULL );
1712
+
1713
+ assert (PyUnicode_Compare (str1 , str2 ) == 0 );
1714
+
1715
+ // str1 is a borrowed reference
1716
+ Py_DECREF (str2 );
1717
+
1718
+ Py_Finalize ();
1719
+ }
1720
+ return 0 ;
1721
+ }
1722
+
1723
+
1724
+
1689
1725
/* *********************************************************
1690
1726
* List of test cases and the function that implements it.
1691
1727
*
@@ -1754,6 +1790,8 @@ static struct TestCase TestCases[] = {
1754
1790
{"test_audit_run_interactivehook" , test_audit_run_interactivehook },
1755
1791
{"test_audit_run_startup" , test_audit_run_startup },
1756
1792
{"test_audit_run_stdin" , test_audit_run_stdin },
1793
+
1794
+ {"test_unicode_id_init" , test_unicode_id_init },
1757
1795
{NULL , NULL }
1758
1796
};
1759
1797
0 commit comments