Skip to content

Commit 4c8b801

Browse files
committed
Make sure that bool(obj) is True for QGIS API objects
bool(obj) in Python has the following semantics: 1. if the object has __bool__() method, return its value 2. if the object has __len__() method, return its value 3. return True So for objects in QGIS API that implement __len__() method, we were getting unexpected behavior - for example, "if layer: ..." would evaluate as False in case the layer was empty, while the usual expectation is that any reference to an object that is not None should evaluate to True.
1 parent fb9e575 commit 4c8b801

10 files changed

+60
-0
lines changed

python/core/auto_generated/qgsfeaturesource.sip.in

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ if the feature count is unknown.
6969
sipRes = sipCpp->featureCount();
7070
%End
7171

72+
//! Ensures that bool(obj) returns true (otherwise __len__() would be used)
73+
int __bool__() const;
74+
%MethodCode
75+
sipRes = true;
76+
%End
77+
7278
virtual long featureCount() const = 0;
7379
%Docstring
7480
Returns the number of features contained in the source, or -1

python/core/auto_generated/qgsfeaturestore.sip.in

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ Returns the number of features contained in the store.
7474
sipRes = sipCpp->count();
7575
%End
7676

77+
//! Ensures that bool(obj) returns true (otherwise __len__() would be used)
78+
int __bool__() const;
79+
%MethodCode
80+
sipRes = true;
81+
%End
82+
7783
QgsFeatureList features() const;
7884
%Docstring
7985
Returns the list of features contained in the store.

python/core/auto_generated/qgsfields.sip.in

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ Returns number of items
103103
sipRes = sipCpp->count();
104104
%End
105105

106+
//! Ensures that bool(obj) returns true (otherwise __len__() would be used)
107+
int __bool__() const;
108+
%MethodCode
109+
sipRes = true;
110+
%End
111+
106112
int size() const;
107113
%Docstring
108114
Returns number of items

python/core/auto_generated/qgsmaplayerstore.sip.in

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ Returns the number of layers contained in the store.
4646
sipRes = sipCpp->count();
4747
%End
4848

49+
//! Ensures that bool(obj) returns true (otherwise __len__() would be used)
50+
int __bool__() const;
51+
%MethodCode
52+
sipRes = true;
53+
%End
54+
4955
QgsMapLayer *mapLayer( const QString &id ) const;
5056
%Docstring
5157
Retrieve a pointer to a layer by layer ``id``.

python/core/auto_generated/qgsvectorlayercache.sip.in

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,12 @@ if the feature count is unknown.
221221
sipRes = sipCpp->featureCount();
222222
%End
223223

224+
//! Ensures that bool(obj) returns true (otherwise __len__() would be used)
225+
int __bool__() const;
226+
%MethodCode
227+
sipRes = true;
228+
%End
229+
224230
long featureCount() const;
225231
%Docstring
226232
Returns the number of features contained in the source, or -1

src/core/qgsfeaturesource.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ class CORE_EXPORT QgsFeatureSource
9393
% MethodCode
9494
sipRes = sipCpp->featureCount();
9595
% End
96+
97+
//! Ensures that bool(obj) returns true (otherwise __len__() would be used)
98+
int __bool__() const;
99+
% MethodCode
100+
sipRes = true;
101+
% End
96102
#endif
97103

98104
/**

src/core/qgsfeaturestore.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ class CORE_EXPORT QgsFeatureStore : public QgsFeatureSink
7979
% MethodCode
8080
sipRes = sipCpp->count();
8181
% End
82+
83+
//! Ensures that bool(obj) returns true (otherwise __len__() would be used)
84+
int __bool__() const;
85+
% MethodCode
86+
sipRes = true;
87+
% End
8288
#endif
8389

8490
/**

src/core/qgsfields.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,12 @@ class CORE_EXPORT QgsFields
133133
% MethodCode
134134
sipRes = sipCpp->count();
135135
% End
136+
137+
//! Ensures that bool(obj) returns true (otherwise __len__() would be used)
138+
int __bool__() const;
139+
% MethodCode
140+
sipRes = true;
141+
% End
136142
#endif
137143

138144
//! Returns number of items

src/core/qgsmaplayerstore.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ class CORE_EXPORT QgsMapLayerStore : public QObject
5959
% MethodCode
6060
sipRes = sipCpp->count();
6161
% End
62+
63+
//! Ensures that bool(obj) returns true (otherwise __len__() would be used)
64+
int __bool__() const;
65+
% MethodCode
66+
sipRes = true;
67+
% End
6268
#endif
6369

6470
/**

src/core/qgsvectorlayercache.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,12 @@ class CORE_EXPORT QgsVectorLayerCache : public QObject
276276
% MethodCode
277277
sipRes = sipCpp->featureCount();
278278
% End
279+
280+
//! Ensures that bool(obj) returns true (otherwise __len__() would be used)
281+
int __bool__() const;
282+
% MethodCode
283+
sipRes = true;
284+
% End
279285
#endif
280286

281287
/**

0 commit comments

Comments
 (0)