Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ARROW-2651: [Python] Fix datetime C API import for PyPy #14539

Merged
merged 2 commits into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions python/pyarrow/src/arrow/python/datetime.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ static PyStructSequence_Desc MonthDayNanoTupleDesc = {

} // namespace

#ifndef PYPY_VERSION
PyDateTime_CAPI* datetime_api = nullptr;

void InitDatetime() {
Expand All @@ -107,6 +108,7 @@ void InitDatetime() {
Py_FatalError("Could not import datetime C API");
}
}
#endif

// The following code is adapted from
// https://github.com/numpy/numpy/blob/master/numpy/core/src/multiarray/datetime.c
Expand Down
6 changes: 6 additions & 0 deletions python/pyarrow/src/arrow/python/datetime.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,22 @@
// C datetime API. This is error-prone and potentially costly.
// Instead, we redefine PyDateTimeAPI to point to a global variable,
// which is initialized once by calling InitDatetime().
#ifdef PYPY_VERSION
#include "datetime.h"
#else
#define PyDateTimeAPI ::arrow::py::internal::datetime_api
#endif

namespace arrow {
namespace py {
namespace internal {

#ifndef PYPY_VERSION
extern PyDateTime_CAPI* datetime_api;

ARROW_PYTHON_EXPORT
void InitDatetime();
#endif

// Returns the MonthDayNano namedtuple type (increments the reference count).
ARROW_PYTHON_EXPORT
Expand Down
4 changes: 4 additions & 0 deletions python/pyarrow/src/arrow/python/pyarrow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ static Status UnwrapError(PyObject* obj, const char* expected_type) {
}

int import_pyarrow() {
#ifdef PYPY_VERSION
PyDateTime_IMPORT;
#else
internal::InitDatetime();
#endif
return ::import_pyarrow__lib();
}

Expand Down