From eccea99621f31b8e52cb5ed75820a5e9c5350ece Mon Sep 17 00:00:00 2001 From: Yuriy Chernyshov Date: Thu, 25 Feb 2021 16:58:06 +0300 Subject: [PATCH 1/2] Fix some constness / char literal issues being found by MSVC standard conforming mode --- python/google/protobuf/pyext/descriptor_pool.cc | 4 ++-- python/google/protobuf/pyext/map_container.cc | 8 ++++---- python/google/protobuf/pyext/message.cc | 10 +++++----- python/google/protobuf/pyext/message.h | 2 +- python/google/protobuf/pyext/message_factory.cc | 4 ++-- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/python/google/protobuf/pyext/descriptor_pool.cc b/python/google/protobuf/pyext/descriptor_pool.cc index 6f1464e3212d..dda018624556 100644 --- a/python/google/protobuf/pyext/descriptor_pool.cc +++ b/python/google/protobuf/pyext/descriptor_pool.cc @@ -176,9 +176,9 @@ static PyDescriptorPool* PyDescriptorPool_NewWithDatabase( // The public DescriptorPool constructor. static PyObject* New(PyTypeObject* type, PyObject* args, PyObject* kwargs) { - static char* kwlist[] = {"descriptor_db", 0}; + static const char* kwlist[] = {"descriptor_db", 0}; PyObject* py_database = NULL; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, &py_database)) { + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", (char**)kwlist, &py_database)) { return NULL; } DescriptorDatabase* database = NULL; diff --git a/python/google/protobuf/pyext/map_container.cc b/python/google/protobuf/pyext/map_container.cc index a0ee16fe8634..9abd292c2d94 100644 --- a/python/google/protobuf/pyext/map_container.cc +++ b/python/google/protobuf/pyext/map_container.cc @@ -462,10 +462,10 @@ int MapReflectionFriend::ScalarMapSetItem(PyObject* _self, PyObject* key, static PyObject* ScalarMapGet(PyObject* self, PyObject* args, PyObject* kwargs) { - static char* kwlist[] = {"key", "default", nullptr}; + static const char* kwlist[] = {"key", "default", nullptr}; PyObject* key; PyObject* default_value = NULL; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O", kwlist, &key, + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O", (char**)kwlist, &key, &default_value)) { return NULL; } @@ -757,10 +757,10 @@ PyObject* MapReflectionFriend::MessageMapToStr(PyObject* _self) { } PyObject* MessageMapGet(PyObject* self, PyObject* args, PyObject* kwargs) { - static char* kwlist[] = {"key", "default", nullptr}; + static const char* kwlist[] = {"key", "default", nullptr}; PyObject* key; PyObject* default_value = NULL; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O", kwlist, &key, + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O", (char**)kwlist, &key, &default_value)) { return NULL; } diff --git a/python/google/protobuf/pyext/message.cc b/python/google/protobuf/pyext/message.cc index 4e74386e2d1b..bd493c969763 100644 --- a/python/google/protobuf/pyext/message.cc +++ b/python/google/protobuf/pyext/message.cc @@ -196,12 +196,12 @@ static int AddDescriptors(PyObject* cls, const Descriptor* descriptor) { } static PyObject* New(PyTypeObject* type, PyObject* args, PyObject* kwargs) { - static char *kwlist[] = {"name", "bases", "dict", 0}; + static const char *kwlist[] = {"name", "bases", "dict", 0}; PyObject *bases, *dict; const char* name; // Check arguments: (name, bases, dict) - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sO!O!:type", kwlist, + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sO!O!:type", (char**)kwlist, &name, &PyTuple_Type, &bases, &PyDict_Type, &dict)) { @@ -546,7 +546,7 @@ PyObject* PickleError_class; // Format an error message for unexpected types. // Always return with an exception set. -void FormatTypeError(PyObject* arg, char* expected_types) { +void FormatTypeError(PyObject* arg, const char* expected_types) { // This function is often called with an exception set. // Clear it to call PyObject_Repr() in good conditions. PyErr_Clear(); @@ -1679,9 +1679,9 @@ static PyObject* InternalSerializeToString( CMessage* self, PyObject* args, PyObject* kwargs, bool require_initialized) { // Parse the "deterministic" kwarg; defaults to False. - static char* kwlist[] = { "deterministic", 0 }; + static const char* kwlist[] = { "deterministic", 0 }; PyObject* deterministic_obj = Py_None; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", (char**)kwlist, &deterministic_obj)) { return NULL; } diff --git a/python/google/protobuf/pyext/message.h b/python/google/protobuf/pyext/message.h index a1e832651237..96e06e5a3d66 100644 --- a/python/google/protobuf/pyext/message.h +++ b/python/google/protobuf/pyext/message.h @@ -329,7 +329,7 @@ PyObject* SetAllowOversizeProtos(PyObject* m, PyObject* arg); #define FULL_MODULE_NAME "google.protobuf.pyext._message" -void FormatTypeError(PyObject* arg, char* expected_types); +void FormatTypeError(PyObject* arg, const char* expected_types); template bool CheckAndGetInteger(PyObject* arg, T* value); bool CheckAndGetDouble(PyObject* arg, double* value); diff --git a/python/google/protobuf/pyext/message_factory.cc b/python/google/protobuf/pyext/message_factory.cc index 5fed13b9430b..8859906148a2 100644 --- a/python/google/protobuf/pyext/message_factory.cc +++ b/python/google/protobuf/pyext/message_factory.cc @@ -77,9 +77,9 @@ PyMessageFactory* NewMessageFactory(PyTypeObject* type, PyDescriptorPool* pool) } PyObject* New(PyTypeObject* type, PyObject* args, PyObject* kwargs) { - static char* kwlist[] = {"pool", 0}; + static const char* kwlist[] = {"pool", 0}; PyObject* pool = NULL; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, &pool)) { + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", (char**)kwlist, &pool)) { return NULL; } ScopedPyObjectPtr owned_pool; From 9230978c35e1ca98b840ad0a628a03fde7b52534 Mon Sep 17 00:00:00 2001 From: Yuriy Chernyshov Date: Thu, 25 Feb 2021 22:08:58 +0300 Subject: [PATCH 2/2] Switch to const_cast from C-style casts --- python/google/protobuf/pyext/descriptor_pool.cc | 2 +- python/google/protobuf/pyext/map_container.cc | 4 ++-- python/google/protobuf/pyext/message.cc | 4 ++-- python/google/protobuf/pyext/message_factory.cc | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/python/google/protobuf/pyext/descriptor_pool.cc b/python/google/protobuf/pyext/descriptor_pool.cc index dda018624556..a24d45d83418 100644 --- a/python/google/protobuf/pyext/descriptor_pool.cc +++ b/python/google/protobuf/pyext/descriptor_pool.cc @@ -178,7 +178,7 @@ static PyObject* New(PyTypeObject* type, PyObject* args, PyObject* kwargs) { static const char* kwlist[] = {"descriptor_db", 0}; PyObject* py_database = NULL; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", (char**)kwlist, &py_database)) { + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", const_cast(kwlist), &py_database)) { return NULL; } DescriptorDatabase* database = NULL; diff --git a/python/google/protobuf/pyext/map_container.cc b/python/google/protobuf/pyext/map_container.cc index 9abd292c2d94..711c30472c55 100644 --- a/python/google/protobuf/pyext/map_container.cc +++ b/python/google/protobuf/pyext/map_container.cc @@ -465,7 +465,7 @@ static PyObject* ScalarMapGet(PyObject* self, PyObject* args, static const char* kwlist[] = {"key", "default", nullptr}; PyObject* key; PyObject* default_value = NULL; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O", (char**)kwlist, &key, + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O", const_cast(kwlist), &key, &default_value)) { return NULL; } @@ -760,7 +760,7 @@ PyObject* MessageMapGet(PyObject* self, PyObject* args, PyObject* kwargs) { static const char* kwlist[] = {"key", "default", nullptr}; PyObject* key; PyObject* default_value = NULL; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O", (char**)kwlist, &key, + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O", const_cast(kwlist), &key, &default_value)) { return NULL; } diff --git a/python/google/protobuf/pyext/message.cc b/python/google/protobuf/pyext/message.cc index bd493c969763..e8e1a17b9760 100644 --- a/python/google/protobuf/pyext/message.cc +++ b/python/google/protobuf/pyext/message.cc @@ -201,7 +201,7 @@ static PyObject* New(PyTypeObject* type, PyObject* args, PyObject* kwargs) { const char* name; // Check arguments: (name, bases, dict) - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sO!O!:type", (char**)kwlist, + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sO!O!:type", const_cast(kwlist), &name, &PyTuple_Type, &bases, &PyDict_Type, &dict)) { @@ -1681,7 +1681,7 @@ static PyObject* InternalSerializeToString( // Parse the "deterministic" kwarg; defaults to False. static const char* kwlist[] = { "deterministic", 0 }; PyObject* deterministic_obj = Py_None; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", (char**)kwlist, + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", const_cast(kwlist), &deterministic_obj)) { return NULL; } diff --git a/python/google/protobuf/pyext/message_factory.cc b/python/google/protobuf/pyext/message_factory.cc index 8859906148a2..5a10c3dc2466 100644 --- a/python/google/protobuf/pyext/message_factory.cc +++ b/python/google/protobuf/pyext/message_factory.cc @@ -79,7 +79,7 @@ PyMessageFactory* NewMessageFactory(PyTypeObject* type, PyDescriptorPool* pool) PyObject* New(PyTypeObject* type, PyObject* args, PyObject* kwargs) { static const char* kwlist[] = {"pool", 0}; PyObject* pool = NULL; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", (char**)kwlist, &pool)) { + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", const_cast(kwlist), &pool)) { return NULL; } ScopedPyObjectPtr owned_pool;