chiark / gitweb /
selpk.pyx: Fix bad cut-and-paste in the callback property.
[mLib-python] / selpk.pyx
CommitLineData
5b1830f3
MW
1### -*-pyrex-*-
2###
3### Selecting packet-buffer
4###
5### (c) 2005 Straylight/Edgeware
6###
579d0169 7
5b1830f3
MW
8###----- Licensing notice ---------------------------------------------------
9###
10### This file is part of the Python interface to mLib.
11###
12### mLib/Python is free software; you can redistribute it and/or modify
13### it under the terms of the GNU General Public License as published by
14### the Free Software Foundation; either version 2 of the License, or
15### (at your option) any later version.
16###
17### mLib/Python is distributed in the hope that it will be useful,
18### but WITHOUT ANY WARRANTY; without even the implied warranty of
19### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20### GNU General Public License for more details.
21###
22### You should have received a copy of the GNU General Public License
23### along with mLib/Python; if not, write to the Free Software Foundation,
24### Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
579d0169 25
26cdef class SelPacketBuffer:
27 cdef selpk p
28 cdef _packet
29 cdef _eof
376ad06d 30 def __cinit__(me, fd, packetproc = None, eofproc = None, *hunoz, **hukairz):
579d0169 31 selpk_init(&me.p, &_sel, _getfd(fd), _selpkfunc, <void *>me)
32 selpk_disable(&me.p)
33 me._packet = _checkcallable(packetproc, 'packet proc')
34 me._eof = _checkcallable(eofproc, 'eof proc')
35 def __dealloc__(me):
36 selpk_destroy(&me.p)
37 property activep:
38 def __get__(me):
39 return _to_bool(me.p.pk.f & PKBUF_ENABLE)
40 property fd:
41 def __get__(me):
42 return me.p.reader.fd
43 property want:
44 def __get__(me):
45 return me.p.pk.want
46 def __set__(me, n):
47 if n <= 0:
48 raise TypeError, 'size must be positive'
49 selpk_want(&me.p, n)
bc57ab58 50 property packetproc:
579d0169 51 def __get__(me):
bc57ab58 52 return me._packet
579d0169 53 def __set__(me, proc):
bc57ab58 54 me._packet = _checkcallable(proc, 'packet proc')
579d0169 55 def __del__(me):
bc57ab58 56 me._packet = None
579d0169 57 property eofproc:
58 def __get__(me):
59 return me._eof
60 def __set__(me, proc):
61 me._eof = _checkcallable(proc, 'eof proc')
62 def __del__(me):
63 me._eof = None
64 def enable(me):
65 if me.p.pk.f & PKBUF_ENABLE:
66 raise ValueError, 'already enabled'
67 selpk_enable(&me.p)
68 me.enabled()
69 return me
70 def disable(me):
71 if not (me.p.pk.f & PKBUF_ENABLE):
72 raise ValueError, 'already disabled'
73 selpk_disable(&me.p)
74 me.disabled()
75 return me
76 def enabled(me):
77 pass
78 def disabled(me):
79 pass
80 def packet(me, pk):
81 return _maybecall(me._packet, (pk,))
82 def eof(me):
83 return _maybecall(me._eof, ())
84
b51b6cf0
MW
85cdef void _selpkfunc(unsigned char *p, size_t n, pkbuf *pk,
86 size_t *keep, void *arg):
579d0169 87 cdef SelPacketBuffer pb
88 cdef void *rp
78911cdb 89 cdef Py_ssize_t rn
579d0169 90 pb = <SelPacketBuffer>arg
91 if p is NULL:
92 pb.eof()
93 else:
94 r = pb.packet(PyString_FromStringAndSize(<char *>p, n))
95 if r is not None:
704500e1 96 PyObject_AsReadBuffer(r, <cvp *>&rp, &rn)
579d0169 97 if rn > n:
98 raise ValueError, 'remaining buffer too large'
99 if rn:
100 memcpy(p + n - rn, rp, rn)
101 keep[0] = rn
102
5b1830f3 103###----- That's all, folks --------------------------------------------------