Skip to content
This repository was archived by the owner on Jun 6, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## [Unreleased]

### Fixed

- 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

## [0.2.1]

### Fixed
Expand Down
9 changes: 8 additions & 1 deletion src/django_lazy_gdal/libgdal.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import logging
import operator
import os
import re
from ctypes import CDLL
Expand All @@ -11,6 +12,7 @@

from django.core.exceptions import ImproperlyConfigured
from django.utils.functional import SimpleLazyObject
from django.utils.functional import new_method_proxy

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

Expand Down Expand Up @@ -204,4 +206,9 @@ def gdal_version_info():
return (int(major), int(minor), subminor and int(subminor))


GDAL_VERSION = SimpleLazyObject(gdal_version_info)
class ComparableSimpleLazyObject(SimpleLazyObject):
__ge__ = new_method_proxy(operator.ge)
__le__ = new_method_proxy(operator.le)


GDAL_VERSION = ComparableSimpleLazyObject(gdal_version_info)
16 changes: 16 additions & 0 deletions tests/test_lazy_libgdal.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,19 @@ def test_load_gdal_failure(mock_find_library):

with pytest.raises(ImproperlyConfigured, match="Could not find the GDAL library"):
lazy_libgdal.lgdal.some_attribute


def test_gdal_version_comparison():
import django_lazy_gdal.libgdal as lazy_libgdal
from django_lazy_gdal.libgdal import ComparableSimpleLazyObject

assert isinstance(lazy_libgdal.GDAL_VERSION, ComparableSimpleLazyObject)

version = ComparableSimpleLazyObject(lambda: (3, 2, 1))

assert version >= (3, 0, 0)
assert version > (2, 9, 9)
assert version < (3, 3, 0)
assert version <= (3, 2, 1)
assert version == (3, 2, 1)
assert version != (3, 0, 0)