Opened 2 years ago

Last modified 2 years ago

#34026 closed Bug

WKBReader Type Error — at Version 1

Reported by: Benoît Vinot Owned by: nobody
Component: GIS Version: 4.1
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description (last modified by Benoît Vinot)

Hi,

There is a potential type error in the implementation of the read method of the WKBReader class in contrib.gis. Here is a small example to create the problem:

from django.contrib.gis.geos.prototypes.io import _WKBReader
wkb='0101000020E6100000C07DD40B0FA8F5BF8794C5DD53DD4740' # <-- string
_WKBReader().read(wkb)

This code works fine:

from django.contrib.gis.geos.prototypes.io import _WKBReader
wkb='0101000020E6100000C07DD40B0FA8F5BF8794C5DD53DD4740'
_WKBReader().read(wkb.encode()) # <--- Converted into bytes

I suggest replacing the current implementation of this method by:

    def read(self, wkb):
        "Return a _pointer_ to C GEOS Geometry object from the given WKB."
        if isinstance(wkb, memoryview):
            wkb_s = bytes(wkb)
            return wkb_reader_read(self.ptr, wkb_s, len(wkb_s))
        elif isinstance(wkb, bytes):
            return wkb_reader_read_hex(self.ptr, wkb, len(wkb))
        elif isinstance(wkb, str):
            wkb_s = wkb.encode() # <------------------- Ensure bytes
            return wkb_reader_read_hex(self.ptr, wkb_s, len(wkb_s))
        else:
            raise TypeError

Change History (1)

comment:1 by Benoît Vinot, 2 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top