Skip to content
Permalink
Browse files

Improve Python __repr__ handling for null geometries

Also avoid massive long __repr__ strings for complex geometries,
as these can flood the Python console (and first aid plugin),
and aren't useful for debugging anyway.

Refs #14640

(cherry picked from commit 7d648e5)
  • Loading branch information
nyalldawson committed Dec 20, 2018
1 parent f62db32 commit c339c3607ce95b0d7597793607f5e3d1e9b34fb8
@@ -168,7 +168,10 @@ Sets the circular string's points

SIP_PYOBJECT __repr__();
%MethodCode
QString str = QStringLiteral( "<QgsCircularString: %1>" ).arg( sipCpp->asWkt() );
QString wkt = sipCpp->asWkt();
if ( wkt.length() > 1000 )
wkt = wkt.left( 1000 ) + QStringLiteral( "..." );
QString str = QStringLiteral( "<QgsCircularString: %1>" ).arg( wkt );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
%End

@@ -172,7 +172,10 @@ Appends first point if not already closed.

SIP_PYOBJECT __repr__();
%MethodCode
QString str = QStringLiteral( "<QgsCompoundCurve: %1>" ).arg( sipCpp->asWkt() );
QString wkt = sipCpp->asWkt();
if ( wkt.length() > 1000 )
wkt = wkt.left( 1000 ) + QStringLiteral( "..." );
QString str = QStringLiteral( "<QgsCompoundCurve: %1>" ).arg( wkt );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
%End

@@ -229,7 +229,10 @@ Returns approximate rotation angle for a vertex. Usually average angle between a

SIP_PYOBJECT __repr__();
%MethodCode
QString str = QStringLiteral( "<QgsCurvePolygon: %1>" ).arg( sipCpp->asWkt() );
QString wkt = sipCpp->asWkt();
if ( wkt.length() > 1000 )
wkt = wkt.left( 1000 ) + QStringLiteral( "..." );
QString str = QStringLiteral( "<QgsCurvePolygon: %1>" ).arg( wkt );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
%End

@@ -1391,7 +1391,16 @@ Exports the geometry to WKT

SIP_PYOBJECT __repr__();
%MethodCode
QString str = QStringLiteral( "<QgsGeometry: %1>" ).arg( sipCpp->asWkt() );
QString str;
if ( sipCpp->isNull() )
str = QStringLiteral( "<QgsGeometry: null>" );
else
{
QString wkt = sipCpp->asWkt();
if ( wkt.length() > 1000 )
wkt = wkt.left( 1000 ) + QStringLiteral( "..." );
str = QStringLiteral( "<QgsGeometry: %1>" ).arg( wkt );
}
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
%End

@@ -329,7 +329,10 @@ of the curve.

SIP_PYOBJECT __repr__();
%MethodCode
QString str = QStringLiteral( "<QgsLineString: %1>" ).arg( sipCpp->asWkt() );
QString wkt = sipCpp->asWkt();
if ( wkt.length() > 1000 )
wkt = wkt.left( 1000 ) + QStringLiteral( "..." );
QString str = QStringLiteral( "<QgsLineString: %1>" ).arg( wkt );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
%End

@@ -58,7 +58,10 @@ Returns a copy of the multi curve, where each component curve has had its line d

SIP_PYOBJECT __repr__();
%MethodCode
QString str = QStringLiteral( "<QgsMultiCurve: %1>" ).arg( sipCpp->asWkt() );
QString wkt = sipCpp->asWkt();
if ( wkt.length() > 1000 )
wkt = wkt.left( 1000 ) + QStringLiteral( "..." );
QString str = QStringLiteral( "<QgsMultiCurve: %1>" ).arg( wkt );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
%End

@@ -55,7 +55,10 @@ Returns the geometry converted to the more generic curve type :py:class:`QgsMult

SIP_PYOBJECT __repr__();
%MethodCode
QString str = QStringLiteral( "<QgsMultiLineString: %1>" ).arg( sipCpp->asWkt() );
QString wkt = sipCpp->asWkt();
if ( wkt.length() > 1000 )
wkt = wkt.left( 1000 ) + QStringLiteral( "..." );
QString str = QStringLiteral( "<QgsMultiLineString: %1>" ).arg( wkt );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
%End

@@ -57,7 +57,10 @@ Multi point geometry collection.

SIP_PYOBJECT __repr__();
%MethodCode
QString str = QStringLiteral( "<QgsMultiPoint: %1>" ).arg( sipCpp->asWkt() );
QString wkt = sipCpp->asWkt();
if ( wkt.length() > 1000 )
wkt = wkt.left( 1000 ) + QStringLiteral( "..." );
QString str = QStringLiteral( "<QgsMultiPoint: %1>" ).arg( wkt );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
%End

@@ -56,7 +56,10 @@ Returns the geometry converted to the more generic curve type :py:class:`QgsMult

SIP_PYOBJECT __repr__();
%MethodCode
QString str = QStringLiteral( "<QgsMultiPolygon: %1>" ).arg( sipCpp->asWkt() );
QString wkt = sipCpp->asWkt();
if ( wkt.length() > 1000 )
wkt = wkt.left( 1000 ) + QStringLiteral( "..." );
QString str = QStringLiteral( "<QgsMultiPolygon: %1>" ).arg( wkt );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
%End

@@ -67,7 +67,10 @@ negative if the point lies outside the polygon.

SIP_PYOBJECT __repr__();
%MethodCode
QString str = QStringLiteral( "<QgsPolygon: %1>" ).arg( sipCpp->asWkt() );
QString wkt = sipCpp->asWkt();
if ( wkt.length() > 1000 )
wkt = wkt.left( 1000 ) + QStringLiteral( "..." );
QString str = QStringLiteral( "<QgsPolygon: %1>" ).arg( wkt );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
%End

@@ -152,7 +152,10 @@ class CORE_EXPORT QgsCircularString: public QgsCurve
#ifdef SIP_RUN
SIP_PYOBJECT __repr__();
% MethodCode
QString str = QStringLiteral( "<QgsCircularString: %1>" ).arg( sipCpp->asWkt() );
QString wkt = sipCpp->asWkt();
if ( wkt.length() > 1000 )
wkt = wkt.left( 1000 ) + QStringLiteral( "..." );
QString str = QStringLiteral( "<QgsCircularString: %1>" ).arg( wkt );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
% End
#endif
@@ -152,7 +152,10 @@ class CORE_EXPORT QgsCompoundCurve: public QgsCurve
#ifdef SIP_RUN
SIP_PYOBJECT __repr__();
% MethodCode
QString str = QStringLiteral( "<QgsCompoundCurve: %1>" ).arg( sipCpp->asWkt() );
QString wkt = sipCpp->asWkt();
if ( wkt.length() > 1000 )
wkt = wkt.left( 1000 ) + QStringLiteral( "..." );
QString str = QStringLiteral( "<QgsCompoundCurve: %1>" ).arg( wkt );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
% End
#endif
@@ -219,7 +219,10 @@ class CORE_EXPORT QgsCurvePolygon: public QgsSurface
#ifdef SIP_RUN
SIP_PYOBJECT __repr__();
% MethodCode
QString str = QStringLiteral( "<QgsCurvePolygon: %1>" ).arg( sipCpp->asWkt() );
QString wkt = sipCpp->asWkt();
if ( wkt.length() > 1000 )
wkt = wkt.left( 1000 ) + QStringLiteral( "..." );
QString str = QStringLiteral( "<QgsCurvePolygon: %1>" ).arg( wkt );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
% End
#endif
@@ -1392,7 +1392,16 @@ class CORE_EXPORT QgsGeometry
#ifdef SIP_RUN
SIP_PYOBJECT __repr__();
% MethodCode
QString str = QStringLiteral( "<QgsGeometry: %1>" ).arg( sipCpp->asWkt() );
QString str;
if ( sipCpp->isNull() )
str = QStringLiteral( "<QgsGeometry: null>" );
else
{
QString wkt = sipCpp->asWkt();
if ( wkt.length() > 1000 )
wkt = wkt.left( 1000 ) + QStringLiteral( "..." );
str = QStringLiteral( "<QgsGeometry: %1>" ).arg( wkt );
}
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
% End
#endif
@@ -359,7 +359,10 @@ class CORE_EXPORT QgsLineString: public QgsCurve
#ifdef SIP_RUN
SIP_PYOBJECT __repr__();
% MethodCode
QString str = QStringLiteral( "<QgsLineString: %1>" ).arg( sipCpp->asWkt() );
QString wkt = sipCpp->asWkt();
if ( wkt.length() > 1000 )
wkt = wkt.left( 1000 ) + QStringLiteral( "..." );
QString str = QStringLiteral( "<QgsLineString: %1>" ).arg( wkt );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
% End
#endif
@@ -76,7 +76,10 @@ class CORE_EXPORT QgsMultiCurve: public QgsGeometryCollection
#ifdef SIP_RUN
SIP_PYOBJECT __repr__();
% MethodCode
QString str = QStringLiteral( "<QgsMultiCurve: %1>" ).arg( sipCpp->asWkt() );
QString wkt = sipCpp->asWkt();
if ( wkt.length() > 1000 )
wkt = wkt.left( 1000 ) + QStringLiteral( "..." );
QString str = QStringLiteral( "<QgsMultiCurve: %1>" ).arg( wkt );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
% End
#endif
@@ -68,7 +68,10 @@ class CORE_EXPORT QgsMultiLineString: public QgsMultiCurve
#ifdef SIP_RUN
SIP_PYOBJECT __repr__();
% MethodCode
QString str = QStringLiteral( "<QgsMultiLineString: %1>" ).arg( sipCpp->asWkt() );
QString wkt = sipCpp->asWkt();
if ( wkt.length() > 1000 )
wkt = wkt.left( 1000 ) + QStringLiteral( "..." );
QString str = QStringLiteral( "<QgsMultiLineString: %1>" ).arg( wkt );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
% End
#endif
@@ -69,7 +69,10 @@ class CORE_EXPORT QgsMultiPoint: public QgsGeometryCollection
#ifdef SIP_RUN
SIP_PYOBJECT __repr__();
% MethodCode
QString str = QStringLiteral( "<QgsMultiPoint: %1>" ).arg( sipCpp->asWkt() );
QString wkt = sipCpp->asWkt();
if ( wkt.length() > 1000 )
wkt = wkt.left( 1000 ) + QStringLiteral( "..." );
QString str = QStringLiteral( "<QgsMultiPoint: %1>" ).arg( wkt );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
% End
#endif
@@ -68,7 +68,10 @@ class CORE_EXPORT QgsMultiPolygon: public QgsMultiSurface
#ifdef SIP_RUN
SIP_PYOBJECT __repr__();
% MethodCode
QString str = QStringLiteral( "<QgsMultiPolygon: %1>" ).arg( sipCpp->asWkt() );
QString wkt = sipCpp->asWkt();
if ( wkt.length() > 1000 )
wkt = wkt.left( 1000 ) + QStringLiteral( "..." );
QString str = QStringLiteral( "<QgsMultiPolygon: %1>" ).arg( wkt );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
% End
#endif
@@ -87,7 +87,10 @@ class CORE_EXPORT QgsPolygon: public QgsCurvePolygon
#ifdef SIP_RUN
SIP_PYOBJECT __repr__();
% MethodCode
QString str = QStringLiteral( "<QgsPolygon: %1>" ).arg( sipCpp->asWkt() );
QString wkt = sipCpp->asWkt();
if ( wkt.length() > 1000 )
wkt = wkt.left( 1000 ) + QStringLiteral( "..." );
QString str = QStringLiteral( "<QgsPolygon: %1>" ).arg( wkt );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
% End
#endif
@@ -26,9 +26,19 @@
class TestPython__repr__(unittest.TestCase):

def testQgsGeometryRepr(self):

g = QgsGeometry()
self.assertEqual(g.__repr__(), '<QgsGeometry: null>')
p = QgsPointXY(123.456, 987.654)
g = QgsGeometry.fromPointXY(p)
self.assertTrue(g.__repr__().startswith('<QgsGeometry: Point (123.456'))
g = QgsGeometry(QgsLineString([QgsPoint(0, 2), QgsPoint(1010, 2)]))
g = g.densifyByCount(1000)
# long strings must be truncated for performance -- otherwise they flood the console/first aid output
self.assertTrue(g.__repr__().startswith('<QgsGeometry: LineString (0 2,'))
self.assertTrue(
g.__repr__().endswith('...>'))
self.assertEqual(len(g.__repr__()), 1018)

def testQgsPointRepr(self):
p = QgsPoint(123.456, 987.654, 100)

0 comments on commit c339c36

Please sign in to comment.
You can’t perform that action at this time.