Skip to content

Commit

Permalink
Add a Python repr for QgsError
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Oct 23, 2018
1 parent adec719 commit 4212cca
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
7 changes: 7 additions & 0 deletions python/core/auto_generated/qgserror.sip.in
Expand Up @@ -126,6 +126,13 @@ messageList return the list of current error messages
:return: current list of error messages
%End


SIP_PYOBJECT __repr__();
%MethodCode
QString str = QStringLiteral( "<QgsError: %1>" ).arg( sipCpp->message( QgsErrorMessage::Text ) );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
%End

};

/************************************************************************
Expand Down
9 changes: 9 additions & 0 deletions src/core/qgserror.h
Expand Up @@ -132,6 +132,15 @@ class CORE_EXPORT QgsError
*/
QList<QgsErrorMessage> messageList() const { return mMessageList; }


#ifdef SIP_RUN
SIP_PYOBJECT __repr__();
% MethodCode
QString str = QStringLiteral( "<QgsError: %1>" ).arg( sipCpp->message( QgsErrorMessage::Text ) );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
% End
#endif

private:
//! List of messages
QList<QgsErrorMessage> mMessageList;
Expand Down
6 changes: 5 additions & 1 deletion tests/src/python/test_python_repr.py
Expand Up @@ -17,7 +17,7 @@
from PyQt5.QtCore import QVariant
from qgis.testing import unittest, start_app
from qgis.core import QgsGeometry, QgsPoint, QgsPointXY, QgsCircle, QgsCircularString, QgsCompoundCurve,\
QgsCurvePolygon, QgsEllipse, QgsLineString, QgsMultiCurve, QgsRectangle, QgsExpression, QgsField
QgsCurvePolygon, QgsEllipse, QgsLineString, QgsMultiCurve, QgsRectangle, QgsExpression, QgsField, QgsError

start_app()

Expand Down Expand Up @@ -121,6 +121,10 @@ def testQgsFieldRepr(self):
f = QgsField('field_name', QVariant.Double, 'double')
self.assertEqual(f.__repr__(), "<QgsField: field_name (double)>")

def testQgsErrorRepr(self):
e = QgsError('you done wrong son', 'dad')
self.assertEqual(e.__repr__(), "<QgsError: dad you done wrong son>")


if __name__ == "__main__":
unittest.main()

0 comments on commit 4212cca

Please sign in to comment.