1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964
|
/*
* Copyright (C) 1998, 1999, 2001 by Bernhard Herzog.
*
* All Rights Reserved
*
* Permission to use, copy, modify, and distribute this software and
* its documentation for any purpose and without fee is hereby granted,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear in
* supporting documentation, and that the name of the author not be
* used in advertising or publicity pertaining to distribution of the
* software without specific, written prior permission.
*
* THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
* NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "filterobj.h"
#include <structmember.h>
/*
* Semantics of the methods read, write and close
* ==============================================
*
*
* size_t write(void *, PyObject * target, const char * buf, size_t length)
*
* When the buffer is full and data is written to the filter or when the
* encode filter is flushed, the buffer data has to be encoded and to be
* written to the target stream. The filter object will call the write
* method to achieve this.
*
* write is supposed to consume at least 1 byte of the data at buf and
* at most length bytes. It returns the number of bytes it actually read
* from buffer.
*
* If successful the return value must be at least 1 and at most length.
* 0 indicates indicates an error, that is, a problem with the
* underlying data target or an error in the encoder. The filter assumes
* that a Python exception has been raised.
*
*
*
* size_t read(void *, PyObject * source, char * buf, size_t length)
*
* When the buffer is empty and data is read from the decode filter, the
* filter calls read to read data from the data source, decode it and
* store into the buffer.
*
* read is supposed to put at least one byte into the buffer beginning
* at buf and at most length bytes. It returns the number of bytes
* actually written to buf.
*
* If successful, the return value must be at least 1 and at most
* length. If unsuccessful, a return value of 0 indicates either EOF in
* the data source or end of data or an error. End of data means that
* the decoder cannot deliver more data for decoder specific reasons,
* even if more data is available in the source. The filter assumes that
* a Python exception has been raised in case of an error.
*
* For compatibility, an EOF or EOD (end of data) condition is not
* converted into an exception.
*
*
* int close(void*, PyObject * target)
*
* Called when the filter is being closed. Any data still contained in
* filter internal state should be written to target. target should not
* be closed. Return 0 on success and EOF on error (and set an
* exception).
*/
#define Filter_IsEncoder(filter) ((filter)->write != NULL)
#define Filter_IsDecoder(filter) ((filter)->read != NULL)
#define Filter_CheckEncodeState(filter) \
(((filter)->flags & (FILTER_CLOSED | FILTER_EOF | FILTER_BAD))\
? setexc(filter) : 1)
#define Filter_CheckDecodeState(filter) \
(((filter)->flags & (FILTER_CLOSED | FILTER_BAD))\
? setexc(filter) : 1)
static int
setexc(FilterObject * self)
{
if (self->flags & FILTER_BAD)
{
PyErr_Format(PyExc_IOError, "filter %s in bad state",
PyString_AsString(self->filtername));
}
else if (self->flags & FILTER_CLOSED)
{
PyErr_Format(PyExc_IOError, "filter %s already closed",
PyString_AsString(self->filtername));
}
else if (self->flags & FILTER_EOF)
{
PyErr_Format(PyExc_EOFError, "filter %s reached EOF",
PyString_AsString(self->filtername));
}
return 0;
}
#define FILTER_BUFSIZE (BUFSIZ - 1)
static FilterObject *
new_filter(PyObject * stream, const char * name, int flags,
filter_close_proc close, filter_dealloc_proc dealloc,
void * client_data)
{
FilterObject * self;
self = PyObject_NEW(FilterObject, &FilterType);
if (!self)
return NULL;
self->buffer = PyMem_NEW(char, FILTER_BUFSIZE + 1);
if (!self->buffer)
{
error:
PyMem_DEL(self);
PyErr_NoMemory();
if (dealloc)
dealloc(client_data);
return NULL;
}
self->filtername = PyString_FromString(name);
if (!self->filtername)
{
PyMem_DEL(self->buffer);
goto error;
}
self->current = self->base = self->buffer + 1;
self->buffer_end = self->base + FILTER_BUFSIZE;
self->end = self->current;
self->stream = stream;
Py_INCREF(self->stream);
self->client_data = client_data;
self->dealloc = dealloc;
self->close = close;
self->write = NULL;
self->read = NULL;
self->flags = flags;
self->streampos = 0;
return self;
}
PyObject *
Filter_NewEncoder(PyObject * target, const char * name, int flags,
filter_write_proc write, filter_close_proc close,
filter_dealloc_proc dealloc, void * client_data)
{
FilterObject * self;
if (!PyFile_Check(target) && !Filter_Check(target))
{
PyErr_SetString(PyExc_TypeError, "target must be file or filter");
return NULL;
}
self = new_filter(target, name, flags, close, dealloc, client_data);
if (!self)
return NULL;
self->write = write;
self->end = self->buffer_end;
return (PyObject*)self;
}
PyObject *
Filter_NewDecoder(PyObject * source, const char * name, int flags,
filter_read_proc read, filter_close_proc close,
filter_dealloc_proc dealloc, void * client_data)
{
FilterObject * self;
/*
if (!PyFile_Check(source) && !Filter_Check(source))
{
PyErr_SetString(PyExc_TypeError, "source must be file or filter");
return NULL;
}
*/
self = new_filter(source, name, flags, close, dealloc, client_data);
if (!self)
return NULL;
self->read = read;
self->end = self->current;
return (PyObject*)self;
}
static void
filter_dealloc(FilterObject * self)
{
Filter_Close((PyObject*)self);
if (self->dealloc)
self->dealloc(self->client_data);
Py_DECREF(self->filtername);
Py_DECREF(self->stream);
PyMem_DEL(self->buffer);
PyMem_DEL(self);
}
static PyObject *
filter_repr(FilterObject * self)
{
char buf[1000];
PyObject * streamrepr;
streamrepr = PyObject_Repr(self->stream);
if (!streamrepr)
return NULL;
sprintf(buf, "<filter %.100s %s %.500s>",
PyString_AsString(self->filtername),
Filter_IsEncoder(self) ? "writing to" : "reading from",
PyString_AsString(streamrepr));
Py_DECREF(streamrepr);
return PyString_FromString(buf);
}
int
_Filter_Overflow(FilterObject * self, int c)
{
if (Filter_Flush((PyObject*)self, 1) != EOF)
{
*self->current++ = c;
return c;
}
return EOF;
}
static int
_Filter_Uflow(FilterObject * self)
{
if (Filter_IsDecoder(self))
{
size_t result;
if (!Filter_CheckDecodeState(self) || self->flags & FILTER_EOF)
return EOF;
if (self->current == self->end)
{
result = self->read(self->client_data, self->stream, self->base,
self->buffer_end - self->base);
if (result == 0)
{
if (PyErr_Occurred())
{
self->flags |= FILTER_BAD;
}
else
{
self->flags |= FILTER_EOF;
}
return EOF;
}
self->current = self->base;
self->end = self->current + result;
self->streampos += result;
}
return *self->current & 0377;
}
return EOF;
}
int
_Filter_Underflow(FilterObject * self)
{
int c;
c = _Filter_Uflow(self);
if (c != EOF)
self->current++;
return c;
}
int
Filter_Flush(PyObject * filter, int flush_target)
{
FilterObject * self;
if (!Filter_Check(filter))
{
PyErr_SetString(PyExc_TypeError, "FilterObject expected");
return EOF;
}
self = (FilterObject*)filter;
if (Filter_IsEncoder(self))
{
size_t result, length;
if (!Filter_CheckEncodeState(self))
return EOF;
length = self->current - self->base;
while (length > 0)
{
result = self->write(self->client_data, self->stream,
self->current - length, length);
if (result == 0)
{
self->flags |= FILTER_BAD;
return EOF;
}
length -= result;
}
self->current = self->base;
/* XXX flush target even if error occurred? */
if (flush_target)
{
if (PyFile_Check(self->stream))
{
int fflush_result;
Py_BEGIN_ALLOW_THREADS
fflush_result = fflush(PyFile_AsFile(self->stream));
Py_END_ALLOW_THREADS
if (result < 0)
{
PyErr_SetFromErrno(PyExc_IOError);
return EOF;
}
return 0;
}
else if (Filter_Check(self->stream))
return Filter_Flush(self->stream, flush_target);
}
return 0;
}
else
{
PyErr_SetString(PyExc_TypeError, "flush requires an encode filter");
}
return EOF;
}
int
Filter_Close(PyObject * filter)
{
FilterObject * self;
int result = 0;
if (!Filter_Check(filter))
{
PyErr_SetString(PyExc_TypeError, "FilterObject expected");
return EOF;
}
self = (FilterObject*)filter;
if (self->flags & FILTER_CLOSED)
/* filter is already closed, do nothing */
return 0;
if (Filter_IsEncoder(self) && Filter_Flush((PyObject*)self, 1) < 0)
return EOF;
if (self->close)
result = self->close(self->client_data, self->stream);
self->flags |= FILTER_CLOSED;
return result;
}
size_t
Filter_Read(PyObject * filter, char * buffer, size_t length)
{
if (length <= 0)
return 0;
if (PyFile_Check(filter))
{
FILE * file = PyFile_AsFile(filter);
size_t result;
Py_BEGIN_ALLOW_THREADS
result = fread(buffer, 1, length, file);
Py_END_ALLOW_THREADS
if (result == 0)
{
if (ferror(file))
{
PyErr_SetFromErrno(PyExc_IOError);
}
return 0;
}
return result;
}
else if (Filter_Check(filter))
{
size_t to_do = length;
size_t count;
char * dest = buffer;
FilterObject * self = (FilterObject*)filter;
if (!Filter_CheckDecodeState(self) || self->flags & FILTER_EOF)
return 0;
for (;;)
{
count = self->end - self->current;
if (count > to_do)
count = to_do;
if (count > 0)
{
memcpy(dest, self->current, count);
self->current += count;
dest += count;
to_do -= count;
}
if (to_do == 0 || _Filter_Uflow(self) == EOF)
break;
}
if (PyErr_Occurred())
return 0;
return length - to_do;
}
PyErr_SetString(PyExc_TypeError,
"filter may be FileObject or FilterObject");
return 0;
}
size_t
Filter_ReadToChar(PyObject * filter, char * buffer, size_t length,
int endchar)
{
if (length <= 0)
return 0;
if (Filter_Check(filter))
{
FilterObject * self = (FilterObject*)filter;
int c;
char * dest = buffer, *end = buffer + length;
for (;;)
{
c = Filter_GETC(self);
if (c == EOF)
break;
*dest++ = c;
if (c == endchar || dest == end)
break;
}
if ((c == EOF && dest == buffer) || PyErr_Occurred())
return 0;
return dest - buffer;
}
else if (PyFile_Check(filter))
{
FILE* file = PyFile_AsFile(filter);
int c;
char * dest = buffer, *end = buffer + length;
Py_BEGIN_ALLOW_THREADS
for (;;)
{
c = getc(file);
if (c == EOF)
break;
*dest++ = c;
if (c == endchar || dest == end)
break;
}
Py_END_ALLOW_THREADS
if (c == EOF && dest == buffer)
{
if (ferror(file))
PyErr_SetFromErrno(PyExc_IOError);
return 0;
}
return dest - buffer;
}
PyErr_SetString(PyExc_TypeError,
"filter must be FilterObject or FileObject");
return 0;
}
#define BUF(v) PyString_AS_STRING((PyStringObject *)v)
PyObject *
Filter_GetLine(PyObject * filter, int n)
{
int n1, n2;
size_t charsread;
char * buf, *end;
PyObject *v;
if (!Filter_Check(filter))
{
PyErr_SetString(PyExc_TypeError, "FilterObject expected");
return NULL;
}
n2 = n > 0 ? n : 100;
v = PyString_FromStringAndSize((char *)NULL, n2);
if (v == NULL)
return NULL;
buf = BUF(v);
end = buf + n2;
for (;;)
{
charsread = Filter_ReadToChar(filter, buf, n2, '\n');
if (charsread == 0)
{
if (PyErr_CheckSignals())
{
Py_DECREF(v);
return NULL;
}
if (n < 0 && buf == BUF(v)) {
Py_DECREF(v);
PyErr_SetString(PyExc_EOFError, "EOF when reading a line");
return NULL;
}
break;
}
buf += charsread;
if (buf[-1] == '\n')
{
if (n < 0)
buf--;
break;
}
if (buf == end)
{
if (n > 0)
break;
n1 = n2;
n2 += 1000;
if (_PyString_Resize(&v, n2) < 0)
return NULL;
buf = BUF(v) + n1;
end = BUF(v) + n2;
}
}
n1 = buf - BUF(v);
if (n1 != n2)
_PyString_Resize(&v, n1);
return v;
}
int Filter_Ungetc(PyObject * filter, int c)
{
if (Filter_Check(filter))
{
FilterObject * self = (FilterObject*)filter;
if (self->current >= self->base)
{
self->current -= 1;
*(self->current) = c;
}
}
else
{
PyErr_SetString(PyExc_TypeError, "FilterObject required");
return -1;
}
return 0;
}
int
Filter_Write(PyObject * filter, const char * buffer, size_t length)
{
if (length <= 0)
return 0;
if (PyFile_Check(filter))
{
FILE * file = PyFile_AsFile(filter);
int result;
Py_BEGIN_ALLOW_THREADS
result = fwrite(buffer, 1, length, file);
Py_END_ALLOW_THREADS
if (result < length)
{
if (ferror(file))
{
PyErr_SetFromErrno(PyExc_IOError);
return EOF;
}
}
return result;
}
else if (Filter_Check(filter))
{
size_t to_do = length;
size_t count;
const unsigned char * src = (const unsigned char*)buffer;
FilterObject * self = (FilterObject*)filter;
for (;;)
{
count = self->end - self->current;
if (count > to_do)
count = to_do;
if (count > 0)
{
memcpy(self->current, src, count);
self->current += count;
src += count;
to_do -= count;
}
if (to_do == 0 || _Filter_Overflow(self, *src++) == EOF)
break;
to_do -= 1;
}
if (to_do != 0 || PyErr_Occurred())
return EOF;
return length - to_do;
}
PyErr_SetString(PyExc_TypeError,
"filter may be FileObject or FilterObject");
return EOF;
}
static PyObject *
filter_read(PyObject * self, PyObject * args)
{
int length;
size_t read;
PyObject * string;
if (!PyArg_ParseTuple(args, "i", &length))
return NULL;
string = PyString_FromStringAndSize((const char*)NULL, length);
if (!string)
return NULL;
read = Filter_Read(self, PyString_AsString(string), length);
if (read == 0)
{
Py_DECREF(string);
if (PyErr_Occurred())
return NULL;
return PyString_FromString("");
}
if (read < length)
{
if (_PyString_Resize(&string, read) < 0)
return NULL;
}
return string;
}
static PyObject *
filter_readline(PyObject * self, PyObject * args)
{
int length = -1;
if (!PyArg_ParseTuple(args, "|i", &length))
return NULL;
if (length == 0)
return PyString_FromString("");
if (length < 0)
length = 0;
return Filter_GetLine(self, length);
}
#if BUFSIZ < 8192
#define SMALLCHUNK 8192
#else
#define SMALLCHUNK BUFSIZ
#endif
static PyObject *
filter_readlines(PyObject *self, PyObject *args)
{
long sizehint = 0;
PyObject *list;
PyObject *line;
char small_buffer[SMALLCHUNK];
char *buffer = small_buffer;
size_t buffersize = SMALLCHUNK;
PyObject *big_buffer = NULL;
size_t nfilled = 0;
size_t nread;
size_t totalread = 0;
char *p, *q, *end;
int err;
if (!PyArg_ParseTuple(args, "|l", &sizehint))
return NULL;
if ((list = PyList_New(0)) == NULL)
return NULL;
for (;;) {
nread = Filter_Read(self, buffer + nfilled, buffersize - nfilled);
if (nread == 0) {
sizehint = 0;
if (!PyErr_Occurred())
break;
error:
Py_DECREF(list);
list = NULL;
goto cleanup;
}
totalread += nread;
p = memchr(buffer+nfilled, '\n', nread);
if (p == NULL) {
/* Need a larger buffer to fit this line */
nfilled += nread;
buffersize *= 2;
if (big_buffer == NULL) {
/* Create the big buffer */
big_buffer = PyString_FromStringAndSize(NULL, buffersize);
if (big_buffer == NULL)
goto error;
buffer = PyString_AS_STRING(big_buffer);
memcpy(buffer, small_buffer, nfilled);
}
else {
/* Grow the big buffer */
if (_PyString_Resize(&big_buffer, buffersize) < 0)
goto error;
buffer = PyString_AS_STRING(big_buffer);
}
continue;
}
end = buffer+nfilled+nread;
q = buffer;
do {
/* Process complete lines */
p++;
line = PyString_FromStringAndSize(q, p-q);
if (line == NULL)
goto error;
err = PyList_Append(list, line);
Py_DECREF(line);
if (err != 0)
goto error;
q = p;
p = memchr(q, '\n', end-q);
} while (p != NULL);
/* Move the remaining incomplete line to the start */
nfilled = end-q;
memmove(buffer, q, nfilled);
if (sizehint > 0)
if (totalread >= (size_t)sizehint)
break;
}
if (nfilled != 0) {
/* Partial last line */
line = PyString_FromStringAndSize(buffer, nfilled);
if (line == NULL)
goto error;
if (sizehint > 0) {
/* Need to complete the last line */
PyObject *rest = Filter_GetLine(self, 0);
if (rest == NULL) {
Py_DECREF(line);
goto error;
}
PyString_Concat(&line, rest);
Py_DECREF(rest);
if (line == NULL)
goto error;
}
err = PyList_Append(list, line);
Py_DECREF(line);
if (err != 0)
goto error;
}
cleanup:
if (big_buffer) {
Py_DECREF(big_buffer);
}
return list;
}
static PyObject *
filter_write(PyObject * self, PyObject * args)
{
const char * buffer;
int length;
if (!PyArg_ParseTuple(args, "s#", &buffer, &length))
return NULL;
if (Filter_Write(self, buffer, length) == EOF)
return NULL;
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
filter_flush(PyObject * self, PyObject * args)
{
int flush_target = 1;
if (!PyArg_ParseTuple(args, "|i", &flush_target))
return NULL;
if (Filter_Flush(self, flush_target) < 0)
return NULL;
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
filter_close(PyObject * self, PyObject * args)
{
if (!PyArg_ParseTuple(args, ""))
return NULL;
if (Filter_Close(self) < 0)
return NULL;
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
filter_seek(FilterObject * self, PyObject * args)
{
int pos;
long cur_pos, offset;
if (!PyArg_ParseTuple(args, "i", &pos))
return NULL;
cur_pos = self->streampos - (self->end - self->current);
offset = pos - cur_pos;
if (self->base - self->current <= offset
&& offset < self->end - self->current)
{
self->current += offset;
/* reset the EOF flag if we're not at the end */
if (self->current < self->end)
self->flags &= ~FILTER_EOF;
}
else
{
PyErr_SetString(PyExc_IOError, "cannot seek to specified position");
return NULL;
}
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
filter_tell(FilterObject * self, PyObject * args)
{
long cur_pos;
if (!PyArg_ParseTuple(args, ""))
return NULL;
cur_pos = self->streampos - (self->end - self->current);
return PyInt_FromLong(cur_pos);
}
#define OFF(x) offsetof(FilterObject, x)
static struct memberlist filter_memberlist[] = {
{"stream", T_OBJECT, OFF(stream), RO},
{"source", T_OBJECT, OFF(stream), RO},
{"target", T_OBJECT, OFF(stream), RO},
{NULL}
};
static struct PyMethodDef filter_methods[] = {
{"read", (PyCFunction)filter_read, 1},
{"write", (PyCFunction)filter_write, 1},
{"readline", (PyCFunction)filter_readline, 1},
{"readlines", (PyCFunction)filter_readlines, 1},
{"flush", (PyCFunction)filter_flush, 1},
{"seek", (PyCFunction)filter_seek, 1},
{"tell", (PyCFunction)filter_tell, 1},
{"close", (PyCFunction)filter_close, 1},
{NULL, NULL}
};
static PyObject *
filter_getattr(PyObject * self, char * name)
{
PyObject * result;
result = Py_FindMethod(filter_methods, self, name);
if (result != NULL)
return result;
PyErr_Clear();
return PyMember_Get((char *)self, filter_memberlist, name);
}
static int
filter_setattr(PyObject * self, char * name, PyObject * v)
{
if (v == NULL) {
PyErr_SetString(PyExc_AttributeError,
"can't delete object attributes");
return -1;
}
return PyMember_Set((char *)self, filter_memberlist, name, v);
}
PyTypeObject FilterType = {
PyObject_HEAD_INIT(&PyType_Type)
0,
"filter",
sizeof(FilterObject),
0,
(destructor)filter_dealloc, /*tp_dealloc*/
(printfunc)0, /*tp_print*/
filter_getattr, /*tp_getattr*/
filter_setattr, /*tp_setattr*/
(cmpfunc)0, /*tp_compare*/
(reprfunc)filter_repr, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash*/
};
|