@@ -31,10 +31,17 @@ use pyo3::exceptions::PyValueError;
3131use pyo3:: ffi:: Py_uintptr_t ;
3232use pyo3:: ffi:: c_str;
3333use pyo3:: import_exception;
34+ use pyo3:: intern;
3435use pyo3:: prelude:: * ;
3536use pyo3:: types:: PyCapsule ;
3637use pyo3:: types:: PyTuple ;
3738
39+ use crate :: classes:: array_class;
40+ use crate :: classes:: data_type_class;
41+ use crate :: classes:: field_class;
42+ use crate :: classes:: record_batch_reader_class;
43+ use crate :: classes:: schema_class;
44+
3845const SCHEMA_NAME : & CStr = c_str ! ( "arrow_schema" ) ;
3946const ARRAY_NAME : & CStr = c_str ! ( "arrow_array" ) ;
4047const ARRAY_STREAM_NAME : & CStr = c_str ! ( "arrow_array_stream" ) ;
@@ -69,13 +76,14 @@ pub trait IntoPyArrow {
6976
7077impl < ' py > FromPyArrow < ' _ , ' py > for DataType {
7178 fn from_pyarrow ( value : & Borrowed < ' _ , ' py , PyAny > ) -> PyResult < Self > {
72- if !value. hasattr ( "__arrow_c_schema__" ) ? {
79+ let py = value. py ( ) ;
80+ if !value. hasattr ( intern ! ( py, "__arrow_c_schema__" ) ) ? {
7381 return Err ( PyValueError :: new_err (
7482 "Expected __arrow_c_schema__ attribute to be set." ,
7583 ) ) ;
7684 }
7785
78- let capsule = value. getattr ( "__arrow_c_schema__" ) ?. call0 ( ) ?;
86+ let capsule = value. getattr ( intern ! ( py , "__arrow_c_schema__" ) ) ?. call0 ( ) ?;
7987 let capsule = capsule. cast :: < PyCapsule > ( ) ?;
8088
8189 let schema_ptr = unsafe {
@@ -92,22 +100,24 @@ impl<'py> FromPyArrow<'_, 'py> for DataType {
92100impl ToPyArrow for DataType {
93101 fn to_pyarrow ( & self , py : Python ) -> PyResult < Py < PyAny > > {
94102 let c_schema = FFI_ArrowSchema :: try_from ( self ) . map_err ( to_py_err) ?;
95- let module = py. import ( "pyarrow" ) ?;
96- let class = module. getattr ( "DataType" ) ?;
97- let dtype = class. call_method1 ( "_import_from_c" , ( & raw const c_schema as Py_uintptr_t , ) ) ?;
103+ let dtype = data_type_class ( py) ?. call_method1 (
104+ intern ! ( py, "_import_from_c" ) ,
105+ ( & raw const c_schema as Py_uintptr_t , ) ,
106+ ) ?;
98107 Ok ( dtype. into ( ) )
99108 }
100109}
101110
102111impl < ' py > FromPyArrow < ' _ , ' py > for Field {
103112 fn from_pyarrow ( value : & Borrowed < ' _ , ' py , PyAny > ) -> PyResult < Self > {
104- if !value. hasattr ( "__arrow_c_schema__" ) ? {
113+ let py = value. py ( ) ;
114+ if !value. hasattr ( intern ! ( py, "__arrow_c_schema__" ) ) ? {
105115 return Err ( PyValueError :: new_err (
106116 "Expected __arrow_c_schema__ attribute to be set." ,
107117 ) ) ;
108118 }
109119
110- let capsule = value. getattr ( "__arrow_c_schema__" ) ?. call0 ( ) ?;
120+ let capsule = value. getattr ( intern ! ( py , "__arrow_c_schema__" ) ) ?. call0 ( ) ?;
111121 let capsule = capsule. cast :: < PyCapsule > ( ) ?;
112122
113123 let schema_ptr = unsafe {
@@ -124,22 +134,24 @@ impl<'py> FromPyArrow<'_, 'py> for Field {
124134impl ToPyArrow for Field {
125135 fn to_pyarrow ( & self , py : Python ) -> PyResult < Py < PyAny > > {
126136 let c_schema = FFI_ArrowSchema :: try_from ( self ) . map_err ( to_py_err) ?;
127- let module = py. import ( "pyarrow" ) ?;
128- let class = module. getattr ( "Field" ) ?;
129- let dtype = class. call_method1 ( "_import_from_c" , ( & raw const c_schema as Py_uintptr_t , ) ) ?;
137+ let dtype = field_class ( py) ?. call_method1 (
138+ intern ! ( py, "_import_from_c" ) ,
139+ ( & raw const c_schema as Py_uintptr_t , ) ,
140+ ) ?;
130141 Ok ( dtype. into ( ) )
131142 }
132143}
133144
134145impl < ' py > FromPyArrow < ' _ , ' py > for Schema {
135146 fn from_pyarrow ( value : & Borrowed < ' _ , ' py , PyAny > ) -> PyResult < Self > {
136- if !value. hasattr ( "__arrow_c_schema__" ) ? {
147+ let py = value. py ( ) ;
148+ if !value. hasattr ( intern ! ( py, "__arrow_c_schema__" ) ) ? {
137149 return Err ( PyValueError :: new_err (
138150 "Expected __arrow_c_schema__ attribute to be set." ,
139151 ) ) ;
140152 }
141153
142- let capsule = value. getattr ( "__arrow_c_schema__" ) ?. call0 ( ) ?;
154+ let capsule = value. getattr ( intern ! ( py , "__arrow_c_schema__" ) ) ?. call0 ( ) ?;
143155 let capsule = capsule. cast :: < PyCapsule > ( ) ?;
144156
145157 let schema_ptr = unsafe {
@@ -157,23 +169,24 @@ impl<'py> FromPyArrow<'_, 'py> for Schema {
157169impl ToPyArrow for Schema {
158170 fn to_pyarrow ( & self , py : Python ) -> PyResult < Py < PyAny > > {
159171 let c_schema = FFI_ArrowSchema :: try_from ( self ) . map_err ( to_py_err) ?;
160- let module = py . import ( "pyarrow" ) ? ;
161- let class = module . getattr ( "Schema" ) ? ;
162- let schema =
163- class . call_method1 ( "_import_from_c" , ( & raw const c_schema as Py_uintptr_t , ) ) ?;
172+ let schema = schema_class ( py ) ? . call_method1 (
173+ intern ! ( py , "_import_from_c" ) ,
174+ ( & raw const c_schema as Py_uintptr_t , ) ,
175+ ) ?;
164176 Ok ( schema. into ( ) )
165177 }
166178}
167179
168180impl < ' py > FromPyArrow < ' _ , ' py > for ArrayData {
169181 fn from_pyarrow ( value : & Borrowed < ' _ , ' py , PyAny > ) -> PyResult < Self > {
170- if !value. hasattr ( "__arrow_c_array__" ) ? {
182+ let py = value. py ( ) ;
183+ if !value. hasattr ( intern ! ( py, "__arrow_c_array__" ) ) ? {
171184 return Err ( PyValueError :: new_err (
172185 "Expected __arrow_c_array__ attribute to be set." ,
173186 ) ) ;
174187 }
175188
176- let tuple = value. getattr ( "__arrow_c_array__" ) ?. call0 ( ) ?;
189+ let tuple = value. getattr ( intern ! ( py , "__arrow_c_array__" ) ) ?. call0 ( ) ?;
177190
178191 if !tuple. is_instance_of :: < PyTuple > ( ) {
179192 return Err ( PyTypeError :: new_err (
@@ -207,10 +220,8 @@ impl ToPyArrow for ArrayData {
207220 let array = FFI_ArrowArray :: new ( self ) ;
208221 let schema = FFI_ArrowSchema :: try_from ( self . data_type ( ) ) . map_err ( to_py_err) ?;
209222
210- let module = py. import ( "pyarrow" ) ?;
211- let class = module. getattr ( "Array" ) ?;
212- let array = class. call_method1 (
213- "_import_from_c" ,
223+ let array = array_class ( py) ?. call_method1 (
224+ intern ! ( py, "_import_from_c" ) ,
214225 (
215226 addr_of ! ( array) as Py_uintptr_t ,
216227 addr_of ! ( schema) as Py_uintptr_t ,
@@ -222,13 +233,14 @@ impl ToPyArrow for ArrayData {
222233
223234impl < ' py > FromPyArrow < ' _ , ' py > for RecordBatch {
224235 fn from_pyarrow ( value : & Borrowed < ' _ , ' py , PyAny > ) -> PyResult < Self > {
225- if !value. hasattr ( "__arrow_c_array__" ) ? {
236+ let py = value. py ( ) ;
237+ if !value. hasattr ( intern ! ( py, "__arrow_c_array__" ) ) ? {
226238 return Err ( PyValueError :: new_err (
227239 "Expected __arrow_c_array__ attribute to be set." ,
228240 ) ) ;
229241 }
230242
231- let tuple = value. getattr ( "__arrow_c_array__" ) ?. call0 ( ) ?;
243+ let tuple = value. getattr ( intern ! ( py , "__arrow_c_array__" ) ) ?. call0 ( ) ?;
232244
233245 if !tuple. is_instance_of :: < PyTuple > ( ) {
234246 return Err ( PyTypeError :: new_err (
@@ -286,20 +298,21 @@ impl ToPyArrow for RecordBatch {
286298 let reader = RecordBatchIterator :: new ( vec ! [ Ok ( self . clone( ) ) ] , self . schema ( ) ) ;
287299 let reader: Box < dyn RecordBatchReader + Send > = Box :: new ( reader) ;
288300 let py_reader = reader. into_pyarrow ( py) ?;
289- py_reader. call_method0 ( py, "read_next_batch" )
301+ py_reader. call_method0 ( py, intern ! ( py , "read_next_batch" ) )
290302 }
291303}
292304
293305/// Supports conversion from `pyarrow.RecordBatchReader` to [ArrowArrayStreamReader].
294306impl < ' py > FromPyArrow < ' _ , ' py > for ArrowArrayStreamReader {
295307 fn from_pyarrow ( value : & Borrowed < ' _ , ' py , PyAny > ) -> PyResult < Self > {
296- if !value. hasattr ( "__arrow_c_stream__" ) ? {
308+ let py = value. py ( ) ;
309+ if !value. hasattr ( intern ! ( py, "__arrow_c_stream__" ) ) ? {
297310 return Err ( PyValueError :: new_err (
298311 "Expected __arrow_c_stream__ attribute to be set." ,
299312 ) ) ;
300313 }
301314
302- let capsule = value. getattr ( "__arrow_c_stream__" ) ?. call0 ( ) ?;
315+ let capsule = value. getattr ( intern ! ( py , "__arrow_c_stream__" ) ) ?. call0 ( ) ?;
303316 let capsule = capsule. cast :: < PyCapsule > ( ) ?;
304317
305318 let array_ptr = capsule
@@ -323,10 +336,9 @@ impl IntoPyArrow for Box<dyn RecordBatchReader + Send> {
323336 fn into_pyarrow ( self , py : Python ) -> PyResult < Py < PyAny > > {
324337 let mut stream = FFI_ArrowArrayStream :: new ( self ) ;
325338
326- let module = py. import ( "pyarrow" ) ?;
327- let class = module. getattr ( "RecordBatchReader" ) ?;
328339 let args = PyTuple :: new ( py, [ & raw mut stream as Py_uintptr_t ] ) ?;
329- let reader = class. call_method1 ( "_import_from_c" , args) ?;
340+ let reader =
341+ record_batch_reader_class ( py) ?. call_method1 ( intern ! ( py, "_import_from_c" ) , args) ?;
330342
331343 Ok ( Py :: from ( reader) )
332344 }
0 commit comments