Ticket #17959: 17959-2.diff

File 17959-2.diff, 9.5 KB (added by Claude Paroz, 7 years ago)

Updated patch

  • django/contrib/gis/gdal/__init__.py

    diff --git a/django/contrib/gis/gdal/__init__.py b/django/contrib/gis/gdal/__init__.py
    index de41df9..c33fbcb 100644
    a b  
    3737try:
    3838    from django.contrib.gis.gdal.driver import Driver
    3939    from django.contrib.gis.gdal.datasource import DataSource
    40     from django.contrib.gis.gdal.libgdal import gdal_version, gdal_full_version, gdal_release_date, GDAL_VERSION
     40    from django.contrib.gis.gdal.libgdal import gdal_version, gdal_full_version, GDAL_VERSION
    4141    from django.contrib.gis.gdal.srs import SpatialReference, CoordTransform
    4242    from django.contrib.gis.gdal.geometries import OGRGeometry
    4343    HAS_GDAL = True
  • django/contrib/gis/gdal/libgdal.py

    diff --git a/django/contrib/gis/gdal/libgdal.py b/django/contrib/gis/gdal/libgdal.py
    index 0d2889d..7c6b128 100644
    a b  
    11from __future__ import unicode_literals
    22
     3import logging
    34import os
    45import re
    5 from ctypes import c_char_p, CDLL
     6from ctypes import c_char_p, c_int, CDLL, CFUNCTYPE
    67from ctypes.util import find_library
     8
    79from django.contrib.gis.gdal.error import OGRException
    810
     11
     12logger = logging.getLogger('django.contrib.gis')
     13
    914# Custom library path set?
    1015try:
    1116    from django.conf import settings
    def gdal_full_version(): 
    7378    "Returns the full GDAL version information."
    7479    return _version_info('')
    7580
    76 def gdal_release_date(date=False):
    77     """
    78     Returns the release date in a string format, e.g, "2007/06/27".
    79     If the date keyword argument is set to True, a Python datetime object
    80     will be returned instead.
    81     """
    82     from datetime import date as date_type
    83     rel = _version_info('RELEASE_DATE')
    84     yy, mm, dd = map(int, (rel[0:4], rel[4:6], rel[6:8]))
    85     d = date_type(yy, mm, dd)
    86     if date: return d
    87     else: return d.strftime('%Y/%m/%d')
    88 
    8981version_regex = re.compile(r'^(?P<major>\d+)\.(?P<minor>\d+)(\.(?P<subminor>\d+))?')
    9082def gdal_version_info():
    9183    ver = gdal_version().decode()
    GDAL_MINOR_VERSION = int(_verinfo['minor']) 
    9991GDAL_SUBMINOR_VERSION = _verinfo['subminor'] and int(_verinfo['subminor'])
    10092GDAL_VERSION = (GDAL_MAJOR_VERSION, GDAL_MINOR_VERSION, GDAL_SUBMINOR_VERSION)
    10193del _verinfo
     94
     95# Set library error handling so as errors are logged
     96CPLErrorHandler = CFUNCTYPE(None, c_int, c_int, c_char_p)
     97def err_handler(error_class, error_number, message):
     98    logger.error('GDAL_ERROR %d: %s' % (error_number, message))
     99err_handler = CPLErrorHandler(err_handler)
     100
     101def function(name, args, restype):
     102    func = std_call(name)
     103    func.argtypes = args
     104    func.restype = restype
     105    return func
     106
     107set_error_handler = function('CPLSetErrorHandler', [CPLErrorHandler], CPLErrorHandler)
     108set_error_handler(err_handler)
  • django/contrib/gis/gdal/tests/test_ds.py

    diff --git a/django/contrib/gis/gdal/tests/test_ds.py b/django/contrib/gis/gdal/tests/test_ds.py
    index 094f65b..69e3054 100644
    a b from django.contrib.gis.gdal import DataSource, Envelope, OGRGeometry, OGRExcept 
    44from django.contrib.gis.gdal.field import OFTReal, OFTInteger, OFTString
    55from django.contrib.gis.geometry.test_data import get_ds_file, TestDS, TEST_DATA
    66
     7
    78# List of acceptable data sources.
    89ds_list = (TestDS('test_point', nfeat=5, nfld=3, geom='POINT', gtype=1, driver='ESRI Shapefile',
    910                  fields={'dbl' : OFTReal, 'int' : OFTInteger, 'str' : OFTString,},
    class DataSourceTest(unittest.TestCase): 
    5960
    6061    def test03a_layers(self):
    6162        "Testing Data Source Layers."
    62         print("\nBEGIN - expecting out of range feature id error; safe to ignore.\n")
    6363        for source in ds_list:
    6464            ds = DataSource(source.ds)
    6565
    class DataSourceTest(unittest.TestCase): 
    108108                        # the feature values here while in this loop.
    109109                        for fld_name in fld_names:
    110110                            self.assertEqual(source.field_values[fld_name][i], feat.get(fld_name))
    111         print("\nEND - expecting out of range feature id error; safe to ignore.")
    112111
    113112    def test03b_layer_slice(self):
    114113        "Test indexing and slicing on Layers."
  • django/contrib/gis/gdal/tests/test_geom.py

    diff --git a/django/contrib/gis/gdal/tests/test_geom.py b/django/contrib/gis/gdal/tests/test_geom.py
    index b22bb62..9b8ae6a 100644
    a b class OGRGeomTest(unittest.TestCase, TestDataMixin): 
    235235        # Both rings in this geometry are not closed.
    236236        poly = OGRGeometry('POLYGON((0 0, 5 0, 5 5, 0 5), (1 1, 2 1, 2 2, 2 1))')
    237237        self.assertEqual(8, poly.point_count)
    238         print("\nBEGIN - expecting IllegalArgumentException; safe to ignore.\n")
    239         try:
    240             c = poly.centroid
    241         except OGRException:
    242             # Should raise an OGR exception, rings are not closed
    243             pass
    244         else:
    245             self.fail('Should have raised an OGRException!')
    246         print("\nEND - expecting IllegalArgumentException; safe to ignore.\n")
     238        with self.assertRaises(OGRException):
     239            _ = poly.centroid
    247240
    248241        poly.close_rings()
    249242        self.assertEqual(10, poly.point_count) # Two closing points should've been added
  • django/contrib/gis/geos/libgeos.py

    diff --git a/django/contrib/gis/geos/libgeos.py b/django/contrib/gis/geos/libgeos.py
    index b31a795..d19644a 100644
    a b  
    66 This module also houses GEOS Pointer utilities, including
    77 get_pointer_arr(), and GEOM_PTR.
    88"""
     9import logging
    910import os
    1011import re
    11 import sys
    1212from ctypes import c_char_p, Structure, CDLL, CFUNCTYPE, POINTER
    1313from ctypes.util import find_library
     14
    1415from django.contrib.gis.geos.error import GEOSException
    1516
     17
     18logger = logging.getLogger('django.contrib.gis')
     19
    1620# Custom library path set?
    1721try:
    1822    from django.conf import settings
    lgeos = CDLL(lib_path) 
    5660# Supposed to mimic the GEOS message handler (C below):
    5761#  typedef void (*GEOSMessageHandler)(const char *fmt, ...);
    5862NOTICEFUNC = CFUNCTYPE(None, c_char_p, c_char_p)
    59 def notice_h(fmt, lst, output_h=sys.stdout):
     63def notice_h(fmt, lst):
    6064    fmt, lst = fmt.decode(), lst.decode()
    6165    try:
    6266        warn_msg = fmt % lst
    6367    except:
    6468        warn_msg = fmt
    65     output_h.write('GEOS_NOTICE: %s\n' % warn_msg)
     69    logger.warn('GEOS_NOTICE: %s\n' % warn_msg)
    6670notice_h = NOTICEFUNC(notice_h)
    6771
    6872ERRORFUNC = CFUNCTYPE(None, c_char_p, c_char_p)
    69 def error_h(fmt, lst, output_h=sys.stderr):
     73def error_h(fmt, lst):
    7074    fmt, lst = fmt.decode(), lst.decode()
    7175    try:
    7276        err_msg = fmt % lst
    7377    except:
    7478        err_msg = fmt
    75     output_h.write('GEOS_ERROR: %s\n' % err_msg)
     79    logger.error('GEOS_ERROR: %s\n' % err_msg)
    7680error_h = ERRORFUNC(error_h)
    7781
    7882#### GEOS Geometry C data structures, and utility functions. ####
  • django/contrib/gis/geos/tests/test_geos.py

    diff --git a/django/contrib/gis/geos/tests/test_geos.py b/django/contrib/gis/geos/tests/test_geos.py
    index cbe5136..283daa4 100644
    a b class GEOSTest(unittest.TestCase, TestDataMixin): 
    147147    def test_errors(self):
    148148        "Testing the Error handlers."
    149149        # string-based
    150         print("\nBEGIN - expecting GEOS_ERROR; safe to ignore.\n")
    151150        for err in self.geometries.errors:
    152             try:
    153                 g = fromstr(err.wkt)
    154             except (GEOSException, ValueError):
    155                 pass
     151            with self.assertRaises((GEOSException, ValueError)):
     152                _ = fromstr(err.wkt)
    156153
    157154        # Bad WKB
    158155        self.assertRaises(GEOSException, GEOSGeometry, memoryview(b'0'))
    159156
    160         print("\nEND - expecting GEOS_ERROR; safe to ignore.\n")
    161 
    162157        class NotAGeometry(object):
    163158            pass
    164159
    class GEOSTest(unittest.TestCase, TestDataMixin): 
    458453
    459454    def test_multipolygons(self):
    460455        "Testing MultiPolygon objects."
    461         print("\nBEGIN - expecting GEOS_NOTICE; safe to ignore.\n")
    462456        prev = fromstr('POINT (0 0)')
    463457        for mp in self.geometries.multipolygons:
    464458            mpoly = fromstr(mp.wkt)
    class GEOSTest(unittest.TestCase, TestDataMixin): 
    477471                    self.assertEqual(p.valid, True)
    478472                self.assertEqual(mpoly.wkt, MultiPolygon(*tuple(poly.clone() for poly in mpoly)).wkt)
    479473
    480         print("\nEND - expecting GEOS_NOTICE; safe to ignore.\n")
    481 
    482474    def test_memory_hijinks(self):
    483475        "Testing Geometry __del__() on rings and polygons."
    484476        #### Memory issues with rings and polygons
    class GEOSTest(unittest.TestCase, TestDataMixin): 
    10251017
    10261018        g = GEOSGeometry("POINT(0 0)")
    10271019        self.assertTrue(g.valid)
    1028         self.assertTrue(isinstance(g.valid_reason, six.string_types))
     1020        self.assertIsInstance(g.valid_reason, six.string_types)
    10291021        self.assertEqual(g.valid_reason, "Valid Geometry")
    10301022
    1031         print("\nBEGIN - expecting GEOS_NOTICE; safe to ignore.\n")
    1032 
    10331023        g = GEOSGeometry("LINESTRING(0 0, 0 0)")
    10341024
    1035         self.assertTrue(not g.valid)
    1036         self.assertTrue(isinstance(g.valid_reason, six.string_types))
     1025        self.assertFalse(g.valid)
     1026        self.assertIsInstance(g.valid_reason, six.string_types)
    10371027        self.assertTrue(g.valid_reason.startswith("Too few points in geometry component"))
    10381028
    1039         print("\nEND - expecting GEOS_NOTICE; safe to ignore.\n")
    1040 
    10411029    @unittest.skipUnless(geos_version_info()['version'] >= '3.2.0', "geos >= 3.2.0 is required")
    10421030    def test_linearref(self):
    10431031        "Testing linear referencing"
Back to Top