Skip to content

Commit c339c36

Browse files
committed
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)
1 parent f62db32 commit c339c36

21 files changed

+102
-20
lines changed

python/core/auto_generated/geometry/qgscircularstring.sip.in

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,10 @@ Sets the circular string's points
168168

169169
SIP_PYOBJECT __repr__();
170170
%MethodCode
171-
QString str = QStringLiteral( "<QgsCircularString: %1>" ).arg( sipCpp->asWkt() );
171+
QString wkt = sipCpp->asWkt();
172+
if ( wkt.length() > 1000 )
173+
wkt = wkt.left( 1000 ) + QStringLiteral( "..." );
174+
QString str = QStringLiteral( "<QgsCircularString: %1>" ).arg( wkt );
172175
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
173176
%End
174177

python/core/auto_generated/geometry/qgscompoundcurve.sip.in

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,10 @@ Appends first point if not already closed.
172172

173173
SIP_PYOBJECT __repr__();
174174
%MethodCode
175-
QString str = QStringLiteral( "<QgsCompoundCurve: %1>" ).arg( sipCpp->asWkt() );
175+
QString wkt = sipCpp->asWkt();
176+
if ( wkt.length() > 1000 )
177+
wkt = wkt.left( 1000 ) + QStringLiteral( "..." );
178+
QString str = QStringLiteral( "<QgsCompoundCurve: %1>" ).arg( wkt );
176179
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
177180
%End
178181

python/core/auto_generated/geometry/qgscurvepolygon.sip.in

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,10 @@ Returns approximate rotation angle for a vertex. Usually average angle between a
229229

230230
SIP_PYOBJECT __repr__();
231231
%MethodCode
232-
QString str = QStringLiteral( "<QgsCurvePolygon: %1>" ).arg( sipCpp->asWkt() );
232+
QString wkt = sipCpp->asWkt();
233+
if ( wkt.length() > 1000 )
234+
wkt = wkt.left( 1000 ) + QStringLiteral( "..." );
235+
QString str = QStringLiteral( "<QgsCurvePolygon: %1>" ).arg( wkt );
233236
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
234237
%End
235238

python/core/auto_generated/geometry/qgsgeometry.sip.in

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1391,7 +1391,16 @@ Exports the geometry to WKT
13911391

13921392
SIP_PYOBJECT __repr__();
13931393
%MethodCode
1394-
QString str = QStringLiteral( "<QgsGeometry: %1>" ).arg( sipCpp->asWkt() );
1394+
QString str;
1395+
if ( sipCpp->isNull() )
1396+
str = QStringLiteral( "<QgsGeometry: null>" );
1397+
else
1398+
{
1399+
QString wkt = sipCpp->asWkt();
1400+
if ( wkt.length() > 1000 )
1401+
wkt = wkt.left( 1000 ) + QStringLiteral( "..." );
1402+
str = QStringLiteral( "<QgsGeometry: %1>" ).arg( wkt );
1403+
}
13951404
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
13961405
%End
13971406

python/core/auto_generated/geometry/qgslinestring.sip.in

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,10 @@ of the curve.
329329

330330
SIP_PYOBJECT __repr__();
331331
%MethodCode
332-
QString str = QStringLiteral( "<QgsLineString: %1>" ).arg( sipCpp->asWkt() );
332+
QString wkt = sipCpp->asWkt();
333+
if ( wkt.length() > 1000 )
334+
wkt = wkt.left( 1000 ) + QStringLiteral( "..." );
335+
QString str = QStringLiteral( "<QgsLineString: %1>" ).arg( wkt );
333336
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
334337
%End
335338

python/core/auto_generated/geometry/qgsmulticurve.sip.in

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ Returns a copy of the multi curve, where each component curve has had its line d
5858

5959
SIP_PYOBJECT __repr__();
6060
%MethodCode
61-
QString str = QStringLiteral( "<QgsMultiCurve: %1>" ).arg( sipCpp->asWkt() );
61+
QString wkt = sipCpp->asWkt();
62+
if ( wkt.length() > 1000 )
63+
wkt = wkt.left( 1000 ) + QStringLiteral( "..." );
64+
QString str = QStringLiteral( "<QgsMultiCurve: %1>" ).arg( wkt );
6265
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
6366
%End
6467

python/core/auto_generated/geometry/qgsmultilinestring.sip.in

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ Returns the geometry converted to the more generic curve type :py:class:`QgsMult
5555

5656
SIP_PYOBJECT __repr__();
5757
%MethodCode
58-
QString str = QStringLiteral( "<QgsMultiLineString: %1>" ).arg( sipCpp->asWkt() );
58+
QString wkt = sipCpp->asWkt();
59+
if ( wkt.length() > 1000 )
60+
wkt = wkt.left( 1000 ) + QStringLiteral( "..." );
61+
QString str = QStringLiteral( "<QgsMultiLineString: %1>" ).arg( wkt );
5962
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
6063
%End
6164

python/core/auto_generated/geometry/qgsmultipoint.sip.in

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ Multi point geometry collection.
5757

5858
SIP_PYOBJECT __repr__();
5959
%MethodCode
60-
QString str = QStringLiteral( "<QgsMultiPoint: %1>" ).arg( sipCpp->asWkt() );
60+
QString wkt = sipCpp->asWkt();
61+
if ( wkt.length() > 1000 )
62+
wkt = wkt.left( 1000 ) + QStringLiteral( "..." );
63+
QString str = QStringLiteral( "<QgsMultiPoint: %1>" ).arg( wkt );
6164
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
6265
%End
6366

python/core/auto_generated/geometry/qgsmultipolygon.sip.in

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ Returns the geometry converted to the more generic curve type :py:class:`QgsMult
5656

5757
SIP_PYOBJECT __repr__();
5858
%MethodCode
59-
QString str = QStringLiteral( "<QgsMultiPolygon: %1>" ).arg( sipCpp->asWkt() );
59+
QString wkt = sipCpp->asWkt();
60+
if ( wkt.length() > 1000 )
61+
wkt = wkt.left( 1000 ) + QStringLiteral( "..." );
62+
QString str = QStringLiteral( "<QgsMultiPolygon: %1>" ).arg( wkt );
6063
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
6164
%End
6265

python/core/auto_generated/geometry/qgspolygon.sip.in

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@ negative if the point lies outside the polygon.
6767

6868
SIP_PYOBJECT __repr__();
6969
%MethodCode
70-
QString str = QStringLiteral( "<QgsPolygon: %1>" ).arg( sipCpp->asWkt() );
70+
QString wkt = sipCpp->asWkt();
71+
if ( wkt.length() > 1000 )
72+
wkt = wkt.left( 1000 ) + QStringLiteral( "..." );
73+
QString str = QStringLiteral( "<QgsPolygon: %1>" ).arg( wkt );
7174
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
7275
%End
7376

0 commit comments

Comments
 (0)