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

paleolimbot pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-nanoarrow.git


The following commit(s) were added to refs/heads/main by this push:
     new 761ead52 fix(Python) Use timezone-aware timestamps in some unit tests 
on Windows (#512)
761ead52 is described below

commit 761ead522ed94998c4c9001249c52ea0c141e20a
Author: Abhishek Singh <[email protected]>
AuthorDate: Sun Jun 9 18:05:50 2024 -0700

    fix(Python) Use timezone-aware timestamps in some unit tests on Windows 
(#512)
    
    Using `datetime.timestamp()` for timestamps close to EPOCH fails on
    Windows crashing Python.
    
    Please check this run
    
https://github.com/apache/arrow-nanoarrow/actions/runs/9428246621/job/25973355558
    
    Following snippet
    
    ```python
     def test_c_array_timestamp_microseconds():
         d1 = int(round(datetime(1970, 1, 1).timestamp() * 1e6))
    ```
    raises error
    
    ```bash
        OSError: [Errno 22] Invalid argument
    ```
    
    The crux of the problem is explained in this [CPython
    issue](https://github.com/python/cpython/issues/81708). Solution is to
    use timezone aware datetime objects
    
    ---------
    
    Co-authored-by: Dewey Dunnington <[email protected]>
---
 python/tests/test_c_array.py  | 36 ++++++++++++++++++------------------
 python/tests/test_c_buffer.py |  8 ++++----
 2 files changed, 22 insertions(+), 22 deletions(-)

diff --git a/python/tests/test_c_array.py b/python/tests/test_c_array.py
index 2db9bdb8..6df96472 100644
--- a/python/tests/test_c_array.py
+++ b/python/tests/test_c_array.py
@@ -16,7 +16,7 @@
 # under the License.
 
 import array
-from datetime import date, datetime
+from datetime import date, datetime, timezone
 
 import pytest
 from nanoarrow._lib import CArrayBuilder, NanoarrowException
@@ -520,9 +520,9 @@ def test_c_array_from_buffers_validation():
 
 
 def test_c_array_timestamp_seconds():
-    d1 = int(round(datetime(1970, 1, 1).timestamp()))
-    d2 = int(round(datetime(1985, 12, 31).timestamp()))
-    d3 = int(round(datetime(2005, 3, 4).timestamp()))
+    d1 = int(round(datetime(1970, 1, 1, tzinfo=timezone.utc).timestamp()))
+    d2 = int(round(datetime(1985, 12, 31, tzinfo=timezone.utc).timestamp()))
+    d3 = int(round(datetime(2005, 3, 4, tzinfo=timezone.utc).timestamp()))
     c_array = na.c_array([d1, d2, d3], na.timestamp("s"))
     assert c_array.length == 3
     assert c_array.null_count == 0
@@ -532,9 +532,9 @@ def test_c_array_timestamp_seconds():
 
 
 def test_c_array_timestamp_seconds_from_pybuffer():
-    d1 = int(round(datetime(1970, 1, 1).timestamp()))
-    d2 = int(round(datetime(1985, 12, 31).timestamp()))
-    d3 = int(round(datetime(2005, 3, 4).timestamp()))
+    d1 = int(round(datetime(1970, 1, 1, tzinfo=timezone.utc).timestamp()))
+    d2 = int(round(datetime(1985, 12, 31, tzinfo=timezone.utc).timestamp()))
+    d3 = int(round(datetime(2005, 3, 4, tzinfo=timezone.utc).timestamp()))
     c_array = na.c_array(array.array("q", [d1, d2, d3]), na.timestamp("s"))
     assert c_array.length == 3
     assert c_array.null_count == 0
@@ -544,9 +544,9 @@ def test_c_array_timestamp_seconds_from_pybuffer():
 
 
 def test_c_array_timestamp_milliseconds():
-    d1 = int(round(datetime(1970, 1, 1).timestamp() * 1e3))
-    d2 = int(round(datetime(1985, 12, 31).timestamp() * 1e3))
-    d3 = int(round(datetime(2005, 3, 4).timestamp() * 1e3))
+    d1 = int(round(datetime(1970, 1, 1, tzinfo=timezone.utc).timestamp() * 
1e3))
+    d2 = int(round(datetime(1985, 12, 31, tzinfo=timezone.utc).timestamp() * 
1e3))
+    d3 = int(round(datetime(2005, 3, 4, tzinfo=timezone.utc).timestamp() * 
1e3))
     c_array = na.c_array([d1, d2, d3], na.timestamp("ms"))
     assert c_array.length == 3
     assert c_array.null_count == 0
@@ -556,9 +556,9 @@ def test_c_array_timestamp_milliseconds():
 
 
 def test_c_array_timestamp_milliseconds_from_pybuffer():
-    d1 = int(round(datetime(1970, 1, 1).timestamp() * 1e3))
-    d2 = int(round(datetime(1985, 12, 31).timestamp() * 1e3))
-    d3 = int(round(datetime(2005, 3, 4).timestamp() * 1e3))
+    d1 = int(round(datetime(1970, 1, 1, tzinfo=timezone.utc).timestamp() * 
1e3))
+    d2 = int(round(datetime(1985, 12, 31, tzinfo=timezone.utc).timestamp() * 
1e3))
+    d3 = int(round(datetime(2005, 3, 4, tzinfo=timezone.utc).timestamp() * 
1e3))
     c_array = na.c_array(array.array("q", [d1, d2, d3]), na.timestamp("ms"))
     assert c_array.length == 3
     assert c_array.null_count == 0
@@ -568,9 +568,9 @@ def test_c_array_timestamp_milliseconds_from_pybuffer():
 
 
 def test_c_array_timestamp_microseconds():
-    d1 = int(round(datetime(1970, 1, 1).timestamp() * 1e6))
-    d2 = int(round(datetime(1985, 12, 31).timestamp() * 1e6))
-    d3 = int(round(datetime(2005, 3, 4).timestamp() * 1e6))
+    d1 = int(round(datetime(1970, 1, 1, tzinfo=timezone.utc).timestamp() * 
1e6))
+    d2 = int(round(datetime(1985, 12, 31, tzinfo=timezone.utc).timestamp() * 
1e6))
+    d3 = int(round(datetime(2005, 3, 4, tzinfo=timezone.utc).timestamp() * 
1e6))
     c_array = na.c_array([d1, d2, d3], na.timestamp("us"))
     assert c_array.length == 3
     assert c_array.null_count == 0
@@ -580,8 +580,8 @@ def test_c_array_timestamp_microseconds():
 
 
 def test_c_array_timestamp_nanoseconds():
-    d1 = int(round(datetime(1970, 1, 1).timestamp() * 1e9))
-    d2 = int(round(datetime(1985, 12, 31).timestamp() * 1e9))
+    d1 = int(round(datetime(1970, 1, 1, tzinfo=timezone.utc).timestamp() * 
1e9))
+    d2 = int(round(datetime(1985, 12, 31, tzinfo=timezone.utc).timestamp() * 
1e9))
     d3 = int(round(datetime(2005, 3, 4).timestamp() * 1e9))
     c_array = na.c_array([d1, d2, d3], na.timestamp("ns"))
     assert c_array.length == 3
diff --git a/python/tests/test_c_buffer.py b/python/tests/test_c_buffer.py
index 94111d5f..fbd5538d 100644
--- a/python/tests/test_c_buffer.py
+++ b/python/tests/test_c_buffer.py
@@ -17,7 +17,7 @@
 
 import struct
 import sys
-from datetime import date, datetime
+from datetime import date, datetime, timezone
 
 import pytest
 from nanoarrow._lib import CBuffer, CBufferBuilder
@@ -366,9 +366,9 @@ def test_c_buffer_bitmap_from_iterable():
 
 
 def test_c_buffer_from_timestamp_iterable():
-    d1 = int(round(datetime(1970, 1, 1).timestamp() * 1e3))
-    d2 = int(round(datetime(1985, 12, 31).timestamp() * 1e3))
-    d3 = int(round(datetime(2005, 3, 4).timestamp() * 1e3))
+    d1 = int(round(datetime(1970, 1, 1, tzinfo=timezone.utc).timestamp() * 
1e3))
+    d2 = int(round(datetime(1985, 12, 31, tzinfo=timezone.utc).timestamp() * 
1e3))
+    d3 = int(round(datetime(2005, 3, 4, tzinfo=timezone.utc).timestamp() * 
1e3))
     with pytest.raises(ValueError):
         na.c_buffer([d1, d2, d3], na.timestamp("ms"))
 

Reply via email to