THRIFT-3699 Fix integer limit symbol includes in Python C extension
This closes #915
diff --git a/lib/py/src/ext/module.cpp b/lib/py/src/ext/module.cpp
index 5ffc155..34ec7f6 100644
--- a/lib/py/src/ext/module.cpp
+++ b/lib/py/src/ext/module.cpp
@@ -21,8 +21,8 @@
#include "types.h"
#include "binary.h"
#include "compact.h"
+#include <limits>
#include <stdint.h>
-#include <sys/resource.h>
// TODO(dreiss): defval appears to be unused. Look into removing it.
// TODO(dreiss): Make parse_spec_args recursive, and cache the output
@@ -87,11 +87,18 @@
}
T protocol;
+#ifdef _MSC_VER
+ // workaround strange VC++ 2015 bug where #else path does not compile
+ int32_t default_limit = INT32_MAX;
+#else
+ int32_t default_limit = std::numeric_limits<int32_t>::max();
+#endif
protocol.setStringLengthLimit(
- as_long_then_delete(PyObject_GetAttr(oprot, INTERN_STRING(string_length_limit)), INT32_MAX));
+ as_long_then_delete(PyObject_GetAttr(oprot, INTERN_STRING(string_length_limit)),
+ default_limit));
protocol.setContainerLengthLimit(
as_long_then_delete(PyObject_GetAttr(oprot, INTERN_STRING(container_length_limit)),
- INT32_MAX));
+ default_limit));
ScopedPyObject transport(PyObject_GetAttr(oprot, INTERN_STRING(trans)));
if (!transport) {
return NULL;
@@ -170,21 +177,6 @@
#endif
- const rlim_t kStackSize = 16 * 1024 * 1024; // min stack size = 16 MB
- struct rlimit rl;
- int result;
-
- result = getrlimit(RLIMIT_STACK, &rl);
- if (result == 0) {
- if (rl.rlim_cur < kStackSize) {
- rl.rlim_cur = kStackSize;
- result = setrlimit(RLIMIT_STACK, &rl);
- if (result != 0) {
- fprintf(stderr, "setrlimit returned result = %d\n", result);
- }
- }
- }
-
#define INIT_INTERN_STRING(value) \
do { \
INTERN_STRING(value) = PyString_InternFromString(#value); \
diff --git a/lib/py/src/ext/protocol.h b/lib/py/src/ext/protocol.h
index bf3a6b8..126dbc3 100644
--- a/lib/py/src/ext/protocol.h
+++ b/lib/py/src/ext/protocol.h
@@ -21,6 +21,8 @@
#define THRIFT_PY_PROTOCOL_H
#include "ext/types.h"
+#include <limits>
+#include <stdint.h>
namespace apache {
namespace thrift {
@@ -30,14 +32,16 @@
class ProtocolBase {
public:
- ProtocolBase() : stringLimit_(INT32_MAX), containerLimit_(INT32_MAX), output_(NULL) {}
+ ProtocolBase()
+ : stringLimit_(std::numeric_limits<int32_t>::max()),
+ containerLimit_(std::numeric_limits<int32_t>::max()),
+ output_(NULL) {}
inline virtual ~ProtocolBase();
bool prepareDecodeBufferFromTransport(PyObject* trans);
PyObject* readStruct(PyObject* output, PyObject* klass, PyObject* spec_seq);
-
bool prepareEncodeBuffer();
bool encodeValue(PyObject* value, TType type, PyObject* typeargs);
diff --git a/lib/py/src/ext/protocol.tcc b/lib/py/src/ext/protocol.tcc
index 9c25841..2f0d083 100644
--- a/lib/py/src/ext/protocol.tcc
+++ b/lib/py/src/ext/protocol.tcc
@@ -20,6 +20,8 @@
#ifndef THRIFT_PY_PROTOCOL_TCC
#define THRIFT_PY_PROTOCOL_TCC
+#include <iterator>
+
#define CHECK_RANGE(v, min, max) (((v) <= (max)) && ((v) >= (min)))
#define INIT_OUTBUF_SIZE 128
@@ -210,7 +212,7 @@
if (INT_CONV_ERROR_OCCURRED(len)) {
return false;
}
- if (!CHECK_RANGE(len, 0, INT32_MAX)) {
+ if (!CHECK_RANGE(len, 0, std::numeric_limits<int32_t>::max())) {
PyErr_SetString(PyExc_OverflowError, "size out of range: exceeded INT32_MAX");
return false;
}
@@ -358,7 +360,8 @@
case T_I08: {
int8_t val;
- if (!parse_pyint(value, &val, INT8_MIN, INT8_MAX)) {
+ if (!parse_pyint(value, &val, std::numeric_limits<int8_t>::min(),
+ std::numeric_limits<int8_t>::max())) {
return false;
}
@@ -368,7 +371,8 @@
case T_I16: {
int16_t val;
- if (!parse_pyint(value, &val, INT16_MIN, INT16_MAX)) {
+ if (!parse_pyint(value, &val, std::numeric_limits<int16_t>::min(),
+ std::numeric_limits<int16_t>::max())) {
return false;
}
@@ -378,7 +382,8 @@
case T_I32: {
int32_t val;
- if (!parse_pyint(value, &val, INT32_MIN, INT32_MAX)) {
+ if (!parse_pyint(value, &val, std::numeric_limits<int32_t>::min(),
+ std::numeric_limits<int32_t>::max())) {
return false;
}
@@ -392,7 +397,8 @@
return false;
}
- if (!CHECK_RANGE(nval, INT64_MIN, INT64_MAX)) {
+ if (!CHECK_RANGE(nval, std::numeric_limits<int64_t>::min(),
+ std::numeric_limits<int64_t>::max())) {
PyErr_SetString(PyExc_OverflowError, "int out of range");
return false;
}
diff --git a/lib/py/src/ext/types.h b/lib/py/src/ext/types.h
index fd0a197..2fc9d9c 100644
--- a/lib/py/src/ext/types.h
+++ b/lib/py/src/ext/types.h
@@ -22,6 +22,11 @@
#include <Python.h>
+#ifdef _MSC_VER
+#define __STDC_LIMIT_MACROS
+#endif
+#include <stdint.h>
+
#if PY_MAJOR_VERSION >= 3
#include <vector>