Fixed #17959 -- Silenced output during GIS tests

This commit is contained in:
Claude Paroz 2012-10-04 22:41:03 +02:00
parent 0ad6d7e612
commit 53c8b2c0c5
5 changed files with 38 additions and 34 deletions

View File

@ -1,11 +1,16 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import logging
import os import os
import re 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 ctypes.util import find_library
from django.contrib.gis.gdal.error import OGRException from django.contrib.gis.gdal.error import OGRException
logger = logging.getLogger('django.contrib.gis')
# Custom library path set? # Custom library path set?
try: try:
from django.conf import settings from django.conf import settings
@ -86,3 +91,18 @@ GDAL_MINOR_VERSION = int(_verinfo['minor'])
GDAL_SUBMINOR_VERSION = _verinfo['subminor'] and int(_verinfo['subminor']) GDAL_SUBMINOR_VERSION = _verinfo['subminor'] and int(_verinfo['subminor'])
GDAL_VERSION = (GDAL_MAJOR_VERSION, GDAL_MINOR_VERSION, GDAL_SUBMINOR_VERSION) GDAL_VERSION = (GDAL_MAJOR_VERSION, GDAL_MINOR_VERSION, GDAL_SUBMINOR_VERSION)
del _verinfo 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 File

@ -4,6 +4,7 @@ from django.contrib.gis.gdal import DataSource, Envelope, OGRGeometry, OGRExcept
from django.contrib.gis.gdal.field import OFTReal, OFTInteger, OFTString from django.contrib.gis.gdal.field import OFTReal, OFTInteger, OFTString
from django.contrib.gis.geometry.test_data import get_ds_file, TestDS, TEST_DATA from django.contrib.gis.geometry.test_data import get_ds_file, TestDS, TEST_DATA
# List of acceptable data sources. # List of acceptable data sources.
ds_list = (TestDS('test_point', nfeat=5, nfld=3, geom='POINT', gtype=1, driver='ESRI Shapefile', ds_list = (TestDS('test_point', nfeat=5, nfld=3, geom='POINT', gtype=1, driver='ESRI Shapefile',
fields={'dbl' : OFTReal, 'int' : OFTInteger, 'str' : OFTString,}, fields={'dbl' : OFTReal, 'int' : OFTInteger, 'str' : OFTString,},
@ -59,7 +60,6 @@ class DataSourceTest(unittest.TestCase):
def test03a_layers(self): def test03a_layers(self):
"Testing Data Source Layers." "Testing Data Source Layers."
print("\nBEGIN - expecting out of range feature id error; safe to ignore.\n")
for source in ds_list: for source in ds_list:
ds = DataSource(source.ds) ds = DataSource(source.ds)
@ -108,7 +108,6 @@ class DataSourceTest(unittest.TestCase):
# the feature values here while in this loop. # the feature values here while in this loop.
for fld_name in fld_names: for fld_name in fld_names:
self.assertEqual(source.field_values[fld_name][i], feat.get(fld_name)) 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): def test03b_layer_slice(self):
"Test indexing and slicing on Layers." "Test indexing and slicing on Layers."

View File

@ -235,15 +235,8 @@ class OGRGeomTest(unittest.TestCase, TestDataMixin):
# Both rings in this geometry are not closed. # 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))') 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) self.assertEqual(8, poly.point_count)
print("\nBEGIN - expecting IllegalArgumentException; safe to ignore.\n") with self.assertRaises(OGRException):
try: _ = poly.centroid
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")
poly.close_rings() poly.close_rings()
self.assertEqual(10, poly.point_count) # Two closing points should've been added self.assertEqual(10, poly.point_count) # Two closing points should've been added

View File

@ -6,13 +6,17 @@
This module also houses GEOS Pointer utilities, including This module also houses GEOS Pointer utilities, including
get_pointer_arr(), and GEOM_PTR. get_pointer_arr(), and GEOM_PTR.
""" """
import logging
import os import os
import re import re
import sys
from ctypes import c_char_p, Structure, CDLL, CFUNCTYPE, POINTER from ctypes import c_char_p, Structure, CDLL, CFUNCTYPE, POINTER
from ctypes.util import find_library from ctypes.util import find_library
from django.contrib.gis.geos.error import GEOSException from django.contrib.gis.geos.error import GEOSException
logger = logging.getLogger('django.contrib.gis')
# Custom library path set? # Custom library path set?
try: try:
from django.conf import settings from django.conf import settings
@ -56,23 +60,23 @@ lgeos = CDLL(lib_path)
# Supposed to mimic the GEOS message handler (C below): # Supposed to mimic the GEOS message handler (C below):
# typedef void (*GEOSMessageHandler)(const char *fmt, ...); # typedef void (*GEOSMessageHandler)(const char *fmt, ...);
NOTICEFUNC = CFUNCTYPE(None, c_char_p, c_char_p) 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() fmt, lst = fmt.decode(), lst.decode()
try: try:
warn_msg = fmt % lst warn_msg = fmt % lst
except: except:
warn_msg = fmt 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) notice_h = NOTICEFUNC(notice_h)
ERRORFUNC = CFUNCTYPE(None, c_char_p, c_char_p) 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() fmt, lst = fmt.decode(), lst.decode()
try: try:
err_msg = fmt % lst err_msg = fmt % lst
except: except:
err_msg = fmt 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) error_h = ERRORFUNC(error_h)
#### GEOS Geometry C data structures, and utility functions. #### #### GEOS Geometry C data structures, and utility functions. ####

View File

@ -147,18 +147,13 @@ class GEOSTest(unittest.TestCase, TestDataMixin):
def test_errors(self): def test_errors(self):
"Testing the Error handlers." "Testing the Error handlers."
# string-based # string-based
print("\nBEGIN - expecting GEOS_ERROR; safe to ignore.\n")
for err in self.geometries.errors: for err in self.geometries.errors:
try: with self.assertRaises((GEOSException, ValueError)):
g = fromstr(err.wkt) _ = fromstr(err.wkt)
except (GEOSException, ValueError):
pass
# Bad WKB # Bad WKB
self.assertRaises(GEOSException, GEOSGeometry, memoryview(b'0')) self.assertRaises(GEOSException, GEOSGeometry, memoryview(b'0'))
print("\nEND - expecting GEOS_ERROR; safe to ignore.\n")
class NotAGeometry(object): class NotAGeometry(object):
pass pass
@ -458,7 +453,6 @@ class GEOSTest(unittest.TestCase, TestDataMixin):
def test_multipolygons(self): def test_multipolygons(self):
"Testing MultiPolygon objects." "Testing MultiPolygon objects."
print("\nBEGIN - expecting GEOS_NOTICE; safe to ignore.\n")
prev = fromstr('POINT (0 0)') prev = fromstr('POINT (0 0)')
for mp in self.geometries.multipolygons: for mp in self.geometries.multipolygons:
mpoly = fromstr(mp.wkt) mpoly = fromstr(mp.wkt)
@ -477,8 +471,6 @@ class GEOSTest(unittest.TestCase, TestDataMixin):
self.assertEqual(p.valid, True) self.assertEqual(p.valid, True)
self.assertEqual(mpoly.wkt, MultiPolygon(*tuple(poly.clone() for poly in mpoly)).wkt) 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): def test_memory_hijinks(self):
"Testing Geometry __del__() on rings and polygons." "Testing Geometry __del__() on rings and polygons."
#### Memory issues with rings and polygons #### Memory issues with rings and polygons
@ -1025,19 +1017,15 @@ class GEOSTest(unittest.TestCase, TestDataMixin):
g = GEOSGeometry("POINT(0 0)") g = GEOSGeometry("POINT(0 0)")
self.assertTrue(g.valid) 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") self.assertEqual(g.valid_reason, "Valid Geometry")
print("\nBEGIN - expecting GEOS_NOTICE; safe to ignore.\n")
g = GEOSGeometry("LINESTRING(0 0, 0 0)") g = GEOSGeometry("LINESTRING(0 0, 0 0)")
self.assertTrue(not g.valid) self.assertFalse(g.valid)
self.assertTrue(isinstance(g.valid_reason, six.string_types)) self.assertIsInstance(g.valid_reason, six.string_types)
self.assertTrue(g.valid_reason.startswith("Too few points in geometry component")) 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") @unittest.skipUnless(geos_version_info()['version'] >= '3.2.0', "geos >= 3.2.0 is required")
def test_linearref(self): def test_linearref(self):
"Testing linear referencing" "Testing linear referencing"