Opened 15 years ago

Closed 15 years ago

#11867 closed (fixed)

if spatialite is installed and pysqlite2.5 is installed without `enable_load_extension` tests won't run

Reported by: Daniel Ring Owned by: nobody
Component: GIS Version: 1.1
Severity: Keywords: enable_load_extension
Cc: Daniel Ring Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Using spatialite with contrib.gis is supported only with pysqlite2.5.0 or higher, and pysqlite must be compiled with enable_load_extension support (see http://geodjango.org/docs/install.html#pysqlite2 ). If spatialite is present, settings.DATABASE_ENGINE == 'sqlite3', and pysqlite is present but without enable_load_extension support, tests/runtests.py fails as follows:

Traceback (most recent call last):
  File "./runtests.py", line 191, in <module>
    django_tests(int(options.verbosity), options.interactive, args)
  File "./runtests.py", line 161, in django_tests
    failures = test_runner(test_labels, verbosity=verbosity, interactive=interactive, extra_tests=extra_tests)
  File "/home/dan/work/django/django/django/test/simple.py", line 191, in run_tests
    connection.creation.create_test_db(verbosity, autoclobber=not interactive)
  File "/home/dan/work/django/django/django/db/backends/creation.py", line 332, in create_test_db
    can_rollback = self._rollback_works()
  File "/home/dan/work/django/django/django/db/backends/creation.py", line 389, in _rollback_works
    cursor = self.connection.cursor()
  File "/home/dan/work/django/django/django/db/backends/__init__.py", line 81, in cursor
    cursor = self._cursor()
  File "/home/dan/work/django/django/django/db/backends/sqlite3/base.py", line 175, in _cursor
    connection_created.send(sender=self.__class__)
  File "/home/dan/work/django/django/django/dispatch/dispatcher.py", line 166, in send
    response = receiver(signal=self, sender=sender, **named)
  File "/home/dan/work/django/django/django/contrib/gis/db/backend/spatialite/__init__.py", line 26, in initialize_spatialite
    connection.connection.enable_load_extension(True)
AttributeError: 'sqlite3.Connection' object has no attribute 'enable_load_extension'

Note that this is not a test failure, but rather a failure of any tests to run at all.

Also, Debian package python-pysqlite2 (currently) defaults to disabling enable_load_extension. (See: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=543252 )

I think either

  1. settings.DATABASE_ENGINE='spatialite' instead of 'sqlite3' should be used (my preference) (or maybe sqlite3_spatialite or something); or
  1. contrib.gis.tests.utils.spatialite should only be set if enable_load_extension is available (contrib.gis tests should be skipped somehow).

Change History (1)

comment:1 by jbronn, 15 years ago

Resolution: fixed
Status: newclosed

This was fixed in trunk when multi-db was merged in. In order to support multiple databases, the spatial backends became full-fledged Django database backends. An example configuration:

DATABASES = {
    'default' : {
        'ENGINE' : 'django.contrib.gis.db.backends.spatialite',
        'NAME'   : 'geodjango.db',
    }
}

Thus, your request for a spatialite backend was fulfilled. If still using the old DATABASE_* settings, then DATABASE_ENGINE='sqlite3' may still be used but 'django.contrib.gis' must also be in your INSTALLED_APPS to enable backwards-compatibility.

Also trunk now has better error-handling in absence of enable_load_extension. Specifically, the spatialite backend, upon initialization, now explicitly checks for support and raises a clear error on what went wrong:

# Enabling extension loading on the SQLite connection.
try:
    self.connection.enable_load_extension(True)
except AttributeError:
    raise ImproperlyConfigured('The pysqlite library does not support C extension loading. '
                               'Both SQLite and pysqlite must be configured to allow '
                               'the loading of extensions to use SpatiaLite.'
                              )

(from [browser:django/trunk/django/contrib/gis/db/backends/spatialite/base.py])

Note: See TracTickets for help on using tickets.
Back to Top