Skip to content

Commit

Permalink
bpo-42882: Add test_embed.test_unicode_id_init() (GH-24198)
Browse files Browse the repository at this point in the history
Test that _PyUnicode_FromId() works when Python is initialized
multiples times.
  • Loading branch information
vstinner committed Jan 12, 2021
1 parent 44bf57a commit 11d13e8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Lib/test/test_embed.py
Expand Up @@ -1472,5 +1472,11 @@ def test_audit_run_stdin(self):
timeout=support.SHORT_TIMEOUT,
returncode=1)

class MiscTests(EmbeddingTestsMixin, unittest.TestCase):
def test_unicode_id_init(self):
# bpo-42882: Test that _PyUnicode_FromId() works
# when Python is initialized multiples times.
self.run_embedded_interpreter("test_unicode_id_init")

if __name__ == "__main__":
unittest.main()
38 changes: 38 additions & 0 deletions Programs/_testembed.c
Expand Up @@ -1686,6 +1686,42 @@ static int test_get_argc_argv(void)
}


static int test_unicode_id_init(void)
{
// bpo-42882: Test that _PyUnicode_FromId() works
// when Python is initialized multiples times.
_Py_IDENTIFIER(test_unicode_id_init);

// Initialize Python once without using the identifier
_testembed_Py_Initialize();
Py_Finalize();

// Now initialize Python multiple times and use the identifier.
// The first _PyUnicode_FromId() call initializes the identifier index.
for (int i=0; i<3; i++) {
_testembed_Py_Initialize();

PyObject *str1, *str2;

str1 = _PyUnicode_FromId(&PyId_test_unicode_id_init);
assert(str1 != NULL);
assert(Py_REFCNT(str1) == 1);

str2 = PyUnicode_FromString("test_unicode_id_init");
assert(str2 != NULL);

assert(PyUnicode_Compare(str1, str2) == 0);

// str1 is a borrowed reference
Py_DECREF(str2);

Py_Finalize();
}
return 0;
}



/* *********************************************************
* List of test cases and the function that implements it.
*
Expand Down Expand Up @@ -1754,6 +1790,8 @@ static struct TestCase TestCases[] = {
{"test_audit_run_interactivehook", test_audit_run_interactivehook},
{"test_audit_run_startup", test_audit_run_startup},
{"test_audit_run_stdin", test_audit_run_stdin},

{"test_unicode_id_init", test_unicode_id_init},
{NULL, NULL}
};

Expand Down

0 comments on commit 11d13e8

Please sign in to comment.