Overly complex__del__ method¶
ID: py/overly-complex-deleteKind: problemSecurity severity: Severity: recommendationPrecision: highTags: - quality - maintainability - complexityQuery suites: - python-security-and-quality.qls
Click to see the query in the CodeQL repository
The__del__ method exists to release any resources held by an object when that object is deleted. The__del__ is called only by the garbage collector which may call it after an indefinite delay or never.
Consequently,__del__ method should not be relied on to release resources, such as file descriptors. Rather, these resources should be released explicitly.
The existence of a complex__del__ method suggests that this is the main or only way to release resources associated with the object.
Recommendation¶
In order to ensure correct cleanup of the object add an explicit close(), or similar, method. Possibly make the object a context manager.
Thedel method should just call close()
Example¶
The first example below shows a class which relies on__del__ to release resources. The second example shows an improved version of the class where__del__ simply calls close.
#Relies on __del__ being called by the garbage collector.classCachedPreferencesFile...def__del__(self):forkey,valueinself.preferences.items():self.write_pair(key,value)self.backing.close()#Better versionclassCachedPreferencesFile...defclose(self):forkey,valueinself.preferences.items():self.write_pair(key,value)self.backing.close()def__del__(self):self.close()
References¶
Python Standard Library:Context manager.