From e81c5eb89e5709145b2b61888d3f85f7d4a1655b Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Tue, 9 Apr 2024 09:43:00 +0200 Subject: [PATCH] switch order of consuming __arrow_c_array__ and __arrow_c_device_array__ --- python/pyarrow/array.pxi | 12 ++++++------ python/pyarrow/table.pxi | 16 ++++++++-------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/python/pyarrow/array.pxi b/python/pyarrow/array.pxi index 827b34602fded..27e8fefe45fea 100644 --- a/python/pyarrow/array.pxi +++ b/python/pyarrow/array.pxi @@ -246,25 +246,25 @@ def array(object obj, type=None, mask=None, size=None, from_pandas=None, if hasattr(obj, '__arrow_array__'): return _handle_arrow_array_protocol(obj, type, mask, size) - elif hasattr(obj, '__arrow_c_array__'): + elif hasattr(obj, '__arrow_c_device_array__'): if type is not None: requested_type = type.__arrow_c_schema__() else: requested_type = None - schema_capsule, array_capsule = obj.__arrow_c_array__(requested_type) - out_array = Array._import_from_c_capsule(schema_capsule, array_capsule) + schema_capsule, array_capsule = obj.__arrow_c_device_array__(requested_type) + out_array = Array._import_from_c_device_capsule(schema_capsule, array_capsule) if type is not None and out_array.type != type: # PyCapsule interface type coercion is best effort, so we need to # check the type of the returned array and cast if necessary out_array = array.cast(type, safe=safe, memory_pool=memory_pool) return out_array - elif hasattr(obj, '__arrow_c_device_array__'): + elif hasattr(obj, '__arrow_c_array__'): if type is not None: requested_type = type.__arrow_c_schema__() else: requested_type = None - schema_capsule, array_capsule = obj.__arrow_c_device_array__(requested_type) - out_array = Array._import_from_c_device_capsule(schema_capsule, array_capsule) + schema_capsule, array_capsule = obj.__arrow_c_array__(requested_type) + out_array = Array._import_from_c_capsule(schema_capsule, array_capsule) if type is not None and out_array.type != type: # PyCapsule interface type coercion is best effort, so we need to # check the type of the returned array and cast if necessary diff --git a/python/pyarrow/table.pxi b/python/pyarrow/table.pxi index a8363275afcd2..96fe51b84f84f 100644 --- a/python/pyarrow/table.pxi +++ b/python/pyarrow/table.pxi @@ -5715,27 +5715,27 @@ def record_batch(data, names=None, schema=None, metadata=None): raise ValueError( "The 'names' argument is not valid when passing a dictionary") return RecordBatch.from_pydict(data, schema=schema, metadata=metadata) - elif hasattr(data, "__arrow_c_array__"): + elif hasattr(data, "__arrow_c_device_array__"): if schema is not None: requested_schema = schema.__arrow_c_schema__() else: requested_schema = None - schema_capsule, array_capsule = data.__arrow_c_array__(requested_schema) - batch = RecordBatch._import_from_c_capsule(schema_capsule, array_capsule) + schema_capsule, array_capsule = data.__arrow_c_device_array__(requested_schema) + batch = RecordBatch._import_from_c_device_capsule(schema_capsule, array_capsule) if schema is not None and batch.schema != schema: - # __arrow_c_array__ coerces schema with best effort, so we might + # __arrow_c_device_array__ coerces schema with best effort, so we might # need to cast it if the producer wasn't able to cast to exact schema. batch = batch.cast(schema) return batch - elif hasattr(data, "__arrow_c_device_array__"): + elif hasattr(data, "__arrow_c_array__"): if schema is not None: requested_schema = schema.__arrow_c_schema__() else: requested_schema = None - schema_capsule, array_capsule = data.__arrow_c_device_array__(requested_schema) - batch = RecordBatch._import_from_c_device_capsule(schema_capsule, array_capsule) + schema_capsule, array_capsule = data.__arrow_c_array__(requested_schema) + batch = RecordBatch._import_from_c_capsule(schema_capsule, array_capsule) if schema is not None and batch.schema != schema: - # __arrow_c_device_array__ coerces schema with best effort, so we might + # __arrow_c_array__ coerces schema with best effort, so we might # need to cast it if the producer wasn't able to cast to exact schema. batch = batch.cast(schema) return batch