Skip to content
This repository was archived by the owner on Jun 6, 2025. It is now read-only.

Commit 6bce4d6

Browse files
fix version comparison operations on lazy GDAL_VERSION object (#11)
1 parent e278f6e commit 6bce4d6

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
1717

1818
## [Unreleased]
1919

20+
### Fixed
21+
22+
- Fixed GDAL version comparison by implementing custom comparison operators for the lazy `GDAL_VERSION` object, allowing Django to compare version information without loading GDAL prematurely
23+
2024
## [0.2.1]
2125

2226
### Fixed

src/django_lazy_gdal/libgdal.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import logging
4+
import operator
45
import os
56
import re
67
from ctypes import CDLL
@@ -11,6 +12,7 @@
1112

1213
from django.core.exceptions import ImproperlyConfigured
1314
from django.utils.functional import SimpleLazyObject
15+
from django.utils.functional import new_method_proxy
1416

1517
logger = logging.getLogger("django.contrib.gis")
1618

@@ -204,4 +206,9 @@ def gdal_version_info():
204206
return (int(major), int(minor), subminor and int(subminor))
205207

206208

207-
GDAL_VERSION = SimpleLazyObject(gdal_version_info)
209+
class ComparableSimpleLazyObject(SimpleLazyObject):
210+
__ge__ = new_method_proxy(operator.ge)
211+
__le__ = new_method_proxy(operator.le)
212+
213+
214+
GDAL_VERSION = ComparableSimpleLazyObject(gdal_version_info)

tests/test_lazy_libgdal.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,19 @@ def test_load_gdal_failure(mock_find_library):
145145

146146
with pytest.raises(ImproperlyConfigured, match="Could not find the GDAL library"):
147147
lazy_libgdal.lgdal.some_attribute
148+
149+
150+
def test_gdal_version_comparison():
151+
import django_lazy_gdal.libgdal as lazy_libgdal
152+
from django_lazy_gdal.libgdal import ComparableSimpleLazyObject
153+
154+
assert isinstance(lazy_libgdal.GDAL_VERSION, ComparableSimpleLazyObject)
155+
156+
version = ComparableSimpleLazyObject(lambda: (3, 2, 1))
157+
158+
assert version >= (3, 0, 0)
159+
assert version > (2, 9, 9)
160+
assert version < (3, 3, 0)
161+
assert version <= (3, 2, 1)
162+
assert version == (3, 2, 1)
163+
assert version != (3, 0, 0)

0 commit comments

Comments
 (0)