From 4838ac6211162b37ee45d1abb6303b492a66f58b Mon Sep 17 00:00:00 2001 From: Jesse Clemens Date: Fri, 22 Jan 2021 13:40:41 -0500 Subject: [PATCH 1/4] Force the builtin module key to be the correct type. Previously it was always going to be a std::string which converted into unicode. Python 2 appears to want module keys to be normal str types, so this was breaking code that expected plain string types in the builtins.keys() data structure --- include/pybind11/detail/internals.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/pybind11/detail/internals.h b/include/pybind11/detail/internals.h index 812505c223..75fcd3c208 100644 --- a/include/pybind11/detail/internals.h +++ b/include/pybind11/detail/internals.h @@ -266,7 +266,7 @@ PYBIND11_NOINLINE inline internals &get_internals() { const PyGILState_STATE state; } gil; - constexpr auto *id = PYBIND11_INTERNALS_ID; + PYBIND11_STR_TYPE id(PYBIND11_INTERNALS_ID); auto builtins = handle(PyEval_GetBuiltins()); if (builtins.contains(id) && isinstance(builtins[id])) { internals_pp = static_cast(capsule(builtins[id])); From cb1fdd393d2a7b1c8f27f68b06ec82d58886e424 Mon Sep 17 00:00:00 2001 From: Jesse Clemens Date: Fri, 22 Jan 2021 17:03:04 -0500 Subject: [PATCH 2/4] Add a simple unit test to ensure all built-in keys are str --- tests/test_modules.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/test_modules.py b/tests/test_modules.py index 5630ccf9b3..20d60417b8 100644 --- a/tests/test_modules.py +++ b/tests/test_modules.py @@ -75,3 +75,11 @@ def test_duplicate_registration(): """Registering two things with the same name""" assert m.duplicate_registration() == [] + + +def test_builtinsKeyType(): + """Test that all the keys in the builtin modules have type str. + + Previous versions of pybind11 would add a unicode key in python 2. + """ + assert {type(k) for k in __builtins__.keys()} == {str} \ No newline at end of file From 6d1aaba7cb7bb30ed9419ebe19f255946023297d Mon Sep 17 00:00:00 2001 From: Jesse Clemens Date: Fri, 22 Jan 2021 17:56:47 -0500 Subject: [PATCH 3/4] Update the unit test so it will also run on pypy --- tests/test_modules.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/test_modules.py b/tests/test_modules.py index 20d60417b8..a7163efdc4 100644 --- a/tests/test_modules.py +++ b/tests/test_modules.py @@ -82,4 +82,9 @@ def test_builtinsKeyType(): Previous versions of pybind11 would add a unicode key in python 2. """ - assert {type(k) for k in __builtins__.keys()} == {str} \ No newline at end of file + if hasattr(__builtins__, 'keys'): + keys = __builtins__.keys() + else: # this is to make pypy happy since builtins is different there. + keys = __builtins__.__dict__.keys() + + assert {type(k) for k in keys} == {str} From 871c6cabbe990c94d1db196339ab919ef9302cbf Mon Sep 17 00:00:00 2001 From: Jesse Clemens Date: Fri, 22 Jan 2021 18:02:13 -0500 Subject: [PATCH 4/4] Run pre-commit. --- tests/test_modules.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_modules.py b/tests/test_modules.py index a7163efdc4..3390031aff 100644 --- a/tests/test_modules.py +++ b/tests/test_modules.py @@ -77,14 +77,14 @@ def test_duplicate_registration(): assert m.duplicate_registration() == [] -def test_builtinsKeyType(): +def test_builtin_key_type(): """Test that all the keys in the builtin modules have type str. Previous versions of pybind11 would add a unicode key in python 2. """ - if hasattr(__builtins__, 'keys'): + if hasattr(__builtins__, "keys"): keys = __builtins__.keys() - else: # this is to make pypy happy since builtins is different there. + else: # this is to make pypy happy since builtins is different there. keys = __builtins__.__dict__.keys() assert {type(k) for k in keys} == {str}