Skip to content

Commit

Permalink
Add frozen parameter to pyclass macro (#282)
Browse files Browse the repository at this point in the history
  • Loading branch information
kylebarron authored Jan 6, 2025
1 parent 816f150 commit d430963
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 26 deletions.
2 changes: 1 addition & 1 deletion pyo3-arrow/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use crate::{PyDataType, PyField};
/// In particular, storing a [FieldRef] is required to persist Arrow extension metadata through the
/// C Data Interface.
#[derive(Debug)]
#[pyclass(module = "arro3.core._core", name = "Array", subclass)]
#[pyclass(module = "arro3.core._core", name = "Array", subclass, frozen)]
pub struct PyArray {
array: ArrayRef,
field: FieldRef,
Expand Down
16 changes: 8 additions & 8 deletions pyo3-arrow/src/array_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::{PyArray, PyChunkedArray, PyField};
/// A Python-facing Arrow array reader.
///
/// This is a wrapper around a [ArrayReader].
#[pyclass(module = "arro3.core._core", name = "ArrayReader", subclass)]
#[pyclass(module = "arro3.core._core", name = "ArrayReader", subclass, frozen)]
pub struct PyArrayReader(pub(crate) Mutex<Option<Box<dyn ArrayReader + Send>>>);

impl PyArrayReader {
Expand Down Expand Up @@ -79,7 +79,7 @@ impl PyArrayReader {

/// Export this to a Python `arro3.core.ArrayReader`.
#[allow(clippy::wrong_self_convention)]
pub fn to_arro3<'py>(&'py mut self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
pub fn to_arro3<'py>(&'py self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
let arro3_mod = py.import(intern!(py, "arro3.core"))?;
arro3_mod.getattr(intern!(py, "ArrayReader"))?.call_method1(
intern!(py, "from_arrow_pycapsule"),
Expand All @@ -89,7 +89,7 @@ impl PyArrayReader {

/// Export this to a Python `nanoarrow.ArrayStream`.
#[allow(clippy::wrong_self_convention)]
pub fn to_nanoarrow<'py>(&'py mut self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
pub fn to_nanoarrow<'py>(&'py self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
to_nanoarrow_array_stream(py, &self.__arrow_c_stream__(py, None)?)
}
}
Expand Down Expand Up @@ -120,7 +120,7 @@ impl PyArrayReader {

#[pyo3(signature = (requested_schema=None))]
fn __arrow_c_stream__<'py>(
&'py mut self,
&'py self,
py: Python<'py>,
requested_schema: Option<Bound<'py, PyCapsule>>,
) -> PyArrowResult<Bound<'py, PyCapsule>> {
Expand All @@ -135,11 +135,11 @@ impl PyArrayReader {

// Return self
// https://stackoverflow.com/a/52056290
fn __iter__<'py>(&'py mut self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
fn __iter__<'py>(&'py self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
self.to_arro3(py)
}

fn __next__(&mut self) -> PyArrowResult<Arro3Array> {
fn __next__(&self) -> PyArrowResult<Arro3Array> {
self.read_next_array()
}

Expand Down Expand Up @@ -189,7 +189,7 @@ impl PyArrayReader {
Ok(PyField::new(self.field_ref()?).into())
}

fn read_all(&mut self) -> PyArrowResult<Arro3ChunkedArray> {
fn read_all(&self) -> PyArrowResult<Arro3ChunkedArray> {
let stream = self
.0
.lock()
Expand All @@ -204,7 +204,7 @@ impl PyArrayReader {
Ok(PyChunkedArray::try_new(arrays, field)?.into())
}

fn read_next_array(&mut self) -> PyArrowResult<Arro3Array> {
fn read_next_array(&self) -> PyArrowResult<Arro3Array> {
let mut inner = self.0.lock().unwrap();
let stream = inner
.as_mut()
Expand Down
4 changes: 2 additions & 2 deletions pyo3-arrow/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use crate::PyArray;
/// The Python buffer protocol is implemented on this buffer to enable zero-copy data transfer of
/// the core buffer into Python. This allows for zero-copy data sharing with numpy via
/// `numpy.frombuffer`.
#[pyclass(module = "arro3.core._core", name = "Buffer", subclass)]
#[pyclass(module = "arro3.core._core", name = "Buffer", subclass, frozen)]
pub struct PyArrowBuffer(Buffer);

impl AsRef<Buffer> for PyArrowBuffer {
Expand Down Expand Up @@ -87,7 +87,7 @@ impl PyArrowBuffer {
/// This is taken from opendal:
/// https://github.com/apache/opendal/blob/d001321b0f9834bc1e2e7d463bcfdc3683e968c9/bindings/python/src/utils.rs#L51-L72
unsafe fn __getbuffer__(
slf: PyRefMut<Self>,
slf: PyRef<Self>,
view: *mut ffi::Py_buffer,
flags: c_int,
) -> PyResult<()> {
Expand Down
2 changes: 1 addition & 1 deletion pyo3-arrow/src/chunked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::{PyArray, PyDataType, PyField, PyScalar};
///
/// This is a wrapper around a [FieldRef] and a `Vec` of [ArrayRef].
#[derive(Debug)]
#[pyclass(module = "arro3.core._core", name = "ChunkedArray", subclass)]
#[pyclass(module = "arro3.core._core", name = "ChunkedArray", subclass, frozen)]
pub struct PyChunkedArray {
chunks: Vec<ArrayRef>,
field: FieldRef,
Expand Down
2 changes: 1 addition & 1 deletion pyo3-arrow/src/datatypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl<'a> FromPyObject<'a> for PyTimeUnit {

/// A Python-facing wrapper around [DataType].
#[derive(PartialEq, Eq, Debug)]
#[pyclass(module = "arro3.core._core", name = "DataType", subclass)]
#[pyclass(module = "arro3.core._core", name = "DataType", subclass, frozen)]
pub struct PyDataType(DataType);

impl PyDataType {
Expand Down
2 changes: 1 addition & 1 deletion pyo3-arrow/src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::PyDataType;
///
/// This is a wrapper around a [FieldRef].
#[derive(Debug)]
#[pyclass(module = "arro3.core._core", name = "Field", subclass)]
#[pyclass(module = "arro3.core._core", name = "Field", subclass, frozen)]
pub struct PyField(FieldRef);

impl PyField {
Expand Down
2 changes: 1 addition & 1 deletion pyo3-arrow/src/record_batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::{PyArray, PyField, PySchema};
/// A Python-facing Arrow record batch.
///
/// This is a wrapper around a [RecordBatch].
#[pyclass(module = "arro3.core._core", name = "RecordBatch", subclass)]
#[pyclass(module = "arro3.core._core", name = "RecordBatch", subclass, frozen)]
#[derive(Debug)]
pub struct PyRecordBatch(RecordBatch);

Expand Down
21 changes: 13 additions & 8 deletions pyo3-arrow/src/record_batch_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ use crate::{PyRecordBatch, PySchema, PyTable};
/// A Python-facing Arrow record batch reader.
///
/// This is a wrapper around a [RecordBatchReader].
#[pyclass(module = "arro3.core._core", name = "RecordBatchReader", subclass)]
#[pyclass(
module = "arro3.core._core",
name = "RecordBatchReader",
subclass,
frozen
)]
pub struct PyRecordBatchReader(pub(crate) Mutex<Option<Box<dyn RecordBatchReader + Send>>>);

impl PyRecordBatchReader {
Expand Down Expand Up @@ -81,7 +86,7 @@ impl PyRecordBatchReader {
}

/// Export this to a Python `arro3.core.RecordBatchReader`.
pub fn to_arro3<'py>(&'py mut self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
pub fn to_arro3<'py>(&'py self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
let arro3_mod = py.import(intern!(py, "arro3.core"))?;
arro3_mod
.getattr(intern!(py, "RecordBatchReader"))?
Expand All @@ -92,7 +97,7 @@ impl PyRecordBatchReader {
}

/// Export this to a Python `nanoarrow.ArrayStream`.
pub fn to_nanoarrow<'py>(&'py mut self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
pub fn to_nanoarrow<'py>(&'py self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
to_nanoarrow_array_stream(py, &self.__arrow_c_stream__(py, None)?)
}

Expand Down Expand Up @@ -155,7 +160,7 @@ impl PyRecordBatchReader {

#[pyo3(signature = (requested_schema=None))]
fn __arrow_c_stream__<'py>(
&'py mut self,
&'py self,
py: Python<'py>,
requested_schema: Option<Bound<'py, PyCapsule>>,
) -> PyArrowResult<Bound<'py, PyCapsule>> {
Expand All @@ -170,11 +175,11 @@ impl PyRecordBatchReader {

// Return self
// https://stackoverflow.com/a/52056290
fn __iter__<'py>(&'py mut self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
fn __iter__<'py>(&'py self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
self.to_arro3(py)
}

fn __next__(&mut self) -> PyArrowResult<Arro3RecordBatch> {
fn __next__(&self) -> PyArrowResult<Arro3RecordBatch> {
self.read_next_batch()
}

Expand Down Expand Up @@ -216,7 +221,7 @@ impl PyRecordBatchReader {
self.0.lock().unwrap().is_none()
}

fn read_all(&mut self) -> PyArrowResult<Arro3Table> {
fn read_all(&self) -> PyArrowResult<Arro3Table> {
let stream = self
.0
.lock()
Expand All @@ -231,7 +236,7 @@ impl PyRecordBatchReader {
Ok(PyTable::try_new(batches, schema)?.into())
}

fn read_next_batch(&mut self) -> PyArrowResult<Arro3RecordBatch> {
fn read_next_batch(&self) -> PyArrowResult<Arro3RecordBatch> {
let mut inner = self.0.lock().unwrap();
let stream = inner
.as_mut()
Expand Down
2 changes: 1 addition & 1 deletion pyo3-arrow/src/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::{PyArray, PyField};

/// A Python-facing Arrow scalar
#[derive(Debug)]
#[pyclass(module = "arro3.core._core", name = "Scalar", subclass)]
#[pyclass(module = "arro3.core._core", name = "Scalar", subclass, frozen)]
pub struct PyScalar {
array: ArrayRef,
field: FieldRef,
Expand Down
2 changes: 1 addition & 1 deletion pyo3-arrow/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::{PyDataType, PyField, PyTable};
///
/// This is a wrapper around a [SchemaRef].
#[derive(Debug)]
#[pyclass(module = "arro3.core._core", name = "Schema", subclass)]
#[pyclass(module = "arro3.core._core", name = "Schema", subclass, frozen)]
pub struct PySchema(SchemaRef);

impl PySchema {
Expand Down
2 changes: 1 addition & 1 deletion pyo3-arrow/src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use crate::{PyChunkedArray, PyField, PyRecordBatch, PyRecordBatchReader, PySchem
/// A Python-facing Arrow table.
///
/// This is a wrapper around a [SchemaRef] and a `Vec` of [RecordBatch].
#[pyclass(module = "arro3.core._core", name = "Table", subclass)]
#[pyclass(module = "arro3.core._core", name = "Table", subclass, frozen)]
#[derive(Debug)]
pub struct PyTable {
batches: Vec<RecordBatch>,
Expand Down

0 comments on commit d430963

Please sign in to comment.