Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #17959 -- Silenced output during GIS tests

  • Loading branch information...
commit 53c8b2c0c52cf999b644184bfe51e9f59d89286e 1 parent 0ad6d7e
@claudep claudep authored
View
22 django/contrib/gis/gdal/libgdal.py
@@ -1,11 +1,16 @@
from __future__ import unicode_literals
+import logging
import os
import re
-from ctypes import c_char_p, CDLL
+from ctypes import c_char_p, c_int, CDLL, CFUNCTYPE
from ctypes.util import find_library
+
from django.contrib.gis.gdal.error import OGRException
+
+logger = logging.getLogger('django.contrib.gis')
+
# Custom library path set?
try:
from django.conf import settings
@@ -86,3 +91,18 @@ def gdal_version_info():
GDAL_SUBMINOR_VERSION = _verinfo['subminor'] and int(_verinfo['subminor'])
GDAL_VERSION = (GDAL_MAJOR_VERSION, GDAL_MINOR_VERSION, GDAL_SUBMINOR_VERSION)
del _verinfo
+
+# Set library error handling so as errors are logged
+CPLErrorHandler = CFUNCTYPE(None, c_int, c_int, c_char_p)
+def err_handler(error_class, error_number, message):
+ logger.error('GDAL_ERROR %d: %s' % (error_number, message))
+err_handler = CPLErrorHandler(err_handler)
+
+def function(name, args, restype):
+ func = std_call(name)
+ func.argtypes = args
+ func.restype = restype
+ return func
+
+set_error_handler = function('CPLSetErrorHandler', [CPLErrorHandler], CPLErrorHandler)
+set_error_handler(err_handler)
View
3  django/contrib/gis/gdal/tests/test_ds.py
@@ -4,6 +4,7 @@
from django.contrib.gis.gdal.field import OFTReal, OFTInteger, OFTString
from django.contrib.gis.geometry.test_data import get_ds_file, TestDS, TEST_DATA
+
# List of acceptable data sources.
ds_list = (TestDS('test_point', nfeat=5, nfld=3, geom='POINT', gtype=1, driver='ESRI Shapefile',
fields={'dbl' : OFTReal, 'int' : OFTInteger, 'str' : OFTString,},
@@ -59,7 +60,6 @@ def test02_invalid_shp(self):
def test03a_layers(self):
"Testing Data Source Layers."
- print("\nBEGIN - expecting out of range feature id error; safe to ignore.\n")
for source in ds_list:
ds = DataSource(source.ds)
@@ -108,7 +108,6 @@ def test03a_layers(self):
# the feature values here while in this loop.
for fld_name in fld_names:
self.assertEqual(source.field_values[fld_name][i], feat.get(fld_name))
- print("\nEND - expecting out of range feature id error; safe to ignore.")
def test03b_layer_slice(self):
"Test indexing and slicing on Layers."
View
11 django/contrib/gis/gdal/tests/test_geom.py
@@ -235,15 +235,8 @@ def test07b_closepolygons(self):
# Both rings in this geometry are not closed.
poly = OGRGeometry('POLYGON((0 0, 5 0, 5 5, 0 5), (1 1, 2 1, 2 2, 2 1))')
self.assertEqual(8, poly.point_count)
- print("\nBEGIN - expecting IllegalArgumentException; safe to ignore.\n")
- try:
- c = poly.centroid
- except OGRException:
- # Should raise an OGR exception, rings are not closed
- pass
- else:
- self.fail('Should have raised an OGRException!')
- print("\nEND - expecting IllegalArgumentException; safe to ignore.\n")
+ with self.assertRaises(OGRException):
+ _ = poly.centroid
poly.close_rings()
self.assertEqual(10, poly.point_count) # Two closing points should've been added
View
14 django/contrib/gis/geos/libgeos.py
@@ -6,13 +6,17 @@
This module also houses GEOS Pointer utilities, including
get_pointer_arr(), and GEOM_PTR.
"""
+import logging
import os
import re
-import sys
from ctypes import c_char_p, Structure, CDLL, CFUNCTYPE, POINTER
from ctypes.util import find_library
+
from django.contrib.gis.geos.error import GEOSException
+
+logger = logging.getLogger('django.contrib.gis')
+
# Custom library path set?
try:
from django.conf import settings
@@ -56,23 +60,23 @@
# Supposed to mimic the GEOS message handler (C below):
# typedef void (*GEOSMessageHandler)(const char *fmt, ...);
NOTICEFUNC = CFUNCTYPE(None, c_char_p, c_char_p)
-def notice_h(fmt, lst, output_h=sys.stdout):
+def notice_h(fmt, lst):
fmt, lst = fmt.decode(), lst.decode()
try:
warn_msg = fmt % lst
except:
warn_msg = fmt
- output_h.write('GEOS_NOTICE: %s\n' % warn_msg)
+ logger.warn('GEOS_NOTICE: %s\n' % warn_msg)
notice_h = NOTICEFUNC(notice_h)
ERRORFUNC = CFUNCTYPE(None, c_char_p, c_char_p)
-def error_h(fmt, lst, output_h=sys.stderr):
+def error_h(fmt, lst):
fmt, lst = fmt.decode(), lst.decode()
try:
err_msg = fmt % lst
except:
err_msg = fmt
- output_h.write('GEOS_ERROR: %s\n' % err_msg)
+ logger.error('GEOS_ERROR: %s\n' % err_msg)
error_h = ERRORFUNC(error_h)
#### GEOS Geometry C data structures, and utility functions. ####
View
22 django/contrib/gis/geos/tests/test_geos.py
@@ -147,18 +147,13 @@ def test_kml(self):
def test_errors(self):
"Testing the Error handlers."
# string-based
- print("\nBEGIN - expecting GEOS_ERROR; safe to ignore.\n")
for err in self.geometries.errors:
- try:
- g = fromstr(err.wkt)
- except (GEOSException, ValueError):
- pass
+ with self.assertRaises((GEOSException, ValueError)):
+ _ = fromstr(err.wkt)
# Bad WKB
self.assertRaises(GEOSException, GEOSGeometry, memoryview(b'0'))
- print("\nEND - expecting GEOS_ERROR; safe to ignore.\n")
-
class NotAGeometry(object):
pass
@@ -458,7 +453,6 @@ def test_polygons(self):
def test_multipolygons(self):
"Testing MultiPolygon objects."
- print("\nBEGIN - expecting GEOS_NOTICE; safe to ignore.\n")
prev = fromstr('POINT (0 0)')
for mp in self.geometries.multipolygons:
mpoly = fromstr(mp.wkt)
@@ -477,8 +471,6 @@ def test_multipolygons(self):
self.assertEqual(p.valid, True)
self.assertEqual(mpoly.wkt, MultiPolygon(*tuple(poly.clone() for poly in mpoly)).wkt)
- print("\nEND - expecting GEOS_NOTICE; safe to ignore.\n")
-
def test_memory_hijinks(self):
"Testing Geometry __del__() on rings and polygons."
#### Memory issues with rings and polygons
@@ -1025,19 +1017,15 @@ def test_valid_reason(self):
g = GEOSGeometry("POINT(0 0)")
self.assertTrue(g.valid)
- self.assertTrue(isinstance(g.valid_reason, six.string_types))
+ self.assertIsInstance(g.valid_reason, six.string_types)
self.assertEqual(g.valid_reason, "Valid Geometry")
- print("\nBEGIN - expecting GEOS_NOTICE; safe to ignore.\n")
-
g = GEOSGeometry("LINESTRING(0 0, 0 0)")
- self.assertTrue(not g.valid)
- self.assertTrue(isinstance(g.valid_reason, six.string_types))
+ self.assertFalse(g.valid)
+ self.assertIsInstance(g.valid_reason, six.string_types)
self.assertTrue(g.valid_reason.startswith("Too few points in geometry component"))
- print("\nEND - expecting GEOS_NOTICE; safe to ignore.\n")
-
@unittest.skipUnless(geos_version_info()['version'] >= '3.2.0', "geos >= 3.2.0 is required")
def test_linearref(self):
"Testing linear referencing"
Please sign in to comment.
Something went wrong with that request. Please try again.