From d54baf2049a71956b26c45e67fe31bf4601517e8 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Tue, 4 Jun 2024 15:26:06 +0200 Subject: [PATCH] fix missing to exit critical section --- Include/internal/pycore_critical_section.h | 4 ++++ Lib/test/test_json/test_dump.py | 4 +++- Modules/_json.c | 4 +++- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Include/internal/pycore_critical_section.h b/Include/internal/pycore_critical_section.h index a11af1bf0c0d8f..adebcae7e24076 100644 --- a/Include/internal/pycore_critical_section.h +++ b/Include/internal/pycore_critical_section.h @@ -99,6 +99,9 @@ extern "C" { _PyCriticalSection_End(&_cs); \ } +# define Py_EXIT_CRITICAL_SECTION() \ + _PyCriticalSection_End(&_cs); + # define Py_BEGIN_CRITICAL_SECTION2(a, b) \ { \ _PyCriticalSection2 _cs2; \ @@ -160,6 +163,7 @@ extern "C" { # define Py_BEGIN_CRITICAL_SECTION_MUT(mut) # define Py_BEGIN_CRITICAL_SECTION(op) # define Py_END_CRITICAL_SECTION() +# define Py_EXIT_CRITICAL_SECTION() # define Py_BEGIN_CRITICAL_SECTION2(a, b) # define Py_END_CRITICAL_SECTION2() # define Py_BEGIN_CRITICAL_SECTION_SEQUENCE_FAST(original) diff --git a/Lib/test/test_json/test_dump.py b/Lib/test/test_json/test_dump.py index 13b40020781bae..fc052dfa37afb9 100644 --- a/Lib/test/test_json/test_dump.py +++ b/Lib/test/test_json/test_dump.py @@ -12,11 +12,13 @@ def test_dump(self): def test_dumps(self): self.assertEqual(self.dumps({}), '{}') - def test_dump_skipkeys(self): + def test_dump_skipkeys_invalid(self): v = {b'invalid_key': False, 'valid_key': True} with self.assertRaises(TypeError): self.json.dumps(v) + def test_dump_skipkeys(self): + v = {b'invalid_key': False, 'valid_key': True} s = self.json.dumps(v, skipkeys=True) o = self.json.loads(s) self.assertIn('valid_key', o) diff --git a/Modules/_json.c b/Modules/_json.c index d2b308eca36d3f..be8224e042d18a 100644 --- a/Modules/_json.c +++ b/Modules/_json.c @@ -1620,8 +1620,10 @@ encoder_listencode_dict(PyEncoderObject *s, _PyUnicodeWriter *writer, while (PyDict_Next(dct, &pos, &key, &value)) { if (encoder_encode_key_value(s, writer, &first, key, value, new_newline_indent, - current_item_separator) < 0) + current_item_separator) < 0) { + Py_EXIT_CRITICAL_SECTION(); goto bail; + } } Py_END_CRITICAL_SECTION(); }