Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reuse the libkeepass context manager #44

Closed
wget opened this issue Jul 28, 2017 · 2 comments
Closed

Reuse the libkeepass context manager #44

wget opened this issue Jul 28, 2017 · 2 comments

Comments

@wget
Copy link

wget commented Jul 28, 2017

Like described in the code, pykeepass has to recreate a write file stream in order to write to the context manager, because the libkeepass does only provide a context manager managing a read-only stream.

Also, since PyKeePass has been developed in a declarative way (a la C) rather than the Pythonic way, the libkeepass context manager isn't reused and pykeepass fakes the use of the libkeypass context manager by calling its underlying method (open.__enter__()) manually.

And finally, when calling the resource manager manually, depending on the garbage collector implementation (e.g.: CPython), the file descriptor might not be freed correctly leading in resources/memory leaks. The garbage collector never calls __exit__() explicitly, only with-statements do that. It's generally bad to depend on the garbage collector to trigger cleanup code anyways. This is why closing the file stream from the underlying library is recommended (especially and the mechanisms included in that lib are being deconstructed by the piece of software making use of it).

@wget
Copy link
Author

wget commented Jul 29, 2017

After further investigation, it appears the __del__ function is only called after all references to the object have been deleted.

In this use case calling the close method from the underlying libkeepass doesn't work as there is no reference to it any more. It this for this particular use case, Python is smart enough to figure out to close the stream used by the underlying library.

The password you provided is incorrect or the KeePass file is invalid or has been corrupted.
Exception ignored in: <bound method PyKeePass.__del__ of <pykeepass.pykeepass.PyKeePass object at 0x7fa9e357dbe0>>
Traceback (most recent call last):
  File "/home/wget/python_libo_to_keepass/pykeepass/pykeepass.py", line 33, in __del__
    self.kdb.close()
AttributeError: 'PyKeePass' object has no attribute 'kdb'

@Evidlo
Copy link
Member

Evidlo commented Aug 28, 2018

This should be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants