Skip to content

Commit

Permalink
Do not dec ref in methodcode
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed Jun 1, 2018
1 parent adcc2e9 commit 7ffdab2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 25 deletions.
4 changes: 2 additions & 2 deletions python/core/auto_generated/qgsprovidermetadata.sip.in
Expand Up @@ -61,8 +61,8 @@ no library is involved.
PyObject *sipResObj;
SIP_BLOCK_THREADS

sipResObj = sipCallMethod( NULL, a2, "N", new QString( dataSource ), sipType_QString, NULL );
Py_DECREF( a2 );
sipResObj = sipCallMethod( NULL, a2, "D", new QString( dataSource ), sipType_QString, NULL );

if ( sipResObj )
{
// Py_DECREF(sipResObj);
Expand Down
4 changes: 2 additions & 2 deletions src/core/qgsprovidermetadata.h
Expand Up @@ -79,8 +79,8 @@ class CORE_EXPORT QgsProviderMetadata
PyObject *sipResObj;
SIP_BLOCK_THREADS

sipResObj = sipCallMethod( NULL, a2, "N", new QString( dataSource ), sipType_QString, NULL );
Py_DECREF( a2 );
sipResObj = sipCallMethod( NULL, a2, "D", new QString( dataSource ), sipType_QString, NULL );

if ( sipResObj )
{
// Py_DECREF(sipResObj);
Expand Down
46 changes: 25 additions & 21 deletions tests/src/python/provider_python.py
Expand Up @@ -99,6 +99,7 @@ def getFeatures(self, request):
# NOTE: this is the same as PyProvider.getFeatures
return PyFeatureIterator(self, request)


class PyProvider(QgsVectorDataProvider):

next_feature_id = 1
Expand Down Expand Up @@ -164,13 +165,15 @@ def fields(self):

def addFeatures(self, flist, flags=None):
# bool addFeatures( QgsFeatureList &flist, QgsFeatureSink::Flags flags = nullptr ) override;
result = False
added = False
for f in flist:
f.setId(self.next_feature_id)
self._features[self.next_feature_id] = f
self.next_feature_id += 1
result = True
return result
added = True
if added:
self.updateExtents()
return added, flist

def deleteFeatures(self, ids):
#bool deleteFeatures( const QgsFeatureIds &id ) override;
Expand All @@ -179,6 +182,8 @@ def deleteFeatures(self, ids):
if id in self._features:
del self._features[id]
removed = True
if removed:
self.updateExtents()
return removed

def addAttributes(self, attrs):
Expand All @@ -189,8 +194,8 @@ def addAttributes(self, attrs):
for f in self._features.values():
old_attrs = f.attributes()
old_attrs.app
old_attrs.append( None )
f.setAttributes( old_attrs )
old_attrs.append(None)
f.setAttributes(old_attrs)
return True
except:
return False
Expand All @@ -200,14 +205,14 @@ def renameAttributes(self, renameAttributes):
result = True
for key, new_name in renamedAttributes:
fieldIndex = key
if fieldIndex < 0 or fieldIndex >= lself._fields.count():
if fieldIndex < 0 or fieldIndex >= lself._fields.count():
result = false
continue
if new_name in self._fields.indexFromName( new_name ) >= 0:
if new_name in self._fields.indexFromName(new_name) >= 0:
#field name already in use
result = False
continue
self._fields[ fieldIndex ].setName( new_name );
self._fields[fieldIndex].setName(new_name)
return True

def deleteAttributes(self, attributes):
Expand All @@ -216,11 +221,11 @@ def deleteAttributes(self, attributes):

# delete attributes one-by-one with decreasing index
for idx in attrIdx:
self._fields.remove( idx )
self._fields.remove(idx)
for f in self._features:
attr = f.attributes()
attr.remove( idx )
f.setAttributes( attr )
attr.remove(idx)
f.setAttributes(attr)
return True

def changeAttributeValues(self, attr_map):
Expand Down Expand Up @@ -257,7 +262,7 @@ def createSpatialIndex(self):

def capabilities(self):
#QgsVectorDataProvider::Capabilities capabilities() const override;
return QgsVectorDataProvider.AddFeatures | QgsVectorDataProvider.DeleteFeatures | QgsVectorDataProvider.ChangeGeometries | QgsVectorDataProvider.ChangeAttributeValues | QgsVectorDataProvider.AddAttributes | QgsVectorDataProvider.DeleteAttributes | QgsVectorDataProvider.RenameAttributes | QgsVectorDataProvider.CreateSpatialIndex | QgsVectorDataProvider.SelectAtId | QgsVectorDataProvider. CircularGeometries
return QgsVectorDataProvider.AddFeatures | QgsVectorDataProvider.DeleteFeatures | QgsVectorDataProvider.ChangeGeometries | QgsVectorDataProvider.ChangeAttributeValues | QgsVectorDataProvider.AddAttributes | QgsVectorDataProvider.DeleteAttributes | QgsVectorDataProvider.RenameAttributes | QgsVectorDataProvider.SelectAtId | QgsVectorDataProvider. CircularGeometries

#/* Implementation of functions from QgsDataProvider */

Expand All @@ -267,21 +272,21 @@ def name(self):

def extent(self):
#QgsRectangle extent() const override;
if self._extent.isEmpty() and not self._features.isEmpty():
if self._extent.isEmpty() and not self._features:
self._extent.setMinimal()
if self._subset_string.isEmpty():
# fast way - iterate through all features
for feat in self._features.values():
if feat.hasGeometry():
self._extent.combineExtentWith( feat.geometry().boundingBox() )
self._extent.combineExtentWith(feat.geometry().boundingBox())
else:
for f in self.getFeatures( QgsFeatureRequest().setSubsetOfAttributes( [] ) ):
for f in self.getFeatures(QgsFeatureRequest().setSubsetOfAttributes([])):
if f.hasGeometry():
self._extent.combineExtentWith( f.geometry().boundingBox() )
elif self._features.isEmpty() :
self._extent.setMinimal();
return mExtent;
self._extent.combineExtentWith(f.geometry().boundingBox())

elif self._features:
self._extent.setMinimal()
return self._extent

def updateExtents(self):
#void updateExtents() override;
Expand All @@ -294,4 +299,3 @@ def isValid(self):
def crs(self):
#QgsCoordinateReferenceSystem crs() const override;
return QgsCoordinateReferenceSystem(4326)

0 comments on commit 7ffdab2

Please sign in to comment.