Skip to content

Commit

Permalink
extract out macros
Browse files Browse the repository at this point in the history
  • Loading branch information
JerAguilon committed Nov 12, 2023
1 parent 1a59b49 commit 43e382e
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 44 deletions.
15 changes: 0 additions & 15 deletions python/pyarrow/src/arrow/python/pyarrow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,6 @@ int import_pyarrow() {
return ::import_pyarrow__lib();
}

#define DEFINE_WRAP_FUNCTIONS(FUNC_SUFFIX, TYPE_NAME) \
bool is_##FUNC_SUFFIX(PyObject* obj) { return ::pyarrow_is_##FUNC_SUFFIX(obj) != 0; } \
\
PyObject* wrap_##FUNC_SUFFIX(const TYPE_NAME& src) { \
return ::pyarrow_wrap_##FUNC_SUFFIX(src); \
} \
Result<TYPE_NAME> unwrap_##FUNC_SUFFIX(PyObject* obj) { \
auto out = ::pyarrow_unwrap_##FUNC_SUFFIX(obj); \
if (IS_VALID(out)) { \
return std::move(out); \
} else { \
return UnwrapError(obj, #TYPE_NAME); \
} \
}

#define IS_VALID(OUT) OUT

DEFINE_WRAP_FUNCTIONS(buffer, std::shared_ptr<Buffer>)
Expand Down
6 changes: 1 addition & 5 deletions python/pyarrow/src/arrow/python/pyarrow.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#pragma once

#include "arrow/python/platform.h"
#include "arrow/python/wrapping.h"

#include <memory>

Expand Down Expand Up @@ -45,11 +46,6 @@ namespace py {
// Returns 0 on success, -1 on error.
ARROW_PYTHON_EXPORT int import_pyarrow();

#define DECLARE_WRAP_FUNCTIONS(FUNC_SUFFIX, TYPE_NAME) \
ARROW_PYTHON_EXPORT bool is_##FUNC_SUFFIX(PyObject*); \
ARROW_PYTHON_EXPORT Result<TYPE_NAME> unwrap_##FUNC_SUFFIX(PyObject*); \
ARROW_PYTHON_EXPORT PyObject* wrap_##FUNC_SUFFIX(const TYPE_NAME&);

DECLARE_WRAP_FUNCTIONS(buffer, std::shared_ptr<Buffer>)

DECLARE_WRAP_FUNCTIONS(data_type, std::shared_ptr<DataType>)
Expand Down
6 changes: 1 addition & 5 deletions python/pyarrow/src/arrow/python/pyarrow_acero.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// under the License.

#include "arrow/python/pyarrow_acero.h"
#include "arrow/python/wrapping.h"

#include <memory>
#include <utility>
Expand All @@ -38,11 +39,6 @@ namespace {
namespace arrow {
namespace py {

static Status UnwrapError(PyObject* obj, const char* expected_type) {
return Status::TypeError("Could not unwrap ", expected_type,
" from Python object of type '", Py_TYPE(obj)->tp_name, "'");
}

int import_pyarrow_acero() {
#ifdef PYPY_VERSION
PyDateTime_IMPORT;
Expand Down
19 changes: 4 additions & 15 deletions python/pyarrow/src/arrow/python/pyarrow_acero.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,19 @@
#pragma once

#include "arrow/python/platform.h"
#include "arrow/python/wrapping.h"

#include <memory>

#include "arrow/python/visibility.h"

#include "arrow/sparse_tensor.h"

// Work around ARROW-2317 (C linkage warning from Cython)
extern "C++" {

namespace arrow {

class Array;
class Buffer;
class DataType;
class Field;
class RecordBatch;
class Schema;
class Status;
class Table;
class Tensor;

namespace acero {
struct Declaration;
}
struct ExecNodeOptions;
} // namespace acero

namespace py {

Expand All @@ -55,6 +43,7 @@ ARROW_PYTHON_EXPORT int import_pyarrow_acero();
ARROW_PYTHON_EXPORT PyObject* wrap_##FUNC_SUFFIX(const TYPE_NAME&);

DECLARE_WRAP_FUNCTIONS(declaration, acero::Declaration)
DECLARE_WRAP_FUNCTIONS(declaration, std::shared_ptr<acero::ExecNodeOptions>)

#undef DECLARE_WRAP_FUNCTIONS

Expand Down
2 changes: 0 additions & 2 deletions python/pyarrow/src/arrow/python/pyarrow_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,4 @@
// under the License.

// For backward compatibility.
#include "arrow/util/config.h"

#include "arrow/python/lib_api.h"
52 changes: 52 additions & 0 deletions python/pyarrow/src/arrow/python/wrapping.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

#pragma once

#include "arrow/python/common.h"
#include "arrow/status.h"

namespace arrow {

namespace py {

static Status UnwrapError(PyObject* obj, const char* expected_type) {
return Status::TypeError("Could not unwrap ", expected_type,
" from Python object of type '", Py_TYPE(obj)->tp_name, "'");
}

#define DECLARE_WRAP_FUNCTIONS(FUNC_SUFFIX, TYPE_NAME) \
ARROW_PYTHON_EXPORT bool is_##FUNC_SUFFIX(PyObject*); \
ARROW_PYTHON_EXPORT Result<TYPE_NAME> unwrap_##FUNC_SUFFIX(PyObject*); \
ARROW_PYTHON_EXPORT PyObject* wrap_##FUNC_SUFFIX(const TYPE_NAME&);

#define DEFINE_WRAP_FUNCTIONS(FUNC_SUFFIX, TYPE_NAME) \
bool is_##FUNC_SUFFIX(PyObject* obj) { return ::pyarrow_is_##FUNC_SUFFIX(obj) != 0; } \
\
PyObject* wrap_##FUNC_SUFFIX(const TYPE_NAME& src) { \
return ::pyarrow_wrap_##FUNC_SUFFIX(src); \
} \
Result<TYPE_NAME> unwrap_##FUNC_SUFFIX(PyObject* obj) { \
auto out = ::pyarrow_unwrap_##FUNC_SUFFIX(obj); \
if (IS_VALID(out)) { \
return std::move(out); \
} else { \
return UnwrapError(obj, #TYPE_NAME); \
} \
}
} // namespace py
} // namespace arrow

0 comments on commit 43e382e

Please sign in to comment.