Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
wonder-sk committed Jun 28, 2018
1 parent fb9e575 commit 4c8b801
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 0 deletions.
6 changes: 6 additions & 0 deletions python/core/auto_generated/qgsfeaturesource.sip.in
Expand Up @@ -69,6 +69,12 @@ if the feature count is unknown.
sipRes = sipCpp->featureCount();
%End

//! Ensures that bool(obj) returns true (otherwise __len__() would be used)
int __bool__() const;
%MethodCode
sipRes = true;
%End

virtual long featureCount() const = 0;
%Docstring
Returns the number of features contained in the source, or -1
Expand Down
6 changes: 6 additions & 0 deletions python/core/auto_generated/qgsfeaturestore.sip.in
Expand Up @@ -74,6 +74,12 @@ Returns the number of features contained in the store.
sipRes = sipCpp->count();
%End

//! Ensures that bool(obj) returns true (otherwise __len__() would be used)
int __bool__() const;
%MethodCode
sipRes = true;
%End

QgsFeatureList features() const;
%Docstring
Returns the list of features contained in the store.
Expand Down
6 changes: 6 additions & 0 deletions python/core/auto_generated/qgsfields.sip.in
Expand Up @@ -103,6 +103,12 @@ Returns number of items
sipRes = sipCpp->count();
%End

//! Ensures that bool(obj) returns true (otherwise __len__() would be used)
int __bool__() const;
%MethodCode
sipRes = true;
%End

int size() const;
%Docstring
Returns number of items
Expand Down
6 changes: 6 additions & 0 deletions python/core/auto_generated/qgsmaplayerstore.sip.in
Expand Up @@ -46,6 +46,12 @@ Returns the number of layers contained in the store.
sipRes = sipCpp->count();
%End

//! Ensures that bool(obj) returns true (otherwise __len__() would be used)
int __bool__() const;
%MethodCode
sipRes = true;
%End

QgsMapLayer *mapLayer( const QString &id ) const;
%Docstring
Retrieve a pointer to a layer by layer ``id``.
Expand Down
6 changes: 6 additions & 0 deletions python/core/auto_generated/qgsvectorlayercache.sip.in
Expand Up @@ -221,6 +221,12 @@ if the feature count is unknown.
sipRes = sipCpp->featureCount();
%End

//! Ensures that bool(obj) returns true (otherwise __len__() would be used)
int __bool__() const;
%MethodCode
sipRes = true;
%End

long featureCount() const;
%Docstring
Returns the number of features contained in the source, or -1
Expand Down
6 changes: 6 additions & 0 deletions src/core/qgsfeaturesource.h
Expand Up @@ -93,6 +93,12 @@ class CORE_EXPORT QgsFeatureSource
% MethodCode
sipRes = sipCpp->featureCount();
% End

//! Ensures that bool(obj) returns true (otherwise __len__() would be used)
int __bool__() const;
% MethodCode
sipRes = true;
% End
#endif

/**
Expand Down
6 changes: 6 additions & 0 deletions src/core/qgsfeaturestore.h
Expand Up @@ -79,6 +79,12 @@ class CORE_EXPORT QgsFeatureStore : public QgsFeatureSink
% MethodCode
sipRes = sipCpp->count();
% End

//! Ensures that bool(obj) returns true (otherwise __len__() would be used)
int __bool__() const;
% MethodCode
sipRes = true;
% End
#endif

/**
Expand Down
6 changes: 6 additions & 0 deletions src/core/qgsfields.h
Expand Up @@ -133,6 +133,12 @@ class CORE_EXPORT QgsFields
% MethodCode
sipRes = sipCpp->count();
% End

//! Ensures that bool(obj) returns true (otherwise __len__() would be used)
int __bool__() const;
% MethodCode
sipRes = true;
% End
#endif

//! Returns number of items
Expand Down
6 changes: 6 additions & 0 deletions src/core/qgsmaplayerstore.h
Expand Up @@ -59,6 +59,12 @@ class CORE_EXPORT QgsMapLayerStore : public QObject
% MethodCode
sipRes = sipCpp->count();
% End

//! Ensures that bool(obj) returns true (otherwise __len__() would be used)
int __bool__() const;
% MethodCode
sipRes = true;
% End
#endif

/**
Expand Down
6 changes: 6 additions & 0 deletions src/core/qgsvectorlayercache.h
Expand Up @@ -276,6 +276,12 @@ class CORE_EXPORT QgsVectorLayerCache : public QObject
% MethodCode
sipRes = sipCpp->featureCount();
% End

//! Ensures that bool(obj) returns true (otherwise __len__() would be used)
int __bool__() const;
% MethodCode
sipRes = true;
% End
#endif

/**
Expand Down

0 comments on commit 4c8b801

Please sign in to comment.