This is an automated email from the ASF dual-hosted git repository.

apitrou pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow.git


The following commit(s) were added to refs/heads/master by this push:
     new 2a5c773611 ARROW-2651: [Python] Fix datetime C API import for PyPy 
(#14539)
2a5c773611 is described below

commit 2a5c773611e1d30c1a1a9503f9640565110d96bc
Author: Matti Picus <[email protected]>
AuthorDate: Mon Oct 31 19:06:03 2022 +0100

    ARROW-2651: [Python] Fix datetime C API import for PyPy (#14539)
    
    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]>
---
 python/pyarrow/src/arrow/python/datetime.cc | 2 ++
 python/pyarrow/src/arrow/python/datetime.h  | 6 ++++++
 python/pyarrow/src/arrow/python/pyarrow.cc  | 4 ++++
 3 files changed, 12 insertions(+)

diff --git a/python/pyarrow/src/arrow/python/datetime.cc 
b/python/pyarrow/src/arrow/python/datetime.cc
index 576d0d8a93..b3606acc2b 100644
--- a/python/pyarrow/src/arrow/python/datetime.cc
+++ b/python/pyarrow/src/arrow/python/datetime.cc
@@ -97,6 +97,7 @@ static PyStructSequence_Desc MonthDayNanoTupleDesc = {
 
 }  // namespace
 
+#ifndef PYPY_VERSION
 PyDateTime_CAPI* datetime_api = nullptr;
 
 void InitDatetime() {
@@ -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
diff --git a/python/pyarrow/src/arrow/python/datetime.h 
b/python/pyarrow/src/arrow/python/datetime.h
index ed3c4f6828..f75d05a0ad 100644
--- a/python/pyarrow/src/arrow/python/datetime.h
+++ b/python/pyarrow/src/arrow/python/datetime.h
@@ -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
diff --git a/python/pyarrow/src/arrow/python/pyarrow.cc 
b/python/pyarrow/src/arrow/python/pyarrow.cc
index c3244b74bf..30d1f04f12 100644
--- a/python/pyarrow/src/arrow/python/pyarrow.cc
+++ b/python/pyarrow/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();
 }
 

Reply via email to