Skip to content

Commit 64d7fa9

Browse files
committed
Added Py2&Py3 compatible cfl_PyInt abstraction
1 parent dad896d commit 64d7fa9

File tree

3 files changed

+49
-10
lines changed

3 files changed

+49
-10
lines changed

confluent_kafka/src/Producer.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ int32_t Producer_partitioner_cb (const rd_kafka_topic_t *rkt,
237237
Py_DECREF(args);
238238

239239
if (result) {
240-
r = (int32_t)PyLong_AsLong(result);
240+
r = (int32_t)cfl_PyInt_AsInt(result);
241241
if (PyErr_Occurred())
242242
printf("FIXME: partition_cb returned wrong type "
243243
"(expected long), how to propagate?\n");
@@ -450,7 +450,7 @@ static PyObject *Producer_poll (Handle *self, PyObject *args,
450450
if (r == -1)
451451
return NULL;
452452

453-
return PyLong_FromLong(r);
453+
return cfl_PyInt_FromInt(r);
454454
}
455455

456456

@@ -478,7 +478,7 @@ static PyObject *Producer_flush (Handle *self, PyObject *args,
478478
return NULL;
479479
}
480480
#endif
481-
return PyLong_FromLong(qlen);
481+
return cfl_PyInt_FromInt(qlen);
482482
}
483483

484484
static PyMethodDef Producer_methods[] = {

confluent_kafka/src/confluent_kafka.c

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ typedef struct {
6666

6767

6868
static PyObject *KafkaError_code (KafkaError *self, PyObject *ignore) {
69-
return PyLong_FromLong(self->code);
69+
return cfl_PyInt_FromInt(self->code);
7070
}
7171

7272
static PyObject *KafkaError_str (KafkaError *self, PyObject *ignore) {
@@ -157,7 +157,7 @@ static PyObject* KafkaError_richcompare (KafkaError *self, PyObject *o2,
157157
if (Py_TYPE(o2) == &KafkaErrorType)
158158
code2 = ((KafkaError *)o2)->code;
159159
else
160-
code2 = (int)PyLong_AsLong(o2);
160+
code2 = cfl_PyInt_AsInt(o2);
161161

162162
switch (op)
163163
{
@@ -340,7 +340,7 @@ static PyObject *Message_topic (Message *self, PyObject *ignore) {
340340

341341
static PyObject *Message_partition (Message *self, PyObject *ignore) {
342342
if (self->partition != RD_KAFKA_PARTITION_UA)
343-
return PyLong_FromLong(self->partition);
343+
return cfl_PyInt_FromInt(self->partition);
344344
else
345345
Py_RETURN_NONE;
346346
}
@@ -1912,12 +1912,35 @@ int cfl_PyObject_GetInt (PyObject *object, const char *attr_name, int *valp,
19121912
return 1;
19131913
}
19141914

1915-
*valp = (int)PyLong_AsLong(o);
1915+
*valp = cfl_PyInt_AsInt(o);
19161916
Py_DECREF(o);
19171917

19181918
return 1;
19191919
}
19201920

1921+
1922+
/**
1923+
* @brief Checks that \p object is a bool (or boolable) and sets
1924+
* \p *valp according to the object.
1925+
*
1926+
* @returns 1 if \p valp was set, or 0 if \p object is not a boolable object.
1927+
* An exception is raised in the error case.
1928+
*/
1929+
int cfl_PyBool_get (PyObject *object, const char *name, int *valp) {
1930+
if (!PyBool_Check(object)) {
1931+
PyErr_Format(PyExc_TypeError,
1932+
"Expected %s to be bool type, not %s",
1933+
name,
1934+
((PyTypeObject *)PyObject_Type(object))->tp_name);
1935+
return 0;
1936+
}
1937+
1938+
*valp = object == Py_True;
1939+
1940+
return 1;
1941+
}
1942+
1943+
19211944
/**
19221945
* @brief Get attribute \p attr_name from \p object and make sure it is
19231946
* a string type.
@@ -1983,7 +2006,7 @@ PyObject *cfl_int32_array_to_py_list (const int32_t *arr, size_t cnt) {
19832006

19842007
for (i = 0 ; i < cnt ; i++)
19852008
PyList_SET_ITEM(list, (Py_ssize_t)i,
1986-
PyLong_FromLong((long)arr[i]));
2009+
cfl_PyInt_FromInt(arr[i]));
19872010

19882011
return list;
19892012
}
@@ -2083,7 +2106,7 @@ static char *KafkaError_add_errs (PyObject *dict, const char *origdoc) {
20832106
if (!descs[i].desc)
20842107
continue;
20852108

2086-
code = PyLong_FromLong(descs[i].code);
2109+
code = cfl_PyInt_FromInt(descs[i].code);
20872110

20882111
PyDict_SetItemString(dict, descs[i].name, code);
20892112

confluent_kafka/src/confluent_kafka.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,13 +263,29 @@ void CallState_resume (CallState *cs);
263263
void CallState_crash (CallState *cs);
264264

265265

266+
/**
267+
* @brief Python 3 renamed the internal PyInt type to PyLong, but the
268+
* type is still exposed as 'int' in Python.
269+
* We use the (cfl_)PyInt name for both Python 2 and 3 to mean an int,
270+
* assuming it will be at least 31 bits+signed on all platforms.
271+
*/
272+
#ifdef PY3
273+
#define cfl_PyInt_Check(o) PyLong_Check(o)
274+
#define cfl_PyInt_AsInt(o) (int)PyLong_AsLong(o)
275+
#define cfl_PyInt_FromInt(v) PyLong_FromLong(v)
276+
#else
277+
#define cfl_PyInt_Check(o) PyInt_Check(o)
278+
#define cfl_PyInt_AsInt(o) (int)PyInt_AsLong(o)
279+
#define cfl_PyInt_FromInt(v) PyInt_FromLong(v)
280+
#endif
281+
266282

267283
PyObject *cfl_PyObject_lookup (const char *modulename, const char *typename);
268284

269285
void cfl_PyDict_SetString (PyObject *dict, const char *name, const char *val);
270286
void cfl_PyDict_SetInt (PyObject *dict, const char *name, int val);
271287
int cfl_PyObject_SetString (PyObject *o, const char *name, const char *val);
272-
int cfl_PyObject_SetLong (PyObject *o, const char *name, long val);
288+
int cfl_PyObject_SetInt (PyObject *o, const char *name, int val);
273289
int cfl_PyObject_GetAttr (PyObject *object, const char *attr_name,
274290
PyObject **valp, const PyTypeObject *py_type,
275291
int required);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy