Skip to content

Commit

Permalink
ARROW-2651: [Python] Fix datetime C API import for PyPy (#14539)
Browse files Browse the repository at this point in the history
As described in the [ARROW-2651](https://issues.apache.org/jira/browse/ARROW-2651) issue, this patch fixes the C datetime module import mechanism for PyPy. 

This is related to #2089 which was closed in favor of the JIRA issue.

Authored-by: mattip <[email protected]>
Signed-off-by: Antoine Pitrou <[email protected]>
  • Loading branch information
mattip authored Oct 31, 2022
1 parent 1164785 commit 2a5c773
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
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

0 comments on commit 2a5c773

Please sign in to comment.