Skip to content

Commit 5a4daa0

Browse files
committed
Add __enter__ and __exit__ methods
1 parent 6997c2b commit 5a4daa0

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

py2bit.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,19 @@ static PyObject *py2bitOpen(PyObject *self, PyObject *args, PyObject *kwds) {
3131
return NULL;
3232
}
3333

34+
PyObject *py2bitEnter(pyTwoBit_t *self, PyObject *args) {
35+
pyTwoBit_t *pytb = self->tb;
36+
37+
if(!pytb) {
38+
PyErr_SetString(PyExc_RuntimeError, "The 2bit file handle is not opened!");
39+
return NULL;
40+
}
41+
42+
Py_INCREF(self);
43+
44+
return (PyObject*) self;
45+
}
46+
3447
static void py2bitDealloc(pyTwoBit_t *self) {
3548
if(self->tb) twobitClose(self->tb);
3649
PyObject_DEL(self);

py2bit.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ typedef struct {
1010
} pyTwoBit_t;
1111

1212
static PyObject* py2bitOpen(PyObject *self, PyObject *args, PyObject *kwds);
13+
static PyObject *py2bitEnter(pyTwoBit_t *pybw, PyObject *args);
1314
static PyObject *py2bitInfo(pyTwoBit_t *pybw, PyObject *args);
1415
static PyObject* py2bitClose(pyTwoBit_t *pybw, PyObject *args);
1516
static PyObject* py2bitChroms(pyTwoBit_t *pybw, PyObject *args);
@@ -145,6 +146,8 @@ bases. Counts may sum to less than the length of the region for the same reason.
145146
>>> tb.bases(tb, \"chr1\", 24, 74, True)\n\
146147
{'A': 6, 'C': 6, 'T': 6, 'G': 6}\n\
147148
>>> tb.close()"},
149+
{"__enter__", (PyCFunction) py2bitEnter, METH_NOARGS, NULL},
150+
{"__exit__", (PyCFunction) py2bitClose, METH_VARARGS, NULL},
148151
{NULL, NULL, 0, NULL}
149152
};
150153

0 commit comments

Comments
 (0)