THRIFT-5225: Use nullptr instead of NULL
Patch: Zezeng Wang
This closes #2168
diff --git a/lib/py/src/ext/module.cpp b/lib/py/src/ext/module.cpp
index 7158b8f..f14ddae 100644
--- a/lib/py/src/ext/module.cpp
+++ b/lib/py/src/ext/module.cpp
@@ -48,20 +48,20 @@
template <typename T>
static PyObject* encode_impl(PyObject* args) {
if (!args)
- return NULL;
+ return nullptr;
- PyObject* enc_obj = NULL;
- PyObject* type_args = NULL;
+ PyObject* enc_obj = nullptr;
+ PyObject* type_args = nullptr;
if (!PyArg_ParseTuple(args, "OO", &enc_obj, &type_args)) {
- return NULL;
+ return nullptr;
}
if (!enc_obj || !type_args) {
- return NULL;
+ return nullptr;
}
T protocol;
if (!protocol.prepareEncodeBuffer() || !protocol.encodeValue(enc_obj, T_STRUCT, type_args)) {
- return NULL;
+ return nullptr;
}
return protocol.getEncodedValue();
@@ -79,11 +79,11 @@
template <typename T>
static PyObject* decode_impl(PyObject* args) {
- PyObject* output_obj = NULL;
- PyObject* oprot = NULL;
- PyObject* typeargs = NULL;
+ PyObject* output_obj = nullptr;
+ PyObject* oprot = nullptr;
+ PyObject* typeargs = nullptr;
if (!PyArg_ParseTuple(args, "OOO", &output_obj, &oprot, &typeargs)) {
- return NULL;
+ return nullptr;
}
T protocol;
@@ -96,16 +96,16 @@
default_limit));
ScopedPyObject transport(PyObject_GetAttr(oprot, INTERN_STRING(trans)));
if (!transport) {
- return NULL;
+ return nullptr;
}
StructTypeArgs parsedargs;
if (!parse_struct_args(&parsedargs, typeargs)) {
- return NULL;
+ return nullptr;
}
if (!protocol.prepareDecodeBufferFromTransport(transport.get())) {
- return NULL;
+ return nullptr;
}
return protocol.readStruct(output_obj, parsedargs.klass, parsedargs.spec);
@@ -141,22 +141,22 @@
{"decode_binary", decode_binary, METH_VARARGS, ""},
{"encode_compact", encode_compact, METH_VARARGS, ""},
{"decode_compact", decode_compact, METH_VARARGS, ""},
- {NULL, NULL, 0, NULL} /* Sentinel */
+ {nullptr, nullptr, 0, nullptr} /* Sentinel */
};
#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef ThriftFastBinaryDef = {PyModuleDef_HEAD_INIT,
"thrift.protocol.fastbinary",
- NULL,
+ nullptr,
0,
ThriftFastBinaryMethods,
- NULL,
- NULL,
- NULL,
- NULL};
+ nullptr,
+ nullptr,
+ nullptr,
+ nullptr};
-#define INITERROR return NULL;
+#define INITERROR return nullptr;
PyObject* PyInit_fastbinary() {
@@ -167,7 +167,7 @@
void initfastbinary() {
PycString_IMPORT;
- if (PycStringIO == NULL)
+ if (PycStringIO == nullptr)
INITERROR
#endif
@@ -193,7 +193,7 @@
#else
Py_InitModule("thrift.protocol.fastbinary", ThriftFastBinaryMethods);
#endif
- if (module == NULL)
+ if (module == nullptr)
INITERROR;
#if PY_MAJOR_VERSION >= 3
diff --git a/lib/py/src/ext/protocol.h b/lib/py/src/ext/protocol.h
index 521b7ee..c0cd437 100644
--- a/lib/py/src/ext/protocol.h
+++ b/lib/py/src/ext/protocol.h
@@ -35,7 +35,7 @@
ProtocolBase()
: stringLimit_((std::numeric_limits<int32_t>::max)()),
containerLimit_((std::numeric_limits<int32_t>::max)()),
- output_(NULL) {}
+ output_(nullptr) {}
inline virtual ~ProtocolBase();
bool prepareDecodeBufferFromTransport(PyObject* trans);
diff --git a/lib/py/src/ext/protocol.tcc b/lib/py/src/ext/protocol.tcc
index ede2bb4..03cfc9b 100644
--- a/lib/py/src/ext/protocol.tcc
+++ b/lib/py/src/ext/protocol.tcc
@@ -48,7 +48,7 @@
PycString_IMPORT;
}
if (!PycStringIO) {
- return NULL;
+ return nullptr;
}
return PycStringIO->NewOutput(size);
}
@@ -83,7 +83,7 @@
PycString_IMPORT;
}
if (!PycStringIO) {
- return NULL;
+ return nullptr;
}
return PycStringIO->cgetvalue(output_);
}
@@ -277,7 +277,7 @@
} else {
// using building functions as this is a rare codepath
ScopedPyObject newiobuf(PyObject_CallFunction(input_.refill_callable.get(), refill_signature,
- *output, rlen, len, NULL));
+ *output, rlen, len, nullptr));
if (!newiobuf) {
return false;
}
@@ -332,7 +332,7 @@
template <typename Impl>
bool ProtocolBase<Impl>::prepareEncodeBuffer() {
output_ = detail::new_encode_buffer(INIT_OUTBUF_SIZE);
- return output_ != NULL;
+ return output_ != nullptr;
}
template <typename Impl>
@@ -484,8 +484,8 @@
return false;
}
Py_ssize_t pos = 0;
- PyObject* k = NULL;
- PyObject* v = NULL;
+ PyObject* k = nullptr;
+ PyObject* v = nullptr;
// TODO(bmaurer): should support any mapping, not just dicts
while (PyDict_Next(value, &pos, &k, &v)) {
if (!encodeValue(k, parsedargs.ktag, parsedargs.ktypeargs)
@@ -647,7 +647,7 @@
case T_BOOL: {
bool v = 0;
if (!impl()->readBool(v)) {
- return NULL;
+ return nullptr;
}
if (v) {
Py_RETURN_TRUE;
@@ -658,21 +658,21 @@
case T_I08: {
int8_t v = 0;
if (!impl()->readI8(v)) {
- return NULL;
+ return nullptr;
}
return PyInt_FromLong(v);
}
case T_I16: {
int16_t v = 0;
if (!impl()->readI16(v)) {
- return NULL;
+ return nullptr;
}
return PyInt_FromLong(v);
}
case T_I32: {
int32_t v = 0;
if (!impl()->readI32(v)) {
- return NULL;
+ return nullptr;
}
return PyInt_FromLong(v);
}
@@ -680,7 +680,7 @@
case T_I64: {
int64_t v = 0;
if (!impl()->readI64(v)) {
- return NULL;
+ return nullptr;
}
// TODO(dreiss): Find out if we can take this fastpath always when
// sizeof(long) == sizeof(long long).
@@ -693,16 +693,16 @@
case T_DOUBLE: {
double v = 0.0;
if (!impl()->readDouble(v)) {
- return NULL;
+ return nullptr;
}
return PyFloat_FromDouble(v);
}
case T_STRING: {
- char* buf = NULL;
+ char* buf = nullptr;
int len = impl()->readString(&buf);
if (len < 0) {
- return NULL;
+ return nullptr;
}
if (isUtf8(typeargs)) {
return PyUnicode_DecodeUTF8(buf, len, 0);
@@ -715,28 +715,28 @@
case T_SET: {
SetListTypeArgs parsedargs;
if (!parse_set_list_args(&parsedargs, typeargs)) {
- return NULL;
+ return nullptr;
}
TType etype = T_STOP;
int32_t len = impl()->readListBegin(etype);
if (len < 0) {
- return NULL;
+ return nullptr;
}
if (len > 0 && !checkType(etype, parsedargs.element_type)) {
- return NULL;
+ return nullptr;
}
bool use_tuple = type == T_LIST && parsedargs.immutable;
ScopedPyObject ret(use_tuple ? PyTuple_New(len) : PyList_New(len));
if (!ret) {
- return NULL;
+ return nullptr;
}
for (int i = 0; i < len; i++) {
PyObject* item = decodeValue(etype, parsedargs.typeargs);
if (!item) {
- return NULL;
+ return nullptr;
}
if (use_tuple) {
PyTuple_SET_ITEM(ret.get(), i, item);
@@ -758,32 +758,32 @@
case T_MAP: {
MapTypeArgs parsedargs;
if (!parse_map_args(&parsedargs, typeargs)) {
- return NULL;
+ return nullptr;
}
TType ktype = T_STOP;
TType vtype = T_STOP;
uint32_t len = impl()->readMapBegin(ktype, vtype);
if (len > 0 && (!checkType(ktype, parsedargs.ktag) || !checkType(vtype, parsedargs.vtag))) {
- return NULL;
+ return nullptr;
}
ScopedPyObject ret(PyDict_New());
if (!ret) {
- return NULL;
+ return nullptr;
}
for (uint32_t i = 0; i < len; i++) {
ScopedPyObject k(decodeValue(ktype, parsedargs.ktypeargs));
if (!k) {
- return NULL;
+ return nullptr;
}
ScopedPyObject v(decodeValue(vtype, parsedargs.vtypeargs));
if (!v) {
- return NULL;
+ return nullptr;
}
if (PyDict_SetItem(ret.get(), k.get(), v.get()) == -1) {
- return NULL;
+ return nullptr;
}
}
@@ -792,12 +792,12 @@
ThriftModule = PyImport_ImportModule("thrift.Thrift");
}
if (!ThriftModule) {
- return NULL;
+ return nullptr;
}
ScopedPyObject cls(PyObject_GetAttr(ThriftModule, INTERN_STRING(TFrozenDict)));
if (!cls) {
- return NULL;
+ return nullptr;
}
ScopedPyObject arg(PyTuple_New(1));
@@ -811,7 +811,7 @@
case T_STRUCT: {
StructTypeArgs parsedargs;
if (!parse_struct_args(&parsedargs, typeargs)) {
- return NULL;
+ return nullptr;
}
return readStruct(Py_None, parsedargs.klass, parsedargs.spec);
}
@@ -823,7 +823,7 @@
case T_U64:
default:
PyErr_Format(PyExc_TypeError, "Unexpected TType for decodeValue: %d", type);
- return NULL;
+ return nullptr;
}
}
@@ -833,26 +833,26 @@
bool immutable = output == Py_None;
ScopedPyObject kwargs;
if (spec_seq_len == -1) {
- return NULL;
+ return nullptr;
}
if (immutable) {
kwargs.reset(PyDict_New());
if (!kwargs) {
PyErr_SetString(PyExc_TypeError, "failed to prepare kwargument storage");
- return NULL;
+ return nullptr;
}
}
detail::ReadStructScope<Impl> scope = detail::readStructScope(this);
if (!scope) {
- return NULL;
+ return nullptr;
}
while (true) {
TType type = T_STOP;
int16_t tag;
if (!impl()->readFieldBegin(type, tag)) {
- return NULL;
+ return nullptr;
}
if (type == T_STOP) {
break;
@@ -860,7 +860,7 @@
if (tag < 0 || tag >= spec_seq_len) {
if (!skip(type)) {
PyErr_SetString(PyExc_TypeError, "Error while skipping unknown field");
- return NULL;
+ return nullptr;
}
continue;
}
@@ -869,38 +869,38 @@
if (item_spec == Py_None) {
if (!skip(type)) {
PyErr_SetString(PyExc_TypeError, "Error while skipping unknown field");
- return NULL;
+ return nullptr;
}
continue;
}
StructItemSpec parsedspec;
if (!parse_struct_item_spec(&parsedspec, item_spec)) {
- return NULL;
+ return nullptr;
}
if (parsedspec.type != type) {
if (!skip(type)) {
PyErr_Format(PyExc_TypeError, "struct field had wrong type: expected %d but got %d",
parsedspec.type, type);
- return NULL;
+ return nullptr;
}
continue;
}
ScopedPyObject fieldval(decodeValue(parsedspec.type, parsedspec.typeargs));
if (!fieldval) {
- return NULL;
+ return nullptr;
}
if ((immutable && PyDict_SetItem(kwargs.get(), parsedspec.attrname, fieldval.get()) == -1)
|| (!immutable && PyObject_SetAttr(output, parsedspec.attrname, fieldval.get()) == -1)) {
- return NULL;
+ return nullptr;
}
}
if (immutable) {
ScopedPyObject args(PyTuple_New(0));
if (!args) {
PyErr_SetString(PyExc_TypeError, "failed to prepare argument storage");
- return NULL;
+ return nullptr;
}
return PyObject_Call(klass, args.get(), kwargs.get());
}
diff --git a/lib/py/src/ext/types.cpp b/lib/py/src/ext/types.cpp
index 68443fb..e8d6939 100644
--- a/lib/py/src/ext/types.cpp
+++ b/lib/py/src/ext/types.cpp
@@ -24,7 +24,7 @@
namespace thrift {
namespace py {
-PyObject* ThriftModule = NULL;
+PyObject* ThriftModule = nullptr;
#if PY_MAJOR_VERSION < 3
char refill_signature[] = {'s', '#', 'i'};
diff --git a/lib/py/src/ext/types.h b/lib/py/src/ext/types.h
index 5cd8dda..9b45dd0 100644
--- a/lib/py/src/ext/types.h
+++ b/lib/py/src/ext/types.h
@@ -83,7 +83,7 @@
// replace with unique_ptr when we're OK with C++11
class ScopedPyObject {
public:
- ScopedPyObject() : obj_(NULL) {}
+ ScopedPyObject() : obj_(nullptr) {}
explicit ScopedPyObject(PyObject* py_object) : obj_(py_object) {}
~ScopedPyObject() {
if (obj_)
@@ -98,7 +98,7 @@
}
PyObject* release() throw() {
PyObject* tmp = obj_;
- obj_ = NULL;
+ obj_ = nullptr;
return tmp;
}
void swap(ScopedPyObject& other) throw() {