Skip to content

Commit 11d13e8

Browse files
authored
bpo-42882: Add test_embed.test_unicode_id_init() (GH-24198)
Test that _PyUnicode_FromId() works when Python is initialized multiples times.
1 parent 44bf57a commit 11d13e8

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

Lib/test/test_embed.py

+6
Original file line numberDiff line numberDiff line change
@@ -1472,5 +1472,11 @@ def test_audit_run_stdin(self):
14721472
timeout=support.SHORT_TIMEOUT,
14731473
returncode=1)
14741474

1475+
class MiscTests(EmbeddingTestsMixin, unittest.TestCase):
1476+
def test_unicode_id_init(self):
1477+
# bpo-42882: Test that _PyUnicode_FromId() works
1478+
# when Python is initialized multiples times.
1479+
self.run_embedded_interpreter("test_unicode_id_init")
1480+
14751481
if __name__ == "__main__":
14761482
unittest.main()

Programs/_testembed.c

+38
Original file line numberDiff line numberDiff line change
@@ -1686,6 +1686,42 @@ static int test_get_argc_argv(void)
16861686
}
16871687

16881688

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+
16891725
/* *********************************************************
16901726
* List of test cases and the function that implements it.
16911727
*
@@ -1754,6 +1790,8 @@ static struct TestCase TestCases[] = {
17541790
{"test_audit_run_interactivehook", test_audit_run_interactivehook},
17551791
{"test_audit_run_startup", test_audit_run_startup},
17561792
{"test_audit_run_stdin", test_audit_run_stdin},
1793+
1794+
{"test_unicode_id_init", test_unicode_id_init},
17571795
{NULL, NULL}
17581796
};
17591797

0 commit comments

Comments
 (0)