Skip to content

Commit 922a7ec

Browse files
committed
Fix Python 3.10 C++ tests (#9128)
The first change is to make sure we always define PY_SSIZE_T_CLEAN before including Python.h. Starting from Python 3.10 this is required. Otherwise we get errors like this: SystemError: PY_SSIZE_T_CLEAN macro must be defined for '#' formats The second change is to update reflection_test.py to account for the fact that with Python 3.10, we get a TypeError even with the C++ implementation when trying to assign a float to a bool field. I'm not sure why this changed with Python 3.10, but it seems like a good thing since this is the desired behavior anyway.
1 parent cb46755 commit 922a7ec

23 files changed

+25
-1
lines changed

python/google/protobuf/internal/api_implementation.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2929
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3030

31+
#define PY_SSIZE_T_CLEAN
3132
#include <Python.h>
3233

3334
namespace google {

python/google/protobuf/internal/reflection_test.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import gc
3838
import operator
3939
import struct
40+
import sys
4041
import warnings
4142
import unittest
4243

@@ -376,7 +377,8 @@ def testSingleScalarTypeSafety(self, message_module):
376377
self.assertRaises(TypeError, setattr, proto, 'optional_float', 'foo')
377378
self.assertRaises(TypeError, setattr, proto, 'optional_double', 'foo')
378379
# TODO(jieluo): Fix type checking difference for python and c extension
379-
if api_implementation.Type() == 'python':
380+
if (api_implementation.Type() == 'python' or
381+
(sys.version_info.major, sys.version_info.minor) >= (3, 10)):
380382
self.assertRaises(TypeError, setattr, proto, 'optional_bool', 1.1)
381383
else:
382384
proto.optional_bool = 1.1

python/google/protobuf/proto_api.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#ifndef GOOGLE_PROTOBUF_PYTHON_PROTO_API_H__
4646
#define GOOGLE_PROTOBUF_PYTHON_PROTO_API_H__
4747

48+
#define PY_SSIZE_T_CLEAN
4849
#include <Python.h>
4950

5051
#include <google/protobuf/descriptor_database.h>

python/google/protobuf/pyext/descriptor.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
#include <google/protobuf/pyext/descriptor.h>
3434

35+
#define PY_SSIZE_T_CLEAN
3536
#include <Python.h>
3637
#include <frameobject.h>
3738

python/google/protobuf/pyext/descriptor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#ifndef GOOGLE_PROTOBUF_PYTHON_CPP_DESCRIPTOR_H__
3434
#define GOOGLE_PROTOBUF_PYTHON_CPP_DESCRIPTOR_H__
3535

36+
#define PY_SSIZE_T_CLEAN
3637
#include <Python.h>
3738

3839
#include <google/protobuf/descriptor.h>

python/google/protobuf/pyext/descriptor_containers.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
// because the Python API is based on C, and does not play well with C++
5050
// inheritance.
5151

52+
#define PY_SSIZE_T_CLEAN
5253
#include <Python.h>
5354

5455
#include <google/protobuf/descriptor.h>

python/google/protobuf/pyext/descriptor_containers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
// Mappings and Sequences of descriptors.
3535
// They implement containers like fields_by_name, EnumDescriptor.values...
3636
// See descriptor_containers.cc for more description.
37+
#define PY_SSIZE_T_CLEAN
3738
#include <Python.h>
3839

3940
namespace google {

python/google/protobuf/pyext/descriptor_database.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#ifndef GOOGLE_PROTOBUF_PYTHON_CPP_DESCRIPTOR_DATABASE_H__
3232
#define GOOGLE_PROTOBUF_PYTHON_CPP_DESCRIPTOR_DATABASE_H__
3333

34+
#define PY_SSIZE_T_CLEAN
3435
#include <Python.h>
3536

3637
#include <google/protobuf/descriptor_database.h>

python/google/protobuf/pyext/descriptor_pool.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
#include <unordered_map>
3434

35+
#define PY_SSIZE_T_CLEAN
3536
#include <Python.h>
3637

3738
#include <google/protobuf/descriptor.pb.h>

python/google/protobuf/pyext/descriptor_pool.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#ifndef GOOGLE_PROTOBUF_PYTHON_CPP_DESCRIPTOR_POOL_H__
3232
#define GOOGLE_PROTOBUF_PYTHON_CPP_DESCRIPTOR_POOL_H__
3333

34+
#define PY_SSIZE_T_CLEAN
3435
#include <Python.h>
3536

3637
#include <unordered_map>

python/google/protobuf/pyext/extension_dict.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#ifndef GOOGLE_PROTOBUF_PYTHON_CPP_EXTENSION_DICT_H__
3535
#define GOOGLE_PROTOBUF_PYTHON_CPP_EXTENSION_DICT_H__
3636

37+
#define PY_SSIZE_T_CLEAN
3738
#include <Python.h>
3839

3940
#include <memory>

python/google/protobuf/pyext/field.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#ifndef GOOGLE_PROTOBUF_PYTHON_CPP_FIELD_H__
3232
#define GOOGLE_PROTOBUF_PYTHON_CPP_FIELD_H__
3333

34+
#define PY_SSIZE_T_CLEAN
3435
#include <Python.h>
3536

3637
namespace google {

python/google/protobuf/pyext/map_container.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#ifndef GOOGLE_PROTOBUF_PYTHON_CPP_MAP_CONTAINER_H__
3232
#define GOOGLE_PROTOBUF_PYTHON_CPP_MAP_CONTAINER_H__
3333

34+
#define PY_SSIZE_T_CLEAN
3435
#include <Python.h>
3536

3637
#include <cstdint>

python/google/protobuf/pyext/message.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#ifndef GOOGLE_PROTOBUF_PYTHON_CPP_MESSAGE_H__
3535
#define GOOGLE_PROTOBUF_PYTHON_CPP_MESSAGE_H__
3636

37+
#define PY_SSIZE_T_CLEAN
3738
#include <Python.h>
3839

3940
#include <cstdint>

python/google/protobuf/pyext/message_factory.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
#include <unordered_map>
3232

33+
#define PY_SSIZE_T_CLEAN
3334
#include <Python.h>
3435

3536
#include <google/protobuf/dynamic_message.h>

python/google/protobuf/pyext/message_factory.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#ifndef GOOGLE_PROTOBUF_PYTHON_CPP_MESSAGE_FACTORY_H__
3232
#define GOOGLE_PROTOBUF_PYTHON_CPP_MESSAGE_FACTORY_H__
3333

34+
#define PY_SSIZE_T_CLEAN
3435
#include <Python.h>
3536

3637
#include <unordered_map>

python/google/protobuf/pyext/message_module.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2929
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3030

31+
#define PY_SSIZE_T_CLEAN
3132
#include <Python.h>
3233

3334
#include <google/protobuf/message_lite.h>

python/google/protobuf/pyext/repeated_composite_container.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#ifndef GOOGLE_PROTOBUF_PYTHON_CPP_REPEATED_COMPOSITE_CONTAINER_H__
3535
#define GOOGLE_PROTOBUF_PYTHON_CPP_REPEATED_COMPOSITE_CONTAINER_H__
3636

37+
#define PY_SSIZE_T_CLEAN
3738
#include <Python.h>
3839

3940
#include <memory>

python/google/protobuf/pyext/repeated_scalar_container.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#ifndef GOOGLE_PROTOBUF_PYTHON_CPP_REPEATED_SCALAR_CONTAINER_H__
3535
#define GOOGLE_PROTOBUF_PYTHON_CPP_REPEATED_SCALAR_CONTAINER_H__
3636

37+
#define PY_SSIZE_T_CLEAN
3738
#include <Python.h>
3839

3940
#include <memory>

python/google/protobuf/pyext/scoped_pyobject_ptr.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
#include <google/protobuf/stubs/common.h>
3737

38+
#define PY_SSIZE_T_CLEAN
3839
#include <Python.h>
3940
namespace google {
4041
namespace protobuf {

python/google/protobuf/pyext/unknown_fields.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
#include <google/protobuf/pyext/unknown_fields.h>
3232

33+
#define PY_SSIZE_T_CLEAN
3334
#include <Python.h>
3435
#include <set>
3536
#include <memory>

python/google/protobuf/pyext/unknown_fields.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#ifndef GOOGLE_PROTOBUF_PYTHON_CPP_UNKNOWN_FIELDS_H__
3232
#define GOOGLE_PROTOBUF_PYTHON_CPP_UNKNOWN_FIELDS_H__
3333

34+
#define PY_SSIZE_T_CLEAN
3435
#include <Python.h>
3536

3637
#include <memory>

python/google/protobuf/python_protobuf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#ifndef GOOGLE_PROTOBUF_PYTHON_PYTHON_PROTOBUF_H__
3737
#define GOOGLE_PROTOBUF_PYTHON_PYTHON_PROTOBUF_H__
3838

39+
#define PY_SSIZE_T_CLEAN
3940
#include <Python.h>
4041

4142
namespace google {

0 commit comments

Comments
 (0)