Skip to content

Commit 743d1fb

Browse files
committed
remove buffer protocol support
1 parent 03291b8 commit 743d1fb

File tree

8 files changed

+9
-171
lines changed

8 files changed

+9
-171
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ name = "zenoh"
3434
crate-type = ["cdylib"]
3535

3636
[features]
37-
default = ["pyo3/abi3-py310", "shared-memory", "zenoh-ext", "zenoh/default"]
37+
default = ["shared-memory", "zenoh-ext", "zenoh/default"]
3838
shared-memory = ["zenoh/shared-memory"]
3939
zenoh-ext = ["dep:zenoh-ext", "zenoh-ext/internal", "zenoh-ext/unstable"]
4040

@@ -43,10 +43,6 @@ maintenance = { status = "actively-developed" }
4343

4444
[dependencies]
4545
paste = "1.0.14"
46-
pyo3 = { version = "0.25.1", features = ["extension-module"] }
46+
pyo3 = { version = "0.25.1", features = ["abi3-py39", "extension-module"] }
4747
zenoh = { version = "1.5.1", git = "https://github.com/eclipse-zenoh/zenoh.git", branch = "main", features = ["internal", "unstable"], default-features = false }
4848
zenoh-ext = { version = "1.5.1", git = "https://github.com/eclipse-zenoh/zenoh.git", branch = "main", features = ["internal"], optional = true }
49-
50-
[build-dependencies]
51-
pyo3-build-config = { version = "0.25.1", features = ["resolve-config"] }
52-

build.rs

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/bytes.rs

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use std::{borrow::Cow, io::Read};
1616
use pyo3::{
1717
exceptions::{PyTypeError, PyValueError},
1818
prelude::*,
19-
types::{PyByteArray, PyBytes, PyMemoryView, PyString},
19+
types::{PyByteArray, PyBytes, PyString},
2020
};
2121

2222
use crate::{
@@ -45,50 +45,13 @@ impl ZBytes {
4545
if let Ok(buf) = obj.downcast_exact::<crate::shm::ZShmMut>() {
4646
return Ok(Self(buf.borrow_mut().take()?.into()));
4747
}
48-
#[cfg(Py_3_11)]
49-
if let Ok(buffer) = pyo3::buffer::PyBuffer::<u8>::get(obj) {
50-
return Ok(Self(buffer.to_vec(obj.py())?.into()));
51-
}
5248
Err(PyTypeError::new_err(format!(
5349
"expected bytes/str type, found '{}'",
5450
obj.get_type().name().unwrap()
5551
)))
5652
}
5753
}
5854

59-
#[cfg(Py_3_11)]
60-
unsafe fn __getbuffer__(
61-
mut this: PyRefMut<Self>,
62-
view: *mut pyo3::ffi::Py_buffer,
63-
flags: std::ffi::c_int,
64-
) -> PyResult<()> {
65-
if view.is_null() {
66-
return Err(pyo3::exceptions::PyBufferError::new_err(
67-
"Buffer ptr is null",
68-
));
69-
}
70-
if flags & pyo3::ffi::PyBUF_WRITABLE != 0 {
71-
return Err(pyo3::exceptions::PyBufferError::new_err(
72-
"ZBytes is not writable",
73-
));
74-
}
75-
let (buf, len) = match this.0.to_bytes() {
76-
Cow::Borrowed(bytes) => (bytes.as_ptr(), bytes.len()),
77-
Cow::Owned(bytes) => {
78-
let (buf, len) = (bytes.as_ptr(), bytes.len());
79-
this.0 = bytes.into();
80-
(buf, len)
81-
}
82-
};
83-
unsafe {
84-
crate::utils::init_buffer(view, flags, buf.cast_mut(), len, true, this.into_ptr())
85-
};
86-
Ok(())
87-
}
88-
89-
#[cfg(Py_3_11)]
90-
unsafe fn __releasebuffer__(&mut self, _view: *mut pyo3::ffi::Py_buffer) {}
91-
9255
fn to_bytes<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyBytes>> {
9356
// Not using `ZBytes::to_bytes`
9457
PyBytes::new_with(py, self.0.len(), |bytes| {
@@ -102,13 +65,6 @@ impl ZBytes {
10265
.map_err(|_| PyValueError::new_err("not an UTF8 error"))
10366
}
10467

105-
fn __getitem__<'py>(
106-
this: &Bound<'py, Self>,
107-
obj: &Bound<'py, PyAny>,
108-
) -> PyResult<Bound<'py, PyAny>> {
109-
PyMemoryView::from(this.as_any())?.get_item(obj)
110-
}
111-
11268
fn __len__(&self) -> usize {
11369
self.0.len()
11470
}

src/shm.rs

Lines changed: 3 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{str, sync::Arc};
33
use pyo3::{
44
exceptions::{PyTypeError, PyValueError},
55
prelude::*,
6-
types::{PyByteArray, PyBytes, PyMemoryView, PySlice, PyString, PyType},
6+
types::{PyByteArray, PyBytes, PySlice, PyString, PyType},
77
};
88
use zenoh::shm::{ChunkAllocResult, PosixShmProviderBackend};
99

@@ -186,7 +186,6 @@ impl ShmProvider {
186186
#[pyclass]
187187
pub(crate) struct ZShmMut {
188188
buf: Option<zenoh::shm::ZShmMut>,
189-
view_count: usize,
190189
}
191190

192191
impl ZShmMut {
@@ -200,25 +199,13 @@ impl ZShmMut {
200199
Ok(self.buf.as_mut().unwrap())
201200
}
202201
pub(crate) fn take(&mut self) -> PyResult<zenoh::shm::ZShmMut> {
203-
if self.view_count > 0 {
204-
return Err(zerror!(
205-
"ZShmMut cannot be converted to ZBytes if it has memoryview in use"
206-
));
207-
}
208202
self.get()?;
209203
Ok(self.buf.take().unwrap())
210204
}
211205
}
212206

213207
#[pymethods]
214208
impl ZShmMut {
215-
fn __getitem__<'py>(
216-
this: &Bound<'py, Self>,
217-
key: &Bound<'py, PyAny>,
218-
) -> PyResult<Bound<'py, PyAny>> {
219-
PyMemoryView::from(this.as_any())?.get_item(key)
220-
}
221-
222209
fn __setitem__(this: &Bound<Self>, key: &Bound<PyAny>, value: &Bound<PyAny>) -> PyResult<()> {
223210
if let Ok(key) = key.extract::<usize>() {
224211
if let Ok(value) = value.extract::<u8>() {
@@ -245,15 +232,8 @@ impl ZShmMut {
245232
} else if let Ok(bytes) = value.downcast::<PyBytes>() {
246233
return copy_bytes(bytes.as_bytes());
247234
}
248-
#[cfg(Py_3_11)]
249-
if let Ok(value) = pyo3::buffer::PyBuffer::<u8>::get(value) {
250-
return value.copy_to_slice(
251-
this.py(),
252-
&mut slice[indices.start as usize..indices.stop as usize],
253-
);
254-
}
255235
}
256-
PyMemoryView::from(this.as_any())?.set_item(key, value)
236+
Err(PyTypeError::new_err("expected bytes like argument"))
257237
}
258238

259239
fn __str__<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyString>> {
@@ -264,53 +244,13 @@ impl ZShmMut {
264244
Ok(PyBytes::new(py, self.get()?))
265245
}
266246

267-
#[cfg(Py_3_11)]
268-
unsafe fn __getbuffer__(
269-
mut this: PyRefMut<Self>,
270-
view: *mut pyo3::ffi::Py_buffer,
271-
flags: std::ffi::c_int,
272-
) -> PyResult<()> {
273-
if view.is_null() {
274-
return Err(pyo3::exceptions::PyBufferError::new_err(
275-
"Buffer ptr is null",
276-
));
277-
}
278-
this.view_count += 1;
279-
let buffer = this.get_mut()?;
280-
unsafe {
281-
crate::utils::init_buffer(
282-
view,
283-
flags,
284-
buffer.as_mut_ptr(),
285-
buffer.len(),
286-
false,
287-
this.into_ptr(),
288-
)
289-
};
290-
Ok(())
291-
}
292-
293-
#[cfg(Py_3_11)]
294-
unsafe fn __releasebuffer__(&mut self, _view: *mut pyo3::ffi::Py_buffer) {
295-
self.view_count -= 1;
296-
}
297-
298247
fn __repr__(&self) -> PyResult<String> {
299248
Ok(format!("{:?}", self.get()?))
300249
}
301250
}
302251

303252
impl From<zenoh::shm::ZShmMut> for ZShmMut {
304253
fn from(value: zenoh::shm::ZShmMut) -> Self {
305-
Self {
306-
buf: Some(value),
307-
view_count: 0,
308-
}
309-
}
310-
}
311-
312-
impl Drop for ZShmMut {
313-
fn drop(&mut self) {
314-
assert_eq!(self.view_count, 0, "there should be no remaining views");
254+
Self { buf: Some(value) }
315255
}
316256
}

src/utils.rs

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -129,40 +129,3 @@ pub(crate) fn duration(obj: &Bound<PyAny>) -> PyResult<Option<Duration>> {
129129
.map(Some)
130130
.map_err(|_| PyValueError::new_err("negative timeout"))
131131
}
132-
133-
#[cfg(Py_3_11)]
134-
pub(crate) unsafe fn init_buffer(
135-
view: *mut pyo3::ffi::Py_buffer,
136-
flags: std::ffi::c_int,
137-
buf: *mut u8,
138-
len: usize,
139-
readonly: bool,
140-
owner: *mut pyo3::ffi::PyObject,
141-
) {
142-
unsafe {
143-
(*view).obj = owner;
144-
(*view).buf = buf as *mut std::ffi::c_void;
145-
(*view).len = len as isize;
146-
(*view).readonly = readonly as std::ffi::c_int;
147-
(*view).itemsize = 1;
148-
(*view).format = if (flags & pyo3::ffi::PyBUF_FORMAT) != 0 {
149-
static B: &std::ffi::CStr = pyo3::ffi::c_str!("B");
150-
B.as_ptr().cast_mut()
151-
} else {
152-
std::ptr::null_mut()
153-
};
154-
(*view).ndim = 1;
155-
(*view).shape = if (flags & pyo3::ffi::PyBUF_ND) != 0 {
156-
&mut (*view).len
157-
} else {
158-
std::ptr::null_mut()
159-
};
160-
(*view).strides = if (flags & pyo3::ffi::PyBUF_STRIDES) != 0 {
161-
&mut (*view).itemsize
162-
} else {
163-
std::ptr::null_mut()
164-
};
165-
(*view).suboffsets = std::ptr::null_mut();
166-
(*view).internal = std::ptr::null_mut();
167-
}
168-
}

zenoh/__init__.pyi

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# Contributors:
1212
# ZettaScale Zenoh Team, <[email protected]>
1313
#
14-
from collections.abc import Buffer, Callable
14+
from collections.abc import Callable
1515
from datetime import datetime, timedelta
1616
from enum import Enum, auto
1717
from pathlib import Path
@@ -1230,21 +1230,15 @@ class ZBytes:
12301230
encouraged to use any data format of their choice like JSON, protobuf,
12311231
flatbuffers, etc."""
12321232

1233-
def __new__(cls, bytes: bytearray | bytes | str | Buffer = None) -> Self: ...
1233+
def __new__(cls, bytes: bytearray | bytes | str | shm.ZShmMut = None) -> Self: ...
12341234
def to_bytes(self) -> bytes: ...
12351235
def to_string(self) -> str: ...
1236-
@overload
1237-
def __getitem__(self, item: int) -> int: ...
1238-
@overload
1239-
def __getitem__(self, item: slice) -> memoryview: ...
12401236
def __bool__(self) -> bool: ...
12411237
def __len__(self) -> int: ...
12421238
def __bytes__(self) -> bytes: ...
12431239
def __str__(self) -> str: ...
12441240
def __eq__(self, other: Any) -> bool: ...
12451241
def __hash__(self) -> int: ...
1246-
def __buffer__(self, flags: int) -> memoryview: ...
1247-
def __release_buffer__(self, view: memoryview) -> None: ...
12481242

12491243
_IntoZBytes = Any
12501244

zenoh/shm.pyi

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
# Contributors:
1212
# ZettaScale Zenoh Team, <[email protected]>
1313
#
14-
from collections.abc import Buffer
1514
from typing import Self, final, overload
1615

1716
@final
@@ -111,12 +110,6 @@ class ZShmMut:
111110
def __bytes__(self) -> bytes: ...
112111
def __str__(self) -> str: ...
113112
@overload
114-
def __getitem__(self, item: int) -> int: ...
115-
@overload
116-
def __getitem__(self, item: slice) -> memoryview: ...
117-
@overload
118113
def __setitem__(self, item: int, value: int): ...
119114
@overload
120-
def __setitem__(self, item: slice, value: Buffer): ...
121-
def __buffer__(self, flags: int) -> memoryview: ...
122-
def __release_buffer__(self, view: memoryview) -> None: ...
115+
def __setitem__(self, item: slice, value: bytes | bytearray): ...

0 commit comments

Comments
 (0)