Skip to content

Commit fcb6c2b

Browse files
committed
Fix some incorrect return values from QgsFeature python bindings
1 parent 6f3b0ca commit fcb6c2b

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

python/core/qgsfeature.sip

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,8 @@ class QgsFeature
293293
PyErr_SetString(PyExc_KeyError, QByteArray::number(a0));
294294
sipIsErr = 1;
295295
}
296+
297+
sipRes = rv;
296298
%End
297299

298300
/** Initialize this feature with the given number of fields. Discard any previously set attribute data.
@@ -412,10 +414,12 @@ class QgsFeature
412414
{
413415
PyErr_SetString(PyExc_KeyError, a0->toAscii());
414416
sipIsErr = 1;
417+
sipRes = false;
415418
}
416419
else
417420
{
418421
sipCpp->deleteAttribute( fieldIdx );
422+
sipRes = true;
419423
}
420424
%End
421425
/** Lookup attribute value from attribute name. Field map must be associated using @link setFields @endlink

tests/src/python/test_qgsfeature.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import qgis # NOQA
1616

1717
import os
18-
from qgis.core import QgsFeature, QgsGeometry, QgsPoint, QgsVectorLayer, NULL
18+
from qgis.core import QgsFeature, QgsGeometry, QgsPoint, QgsVectorLayer, NULL, QgsFields, QgsField
1919
from qgis.testing import start_app, unittest
2020
from utilities import unitTestDataPath
2121

@@ -66,13 +66,22 @@ def test_Attributes(self):
6666

6767
assert myAttributes == myExpectedAttributes, myMessage
6868

69-
def test_SetAttribute(self):
69+
def test_SetAttributes(self):
7070
feat = QgsFeature()
7171
feat.initAttributes(1)
7272
feat.setAttributes([0])
7373
feat.setAttributes([NULL])
7474
assert [NULL] == feat.attributes()
7575

76+
def test_setAttribute(self):
77+
feat = QgsFeature()
78+
feat.initAttributes(1)
79+
with self.assertRaises(KeyError):
80+
feat.setAttribute(-1, 5)
81+
with self.assertRaises(KeyError):
82+
feat.setAttribute(10, 5)
83+
self.assertTrue(feat.setAttribute(0, 5))
84+
7685
def test_DeleteAttribute(self):
7786
feat = QgsFeature()
7887
feat.initAttributes(3)
@@ -85,6 +94,22 @@ def test_DeleteAttribute(self):
8594
myMessage = '\nExpected: %s\nGot: %s' % (str(myExpectedAttrs), str(myAttrs))
8695
assert myAttrs == myExpectedAttrs, myMessage
8796

97+
def test_DeleteAttributeByName(self):
98+
fields = QgsFields()
99+
field1 = QgsField('my_field')
100+
fields.append(field1)
101+
field2 = QgsField('my_field2')
102+
fields.append(field2)
103+
104+
feat = QgsFeature(fields)
105+
feat.initAttributes(2)
106+
feat[0] = "text1"
107+
feat[1] = "text2"
108+
with self.assertRaises(KeyError):
109+
feat.deleteAttribute('not present')
110+
self.assertTrue(feat.deleteAttribute('my_field'))
111+
self.assertEqual(feat.attributes(), ['text2'])
112+
88113
def test_SetGeometry(self):
89114
feat = QgsFeature()
90115
feat.setGeometry(QgsGeometry.fromPoint(QgsPoint(123, 456)))

0 commit comments

Comments
 (0)