Skip to content
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

Fixing broken windows builds on python < 3.8 #151

Merged
merged 23 commits into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
8e122b8
Implemented pack_bytes and pack_command.
prokazov-redis Jan 12, 2023
be55776
Bump version + make it work in a clean environment
prokazov-redis Jan 14, 2023
2d45fb1
pack_command that passes redis-py test (*almost*)
prokazov-redis Jan 16, 2023
fdb1314
Removed pack-bytes and added some tests.
prokazov-redis Jan 18, 2023
96e5d8e
Removed pack-bytes and added some tests.
prokazov-redis Jan 18, 2023
143a81c
Merge branch 'pack_command'
prokazov-redis Jan 18, 2023
ff5ccaa
even more clean up.
prokazov-redis Jan 18, 2023
83edbe8
Re-factored tests and added a couple more ones.
prokazov Jan 19, 2023
b54372d
1) Fix the compiler warning in pack.c
prokazov Jan 19, 2023
a5f8a3b
1) Fixed typo.
prokazov-redis Jan 23, 2023
241000c
1) Fixed typo.
prokazov-redis Jan 23, 2023
4a416d7
Autopep-8 on setup.py
prokazov-redis Jan 23, 2023
5807460
Drop extra link args
prokazov Jan 25, 2023
8643b7c
add conditional compile flags excluding windows and mac
zalmane Jan 25, 2023
3517354
Remove egg-info
prokazov Jan 25, 2023
05fe090
1) add conditional compile flags excluding windows and mac
prokazov-redis Jan 25, 2023
2ffa362
Actually use conditional compile flags
prokazov Jan 25, 2023
1758fe9
Fix the Windows build.
prokazov-redis Jan 26, 2023
11fa235
Re-factored setup.py and actually fixed the Windows build.
prokazov-redis Jan 26, 2023
d1a5ebe
Apparently 'win' is not the same as win32.
prokazov-redis Jan 26, 2023
493d6a7
Merge branch 'master' into master
chayim Jan 30, 2023
1f1af09
1) Don't use Bsymbolic linker arg
prokazov Jan 31, 2023
02abb23
Merge branch 'master' into Fix-for-Win32-build-break
prokazov Jan 31, 2023
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
Next Next commit
Implemented pack_bytes and pack_command.
  • Loading branch information
prokazov-redis committed Jan 12, 2023
commit 8e122b895fc03944a02fa73338c801117034bc18
9 changes: 7 additions & 2 deletions hiredis/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from .hiredis import Reader, HiredisError, ProtocolError, ReplyError
from .hiredis import Reader, HiredisError, pack_bytes, pack_command, ProtocolError, ReplyError
from .version import __version__

__all__ = [
"Reader", "HiredisError", "ProtocolError", "ReplyError",
"Reader",
"HiredisError",
"pack_bytes",
"pack_command",
"ProtocolError",
"ReplyError",
"__version__"]
8 changes: 7 additions & 1 deletion hiredis/hiredis.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Callable, Optional, Union
from typing import Any, Callable, Optional, Union, Tuple

class HiredisError(Exception): ...
class ProtocolError(HiredisError): ...
Expand All @@ -24,3 +24,9 @@ class Reader:
def set_encoding(
self, encoding: Optional[str] = ..., errors: Optional[str] = ...
) -> None: ...


def pack_bytes(bytes: bytes): ...


def pack_command(cmd: Tuple[str|int|float|bytes]): ...
38 changes: 37 additions & 1 deletion src/hiredis.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "hiredis.h"
#include "reader.h"
#include "pack.h"

static int hiredis_ModuleTraverse(PyObject *m, visitproc visit, void *arg) {
Py_VISIT(GET_STATE(m)->HiErr_Base);
Expand All @@ -15,12 +16,47 @@ static int hiredis_ModuleClear(PyObject *m) {
return 0;
}

static PyObject*
py_pack_command(PyObject* self, PyObject* cmd)
{
return pack_command(cmd);
}

static PyObject*
py_pack_bytes(PyObject* self, PyObject* cmd)
{
return pack_bytes(cmd);
}

PyDoc_STRVAR(pack_command_doc, "Pack ...... ");
PyDoc_STRVAR(pack_bytes_doc, "Pack ...... ");

PyMethodDef pack_command_method = {
"pack_command", /* The name as a C string. */
(PyCFunction) py_pack_command, /* The C function to invoke. */
METH_O, /* Flags telling Python how to invoke */
pack_command_doc, /* The docstring as a C string. */
};

PyMethodDef pack_bytes_method = {
"pack_bytes", /* The name as a C string. */
(PyCFunction) py_pack_bytes, /* The C function to invoke. */
METH_O, /* Flags telling Python how to invoke */
pack_bytes_doc, /* The docstring as a C string. */
};

PyMethodDef methods[] = {
{"pack_command", (PyCFunction) py_pack_command, METH_O, pack_command_doc},
{"pack_bytes", (PyCFunction) py_pack_bytes, METH_O, pack_bytes_doc},
{NULL},
};

static struct PyModuleDef hiredis_ModuleDef = {
PyModuleDef_HEAD_INIT,
MOD_HIREDIS,
NULL,
sizeof(struct hiredis_ModuleState), /* m_size */
NULL, /* m_methods */
methods, /* m_methods */
NULL, /* m_reload */
hiredis_ModuleTraverse, /* m_traverse */
hiredis_ModuleClear, /* m_clear */
Expand Down
111 changes: 111 additions & 0 deletions src/pack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#include "pack.h"
#include <hiredis/hiredis.h>

static
sds add_to_buffer(sds curr_buff, char *cmd, uint len)
{
assert(curr_buff);
assert(sds);
sds newbuf = sdscatlen(curr_buff, cmd, len);
if (newbuf == NULL) {
PyErr_NoMemory();
return NULL;
}

return newbuf;
}

PyObject*
pack_command(PyObject* cmd)
{
char *buf = sdsempty();
assert(cmd);
char *obj_buf = NULL;
Py_ssize_t obj_len = 0;

if (cmd == NULL || !PyTuple_Check(cmd)) {
PyErr_SetString(PyExc_TypeError,
"The argument must be a tuple of str, int, float or bytes.");
return NULL;
}

if (buf == NULL) {
return PyErr_NoMemory();
}

for (Py_ssize_t i = 0; i < PyTuple_Size(cmd); i++) {
PyObject *item = PyTuple_GetItem(cmd, i);
sds temp_ptr = NULL;
if (PyBytes_Check(item)) {
// check result?
PyBytes_AsStringAndSize(item, &obj_buf, &obj_len);
temp_ptr = add_to_buffer(buf, obj_buf, obj_len);
if (temp_ptr == NULL) {
return NULL;
}

buf = temp_ptr;

} else if (PyUnicode_Check(item)) {

obj_buf = PyUnicode_AsUTF8AndSize(item, &obj_len);
if (obj_buf == NULL) {
sds_free(buf);
return NULL;
}

temp_ptr = add_to_buffer(buf, obj_buf, obj_len);
if (temp_ptr == NULL) {
return NULL;
}

buf = temp_ptr;
} else if PyLong_Check(item) {
} else {
printf("not yet\n");
}

temp_ptr = add_to_buffer(buf, " ", 1);
if (temp_ptr == NULL) {
return NULL;
}
buf = temp_ptr;
}

obj_len = redisFormatCommand(&obj_buf, buf);

PyObject *result = PyBytes_FromStringAndSize(obj_buf, obj_len);

hi_free(obj_buf);
//sds_free(buf);

return result;
}


PyObject*
pack_bytes(PyObject* cmd)
{
assert(cmd);
if (cmd == NULL || !PyBytes_Check(cmd)) {
PyErr_SetString(PyExc_TypeError,
"The argument must be a tuple of str, int, float or bytes.");
return NULL;
}

char *obj_buf = NULL;
Py_ssize_t obj_len = 0;

if(PyBytes_AsStringAndSize(cmd, &obj_buf, &obj_len) == -1) {
return NULL;
}

char * str_result = NULL;
obj_len = redisFormatCommand(&str_result, obj_buf);

PyObject *result = PyBytes_FromStringAndSize(str_result, obj_len);

hi_free(str_result);

return result;
}
9 changes: 9 additions & 0 deletions src/pack.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef __PACK_H
#define __PACK_H

#include <Python.h>

extern PyObject* pack_command(PyObject* cmd);
extern PyObject* pack_bytes(PyObject* bytes);

#endif