Fixed #29484 -- Removed the need to specify SPATIALITE_LIBRARY_PATH with Spatialite 4.2+.

Thanks Tim Graham for the review.
This commit is contained in:
Claude Paroz 2018-06-01 18:26:14 +02:00
parent 86988dd890
commit f3836144db
2 changed files with 22 additions and 18 deletions

View File

@ -27,13 +27,12 @@ class DatabaseWrapper(SQLiteDatabaseWrapper):
# (`libspatialite`). If it's not in the system library path (e.g., it # (`libspatialite`). If it's not in the system library path (e.g., it
# cannot be found by `ctypes.util.find_library`), then it may be set # cannot be found by `ctypes.util.find_library`), then it may be set
# manually in the settings via the `SPATIALITE_LIBRARY_PATH` setting. # manually in the settings via the `SPATIALITE_LIBRARY_PATH` setting.
self.spatialite_lib = getattr(settings, 'SPATIALITE_LIBRARY_PATH', self.lib_spatialite_paths = [name for name in [
find_library('spatialite')) getattr(settings, 'SPATIALITE_LIBRARY_PATH', None),
if not self.spatialite_lib: 'mod_spatialite.so',
raise ImproperlyConfigured('Unable to locate the SpatiaLite library. ' 'mod_spatialite',
'Make sure it is in your library path, or set ' find_library('spatialite'),
'SPATIALITE_LIBRARY_PATH in your settings.' ] if name is not None]
)
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
def get_new_connection(self, conn_params): def get_new_connection(self, conn_params):
@ -47,12 +46,23 @@ class DatabaseWrapper(SQLiteDatabaseWrapper):
'extension loading.' 'extension loading.'
) )
# Load the SpatiaLite library extension on the connection. # Load the SpatiaLite library extension on the connection.
try: for path in self.lib_spatialite_paths:
conn.load_extension(self.spatialite_lib) try:
except Exception as exc: conn.load_extension(path)
except Exception:
if getattr(settings, 'SPATIALITE_LIBRARY_PATH', None):
raise ImproperlyConfigured(
'Unable to load the SpatiaLite library extension '
'as specified in your SPATIALITE_LIBRARY_PATH setting.'
)
continue
else:
break
else:
raise ImproperlyConfigured( raise ImproperlyConfigured(
'Unable to load the SpatiaLite library extension "%s"' % self.spatialite_lib 'Unable to load the SpatiaLite library extension. '
) from exc 'Library names tried: %s' % ', '.join(self.lib_spatialite_paths)
)
return conn return conn
def prepare_database(self): def prepare_database(self):

View File

@ -21,12 +21,6 @@ In any case, you should always be able to :ref:`install from source
__ https://www.gaia-gis.it/fossil/libspatialite __ https://www.gaia-gis.it/fossil/libspatialite
__ https://www.gaia-gis.it/gaia-sins/ __ https://www.gaia-gis.it/gaia-sins/
.. admonition:: ``SPATIALITE_LIBRARY_PATH`` setting required for SpatiaLite 4.2+
If you're using SpatiaLite 4.2+, you must put this in your ``settings.py``::
SPATIALITE_LIBRARY_PATH = 'mod_spatialite'
.. _spatialite_source: .. _spatialite_source:
Installing from source Installing from source