[Python-Dev] Cycle collection enhancement idea (original) (raw)
"Martin v. Löwis" martin at v.loewis.de
Sat Jun 28 17:32:08 CEST 2008
- Previous message: [Python-Dev] Cycle collection enhancement idea
- Next message: [Python-Dev] Cycle collection enhancement idea
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Example:
import os class RunningFile(object): filename = '/tmp/running' def init(self): open(self.filename, 'wb') def del(self): os.unlink(self.filename) runningfile = RunningFile() The deller object is in a cycle as described above [as well as the Deller class itself].
I think you are mistaken here. The RunningFile instance in above code is not part of a cycle. It doesn't have any instance variables (i.e. its dict is empty), and it only refers to its class, which (AFAICT) doesn't refer back to the instance.
When Python exits, it could call deller.del() and then collect the cycle. But Python does the wrong thing here, and gets rid of the globals before calling del: Exception exceptions.AttributeError: "'NoneType' object has no attribute 'unlink'" in <bound method RunningFile._del_ of_ _<_main_.RunningFile object at 0x7f9655eb92d0>> ignored
This is a different issue. For shutdown, Python doesn't rely on cyclic garbage collection (only). Instead, all modules get forcefully cleared, causing this problem.
I believe applying the above enhancement would solve these problems.
No, they wouldn't.
To work around the real problem in your case, put everything that the destructor uses into an instance or class attribute:
class RunningFile(object): filename = '/tmp/running' _unlink = os.unlink def init(self): open(self.filename, 'wb') def del(self): self._unlink(self.filename)
Regards, Martin
- Previous message: [Python-Dev] Cycle collection enhancement idea
- Next message: [Python-Dev] Cycle collection enhancement idea
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]