forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuffer.rs
More file actions
250 lines (217 loc) · 6.75 KB
/
Copy pathbuffer.rs
File metadata and controls
250 lines (217 loc) · 6.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
use crate::{
AsObject, PyObject, PyObjectRef, PyResult, TryFromBorrowedObject, TryFromObject,
VirtualMachine,
builtins::{PyStr, PyStrRef},
common::borrow::{BorrowedValue, BorrowedValueMut},
protocol::{BufferFlags, PyBuffer},
};
// Python/getargs.c
/// any bytes-like object. Like the `y*` format code for `PyArg_Parse` in CPython.
#[derive(Debug, Traverse)]
pub struct ArgBytesLike(PyBuffer);
impl PyObject {
pub fn try_bytes_like<F, R>(&self, vm: &VirtualMachine, f: F) -> PyResult<R>
where
F: FnOnce(&[u8]) -> R,
{
let buffer = PyBuffer::from_object(vm, self, BufferFlags::SIMPLE)?;
buffer
.as_contiguous()
.map(|x| f(&x))
.ok_or_else(|| vm.new_buffer_error("non-contiguous buffer is not a bytes-like object"))
}
pub fn try_rw_bytes_like<F, R>(&self, vm: &VirtualMachine, f: F) -> PyResult<R>
where
F: FnOnce(&mut [u8]) -> R,
{
let buffer = PyBuffer::from_object(vm, self, BufferFlags::WRITABLE)?;
buffer
.as_contiguous_mut()
.map(|mut x| f(&mut x))
.ok_or_else(|| vm.new_type_error("buffer is not a read-write bytes-like object"))
}
}
impl ArgBytesLike {
#[must_use]
pub fn borrow_buf(&self) -> BorrowedValue<'_, [u8]> {
unsafe { self.0.contiguous_unchecked() }
}
pub fn with_ref<F, R>(&self, f: F) -> R
where
F: FnOnce(&[u8]) -> R,
{
f(&self.borrow_buf())
}
#[must_use]
pub const fn len(&self) -> usize {
self.0.desc.len
}
#[must_use]
pub const fn is_empty(&self) -> bool {
self.len() == 0
}
#[must_use]
pub fn as_object(&self) -> &PyObject {
&self.0.obj
}
}
impl From<ArgBytesLike> for PyBuffer {
fn from(buffer: ArgBytesLike) -> Self {
buffer.0
}
}
impl From<ArgBytesLike> for PyObjectRef {
fn from(buffer: ArgBytesLike) -> Self {
buffer.as_object().to_owned()
}
}
impl ArgBytesLike {
fn from_request(vm: &VirtualMachine, obj: &PyObject, flags: BufferFlags) -> PyResult<Self> {
let buffer = PyBuffer::from_object(vm, obj, flags)?;
if buffer.desc.is_contiguous() {
Ok(Self(buffer))
} else {
Err(vm.new_buffer_error("non-contiguous buffer is not a bytes-like object"))
}
}
}
impl<'a> TryFromBorrowedObject<'a> for ArgBytesLike {
fn try_from_borrowed_object(vm: &VirtualMachine, obj: &'a PyObject) -> PyResult<Self> {
Self::from_request(vm, obj, BufferFlags::SIMPLE)
}
}
/// A bytes-like object asked for as `PyBUF_CONTIG_RO`, which is what a shape is
/// requested with rather than assumed.
#[derive(Debug, Traverse)]
pub struct ArgContiguousBytesLike(ArgBytesLike);
impl core::ops::Deref for ArgContiguousBytesLike {
type Target = ArgBytesLike;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<'a> TryFromBorrowedObject<'a> for ArgContiguousBytesLike {
fn try_from_borrowed_object(vm: &VirtualMachine, obj: &'a PyObject) -> PyResult<Self> {
ArgBytesLike::from_request(vm, obj, BufferFlags::CONTIG_RO).map(Self)
}
}
/// A memory buffer, read-write access. Like the `w*` format code for `PyArg_Parse` in CPython.
#[derive(Debug, Traverse)]
pub struct ArgMemoryBuffer(PyBuffer);
impl ArgMemoryBuffer {
#[must_use]
pub fn borrow_buf_mut(&self) -> BorrowedValueMut<'_, [u8]> {
unsafe { self.0.contiguous_mut_unchecked() }
}
pub fn with_ref<F, R>(&self, f: F) -> R
where
F: FnOnce(&mut [u8]) -> R,
{
f(&mut self.borrow_buf_mut())
}
#[must_use]
pub const fn len(&self) -> usize {
self.0.desc.len
}
#[must_use]
pub const fn is_empty(&self) -> bool {
self.len() == 0
}
}
impl From<ArgMemoryBuffer> for PyBuffer {
fn from(buffer: ArgMemoryBuffer) -> Self {
buffer.0
}
}
impl<'a> TryFromBorrowedObject<'a> for ArgMemoryBuffer {
fn try_from_borrowed_object(vm: &VirtualMachine, obj: &'a PyObject) -> PyResult<Self> {
let buffer = PyBuffer::from_object(vm, obj, BufferFlags::WRITABLE).map_err(|exc| {
if obj.check_buffer() {
// An exporter that cannot serve the request leaves the argument
// simply the wrong kind of object, as `PyArg_Parse` reports it.
vm.new_type_error("buffer is not a read-write bytes-like object")
} else {
exc
}
})?;
if !buffer.desc.is_contiguous() {
Err(vm.new_buffer_error("non-contiguous buffer is not a bytes-like object"))
} else if buffer.desc.readonly {
Err(vm.new_type_error("buffer is not a read-write bytes-like object"))
} else {
Ok(Self(buffer))
}
}
}
/// A text string or bytes-like object. Like the `s*` format code for `PyArg_Parse` in CPython.
pub enum ArgStrOrBytesLike {
Buf(ArgBytesLike),
Str(PyStrRef),
}
impl ArgStrOrBytesLike {
#[must_use]
pub fn as_object(&self) -> &PyObject {
match self {
Self::Buf(b) => b.as_object(),
Self::Str(s) => s.as_object(),
}
}
}
impl TryFromObject for ArgStrOrBytesLike {
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
obj.downcast()
.map(Self::Str)
.or_else(|obj| ArgBytesLike::try_from_object(vm, obj).map(Self::Buf))
}
}
impl ArgStrOrBytesLike {
#[must_use]
pub fn borrow_bytes(&self) -> BorrowedValue<'_, [u8]> {
match self {
Self::Buf(b) => b.borrow_buf(),
Self::Str(s) => s.as_bytes().into(),
}
}
}
#[derive(Debug)]
pub enum ArgAsciiBuffer {
String(PyStrRef),
Buffer(ArgBytesLike),
}
impl TryFromObject for ArgAsciiBuffer {
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
match obj.downcast::<PyStr>() {
Ok(string) => {
if string.as_wtf8().is_ascii() {
Ok(Self::String(string))
} else {
Err(vm.new_value_error("string argument should contain only ASCII characters"))
}
}
Err(obj) => ArgBytesLike::try_from_object(vm, obj).map(ArgAsciiBuffer::Buffer),
}
}
}
impl ArgAsciiBuffer {
#[must_use]
pub fn len(&self) -> usize {
match self {
Self::String(s) => s.as_wtf8().len(),
Self::Buffer(buffer) => buffer.len(),
}
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.len() == 0
}
#[inline]
pub fn with_ref<F, R>(&self, f: F) -> R
where
F: FnOnce(&[u8]) -> R,
{
match self {
Self::String(s) => f(s.as_bytes()),
Self::Buffer(buffer) => buffer.with_ref(f),
}
}
}