8000 Introduce new type for RESP3 PUSH notifications by uglide · Pull Request #208 · redis/hiredis-py · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Introduce new type for RESP3 PUSH notifications #208

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion hiredis/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from hiredis.hiredis import Reader, HiredisError, pack_command, ProtocolError, ReplyError
from hiredis.hiredis import Reader, HiredisError, pack_command, ProtocolError, ReplyError, PushNotification
from hiredis.version import __version__

__all__ = [
"Reader",
"HiredisError",
"pack_command",
"ProtocolError",
"PushNotification",
"ReplyError",
"__version__"]
4 changes: 4 additions & 0 deletions hiredis/hiredis.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ class ReplyError(HiredisError):
...


class PushNotification(list):
...


class Reader:
def __init__(
self,
Expand Down
8 changes: 8 additions & 0 deletions src/hiredis.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ PyMODINIT_FUNC PyInit_hiredis(void)
return NULL;
}

PushNotificationType.tp_base = &PyList_Type;
if (PyType_Ready(&PushNotificationType) < 0) {
return NULL;
}

mod_hiredis = PyModule_Create(&hiredis_ModuleDef);

/* Setup custom exceptions */
Expand All @@ -79,5 +84,8 @@ PyMODINIT_FUNC PyInit_hiredis(void)
Py_INCREF(&hiredis_ReaderType);
PyModule_AddObject(mod_hiredis, "Reader", (PyObject *)&hiredis_ReaderType);

Py_INCREF(&PushNotificationType);
PyModule_AddObject(mod_hiredis, "PushNotification", (PyObject *)&PushNotificationType);

return mod_hiredis;
}
67 changes: 67 additions & 0 deletions src/reader.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "reader.h"

#include <assert.h>
#include <Python.h>

static void Reader_dealloc(hiredis_ReaderObject *self);
static int Reader_traverse(hiredis_ReaderObject *self, visitproc visit, void *arg);
Expand All @@ -14,6 +15,10 @@ static PyObject *Reader_len(hiredis_ReaderObject *self);
static PyObject *Reader_has_data(hiredis_ReaderObject *self);
static PyObject *Reader_set_encoding(hiredis_ReaderObject *self, PyObject *args, PyObject *kwds);

static int PushNotificationType_init(PushNotificationObject *self, PyObject *args, PyObject *kwds);
/* Create a new instance of PushNotificationType with preallocated number of elements */
static PyObject* PushNotificationType_New(Py_ssize_t size);

static PyMethodDef hiredis_ReaderMethods[] = {
{"feed", (PyCFunction)Reader_feed, METH_VARARGS, NULL },
{"gets", (PyCFunction)Reader_gets, METH_VARARGS, NULL },
Expand Down Expand Up @@ -66,6 +71,16 @@ PyTypeObject hiredis_ReaderType = {
Reader_new, /*tp_new */
};

PyTypeObject PushNotificationType = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = MOD_HIREDIS ".PushNotification",
.tp_basicsize = sizeof(PushNotificationObject),
.tp_itemsize = 0,
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
.tp_doc = "Redis PUSH notification type",
.tp_init = (initproc) PushNotificationType_init,
};

static void *tryParentize(const redisReadTask *task, PyObject *obj) {
PyObject *parent;
if (task && task->parent) {
Expand Down Expand Up @@ -165,6 +180,9 @@ static void *createArrayObject(const redisReadTask *task, size_t elements) {
case REDIS_REPLY_MAP:
obj = PyDict_New();
break;
case REDIS_REPLY_PUSH:
obj = PushNotificationType_New(elements);
break;
default:
obj = PyList_New(elements);
}
Expand Down Expand Up @@ -199,6 +217,55 @@ static void freeObject(void *obj) {
Py_XDECREF(obj);
}

static int PushNotificationType_init(PushNotificationObject *self, PyObject *args, PyObject *kwds) {
return PyList_Type.tp_init((PyObject *)self, args, kwds);
}

/* Create a new instance of PushNotificationType with preallocated number of elements */
static PyObject* PushNotificationType_New(Py_ssize_t size) {
/* Check for negative size */
if (size < 0) {
PyErr_SetString(PyExc_SystemError, "negative list size");
return NULL;
}

/* Check for potential overflow */
if ((size_t)size > PY_SSIZE_T_MAX / sizeof(PyObject*)) {
return PyErr_NoMemory();
}

/* Create a new instance of PushNotificationType */
PyObject* obj = PyType_GenericNew(&PushNotificationType, NULL, NULL);
if (obj == NULL) {
return NULL;
}

/* Cast to PyListObject to access its fields */
PyListObject* op = (PyListObject*)obj;

/* Allocate memory for the list items if size > 0 */
if (size > 0) {
Copy link
Preview
Copilot AI May 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a zero-element PushNotification (i.e., when size == 0), the ob_item field is not explicitly initialized. Consider initializing ob_item to a valid empty list pointer to ensure proper list behavior.

Copilot uses AI. Check for mistakes.

size_t nbytes = (size_t)size * sizeof(PyObject*);
op->ob_item = (PyObject**)PyMem_Malloc(nbytes);
if (op->ob_item == NULL) {
Py_DECREF(obj);
return PyErr_NoMemory();
}
/* Initialize memory to zeros */
memset(op->ob_item, 0, nbytes);
}

/* Set the size and allocated fields */
#if PY_VERSION_HEX >= 0x03090000
Py_SET_SIZE(op, size);
#else
Py_SIZE(op) = size;
#endif
op->allocated = size;

return obj;
}

redisReplyObjectFunctions hiredis_ObjectFunctions = {
createStringObject, // void *(*createString)(const redisReadTask*, char*, size_t);
createArrayObject, // void *(*createArray)(const redisReadTask*, size_t);
Expand Down
5 changes: 5 additions & 0 deletions src/reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ typedef struct {
} error;
} hiredis_ReaderObject;

typedef struct {
PyListObject list;
} PushNotificationObject;

extern PyTypeObject hiredis_ReaderType;
extern PyTypeObject PushNotificationType;
extern redisReplyObjectFunctions hiredis_ObjectFunctions;

#endif
6 changes: 4 additions & 2 deletions tests/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,11 @@ def test_dict_with_unhashable_key(reader):
with pytest.raises(TypeError):
reader.gets()

def test_vector(reader):
def test_vector(reader):
reader.feed(b">4\r\n+pubsub\r\n+message\r\n+channel\r\n+message\r\n")
assert [b"pubsub", b"message", b"channel", b"message"] == reader.gets()
result = reader.gets()
assert isinstance(result, hiredis.PushNotification)
assert [b"pubsub", b"message", b"channel", b"message"] == result

def test_verbatim_string(reader):
value = b"text"
Expand Down
Loading
0