Mercurial > cpython-lru_cache-weakref
annotate Objects/bufferobject.c @ 46075:84bb7701efba legacy-trunk
Make Py3k warnings consistent w.r.t. punctuation; also respect the
EOL 80 limit and supply more alternatives in warning messages.
| author | Georg Brandl <georg@python.org> |
|---|---|
| date | Tue, 25 Mar 2008 08:29:14 +0000 |
| parents | 9b3f0fa5d8ca |
| children | 2a866ea66509 |
| 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 } |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36082
diff
changeset
|
210 /* XXX: check for overflow in multiply */ |
|
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
|
211 /* 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
|
212 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
|
213 if ( o == NULL ) |
|
11175
fab81df566d0
PyBuffer_New(): Raise ValueError if size is negative (the other
Fred Drake <fdrake@acm.org>
parents:
10460
diff
changeset
|
214 return PyErr_NoMemory(); |
|
13861
f3d5f71ccbb4
Removing warnings found by gcc -Wall
Moshe Zadka <moshez@math.huji.ac.il>
parents:
13449
diff
changeset
|
215 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
|
216 |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
217 b->b_base = NULL; |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
218 b->b_ptr = (void *)(b + 1); |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
219 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
|
220 b->b_offset = 0; |
|
9278
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
221 b->b_readonly = 0; |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
222 b->b_hash = -1; |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
223 |
|
13861
f3d5f71ccbb4
Removing warnings found by gcc -Wall
Moshe Zadka <moshez@math.huji.ac.il>
parents:
13449
diff
changeset
|
224 return o; |
|
9278
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
225 } |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
226 |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
227 /* Methods */ |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
228 |
|
23868
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
22651
diff
changeset
|
229 static PyObject * |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
22651
diff
changeset
|
230 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
|
231 { |
|
46074
9b3f0fa5d8ca
#2355: py3k warning for buffer().
Georg Brandl <georg@python.org>
parents:
43464
diff
changeset
|
232 if (Py_Py3kWarningFlag && |
|
9b3f0fa5d8ca
#2355: py3k warning for buffer().
Georg Brandl <georg@python.org>
parents:
43464
diff
changeset
|
233 PyErr_WarnEx(PyExc_DeprecationWarning, |
|
46075
84bb7701efba
Make Py3k warnings consistent w.r.t. punctuation; also respect the
Georg Brandl <georg@python.org>
parents:
46074
diff
changeset
|
234 "buffer() not supported in 3.x; " |
|
84bb7701efba
Make Py3k warnings consistent w.r.t. punctuation; also respect the
Georg Brandl <georg@python.org>
parents:
46074
diff
changeset
|
235 "use memoryview()", 1) < 0) |
|
46074
9b3f0fa5d8ca
#2355: py3k warning for buffer().
Georg Brandl <georg@python.org>
parents:
43464
diff
changeset
|
236 return NULL; |
|
9b3f0fa5d8ca
#2355: py3k warning for buffer().
Georg Brandl <georg@python.org>
parents:
43464
diff
changeset
|
237 |
|
23868
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
22651
diff
changeset
|
238 PyObject *ob; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36082
diff
changeset
|
239 Py_ssize_t offset = 0; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36082
diff
changeset
|
240 Py_ssize_t size = Py_END_OF_BUFFER; |
|
23868
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
22651
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 ) |
|
36510
0f3894bb2ada
Support %zd in PyErr_Format and PyString_FromFormat.
Martin v. Löwis <martin@v.loewis.de>
parents:
36509
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 | 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 |
|
19476
0e70df98424e
repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents:
18933
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 | 302 self->b_size, |
| 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 |
|
047148089c35
Document one of the many problems with the buffer object.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
31638
diff
changeset
|
323 * be immutable (e.g. PyString_Check() is true). Another idea would |
|
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; |
|
37589
aeee85b6a5dd
More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents:
36533
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 | 388 PyErr_SetString(PyExc_TypeError, |
| 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 |
|
31640
42f8820686c9
Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
31639
diff
changeset
|
406 ob = PyString_FromStringAndSize(NULL, size + count); |
|
36082
3f358afbb3d3
Check return result for error
Neal Norwitz <nnorwitz@gmail.com>
parents:
35450
diff
changeset
|
407 if ( ob == NULL ) |
|
3f358afbb3d3
Check return result for error
Neal Norwitz <nnorwitz@gmail.com>
parents:
35450
diff
changeset
|
408 return NULL; |
|
31640
42f8820686c9
Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
31639
diff
changeset
|
409 p = PyString_AS_STRING(ob); |
|
42f8820686c9
Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
31639
diff
changeset
|
410 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
|
411 memcpy(p + size, ptr2, count); |
|
9278
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
412 |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
413 /* 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
|
414 p[size + count] = '\0'; |
|
9278
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
415 |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
416 return ob; |
|
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 |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
419 static PyObject * |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36082
diff
changeset
|
420 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
|
421 { |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
422 PyObject *ob; |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
423 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
|
424 void *ptr; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36082
diff
changeset
|
425 Py_ssize_t size; |
|
9278
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
426 |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
427 if ( count < 0 ) |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
428 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
|
429 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
|
430 return NULL; |
|
9278
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
431 ob = PyString_FromStringAndSize(NULL, size * count); |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
432 if ( ob == NULL ) |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
433 return NULL; |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
434 |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
435 p = PyString_AS_STRING(ob); |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
436 while ( count-- ) |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
437 { |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
438 memcpy(p, ptr, size); |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
439 p += size; |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
440 } |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
441 |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
442 /* 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
|
443 *p = '\0'; |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
444 |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
445 return ob; |
|
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 static PyObject * |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36082
diff
changeset
|
449 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
|
450 { |
|
31640
42f8820686c9
Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
31639
diff
changeset
|
451 void *ptr; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36082
diff
changeset
|
452 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
|
453 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
|
454 return NULL; |
|
42f8820686c9
Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
31639
diff
changeset
|
455 if ( idx < 0 || idx >= size ) { |
|
9278
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
456 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
|
457 return NULL; |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
458 } |
|
31640
42f8820686c9
Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
31639
diff
changeset
|
459 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
|
460 } |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
461 |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
462 static PyObject * |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36082
diff
changeset
|
463 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
|
464 { |
|
31640
42f8820686c9
Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
31639
diff
changeset
|
465 void *ptr; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36082
diff
changeset
|
466 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
|
467 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
|
468 return NULL; |
|
9278
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
469 if ( left < 0 ) |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
470 left = 0; |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
471 if ( right < 0 ) |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
472 right = 0; |
|
31640
42f8820686c9
Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
31639
diff
changeset
|
473 if ( right > size ) |
|
42f8820686c9
Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
31639
diff
changeset
|
474 right = size; |
|
9278
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
475 if ( right < left ) |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
476 right = left; |
|
31640
42f8820686c9
Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
31639
diff
changeset
|
477 return PyString_FromStringAndSize((char *)ptr + left, |
| 10460 | 478 right - left); |
|
9278
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
479 } |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
480 |
|
42951
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
481 static PyObject * |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
482 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
|
483 { |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
484 void *p; |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
485 Py_ssize_t size; |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
486 |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
487 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
|
488 return NULL; |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
489 if (PyIndex_Check(item)) { |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
490 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
|
491 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
|
492 return NULL; |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
493 if (i < 0) |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
494 i += size; |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
495 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
|
496 } |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
497 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
|
498 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
|
499 |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
500 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
|
501 &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
|
502 return NULL; |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
503 } |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
504 |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
505 if (slicelength <= 0) |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
506 return PyString_FromStringAndSize("", 0); |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
507 else if (step == 1) |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
508 return PyString_FromStringAndSize((char *)p + start, |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
509 stop - start); |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
510 else { |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
511 PyObject *result; |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
512 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
|
513 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
|
514 |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
515 if (result_buf == NULL) |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
516 return PyErr_NoMemory(); |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
517 |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
518 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
|
519 cur += step, i++) { |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
520 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
|
521 } |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
522 |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
523 result = PyString_FromStringAndSize(result_buf, |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
524 slicelength); |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
525 PyMem_Free(result_buf); |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
526 return result; |
|
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 } |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
529 else { |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
530 PyErr_SetString(PyExc_TypeError, |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
531 "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
|
532 return NULL; |
|
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 |
|
9278
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
536 static int |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36082
diff
changeset
|
537 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
|
538 { |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
539 PyBufferProcs *pb; |
|
31640
42f8820686c9
Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
31639
diff
changeset
|
540 void *ptr1, *ptr2; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36082
diff
changeset
|
541 Py_ssize_t size; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36082
diff
changeset
|
542 Py_ssize_t count; |
|
9278
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
543 |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
544 if ( self->b_readonly ) { |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
545 PyErr_SetString(PyExc_TypeError, |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
546 "buffer is read-only"); |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
547 return -1; |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
548 } |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
549 |
|
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
|
550 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
|
551 return -1; |
|
42f8820686c9
Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
31639
diff
changeset
|
552 |
|
42f8820686c9
Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
31639
diff
changeset
|
553 if (idx < 0 || idx >= size) { |
|
9278
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
554 PyErr_SetString(PyExc_IndexError, |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
555 "buffer assignment index out of range"); |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
556 return -1; |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
557 } |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
558 |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
559 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
|
560 if ( pb == NULL || |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
561 pb->bf_getreadbuffer == NULL || |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
562 pb->bf_getsegcount == NULL ) |
|
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 PyErr_BadArgument(); |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
565 return -1; |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
566 } |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
567 if ( (*pb->bf_getsegcount)(other, NULL) != 1 ) |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
568 { |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
569 /* ### use a different exception type/message? */ |
| 10460 | 570 PyErr_SetString(PyExc_TypeError, |
| 571 "single-segment buffer object expected"); | |
|
9278
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
572 return -1; |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
573 } |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
574 |
|
31640
42f8820686c9
Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
31639
diff
changeset
|
575 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
|
576 return -1; |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
577 if ( count != 1 ) { |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
578 PyErr_SetString(PyExc_TypeError, |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
579 "right operand must be a single byte"); |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
580 return -1; |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
581 } |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
582 |
|
31640
42f8820686c9
Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
31639
diff
changeset
|
583 ((char *)ptr1)[idx] = *(char *)ptr2; |
|
9278
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
584 return 0; |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
585 } |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
586 |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
587 static int |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36082
diff
changeset
|
588 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
|
589 { |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
590 PyBufferProcs *pb; |
|
31640
42f8820686c9
Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
31639
diff
changeset
|
591 void *ptr1, *ptr2; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36082
diff
changeset
|
592 Py_ssize_t size; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36082
diff
changeset
|
593 Py_ssize_t slice_len; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36082
diff
changeset
|
594 Py_ssize_t count; |
|
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 if ( self->b_readonly ) { |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
597 PyErr_SetString(PyExc_TypeError, |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
598 "buffer is read-only"); |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
599 return -1; |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
600 } |
|
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 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
|
603 if ( pb == NULL || |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
604 pb->bf_getreadbuffer == NULL || |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
605 pb->bf_getsegcount == NULL ) |
|
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 PyErr_BadArgument(); |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
608 return -1; |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
609 } |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
610 if ( (*pb->bf_getsegcount)(other, NULL) != 1 ) |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
611 { |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
612 /* ### use a different exception type/message? */ |
| 10460 | 613 PyErr_SetString(PyExc_TypeError, |
| 614 "single-segment buffer object expected"); | |
|
9278
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
615 return -1; |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
616 } |
|
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
|
617 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
|
618 return -1; |
|
42f8820686c9
Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
31639
diff
changeset
|
619 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
|
620 return -1; |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
621 |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
622 if ( left < 0 ) |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
623 left = 0; |
|
31640
42f8820686c9
Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
31639
diff
changeset
|
624 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
|
625 left = size; |
|
9278
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
626 if ( right < left ) |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
627 right = left; |
|
31640
42f8820686c9
Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
31639
diff
changeset
|
628 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
|
629 right = size; |
|
9278
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
630 slice_len = right - left; |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
631 |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
632 if ( count != slice_len ) { |
| 10460 | 633 PyErr_SetString( |
| 634 PyExc_TypeError, | |
| 635 "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
|
636 return -1; |
|
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 |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
639 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
|
640 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
|
641 |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
642 return 0; |
|
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 |
|
42951
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
645 static int |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
646 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
|
647 { |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
648 PyBufferProcs *pb; |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
649 void *ptr1, *ptr2; |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
650 Py_ssize_t selfsize; |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
651 Py_ssize_t othersize; |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
652 |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
653 if ( self->b_readonly ) { |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
654 PyErr_SetString(PyExc_TypeError, |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
655 "buffer is read-only"); |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
656 return -1; |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
657 } |
|
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 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
|
660 if ( pb == NULL || |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
661 pb->bf_getreadbuffer == NULL || |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
662 pb->bf_getsegcount == NULL ) |
|
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 PyErr_BadArgument(); |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
665 return -1; |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
666 } |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
667 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
|
668 { |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
669 /* ### 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
|
670 PyErr_SetString(PyExc_TypeError, |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
671 "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
|
672 return -1; |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
673 } |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
674 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
|
675 return -1; |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
676 if (PyIndex_Check(item)) { |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
677 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
|
678 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
|
679 return -1; |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
680 if (i < 0) |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
681 i += selfsize; |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
682 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
|
683 } |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
684 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
|
685 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
|
686 |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
687 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
|
688 &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
|
689 return -1; |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
690 |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
691 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
|
692 return -1; |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
693 |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
694 if (othersize != slicelength) { |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
695 PyErr_SetString( |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
696 PyExc_TypeError, |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
697 "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
|
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 |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
701 if (slicelength == 0) |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
702 return 0; |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
703 else if (step == 1) { |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
704 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
|
705 return 0; |
|
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 else { |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
708 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
|
709 |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
710 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
|
711 cur += step, i++) { |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
712 ((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
|
713 } |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
714 |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
715 return 0; |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
716 } |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
717 } else { |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
718 PyErr_SetString(PyExc_TypeError, |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
719 "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
|
720 return -1; |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
721 } |
|
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 |
|
9278
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
724 /* Buffer methods */ |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
725 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36082
diff
changeset
|
726 static Py_ssize_t |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36082
diff
changeset
|
727 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
|
728 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36082
diff
changeset
|
729 Py_ssize_t size; |
|
9278
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
730 if ( idx != 0 ) { |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
731 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
|
732 "accessing non-existent buffer segment"); |
|
9278
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
733 return -1; |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
734 } |
|
38693
72e37ada4170
Fix inconsistency in naming within an enum.
Brett Cannon <bcannon@gmail.com>
parents:
38676
diff
changeset
|
735 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
|
736 return -1; |
|
42f8820686c9
Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
31639
diff
changeset
|
737 return size; |
|
9278
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
738 } |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
739 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36082
diff
changeset
|
740 static Py_ssize_t |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36082
diff
changeset
|
741 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
|
742 { |
|
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
|
743 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
|
744 |
|
9278
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
745 if ( self->b_readonly ) |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
746 { |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
747 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
|
748 return -1; |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
749 } |
|
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
|
750 |
|
5bc7272b81e5
Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents:
37589
diff
changeset
|
751 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
|
752 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
|
753 "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
|
754 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
|
755 } |
|
38693
72e37ada4170
Fix inconsistency in naming within an enum.
Brett Cannon <bcannon@gmail.com>
parents:
38676
diff
changeset
|
756 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
|
757 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
|
758 return size; |
|
9278
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
759 } |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
760 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36082
diff
changeset
|
761 static Py_ssize_t |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36082
diff
changeset
|
762 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
|
763 { |
|
31640
42f8820686c9
Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
31639
diff
changeset
|
764 void *ptr; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36082
diff
changeset
|
765 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
|
766 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
|
767 return -1; |
|
42f8820686c9
Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
31639
diff
changeset
|
768 if (lenp) |
|
42f8820686c9
Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
31639
diff
changeset
|
769 *lenp = size; |
|
9278
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
770 return 1; |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
771 } |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
772 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36082
diff
changeset
|
773 static Py_ssize_t |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36082
diff
changeset
|
774 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
|
775 { |
|
31640
42f8820686c9
Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
31639
diff
changeset
|
776 void *ptr; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36082
diff
changeset
|
777 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
|
778 if ( idx != 0 ) { |
|
083efa076366
Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents:
9286
diff
changeset
|
779 PyErr_SetString(PyExc_SystemError, |
|
083efa076366
Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents:
9286
diff
changeset
|
780 "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
|
781 return -1; |
|
083efa076366
Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents:
9286
diff
changeset
|
782 } |
|
38693
72e37ada4170
Fix inconsistency in naming within an enum.
Brett Cannon <bcannon@gmail.com>
parents:
38676
diff
changeset
|
783 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
|
784 return -1; |
|
42f8820686c9
Make buffer objects based on mutable objects (like array) safe.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
31639
diff
changeset
|
785 *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
|
786 return size; |
|
9305
083efa076366
Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents:
9286
diff
changeset
|
787 } |
|
083efa076366
Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents:
9286
diff
changeset
|
788 |
|
9278
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
789 static PySequenceMethods buffer_as_sequence = { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36082
diff
changeset
|
790 (lenfunc)buffer_length, /*sq_length*/ |
|
9278
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
791 (binaryfunc)buffer_concat, /*sq_concat*/ |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36082
diff
changeset
|
792 (ssizeargfunc)buffer_repeat, /*sq_repeat*/ |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36082
diff
changeset
|
793 (ssizeargfunc)buffer_item, /*sq_item*/ |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36082
diff
changeset
|
794 (ssizessizeargfunc)buffer_slice, /*sq_slice*/ |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36082
diff
changeset
|
795 (ssizeobjargproc)buffer_ass_item, /*sq_ass_item*/ |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36082
diff
changeset
|
796 (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
|
797 }; |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
798 |
|
42951
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
799 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
|
800 (lenfunc)buffer_length, |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
801 (binaryfunc)buffer_subscript, |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
802 (objobjargproc)buffer_ass_subscript, |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
803 }; |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42333
diff
changeset
|
804 |
|
9278
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
805 static PyBufferProcs buffer_as_buffer = { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36082
diff
changeset
|
806 (readbufferproc)buffer_getreadbuf, |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36082
diff
changeset
|
807 (writebufferproc)buffer_getwritebuf, |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36082
diff
changeset
|
808 (segcountproc)buffer_getsegcount, |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36082
diff
changeset
|
809 (charbufferproc)buffer_getcharbuf, |
|
9278
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
810 }; |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
811 |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
812 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
|
813 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|
9278
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
814 "buffer", |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
815 sizeof(PyBufferObject), |
|
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
816 0, |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
14375
diff
changeset
|
817 (destructor)buffer_dealloc, /* tp_dealloc */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
14375
diff
changeset
|
818 0, /* tp_print */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
14375
diff
changeset
|
819 0, /* tp_getattr */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
14375
diff
changeset
|
820 0, /* tp_setattr */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
14375
diff
changeset
|
821 (cmpfunc)buffer_compare, /* tp_compare */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
14375
diff
changeset
|
822 (reprfunc)buffer_repr, /* tp_repr */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
14375
diff
changeset
|
823 0, /* tp_as_number */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
14375
diff
changeset
|
824 &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
|
825 &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
|
826 (hashfunc)buffer_hash, /* tp_hash */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
14375
diff
changeset
|
827 0, /* tp_call */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
14375
diff
changeset
|
828 (reprfunc)buffer_str, /* tp_str */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
14375
diff
changeset
|
829 PyObject_GenericGetAttr, /* tp_getattro */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
14375
diff
changeset
|
830 0, /* tp_setattro */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
14375
diff
changeset
|
831 &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
|
832 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
|
833 buffer_doc, /* tp_doc */ |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
22651
diff
changeset
|
834 0, /* tp_traverse */ |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
22651
diff
changeset
|
835 0, /* tp_clear */ |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
22651
diff
changeset
|
836 0, /* tp_richcompare */ |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
22651
diff
changeset
|
837 0, /* tp_weaklistoffset */ |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
22651
diff
changeset
|
838 0, /* tp_iter */ |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
22651
diff
changeset
|
839 0, /* tp_iternext */ |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
22651
diff
changeset
|
840 0, /* tp_methods */ |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
22651
diff
changeset
|
841 0, /* tp_members */ |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
22651
diff
changeset
|
842 0, /* tp_getset */ |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
22651
diff
changeset
|
843 0, /* tp_base */ |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
22651
diff
changeset
|
844 0, /* tp_dict */ |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
22651
diff
changeset
|
845 0, /* tp_descr_get */ |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
22651
diff
changeset
|
846 0, /* tp_descr_set */ |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
22651
diff
changeset
|
847 0, /* tp_dictoffset */ |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
22651
diff
changeset
|
848 0, /* tp_init */ |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
22651
diff
changeset
|
849 0, /* tp_alloc */ |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
22651
diff
changeset
|
850 buffer_new, /* tp_new */ |
|
9278
c39218a68df1
Add Greg Stein's buffer object API.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
851 }; |
