annotate Objects/bufferobject.c @ 57303:4aa4c19dfaed legacy-trunk

#1757126: fix typo with the cyrillic_asian alias
author Philip Jenvey <pjenvey@underboss.org>
date Tue, 17 Nov 2009 02:42:26 +0000
parents 0bf6c42ab4e4
children fd25030223b0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 /* Buffer object implementation */
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 #include "Python.h"
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 typedef struct {
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 PyObject_HEAD
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 PyObject *b_base;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 void *b_ptr;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36082
diff changeset
11 Py_ssize_t b_size;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36082
diff changeset
12 Py_ssize_t b_offset;
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 int b_readonly;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 long b_hash;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 } PyBufferObject;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
16
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
17
38676
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
18 enum buffer_t {
38693
72e37ada4170 Fix inconsistency in naming within an enum.
Brett Cannon <bcannon@gmail.com>
parents: 38676
diff changeset
19 READ_BUFFER,
72e37ada4170 Fix inconsistency in naming within an enum.
Brett Cannon <bcannon@gmail.com>
parents: 38676
diff changeset
20 WRITE_BUFFER,
72e37ada4170 Fix inconsistency in naming within an enum.
Brett Cannon <bcannon@gmail.com>
parents: 38676
diff changeset
21 CHAR_BUFFER,
41996
1bbcd392417d Bug #1733488: Fix compilation of bufferobject.c on AIX.
Martin v. Löwis <martin@v.loewis.de>
parents: 39395
diff changeset
22 ANY_BUFFER
38676
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
23 };
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
24
31640
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
25 static int
38676
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
26 get_buf(PyBufferObject *self, void **ptr, Py_ssize_t *size,
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
27 enum buffer_t buffer_type)
31640
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
28 {
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
29 if (self->b_base == NULL) {
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
30 assert (ptr != NULL);
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
31 *ptr = self->b_ptr;
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
32 *size = self->b_size;
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
33 }
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
34 else {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36082
diff changeset
35 Py_ssize_t count, offset;
38731
0b3fe2b734a1 Suppress warning on MacOSX about possible use before set of proc.
Skip Montanaro <skip@pobox.com>
parents: 38693
diff changeset
36 readbufferproc proc = 0;
31640
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
37 PyBufferProcs *bp = self->b_base->ob_type->tp_as_buffer;
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
38 if ((*bp->bf_getsegcount)(self->b_base, NULL) != 1) {
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
39 PyErr_SetString(PyExc_TypeError,
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
40 "single-segment buffer object expected");
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
41 return 0;
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
42 }
38693
72e37ada4170 Fix inconsistency in naming within an enum.
Brett Cannon <bcannon@gmail.com>
parents: 38676
diff changeset
43 if ((buffer_type == READ_BUFFER) ||
38676
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
44 ((buffer_type == ANY_BUFFER) && self->b_readonly))
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
45 proc = bp->bf_getreadbuffer;
38693
72e37ada4170 Fix inconsistency in naming within an enum.
Brett Cannon <bcannon@gmail.com>
parents: 38676
diff changeset
46 else if ((buffer_type == WRITE_BUFFER) ||
38676
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
47 (buffer_type == ANY_BUFFER))
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
48 proc = (readbufferproc)bp->bf_getwritebuffer;
38693
72e37ada4170 Fix inconsistency in naming within an enum.
Brett Cannon <bcannon@gmail.com>
parents: 38676
diff changeset
49 else if (buffer_type == CHAR_BUFFER) {
38676
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
50 if (!PyType_HasFeature(self->ob_type,
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
51 Py_TPFLAGS_HAVE_GETCHARBUFFER)) {
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
52 PyErr_SetString(PyExc_TypeError,
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
53 "Py_TPFLAGS_HAVE_GETCHARBUFFER needed");
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
54 return 0;
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
55 }
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
56 proc = (readbufferproc)bp->bf_getcharbuffer;
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
57 }
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
58 if (!proc) {
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
59 char *buffer_type_name;
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
60 switch (buffer_type) {
38693
72e37ada4170 Fix inconsistency in naming within an enum.
Brett Cannon <bcannon@gmail.com>
parents: 38676
diff changeset
61 case READ_BUFFER:
38676
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
62 buffer_type_name = "read";
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
63 break;
38693
72e37ada4170 Fix inconsistency in naming within an enum.
Brett Cannon <bcannon@gmail.com>
parents: 38676
diff changeset
64 case WRITE_BUFFER:
38676
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
65 buffer_type_name = "write";
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
66 break;
38693
72e37ada4170 Fix inconsistency in naming within an enum.
Brett Cannon <bcannon@gmail.com>
parents: 38676
diff changeset
67 case CHAR_BUFFER:
38676
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
68 buffer_type_name = "char";
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
69 break;
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
70 default:
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
71 buffer_type_name = "no";
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
72 break;
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
73 }
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
74 PyErr_Format(PyExc_TypeError,
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
75 "%s buffer type not available",
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
76 buffer_type_name);
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
77 return 0;
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
78 }
31640
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
79 if ((count = (*proc)(self->b_base, 0, ptr)) < 0)
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
80 return 0;
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
81 /* apply constraints to the start/end */
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
82 if (self->b_offset > count)
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
83 offset = count;
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
84 else
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
85 offset = self->b_offset;
31810
7a120400888a Correct code to advance ptr to be well-formed C.
Martin v. Löwis <martin@v.loewis.de>
parents: 31640
diff changeset
86 *(char **)ptr = *(char **)ptr + offset;
31640
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
87 if (self->b_size == Py_END_OF_BUFFER)
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
88 *size = count;
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
89 else
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
90 *size = self->b_size;
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
91 if (offset + *size > count)
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
92 *size = count - offset;
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
93 }
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
94 return 1;
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
95 }
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
96
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
97
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36082
diff changeset
99 buffer_from_memory(PyObject *base, Py_ssize_t size, Py_ssize_t offset, void *ptr,
31640
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
100 int readonly)
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 {
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 PyBufferObject * b;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
103
31640
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
104 if (size < 0 && size != Py_END_OF_BUFFER) {
10427
e6799a855af7 Added check for negative offset for PyBuffer_FromObject and check for
Guido van Rossum <guido@python.org>
parents: 9305
diff changeset
105 PyErr_SetString(PyExc_ValueError,
e6799a855af7 Added check for negative offset for PyBuffer_FromObject and check for
Guido van Rossum <guido@python.org>
parents: 9305
diff changeset
106 "size must be zero or positive");
e6799a855af7 Added check for negative offset for PyBuffer_FromObject and check for
Guido van Rossum <guido@python.org>
parents: 9305
diff changeset
107 return NULL;
e6799a855af7 Added check for negative offset for PyBuffer_FromObject and check for
Guido van Rossum <guido@python.org>
parents: 9305
diff changeset
108 }
33559
be8cff4a1eca Fix buffer offset calculation (need to compute it before changing
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31810
diff changeset
109 if (offset < 0) {
be8cff4a1eca Fix buffer offset calculation (need to compute it before changing
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31810
diff changeset
110 PyErr_SetString(PyExc_ValueError,
be8cff4a1eca Fix buffer offset calculation (need to compute it before changing
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31810
diff changeset
111 "offset must be zero or positive");
be8cff4a1eca Fix buffer offset calculation (need to compute it before changing
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31810
diff changeset
112 return NULL;
be8cff4a1eca Fix buffer offset calculation (need to compute it before changing
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31810
diff changeset
113 }
10427
e6799a855af7 Added check for negative offset for PyBuffer_FromObject and check for
Guido van Rossum <guido@python.org>
parents: 9305
diff changeset
114
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 b = PyObject_NEW(PyBufferObject, &PyBuffer_Type);
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 if ( b == NULL )
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 return NULL;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
118
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 Py_XINCREF(base);
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 b->b_base = base;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 b->b_ptr = ptr;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 b->b_size = size;
31640
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
123 b->b_offset = offset;
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 b->b_readonly = readonly;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 b->b_hash = -1;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
126
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 return (PyObject *) b;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 }
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
129
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36082
diff changeset
131 buffer_from_object(PyObject *base, Py_ssize_t size, Py_ssize_t offset, int readonly)
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 {
33560
97cb9eea212e Ensure negative offsets cannot be passed to buffer(). When composing
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 33559
diff changeset
133 if (offset < 0) {
97cb9eea212e Ensure negative offsets cannot be passed to buffer(). When composing
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 33559
diff changeset
134 PyErr_SetString(PyExc_ValueError,
97cb9eea212e Ensure negative offsets cannot be passed to buffer(). When composing
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 33559
diff changeset
135 "offset must be zero or positive");
97cb9eea212e Ensure negative offsets cannot be passed to buffer(). When composing
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 33559
diff changeset
136 return NULL;
97cb9eea212e Ensure negative offsets cannot be passed to buffer(). When composing
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 33559
diff changeset
137 }
33559
be8cff4a1eca Fix buffer offset calculation (need to compute it before changing
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31810
diff changeset
138 if ( PyBuffer_Check(base) && (((PyBufferObject *)base)->b_base) ) {
be8cff4a1eca Fix buffer offset calculation (need to compute it before changing
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31810
diff changeset
139 /* another buffer, refer to the base object */
33560
97cb9eea212e Ensure negative offsets cannot be passed to buffer(). When composing
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 33559
diff changeset
140 PyBufferObject *b = (PyBufferObject *)base;
97cb9eea212e Ensure negative offsets cannot be passed to buffer(). When composing
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 33559
diff changeset
141 if (b->b_size != Py_END_OF_BUFFER) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36082
diff changeset
142 Py_ssize_t base_size = b->b_size - offset;
33560
97cb9eea212e Ensure negative offsets cannot be passed to buffer(). When composing
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 33559
diff changeset
143 if (base_size < 0)
97cb9eea212e Ensure negative offsets cannot be passed to buffer(). When composing
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 33559
diff changeset
144 base_size = 0;
97cb9eea212e Ensure negative offsets cannot be passed to buffer(). When composing
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 33559
diff changeset
145 if (size == Py_END_OF_BUFFER || size > base_size)
97cb9eea212e Ensure negative offsets cannot be passed to buffer(). When composing
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 33559
diff changeset
146 size = base_size;
97cb9eea212e Ensure negative offsets cannot be passed to buffer(). When composing
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 33559
diff changeset
147 }
97cb9eea212e Ensure negative offsets cannot be passed to buffer(). When composing
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 33559
diff changeset
148 offset += b->b_offset;
97cb9eea212e Ensure negative offsets cannot be passed to buffer(). When composing
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 33559
diff changeset
149 base = b->b_base;
10427
e6799a855af7 Added check for negative offset for PyBuffer_FromObject and check for
Guido van Rossum <guido@python.org>
parents: 9305
diff changeset
150 }
31640
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
151 return buffer_from_memory(base, size, offset, NULL, readonly);
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 }
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
153
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
154
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36082
diff changeset
156 PyBuffer_FromObject(PyObject *base, Py_ssize_t offset, Py_ssize_t size)
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 {
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 PyBufferProcs *pb = base->ob_type->tp_as_buffer;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
159
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 if ( pb == NULL ||
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 pb->bf_getreadbuffer == NULL ||
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 pb->bf_getsegcount == NULL )
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 {
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 PyErr_SetString(PyExc_TypeError, "buffer object expected");
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 return NULL;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 }
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
167
31640
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
168 return buffer_from_object(base, size, offset, 1);
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 }
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
170
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36082
diff changeset
172 PyBuffer_FromReadWriteObject(PyObject *base, Py_ssize_t offset, Py_ssize_t size)
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 {
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 PyBufferProcs *pb = base->ob_type->tp_as_buffer;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
175
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 if ( pb == NULL ||
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 pb->bf_getwritebuffer == NULL ||
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 pb->bf_getsegcount == NULL )
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
179 {
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 PyErr_SetString(PyExc_TypeError, "buffer object expected");
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
181 return NULL;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 }
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
183
31640
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
184 return buffer_from_object(base, size, offset, 0);
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 }
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
186
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
187 PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36082
diff changeset
188 PyBuffer_FromMemory(void *ptr, Py_ssize_t size)
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 {
31640
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
190 return buffer_from_memory(NULL, size, 0, ptr, 1);
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
191 }
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
192
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36082
diff changeset
194 PyBuffer_FromReadWriteMemory(void *ptr, Py_ssize_t size)
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 {
31640
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
196 return buffer_from_memory(NULL, size, 0, ptr, 0);
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 }
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
198
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
199 PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36082
diff changeset
200 PyBuffer_New(Py_ssize_t size)
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
201 {
13861
f3d5f71ccbb4 Removing warnings found by gcc -Wall
Moshe Zadka <moshez@math.huji.ac.il>
parents: 13449
diff changeset
202 PyObject *o;
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
203 PyBufferObject * b;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
204
11175
fab81df566d0 PyBuffer_New(): Raise ValueError if size is negative (the other
Fred Drake <fdrake@acm.org>
parents: 10460
diff changeset
205 if (size < 0) {
fab81df566d0 PyBuffer_New(): Raise ValueError if size is negative (the other
Fred Drake <fdrake@acm.org>
parents: 10460
diff changeset
206 PyErr_SetString(PyExc_ValueError,
fab81df566d0 PyBuffer_New(): Raise ValueError if size is negative (the other
Fred Drake <fdrake@acm.org>
parents: 10460
diff changeset
207 "size must be zero or positive");
fab81df566d0 PyBuffer_New(): Raise ValueError if size is negative (the other
Fred Drake <fdrake@acm.org>
parents: 10460
diff changeset
208 return NULL;
fab81df566d0 PyBuffer_New(): Raise ValueError if size is negative (the other
Fred Drake <fdrake@acm.org>
parents: 10460
diff changeset
209 }
47587
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
210 if (sizeof(*b) > PY_SSIZE_T_MAX - size) {
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
211 /* unlikely */
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
212 return PyErr_NoMemory();
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
213 }
24898
a869d2de536f Call me anal, but there was a particular phrase that was speading to
Guido van Rossum <guido@python.org>
parents: 23972
diff changeset
214 /* Inline PyObject_New */
37589
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36533
diff changeset
215 o = (PyObject *)PyObject_MALLOC(sizeof(*b) + size);
13861
f3d5f71ccbb4 Removing warnings found by gcc -Wall
Moshe Zadka <moshez@math.huji.ac.il>
parents: 13449
diff changeset
216 if ( o == NULL )
11175
fab81df566d0 PyBuffer_New(): Raise ValueError if size is negative (the other
Fred Drake <fdrake@acm.org>
parents: 10460
diff changeset
217 return PyErr_NoMemory();
13861
f3d5f71ccbb4 Removing warnings found by gcc -Wall
Moshe Zadka <moshez@math.huji.ac.il>
parents: 13449
diff changeset
218 b = (PyBufferObject *) PyObject_INIT(o, &PyBuffer_Type);
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
219
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 b->b_base = NULL;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 b->b_ptr = (void *)(b + 1);
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
222 b->b_size = size;
31640
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
223 b->b_offset = 0;
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
224 b->b_readonly = 0;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
225 b->b_hash = -1;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
226
13861
f3d5f71ccbb4 Removing warnings found by gcc -Wall
Moshe Zadka <moshez@math.huji.ac.il>
parents: 13449
diff changeset
227 return o;
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
228 }
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
229
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
230 /* Methods */
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
231
23868
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 22651
diff changeset
232 static PyObject *
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 22651
diff changeset
233 buffer_new(PyTypeObject *type, PyObject *args, PyObject *kw)
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 22651
diff changeset
234 {
46080
2a866ea66509 Move declarations to block start.
Georg Brandl <georg@python.org>
parents: 46075
diff changeset
235 PyObject *ob;
2a866ea66509 Move declarations to block start.
Georg Brandl <georg@python.org>
parents: 46075
diff changeset
236 Py_ssize_t offset = 0;
2a866ea66509 Move declarations to block start.
Georg Brandl <georg@python.org>
parents: 46075
diff changeset
237 Py_ssize_t size = Py_END_OF_BUFFER;
2a866ea66509 Move declarations to block start.
Georg Brandl <georg@python.org>
parents: 46075
diff changeset
238
48193
0bf6c42ab4e4 Change the warning emitted for using the buffer() object; memoryview() in 3.0
Brett Cannon <bcannon@gmail.com>
parents: 48114
diff changeset
239 if (PyErr_WarnPy3k("buffer() not supported in 3.x", 1) < 0)
46074
9b3f0fa5d8ca #2355: py3k warning for buffer().
Georg Brandl <georg@python.org>
parents: 43464
diff changeset
240 return NULL;
9b3f0fa5d8ca #2355: py3k warning for buffer().
Georg Brandl <georg@python.org>
parents: 43464
diff changeset
241
35450
97c97194e64b Disallow keyword arguments for type constructors that don't use them
Georg Brandl <georg@python.org>
parents: 33560
diff changeset
242 if (!_PyArg_NoKeywords("buffer()", kw))
97c97194e64b Disallow keyword arguments for type constructors that don't use them
Georg Brandl <georg@python.org>
parents: 33560
diff changeset
243 return NULL;
97c97194e64b Disallow keyword arguments for type constructors that don't use them
Georg Brandl <georg@python.org>
parents: 33560
diff changeset
244
36533
ed07c1a5e24c Use proper PyArg_Parse format char for Py_ssize_t, instead of 'l', in
Thomas Wouters <thomas@python.org>
parents: 36511
diff changeset
245 if (!PyArg_ParseTuple(args, "O|nn:buffer", &ob, &offset, &size))
23868
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 22651
diff changeset
246 return NULL;
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 22651
diff changeset
247 return PyBuffer_FromObject(ob, offset, size);
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 22651
diff changeset
248 }
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 22651
diff changeset
249
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 22651
diff changeset
250 PyDoc_STRVAR(buffer_doc,
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 22651
diff changeset
251 "buffer(object [, offset[, size]])\n\
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 22651
diff changeset
252 \n\
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 22651
diff changeset
253 Create a new buffer object which references the given object.\n\
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 22651
diff changeset
254 The buffer will reference a slice of the target object from the\n\
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 22651
diff changeset
255 start of the object (or at the specified offset). The slice will\n\
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 22651
diff changeset
256 extend to the end of the target object (or with the specified size).");
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 22651
diff changeset
257
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 22651
diff changeset
258
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
259 static void
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
260 buffer_dealloc(PyBufferObject *self)
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
261 {
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
262 Py_XDECREF(self->b_base);
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11175
diff changeset
263 PyObject_DEL(self);
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
264 }
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
265
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
266 static int
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
267 buffer_compare(PyBufferObject *self, PyBufferObject *other)
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
268 {
31640
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
269 void *p1, *p2;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36082
diff changeset
270 Py_ssize_t len_self, len_other, min_len;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36082
diff changeset
271 int cmp;
31640
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
272
38676
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
273 if (!get_buf(self, &p1, &len_self, ANY_BUFFER))
31640
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
274 return -1;
38676
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
275 if (!get_buf(other, &p2, &len_other, ANY_BUFFER))
31640
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
276 return -1;
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
277 min_len = (len_self < len_other) ? len_self : len_other;
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
278 if (min_len > 0) {
31640
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
279 cmp = memcmp(p1, p2, min_len);
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
280 if (cmp != 0)
39395
997788de8667 memcmp() can return values other than -1, 0, and +1 but tp_compare
Thomas Heller <theller@ctypes.org>
parents: 38731
diff changeset
281 return cmp < 0 ? -1 : 1;
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
282 }
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
283 return (len_self < len_other) ? -1 : (len_self > len_other) ? 1 : 0;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
284 }
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
285
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
286 static PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
287 buffer_repr(PyBufferObject *self)
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
288 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36082
diff changeset
289 const char *status = self->b_readonly ? "read-only" : "read-write";
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
290
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
291 if ( self->b_base == NULL )
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
292 return PyString_FromFormat("<%s buffer ptr %p, size %zd at %p>",
19476
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 18933
diff changeset
293 status,
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 18933
diff changeset
294 self->b_ptr,
36511
1596ff5c79b5 Revert 42400.
Martin v. Löwis <martin@v.loewis.de>
parents: 36510
diff changeset
295 self->b_size,
19476
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 18933
diff changeset
296 self);
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
297 else
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
298 return PyString_FromFormat(
36510
0f3894bb2ada Support %zd in PyErr_Format and PyString_FromFormat.
Martin v. Löwis <martin@v.loewis.de>
parents: 36509
diff changeset
299 "<%s buffer for %p, size %zd, offset %zd at %p>",
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
300 status,
13210
ff243b0f16ee Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
301 self->b_base,
36511
1596ff5c79b5 Revert 42400.
Martin v. Löwis <martin@v.loewis.de>
parents: 36510
diff changeset
302 self->b_size,
1596ff5c79b5 Revert 42400.
Martin v. Löwis <martin@v.loewis.de>
parents: 36510
diff changeset
303 self->b_offset,
13210
ff243b0f16ee Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
304 self);
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
305 }
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
306
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
307 static long
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
308 buffer_hash(PyBufferObject *self)
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
309 {
31640
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
310 void *ptr;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36082
diff changeset
311 Py_ssize_t size;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36082
diff changeset
312 register Py_ssize_t len;
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
313 register unsigned char *p;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
314 register long x;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
315
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
316 if ( self->b_hash != -1 )
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
317 return self->b_hash;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
318
31639
047148089c35 Document one of the many problems with the buffer object.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31638
diff changeset
319 /* XXX potential bugs here, a readonly buffer does not imply that the
047148089c35 Document one of the many problems with the buffer object.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31638
diff changeset
320 * underlying memory is immutable. b_readonly is a necessary but not
047148089c35 Document one of the many problems with the buffer object.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31638
diff changeset
321 * sufficient condition for a buffer to be hashable. Perhaps it would
047148089c35 Document one of the many problems with the buffer object.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31638
diff changeset
322 * be better to only allow hashing if the underlying object is known to
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
323 * be immutable (e.g. PyString_Check() is true). Another idea would
31639
047148089c35 Document one of the many problems with the buffer object.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31638
diff changeset
324 * be to call tp_hash on the underlying object and see if it raises
047148089c35 Document one of the many problems with the buffer object.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31638
diff changeset
325 * an error. */
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
326 if ( !self->b_readonly )
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
327 {
31639
047148089c35 Document one of the many problems with the buffer object.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31638
diff changeset
328 PyErr_SetString(PyExc_TypeError,
047148089c35 Document one of the many problems with the buffer object.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31638
diff changeset
329 "writable buffers are not hashable");
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
330 return -1;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
331 }
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
332
38676
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
333 if (!get_buf(self, &ptr, &size, ANY_BUFFER))
31640
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
334 return -1;
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
335 p = (unsigned char *) ptr;
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
336 len = size;
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
337 x = *p << 7;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
338 while (--len >= 0)
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
339 x = (1000003*x) ^ *p++;
31640
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
340 x ^= size;
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
341 if (x == -1)
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
342 x = -2;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
343 self->b_hash = x;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
344 return x;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
345 }
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
346
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
347 static PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
348 buffer_str(PyBufferObject *self)
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
349 {
31640
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
350 void *ptr;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36082
diff changeset
351 Py_ssize_t size;
38676
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
352 if (!get_buf(self, &ptr, &size, ANY_BUFFER))
31640
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
353 return NULL;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
354 return PyString_FromStringAndSize((const char *)ptr, size);
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
355 }
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
356
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
357 /* Sequence methods */
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
358
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36082
diff changeset
359 static Py_ssize_t
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
360 buffer_length(PyBufferObject *self)
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
361 {
31640
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
362 void *ptr;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36082
diff changeset
363 Py_ssize_t size;
38676
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
364 if (!get_buf(self, &ptr, &size, ANY_BUFFER))
31640
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
365 return -1;
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
366 return size;
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
367 }
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
368
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
369 static PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
370 buffer_concat(PyBufferObject *self, PyObject *other)
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
371 {
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
372 PyBufferProcs *pb = other->ob_type->tp_as_buffer;
31640
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
373 void *ptr1, *ptr2;
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
374 char *p;
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
375 PyObject *ob;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36082
diff changeset
376 Py_ssize_t size, count;
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
377
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
378 if ( pb == NULL ||
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
379 pb->bf_getreadbuffer == NULL ||
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
380 pb->bf_getsegcount == NULL )
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
381 {
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
382 PyErr_BadArgument();
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
383 return NULL;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
384 }
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
385 if ( (*pb->bf_getsegcount)(other, NULL) != 1 )
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
386 {
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
387 /* ### use a different exception type/message? */
10460
62781914fadb Folded long lines.
Guido van Rossum <guido@python.org>
parents: 10427
diff changeset
388 PyErr_SetString(PyExc_TypeError,
62781914fadb Folded long lines.
Guido van Rossum <guido@python.org>
parents: 10427
diff changeset
389 "single-segment buffer object expected");
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
390 return NULL;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
391 }
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
392
38676
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
393 if (!get_buf(self, &ptr1, &size, ANY_BUFFER))
31640
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
394 return NULL;
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
395
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
396 /* optimize special case */
31640
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
397 if ( size == 0 )
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
398 {
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
399 Py_INCREF(other);
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
400 return other;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
401 }
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
402
31640
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
403 if ( (count = (*pb->bf_getreadbuffer)(other, 0, &ptr2)) < 0 )
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
404 return NULL;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
405
47587
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
406 assert(count <= PY_SIZE_MAX - size);
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
407
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
408 ob = PyString_FromStringAndSize(NULL, size + count);
36082
3f358afbb3d3 Check return result for error
Neal Norwitz <nnorwitz@gmail.com>
parents: 35450
diff changeset
409 if ( ob == NULL )
3f358afbb3d3 Check return result for error
Neal Norwitz <nnorwitz@gmail.com>
parents: 35450
diff changeset
410 return NULL;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
411 p = PyString_AS_STRING(ob);
31640
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
412 memcpy(p, ptr1, size);
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
413 memcpy(p + size, ptr2, count);
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
414
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
415 /* there is an extra byte in the string object, so this is safe */
31640
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
416 p[size + count] = '\0';
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
417
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
418 return ob;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
419 }
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
420
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
421 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36082
diff changeset
422 buffer_repeat(PyBufferObject *self, Py_ssize_t count)
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
423 {
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
424 PyObject *ob;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
425 register char *p;
31640
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
426 void *ptr;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36082
diff changeset
427 Py_ssize_t size;
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
428
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
429 if ( count < 0 )
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
430 count = 0;
38676
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
431 if (!get_buf(self, &ptr, &size, ANY_BUFFER))
31640
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
432 return NULL;
48114
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 47587
diff changeset
433 if (count > PY_SSIZE_T_MAX / size) {
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 47587
diff changeset
434 PyErr_SetString(PyExc_MemoryError, "result too large");
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 47587
diff changeset
435 return NULL;
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 47587
diff changeset
436 }
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
437 ob = PyString_FromStringAndSize(NULL, size * count);
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
438 if ( ob == NULL )
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
439 return NULL;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
440
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
441 p = PyString_AS_STRING(ob);
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
442 while ( count-- )
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
443 {
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
444 memcpy(p, ptr, size);
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
445 p += size;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
446 }
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
447
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
448 /* there is an extra byte in the string object, so this is safe */
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
449 *p = '\0';
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
450
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
451 return ob;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
452 }
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
453
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
454 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36082
diff changeset
455 buffer_item(PyBufferObject *self, Py_ssize_t idx)
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
456 {
31640
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
457 void *ptr;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36082
diff changeset
458 Py_ssize_t size;
38676
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
459 if (!get_buf(self, &ptr, &size, ANY_BUFFER))
31640
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
460 return NULL;
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
461 if ( idx < 0 || idx >= size ) {
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
462 PyErr_SetString(PyExc_IndexError, "buffer index out of range");
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
463 return NULL;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
464 }
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
465 return PyString_FromStringAndSize((char *)ptr + idx, 1);
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
466 }
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
467
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
468 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36082
diff changeset
469 buffer_slice(PyBufferObject *self, Py_ssize_t left, Py_ssize_t right)
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
470 {
31640
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
471 void *ptr;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36082
diff changeset
472 Py_ssize_t size;
38676
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
473 if (!get_buf(self, &ptr, &size, ANY_BUFFER))
31640
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
474 return NULL;
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
475 if ( left < 0 )
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
476 left = 0;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
477 if ( right < 0 )
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
478 right = 0;
31640
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
479 if ( right > size )
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
480 right = size;
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
481 if ( right < left )
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
482 right = left;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
483 return PyString_FromStringAndSize((char *)ptr + left,
10460
62781914fadb Folded long lines.
Guido van Rossum <guido@python.org>
parents: 10427
diff changeset
484 right - left);
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
485 }
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
486
42951
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
487 static PyObject *
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
488 buffer_subscript(PyBufferObject *self, PyObject *item)
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
489 {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
490 void *p;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
491 Py_ssize_t size;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
492
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
493 if (!get_buf(self, &p, &size, ANY_BUFFER))
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
494 return NULL;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
495 if (PyIndex_Check(item)) {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
496 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
497 if (i == -1 && PyErr_Occurred())
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
498 return NULL;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
499 if (i < 0)
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
500 i += size;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
501 return buffer_item(self, i);
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
502 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
503 else if (PySlice_Check(item)) {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
504 Py_ssize_t start, stop, step, slicelength, cur, i;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
505
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
506 if (PySlice_GetIndicesEx((PySliceObject*)item, size,
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
507 &start, &stop, &step, &slicelength) < 0) {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
508 return NULL;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
509 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
510
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
511 if (slicelength <= 0)
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
512 return PyString_FromStringAndSize("", 0);
42951
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
513 else if (step == 1)
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
514 return PyString_FromStringAndSize((char *)p + start,
42951
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
515 stop - start);
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
516 else {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
517 PyObject *result;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
518 char *source_buf = (char *)p;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
519 char *result_buf = (char *)PyMem_Malloc(slicelength);
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
520
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
521 if (result_buf == NULL)
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
522 return PyErr_NoMemory();
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
523
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
524 for (cur = start, i = 0; i < slicelength;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
525 cur += step, i++) {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
526 result_buf[i] = source_buf[cur];
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
527 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
528
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
529 result = PyString_FromStringAndSize(result_buf,
42951
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
530 slicelength);
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
531 PyMem_Free(result_buf);
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
532 return result;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
533 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
534 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
535 else {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
536 PyErr_SetString(PyExc_TypeError,
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
537 "sequence index must be integer");
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
538 return NULL;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
539 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
540 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
541
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
542 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36082
diff changeset
543 buffer_ass_item(PyBufferObject *self, Py_ssize_t idx, PyObject *other)
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
544 {
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
545 PyBufferProcs *pb;
31640
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
546 void *ptr1, *ptr2;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36082
diff changeset
547 Py_ssize_t size;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36082
diff changeset
548 Py_ssize_t count;
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
549
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
550 if ( self->b_readonly ) {
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
551 PyErr_SetString(PyExc_TypeError,
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
552 "buffer is read-only");
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
553 return -1;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
554 }
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
555
38676
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
556 if (!get_buf(self, &ptr1, &size, ANY_BUFFER))
31640
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
557 return -1;
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
558
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
559 if (idx < 0 || idx >= size) {
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
560 PyErr_SetString(PyExc_IndexError,
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
561 "buffer assignment index out of range");
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
562 return -1;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
563 }
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
564
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
565 pb = other ? other->ob_type->tp_as_buffer : NULL;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
566 if ( pb == NULL ||
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
567 pb->bf_getreadbuffer == NULL ||
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
568 pb->bf_getsegcount == NULL )
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
569 {
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
570 PyErr_BadArgument();
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
571 return -1;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
572 }
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
573 if ( (*pb->bf_getsegcount)(other, NULL) != 1 )
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
574 {
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
575 /* ### use a different exception type/message? */
10460
62781914fadb Folded long lines.
Guido van Rossum <guido@python.org>
parents: 10427
diff changeset
576 PyErr_SetString(PyExc_TypeError,
62781914fadb Folded long lines.
Guido van Rossum <guido@python.org>
parents: 10427
diff changeset
577 "single-segment buffer object expected");
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
578 return -1;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
579 }
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
580
31640
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
581 if ( (count = (*pb->bf_getreadbuffer)(other, 0, &ptr2)) < 0 )
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
582 return -1;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
583 if ( count != 1 ) {
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
584 PyErr_SetString(PyExc_TypeError,
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
585 "right operand must be a single byte");
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
586 return -1;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
587 }
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
588
31640
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
589 ((char *)ptr1)[idx] = *(char *)ptr2;
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
590 return 0;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
591 }
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
592
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
593 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36082
diff changeset
594 buffer_ass_slice(PyBufferObject *self, Py_ssize_t left, Py_ssize_t right, PyObject *other)
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
595 {
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
596 PyBufferProcs *pb;
31640
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
597 void *ptr1, *ptr2;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36082
diff changeset
598 Py_ssize_t size;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36082
diff changeset
599 Py_ssize_t slice_len;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36082
diff changeset
600 Py_ssize_t count;
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
601
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
602 if ( self->b_readonly ) {
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
603 PyErr_SetString(PyExc_TypeError,
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
604 "buffer is read-only");
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
605 return -1;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
606 }
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
607
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
608 pb = other ? other->ob_type->tp_as_buffer : NULL;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
609 if ( pb == NULL ||
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
610 pb->bf_getreadbuffer == NULL ||
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
611 pb->bf_getsegcount == NULL )
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
612 {
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
613 PyErr_BadArgument();
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
614 return -1;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
615 }
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
616 if ( (*pb->bf_getsegcount)(other, NULL) != 1 )
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
617 {
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
618 /* ### use a different exception type/message? */
10460
62781914fadb Folded long lines.
Guido van Rossum <guido@python.org>
parents: 10427
diff changeset
619 PyErr_SetString(PyExc_TypeError,
62781914fadb Folded long lines.
Guido van Rossum <guido@python.org>
parents: 10427
diff changeset
620 "single-segment buffer object expected");
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
621 return -1;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
622 }
38676
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
623 if (!get_buf(self, &ptr1, &size, ANY_BUFFER))
31640
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
624 return -1;
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
625 if ( (count = (*pb->bf_getreadbuffer)(other, 0, &ptr2)) < 0 )
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
626 return -1;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
627
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
628 if ( left < 0 )
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
629 left = 0;
31640
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
630 else if ( left > size )
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
631 left = size;
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
632 if ( right < left )
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
633 right = left;
31640
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
634 else if ( right > size )
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
635 right = size;
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
636 slice_len = right - left;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
637
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
638 if ( count != slice_len ) {
10460
62781914fadb Folded long lines.
Guido van Rossum <guido@python.org>
parents: 10427
diff changeset
639 PyErr_SetString(
62781914fadb Folded long lines.
Guido van Rossum <guido@python.org>
parents: 10427
diff changeset
640 PyExc_TypeError,
62781914fadb Folded long lines.
Guido van Rossum <guido@python.org>
parents: 10427
diff changeset
641 "right operand length must match slice length");
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
642 return -1;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
643 }
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
644
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
645 if ( slice_len )
31640
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
646 memcpy((char *)ptr1 + left, ptr2, slice_len);
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
647
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
648 return 0;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
649 }
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
650
42951
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
651 static int
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
652 buffer_ass_subscript(PyBufferObject *self, PyObject *item, PyObject *value)
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
653 {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
654 PyBufferProcs *pb;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
655 void *ptr1, *ptr2;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
656 Py_ssize_t selfsize;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
657 Py_ssize_t othersize;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
658
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
659 if ( self->b_readonly ) {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
660 PyErr_SetString(PyExc_TypeError,
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
661 "buffer is read-only");
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
662 return -1;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
663 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
664
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
665 pb = value ? value->ob_type->tp_as_buffer : NULL;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
666 if ( pb == NULL ||
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
667 pb->bf_getreadbuffer == NULL ||
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
668 pb->bf_getsegcount == NULL )
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
669 {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
670 PyErr_BadArgument();
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
671 return -1;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
672 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
673 if ( (*pb->bf_getsegcount)(value, NULL) != 1 )
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
674 {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
675 /* ### use a different exception type/message? */
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
676 PyErr_SetString(PyExc_TypeError,
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
677 "single-segment buffer object expected");
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
678 return -1;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
679 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
680 if (!get_buf(self, &ptr1, &selfsize, ANY_BUFFER))
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
681 return -1;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
682 if (PyIndex_Check(item)) {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
683 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
684 if (i == -1 && PyErr_Occurred())
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
685 return -1;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
686 if (i < 0)
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
687 i += selfsize;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
688 return buffer_ass_item(self, i, value);
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
689 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
690 else if (PySlice_Check(item)) {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
691 Py_ssize_t start, stop, step, slicelength;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
692
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
693 if (PySlice_GetIndicesEx((PySliceObject *)item, selfsize,
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
694 &start, &stop, &step, &slicelength) < 0)
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
695 return -1;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
696
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
697 if ((othersize = (*pb->bf_getreadbuffer)(value, 0, &ptr2)) < 0)
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
698 return -1;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
699
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
700 if (othersize != slicelength) {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
701 PyErr_SetString(
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
702 PyExc_TypeError,
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
703 "right operand length must match slice length");
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
704 return -1;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
705 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
706
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
707 if (slicelength == 0)
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
708 return 0;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
709 else if (step == 1) {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
710 memcpy((char *)ptr1 + start, ptr2, slicelength);
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
711 return 0;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
712 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
713 else {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
714 Py_ssize_t cur, i;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
715
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
716 for (cur = start, i = 0; i < slicelength;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
717 cur += step, i++) {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
718 ((char *)ptr1)[cur] = ((char *)ptr2)[i];
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
719 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
720
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
721 return 0;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
722 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
723 } else {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
724 PyErr_SetString(PyExc_TypeError,
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
725 "buffer indices must be integers");
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
726 return -1;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
727 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
728 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
729
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
730 /* Buffer methods */
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
731
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36082
diff changeset
732 static Py_ssize_t
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36082
diff changeset
733 buffer_getreadbuf(PyBufferObject *self, Py_ssize_t idx, void **pp)
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
734 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36082
diff changeset
735 Py_ssize_t size;
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
736 if ( idx != 0 ) {
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
737 PyErr_SetString(PyExc_SystemError,
9305
083efa076366 Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents: 9286
diff changeset
738 "accessing non-existent buffer segment");
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
739 return -1;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
740 }
38693
72e37ada4170 Fix inconsistency in naming within an enum.
Brett Cannon <bcannon@gmail.com>
parents: 38676
diff changeset
741 if (!get_buf(self, pp, &size, READ_BUFFER))
31640
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
742 return -1;
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
743 return size;
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
744 }
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
745
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36082
diff changeset
746 static Py_ssize_t
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36082
diff changeset
747 buffer_getwritebuf(PyBufferObject *self, Py_ssize_t idx, void **pp)
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
748 {
38676
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
749 Py_ssize_t size;
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
750
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
751 if ( self->b_readonly )
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
752 {
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
753 PyErr_SetString(PyExc_TypeError, "buffer is read-only");
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
754 return -1;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
755 }
38676
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
756
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
757 if ( idx != 0 ) {
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
758 PyErr_SetString(PyExc_SystemError,
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
759 "accessing non-existent buffer segment");
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
760 return -1;
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
761 }
38693
72e37ada4170 Fix inconsistency in naming within an enum.
Brett Cannon <bcannon@gmail.com>
parents: 38676
diff changeset
762 if (!get_buf(self, pp, &size, WRITE_BUFFER))
38676
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
763 return -1;
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
764 return size;
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
765 }
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
766
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36082
diff changeset
767 static Py_ssize_t
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36082
diff changeset
768 buffer_getsegcount(PyBufferObject *self, Py_ssize_t *lenp)
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
769 {
31640
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
770 void *ptr;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36082
diff changeset
771 Py_ssize_t size;
38676
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
772 if (!get_buf(self, &ptr, &size, ANY_BUFFER))
31640
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
773 return -1;
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
774 if (lenp)
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
775 *lenp = size;
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
776 return 1;
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
777 }
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
778
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36082
diff changeset
779 static Py_ssize_t
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36082
diff changeset
780 buffer_getcharbuf(PyBufferObject *self, Py_ssize_t idx, const char **pp)
9305
083efa076366 Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents: 9286
diff changeset
781 {
31640
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
782 void *ptr;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36082
diff changeset
783 Py_ssize_t size;
9305
083efa076366 Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents: 9286
diff changeset
784 if ( idx != 0 ) {
083efa076366 Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents: 9286
diff changeset
785 PyErr_SetString(PyExc_SystemError,
083efa076366 Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents: 9286
diff changeset
786 "accessing non-existent buffer segment");
083efa076366 Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents: 9286
diff changeset
787 return -1;
083efa076366 Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents: 9286
diff changeset
788 }
38693
72e37ada4170 Fix inconsistency in naming within an enum.
Brett Cannon <bcannon@gmail.com>
parents: 38676
diff changeset
789 if (!get_buf(self, &ptr, &size, CHAR_BUFFER))
31640
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
790 return -1;
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
791 *pp = (const char *)ptr;
42f8820686c9 Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31639
diff changeset
792 return size;
9305
083efa076366 Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents: 9286
diff changeset
793 }
083efa076366 Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents: 9286
diff changeset
794
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
795 static PySequenceMethods buffer_as_sequence = {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36082
diff changeset
796 (lenfunc)buffer_length, /*sq_length*/
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
797 (binaryfunc)buffer_concat, /*sq_concat*/
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36082
diff changeset
798 (ssizeargfunc)buffer_repeat, /*sq_repeat*/
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36082
diff changeset
799 (ssizeargfunc)buffer_item, /*sq_item*/
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36082
diff changeset
800 (ssizessizeargfunc)buffer_slice, /*sq_slice*/
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36082
diff changeset
801 (ssizeobjargproc)buffer_ass_item, /*sq_ass_item*/
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36082
diff changeset
802 (ssizessizeobjargproc)buffer_ass_slice, /*sq_ass_slice*/
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
803 };
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
804
42951
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
805 static PyMappingMethods buffer_as_mapping = {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
806 (lenfunc)buffer_length,
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
807 (binaryfunc)buffer_subscript,
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
808 (objobjargproc)buffer_ass_subscript,
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
809 };
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
810
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
811 static PyBufferProcs buffer_as_buffer = {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36082
diff changeset
812 (readbufferproc)buffer_getreadbuf,
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36082
diff changeset
813 (writebufferproc)buffer_getwritebuf,
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36082
diff changeset
814 (segcountproc)buffer_getsegcount,
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36082
diff changeset
815 (charbufferproc)buffer_getcharbuf,
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
816 };
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
817
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
818 PyTypeObject PyBuffer_Type = {
42333
6620a510e9a9 PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents: 41996
diff changeset
819 PyVarObject_HEAD_INIT(&PyType_Type, 0)
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
820 "buffer",
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
821 sizeof(PyBufferObject),
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
822 0,
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
823 (destructor)buffer_dealloc, /* tp_dealloc */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
824 0, /* tp_print */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
825 0, /* tp_getattr */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
826 0, /* tp_setattr */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
827 (cmpfunc)buffer_compare, /* tp_compare */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
828 (reprfunc)buffer_repr, /* tp_repr */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
829 0, /* tp_as_number */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
830 &buffer_as_sequence, /* tp_as_sequence */
42951
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
831 &buffer_as_mapping, /* tp_as_mapping */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
832 (hashfunc)buffer_hash, /* tp_hash */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
833 0, /* tp_call */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
834 (reprfunc)buffer_str, /* tp_str */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
835 PyObject_GenericGetAttr, /* tp_getattro */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
836 0, /* tp_setattro */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
837 &buffer_as_buffer, /* tp_as_buffer */
38676
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 37589
diff changeset
838 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GETCHARBUFFER, /* tp_flags */
23868
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 22651
diff changeset
839 buffer_doc, /* tp_doc */
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 22651
diff changeset
840 0, /* tp_traverse */
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 22651
diff changeset
841 0, /* tp_clear */
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 22651
diff changeset
842 0, /* tp_richcompare */
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 22651
diff changeset
843 0, /* tp_weaklistoffset */
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 22651
diff changeset
844 0, /* tp_iter */
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 22651
diff changeset
845 0, /* tp_iternext */
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 22651
diff changeset
846 0, /* tp_methods */
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 22651
diff changeset
847 0, /* tp_members */
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 22651
diff changeset
848 0, /* tp_getset */
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 22651
diff changeset
849 0, /* tp_base */
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 22651
diff changeset
850 0, /* tp_dict */
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 22651
diff changeset
851 0, /* tp_descr_get */
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 22651
diff changeset
852 0, /* tp_descr_set */
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 22651
diff changeset
853 0, /* tp_dictoffset */
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 22651
diff changeset
854 0, /* tp_init */
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 22651
diff changeset
855 0, /* tp_alloc */
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 22651
diff changeset
856 buffer_new, /* tp_new */
9278
c39218a68df1 Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff changeset
857 };