From 35ecf36b6816cd76bd56c1e8f76fa845baa96a5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20Zim=C3=A1nyi?= Date: Sat, 31 Oct 2026 15:15:39 +0100 Subject: [PATCH] Set the date and interval styles of pymeos_initialize from their names pymeos_initialize sets a date style or an interval style given by name through meos_set_datestyle and meos_set_intervalstyle, passing each the null value its extra parameter takes in C: _ffi.NULL for the void pointer of meos_set_datestyle and 0 for the int of meos_set_intervalstyle. MEOS overwrites extra with the style it parses from the name, so the value passed is never read. tests/meos_init_test.py sets each date style and interval style by name and reads it back with meos_get_datestyle and meos_get_intervalstyle, restoring the styles conftest initializes after each case. --- pymeos/meos_init.py | 5 +++-- tests/meos_init_test.py | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 tests/meos_init_test.py diff --git a/pymeos/meos_init.py b/pymeos/meos_init.py index d2884852..6508206c 100644 --- a/pymeos/meos_init.py +++ b/pymeos/meos_init.py @@ -6,6 +6,7 @@ meos_set_datestyle, meos_set_intervalstyle, ) +from pymeos_cffi.functions import _ffi def pymeos_initialize( @@ -29,13 +30,13 @@ def pymeos_initialize( if date_style is not None: if isinstance(date_style, str): - meos_set_datestyle(date_style, None) + meos_set_datestyle(date_style, _ffi.NULL) else: meos_set_datestyle(*date_style) if interval_style is not None: if isinstance(interval_style, str): - meos_set_intervalstyle(interval_style, None) + meos_set_intervalstyle(interval_style, 0) else: meos_set_intervalstyle(*interval_style) diff --git a/tests/meos_init_test.py b/tests/meos_init_test.py new file mode 100644 index 00000000..829aac13 --- /dev/null +++ b/tests/meos_init_test.py @@ -0,0 +1,38 @@ +import pytest +from pymeos_cffi import meos_get_datestyle, meos_get_intervalstyle + +from pymeos import pymeos_initialize +from tests.conftest import TestPyMEOS + + +# Laid out as #TestTInterpolation; each test restores the styles it changes, since +# every other test runs under the styles conftest initializes. +class TestPyMEOSInitialize(TestPyMEOS): + @pytest.mark.parametrize( + "date_style, expected", + [ + ("SQL, DMY", "SQL, DMY"), + ("German", "German, DMY"), + ("Postgres, MDY", "Postgres, MDY"), + ], + ids=["SQL DMY", "German", "Postgres MDY"], + ) + def test_date_style(self, date_style, expected): + previous = meos_get_datestyle() + try: + pymeos_initialize("UTC", date_style=date_style) + assert meos_get_datestyle() == expected + finally: + pymeos_initialize("UTC", date_style=previous) + + @pytest.mark.parametrize( + "interval_style", + ["postgres", "postgres_verbose", "sql_standard", "iso_8601"], + ) + def test_interval_style(self, interval_style): + previous = meos_get_intervalstyle() + try: + pymeos_initialize("UTC", interval_style=interval_style) + assert meos_get_intervalstyle() == interval_style + finally: + pymeos_initialize("UTC", interval_style=previous)