[Django] #28341: GeometryField doesn't create GEOSGeometry objects lazily anymore

17 views
Skip to first unread message

Django

unread,
Jun 27, 2017, 7:08:47 AM6/27/17
to django-...@googlegroups.com
#28341: GeometryField doesn't create GEOSGeometry objects lazily anymore
-------------------------------------------+------------------------
Reporter: Sergey Fedoseev | Owner: nobody
Type: Bug | Status: new
Component: GIS | Version: 1.8
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------------+------------------------
Bisected to e9103402c0fa873aea58a6a11dba510cd308cb84 on test project with
this script:

{{{
import mock
import sys

with mock.patch('django.utils.six.moves.html_parser'):
import django
from django.test import TestCase
from django.contrib.gis.geos import Point

from test_app.models import City


def side_effect(*args, **kwargs):
sys.exit(1)


django.setup()


with mock.patch('django.contrib.gis.db.models.fields.Geometry.__init__',
side_effect=side_effect):
City.objects.first()
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/28341>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jun 27, 2017, 7:09:02 AM6/27/17
to django-...@googlegroups.com
#28341: GeometryField doesn't create GEOSGeometry objects lazily anymore
-------------------------------------+-------------------------------------
Reporter: Sergey Fedoseev | Owner: Sergey
| Fedoseev
Type: Bug | Status: assigned
Component: GIS | Version: 1.8
Severity: Normal | Resolution:

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sergey Fedoseev):

* owner: nobody => Sergey Fedoseev
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/28341#comment:1>

Django

unread,
Jun 27, 2017, 7:11:11 AM6/27/17
to django-...@googlegroups.com
#28341: GeometryField doesn't create GEOSGeometry objects lazily anymore
-------------------------------------+-------------------------------------
Reporter: Sergey Fedoseev | Owner: Sergey
| Fedoseev
Type: Bug | Status: assigned
Component: GIS | Version: 1.8
Severity: Normal | Resolution:

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Sergey Fedoseev:

Old description:

> Bisected to e9103402c0fa873aea58a6a11dba510cd308cb84 on test project with
> this script:
>
> {{{
> import mock
> import sys
>
> with mock.patch('django.utils.six.moves.html_parser'):
> import django
> from django.test import TestCase
> from django.contrib.gis.geos import Point
>
> from test_app.models import City
>

> def side_effect(*args, **kwargs):
> sys.exit(1)
>

> django.setup()
>

> with mock.patch('django.contrib.gis.db.models.fields.Geometry.__init__',
> side_effect=side_effect):
> City.objects.first()
> }}}

New description:

Bisected to e9103402c0fa873aea58a6a11dba510cd308cb84 on test project with
this script:

{{{
import mock
import sys

with mock.patch('django.utils.six.moves.html_parser'):
import django
from django.test import TestCase
from django.contrib.gis.geos import Point

from test_app.models import City


def side_effect(*args, **kwargs):
sys.exit(1)


django.setup()


with mock.patch('django.contrib.gis.db.models.fields.Geometry.__init__',
side_effect=side_effect):
City.objects.first()
}}}

This laziness is declared in
[https://docs.djangoproject.com/en/dev/ref/contrib/gis/tutorial/#lazy-
geometries GeoDjango Tutorial].

--

--
Ticket URL: <https://code.djangoproject.com/ticket/28341#comment:2>

Django

unread,
Jun 27, 2017, 9:37:04 AM6/27/17
to django-...@googlegroups.com
#28341: GeometryField doesn't create GEOSGeometry objects lazily anymore
-------------------------------------+-------------------------------------
Reporter: Sergey Fedoseev | Owner: Sergey
| Fedoseev
Type: Bug | Status: assigned
Component: GIS | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/28341#comment:3>

Django

unread,
Jul 17, 2017, 9:29:17 AM7/17/17
to django-...@googlegroups.com
#28341: GeometryField doesn't create GEOSGeometry objects lazily anymore
-------------------------------------+-------------------------------------
Reporter: Sergey Fedoseev | Owner: Srinivas
| Reddy Thatiparthy
Type: Bug | Status: assigned
Component: GIS | Version: 1.8

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Srinivas Reddy Thatiparthy):

* owner: Sergey Fedoseev => Srinivas Reddy Thatiparthy


--
Ticket URL: <https://code.djangoproject.com/ticket/28341#comment:4>

Django

unread,
Jul 17, 2017, 9:30:17 AM7/17/17
to django-...@googlegroups.com
#28341: GeometryField doesn't create GEOSGeometry objects lazily anymore
-------------------------------------+-------------------------------------
Reporter: Sergey Fedoseev | Owner: Srinivas
| Reddy Thatiparthy
Type: Bug | Status: assigned
Component: GIS | Version: 1.8

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Srinivas Reddy Thatiparthy):

* owner: (none) => Srinivas Reddy Thatiparthy


* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/28341#comment:6>

Django

unread,
Jul 17, 2017, 9:30:27 AM7/17/17
to django-...@googlegroups.com
#28341: GeometryField doesn't create GEOSGeometry objects lazily anymore
---------------------------------+------------------------------------
Reporter: Sergey Fedoseev | Owner: (none)

Type: Bug | Status: new
Component: GIS | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------

Changes (by Srinivas Reddy Thatiparthy):

* status: assigned => new
* owner: Srinivas Reddy Thatiparthy => (none)


--
Ticket URL: <https://code.djangoproject.com/ticket/28341#comment:5>

Django

unread,
Jul 17, 2017, 9:31:59 AM7/17/17
to django-...@googlegroups.com
#28341: GeometryField doesn't create GEOSGeometry objects lazily anymore
-------------------------------------+-------------------------------------
Reporter: Sergey Fedoseev | Owner: Srinivas
| Reddy Thatiparthy
Type: Bug | Status: assigned
Component: GIS | Version: 1.8

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Srinivas Reddy Thatiparthy):

Apologies for spam - Are you working on this ? @SergeyFedoseev

--
Ticket URL: <https://code.djangoproject.com/ticket/28341#comment:7>

Django

unread,
Jul 17, 2017, 10:57:02 AM7/17/17
to django-...@googlegroups.com
#28341: GeometryField doesn't create GEOSGeometry objects lazily anymore
-------------------------------------+-------------------------------------
Reporter: Sergey Fedoseev | Owner: Srinivas
| Reddy Thatiparthy
Type: Bug | Status: assigned
Component: GIS | Version: 1.8

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Sergey Fedoseev):

Feel free to work on it.

--
Ticket URL: <https://code.djangoproject.com/ticket/28341#comment:8>

Django

unread,
4:40 AM (13 hours ago) 4:40 AM
to django-...@googlegroups.com
#28341: GeometryField doesn't create GEOSGeometry objects lazily anymore
-------------------------------------+-------------------------------------
Reporter: Sergey Fedoseev | Owner: Abhimanyu
| Singh Negi
Type: Bug | Status: assigned
Component: GIS | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Abhimanyu Singh Negi):

* owner: (none) => Abhimanyu Singh Negi

Comment:

Hi, I spent some time digging into this and I think I understand what’s
going on.

Right now geometry objects are being created too early. The conversion
happens in the database converter layer —
BaseSpatialOperations.get_db_converters() adds a geometry converter, and
that converter runs in SQLCompiler.apply_converters(). So GEOSGeometry
gets created while rows are being fetched, before the values ever reach
the model instance. Because of that, SpatialProxy never sees raw values
and the lazy-loading path isn’t used.

I traced this through the ORM flow and confirmed that model instances
already contain GEOSGeometry objects in instance.__dict__, which explains
why accessing the field later doesn’t trigger any conversion.

One important detail is that values() and values_list() depend on
converters returning geometry objects immediately, so converters can’t
just be removed. The fix needs to keep eager conversion for those cases
but allow raw values when loading model instances so SpatialProxy can do
the conversion lazily.

I’m going to try implementing this by adjusting the geometry converter /
registration logic so conversion is skipped when loading model instances,
then add a regression test and run the GIS test suite to make sure nothing
else breaks.

Assigning this issue to myself for implementation.
--
Ticket URL: <https://code.djangoproject.com/ticket/28341#comment:9>
Reply all
Reply to author
Forward
0 new messages