@@ -1124,14 +1124,16 @@ int InitAttributes(CMessage* self, PyObject* args, PyObject* kwargs) {
1124
1124
Descriptor::WELLKNOWNTYPE_STRUCT) {
1125
1125
ScopedPyObjectPtr ok (PyObject_CallMethod (
1126
1126
reinterpret_cast <PyObject*>(cmessage), " update" , " O" , value));
1127
- if (ok.get () == nullptr && PyDict_Size (value) == 1 &&
1128
- PyDict_Contains (value, PyUnicode_FromString (" fields" ))) {
1129
- // Fallback to init as normal message field.
1130
- PyErr_Clear ();
1131
- PyObject* tmp = Clear (cmessage);
1132
- Py_DECREF (tmp);
1133
- if (InitAttributes (cmessage, nullptr , value) < 0 ) {
1134
- return -1 ;
1127
+ if (ok.get () == nullptr && PyDict_Size (value) == 1 ) {
1128
+ ScopedPyObjectPtr fields_str (PyUnicode_FromString (" fields" ));
1129
+ if (PyDict_Contains (value, fields_str.get ())) {
1130
+ // Fallback to init as normal message field.
1131
+ PyErr_Clear ();
1132
+ PyObject* tmp = Clear (cmessage);
1133
+ Py_DECREF (tmp);
1134
+ if (InitAttributes (cmessage, nullptr , value) < 0 ) {
1135
+ return -1 ;
1136
+ }
1135
1137
}
1136
1138
}
1137
1139
} else {
@@ -2391,21 +2393,19 @@ PyObject* Contains(CMessage* self, PyObject* arg) {
2391
2393
const Reflection* reflection = message->GetReflection ();
2392
2394
const FieldDescriptor* map_field = descriptor->FindFieldByName (" fields" );
2393
2395
const FieldDescriptor* key_field = map_field->message_type ()->map_key ();
2394
- PyObject* py_string = CheckString (arg, key_field);
2395
- if (! py_string) {
2396
+ ScopedPyObjectPtr py_string ( CheckString (arg, key_field) );
2397
+ if (py_string. get () == nullptr ) {
2396
2398
PyErr_SetString (PyExc_TypeError,
2397
2399
" The key passed to Struct message must be a str." );
2398
2400
return nullptr ;
2399
2401
}
2400
2402
char * value;
2401
2403
Py_ssize_t value_len;
2402
- if (PyBytes_AsStringAndSize (py_string, &value, &value_len) < 0 ) {
2403
- Py_DECREF (py_string);
2404
+ if (PyBytes_AsStringAndSize (py_string.get (), &value, &value_len) < 0 ) {
2404
2405
Py_RETURN_FALSE;
2405
2406
}
2406
2407
std::string key_str;
2407
2408
key_str.assign (value, value_len);
2408
- Py_DECREF (py_string);
2409
2409
2410
2410
MapKey map_key;
2411
2411
map_key.SetStringValue (key_str);
@@ -2414,9 +2414,9 @@ PyObject* Contains(CMessage* self, PyObject* arg) {
2414
2414
}
2415
2415
case Descriptor::WELLKNOWNTYPE_LISTVALUE: {
2416
2416
// For WKT ListValue, check if the key is in the items.
2417
- PyObject* items = PyObject_CallMethod (reinterpret_cast <PyObject*>(self),
2418
- " items" , nullptr );
2419
- return PyBool_FromLong (PySequence_Contains (items, arg));
2417
+ ScopedPyObjectPtr items ( PyObject_CallMethod (
2418
+ reinterpret_cast <PyObject*>(self), " items" , nullptr ) );
2419
+ return PyBool_FromLong (PySequence_Contains (items. get () , arg));
2420
2420
}
2421
2421
default :
2422
2422
// For other messages, check with HasField.
0 commit comments