Skip to content

Instantly share code, notes, and snippets.

@mattip
Created September 11, 2020 12:17
Show Gist options
  • Save mattip/c9c8398b58721ae5893dc8134c353f28 to your computer and use it in GitHub Desktop.
Save mattip/c9c8398b58721ae5893dc8134c353f28 to your computer and use it in GitHub Desktop.
arrow with pypy3
diff --git a/cpp/src/arrow/python/datetime.cc b/cpp/src/arrow/python/datetime.cc
index 4eeab7f5a..9a7624ef7 100644
--- a/cpp/src/arrow/python/datetime.cc
+++ b/cpp/src/arrow/python/datetime.cc
@@ -74,6 +74,7 @@ bool MatchFixedOffset(const std::string& tz, util::string_view* sign,
} // namespace
+#ifndef PYPY_VERSION
PyDateTime_CAPI* datetime_api = nullptr;
void InitDatetime() {
@@ -84,6 +85,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
diff --git a/cpp/src/arrow/python/datetime.h b/cpp/src/arrow/python/datetime.h
index 4f3adb4cd..5038d6925 100644
--- a/cpp/src/arrow/python/datetime.h
+++ b/cpp/src/arrow/python/datetime.h
@@ -31,16 +31,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
ARROW_PYTHON_EXPORT
inline int64_t PyTime_to_us(PyObject* pytime) {
diff --git a/cpp/src/arrow/python/pyarrow.cc b/cpp/src/arrow/python/pyarrow.cc
index 12883f547..a6b8490e0 100644
--- a/cpp/src/arrow/python/pyarrow.cc
+++ b/cpp/src/arrow/python/pyarrow.cc
@@ -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();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment