Skip to content

Commit 3ec152a

Browse files
committed
client(py): move hopper client methods onto hopper pipe object
1 parent b80ab3e commit 3ec152a

File tree

2 files changed

+39
-37
lines changed

2 files changed

+39
-37
lines changed

client/py/hoppermodule.c

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ Hopper_Pipe_SETSTR(endpoint)
1515
Hopper_Pipe_SETSTR(hopper);
1616
// clang-format on
1717

18-
static PyObject *_hopper_pipe_new(PyTypeObject *type, PyObject *args,
19-
PyObject *kwds) {
20-
struct _hopper_pipe *self = (struct _hopper_pipe *)type->tp_alloc(type, 0);
18+
static PyObject *hopper_pipe_new(PyTypeObject *type, PyObject *args,
19+
PyObject *kwds) {
20+
struct py_hopper_pipe *self =
21+
(struct py_hopper_pipe *)type->tp_alloc(type, 0);
2122
if (!self)
2223
return NULL;
2324

@@ -40,8 +41,8 @@ static PyObject *_hopper_pipe_new(PyTypeObject *type, PyObject *args,
4041
return (PyObject *)self;
4142
}
4243

43-
static int _hopper_pipe_init(struct _hopper_pipe *self, PyObject *args,
44-
PyObject *kwds) {
44+
static int hopper_pipe_init(struct py_hopper_pipe *self, PyObject *args,
45+
PyObject *kwds) {
4546
static char *kwlist[] = {"name", "endpoint", "hopper", "fd", "flags", NULL};
4647
PyObject *name = NULL, *endpoint = NULL, *hopper = NULL;
4748

@@ -61,7 +62,7 @@ static int _hopper_pipe_init(struct _hopper_pipe *self, PyObject *args,
6162
return 0;
6263
}
6364

64-
static void _hopper_pipe_dealloc(struct _hopper_pipe *self) {
65+
static void hopper_pipe_dealloc(struct py_hopper_pipe *self) {
6566
assert(self->name);
6667
assert(self->endpoint);
6768
assert(self->hopper);
@@ -72,14 +73,20 @@ static void _hopper_pipe_dealloc(struct _hopper_pipe *self) {
7273
Py_TYPE(self)->tp_free((PyObject *)self);
7374
}
7475

75-
static PyMemberDef _hopper_pipe_members[] = {
76-
{"fd", Py_T_INT, offsetof(struct _hopper_pipe, fd), 0,
76+
static PyObject *hopper_pipe_open(PyObject *self, PyObject *args) {}
77+
static PyObject *hopper_pipe_close(PyObject *self, PyObject *args) {}
78+
static PyObject *hopper_pipe_read(PyObject *self, PyObject *args) {}
79+
static PyObject *hopper_pipe_write(PyObject *self, PyObject *args) {}
80+
81+
static PyMemberDef hopper_pipe_members[] = {
82+
{"fd", Py_T_INT, offsetof(struct py_hopper_pipe, fd), 0,
7783
"pipe file descriptor"},
78-
{"flags", Py_T_INT, offsetof(struct _hopper_pipe, flags), 0, "pipe flags"},
84+
{"flags", Py_T_INT, offsetof(struct py_hopper_pipe, flags), 0,
85+
"pipe flags"},
7986
{NULL},
8087
};
8188

82-
static PyGetSetDef _hopper_get_set[] = {
89+
static PyGetSetDef hopper_pipe_get_set[] = {
8390
{"name", (getter)_hopper_pipe_getname, (setter)_hopper_pipe_setname,
8491
"pipe name", NULL},
8592
{"endpoint", (getter)_hopper_pipe_getendpoint,
@@ -88,46 +95,42 @@ static PyGetSetDef _hopper_get_set[] = {
8895
"pipe hopper", NULL},
8996
};
9097

98+
static PyMethodDef hopper_pipe_methods[] = {
99+
{"open", hopper_pipe_open, METH_VARARGS, ""},
100+
{"close", hopper_pipe_close, METH_VARARGS, ""},
101+
{"read", hopper_pipe_read, METH_VARARGS, ""},
102+
{"write", hopper_pipe_write, METH_VARARGS, ""},
103+
{NULL, NULL, 0, NULL},
104+
};
105+
91106
// clang-format off
92-
static PyTypeObject _hopper_pipe_object = {
107+
static PyTypeObject hopper_pipe_type = {
93108
.ob_base = PyVarObject_HEAD_INIT(NULL, 0)
94109
.tp_name = "hopper.HopperPipe",
95110
.tp_doc = PyDoc_STR("A object representing a Hopper pipe"),
96-
.tp_basicsize = sizeof(struct _hopper_pipe),
111+
.tp_basicsize = sizeof(struct py_hopper_pipe),
97112
.tp_itemsize = 0,
98113
.tp_flags = Py_TPFLAGS_DEFAULT,
99-
.tp_new = _hopper_pipe_new,
100-
.tp_init = (initproc)_hopper_pipe_init,
101-
.tp_dealloc = (destructor)_hopper_pipe_dealloc,
102-
.tp_members = _hopper_pipe_members,
103-
.tp_getset = _hopper_get_set,
114+
.tp_new = hopper_pipe_new,
115+
.tp_init = (initproc)hopper_pipe_init,
116+
.tp_dealloc = (destructor)hopper_pipe_dealloc,
117+
.tp_members = hopper_pipe_members,
118+
.tp_getset = hopper_pipe_get_set,
119+
.tp_methods = hopper_pipe_methods,
104120
};
105121
// clang-format on
106122

107-
static PyObject *hopper_open_pipe(PyObject *self, PyObject *args) {}
108-
static PyObject *hopper_close_pipe(PyObject *self, PyObject *args) {}
109-
static PyObject *hopper_read_pipe(PyObject *self, PyObject *args) {}
110-
static PyObject *hopper_write_pipe(PyObject *self, PyObject *args) {}
111-
112123
static int hopper_module_exec(PyObject *m) {
113-
if (PyType_Ready(&_hopper_pipe_object) < 0)
124+
if (PyType_Ready(&hopper_pipe_type) < 0)
114125
return -1;
115126

116-
if (PyModule_AddObjectRef(m, "HopperPipe",
117-
(PyObject *)&_hopper_pipe_object) < 0)
127+
if (PyModule_AddObjectRef(m, "HopperPipe", (PyObject *)&hopper_pipe_type) <
128+
0)
118129
return -1;
119130

120131
return 0;
121132
}
122133

123-
static PyMethodDef hopper_methods[] = {
124-
{"open", hopper_open_pipe, METH_VARARGS, ""},
125-
{"close", hopper_close_pipe, METH_VARARGS, ""},
126-
{"read", hopper_read_pipe, METH_VARARGS, ""},
127-
{"write", hopper_write_pipe, METH_VARARGS, ""},
128-
{NULL, NULL, 0, NULL},
129-
};
130-
131134
static PyModuleDef_Slot hopper_slots[] = {
132135
{Py_mod_exec, hopper_module_exec},
133136
{Py_mod_multiple_interpreters, Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED},
@@ -138,7 +141,6 @@ static struct PyModuleDef hoppermodule = {
138141
.m_base = PyModuleDef_HEAD_INIT,
139142
.m_name = "hopper",
140143
.m_doc = NULL,
141-
.m_methods = hopper_methods,
142144
.m_slots = hopper_slots,
143145
};
144146

include/hopper/client/py/hoppermodule.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include <Python.h>
66

77
// clang-format off
8-
struct _hopper_pipe {
8+
struct py_hopper_pipe {
99
PyObject_HEAD
1010
PyObject *name;
1111
PyObject *endpoint;
@@ -17,13 +17,13 @@ struct _hopper_pipe {
1717

1818
// These macros are horrible, but hard to replace :(
1919
#define Hopper_Pipe_GET(field) \
20-
static PyObject *_hopper_pipe_get##field(struct _hopper_pipe *self, \
20+
static PyObject *_hopper_pipe_get##field(struct py_hopper_pipe *self, \
2121
void *closure) { \
2222
return Py_NewRef(self->field); \
2323
}
2424

2525
#define Hopper_Pipe_SETSTR(field) \
26-
static int _hopper_pipe_set##field(struct _hopper_pipe *self, \
26+
static int _hopper_pipe_set##field(struct py_hopper_pipe *self, \
2727
PyObject *value, void *closure) { \
2828
if (!value) { \
2929
PyErr_SetString(PyExc_TypeError, "cannot delete " #field); \

0 commit comments

Comments
 (0)