Skip to content

Add context manager version of ZimCreator to enforce automatic finalization - #18

Closed
pirate wants to merge 1 commit into
cython-blendfrom
context-manager
Closed

pirate wants to merge 1 commit into
cython-blendfrom
context-manager

Conversation

@pirate

@pirate pirate commented Apr 21, 2020

Copy link
Copy Markdown
Contributor

Usage:

from libzim import zimcreator, ZimArticle...

with zimcreator(...) as zc:
    zc.add(ZimArticle(...))

# zc.finalize() is called automatically on leaving the context

@pirate
pirate requested a review from jdcaballerov April 21, 2020 21:26
@kelson42 kelson42 linked an issue Apr 22, 2020 that may be closed by this pull request
@kelson42

Copy link
Copy Markdown
Contributor

@pirate I have added #14 as linked issue, I guess this is the issue which is fixed here. Thank you very much for finally have made the proper fix in place of just fixing the test. Really much appreciated.

@kelson42
kelson42 requested a review from mgautierfr April 22, 2020 07:49
@rgaudin

rgaudin commented Apr 22, 2020 •

Copy link
Copy Markdown
Member

@kelson42 I'm not sure this issue will be fixed by this PR. This is an additional convenient way of using the creator that calls finalize() for you but:

  • It's not yet used in test_libzim.py
  • @mgautierfr mentioned a libzim issue in finalize must be always called. #14 that may need to be tracked: you can't always use a context manager (especially in our current scrapers) so it's user's responsibility to call it to get expected behavior but are there side consequences in not doing so that should be prevented?

@mgautierfr

Copy link
Copy Markdown
Contributor

It is syntaxic sugar, but I prefer to have a classmethod open as contentmanager (move the function as a classmethod open in ZimCreator):

with Zimcreator.open(...) as zc:
    zc.add(ZimArticle(...))

or directly with to constructor (implement __enter__ and __exit__ in ZimCreator :

with Zimcreator(....) as zc:
    ...

you can't always use a context manager (especially in our current scrapers)

I'm curious to see why.

so it's user's responsibility to call it to get expected behavior

Yes, if you don't use a context manager you have to close/finalize the object yourself.
It is like for a plain file object open to write in it. If you don't close the file and quit your program, you may have data not written to the fs.

but are there side consequences in not doing so that should be prevented?

For now, if you don't call finalize before destroying the creator, you have a sigsev fault in libzim.

@mgautierfr

Copy link
Copy Markdown
Contributor

Please rebase this PR on master.

@jdcaballerov

jdcaballerov commented Apr 25, 2020 •

Copy link
Copy Markdown
Contributor

Suggestions implemented based of master in #24

@kelson42
kelson42 deleted the context-manager branch June 14, 2020 16:44
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

Successfully merging this pull request may close these issues.

finalize must be always called.

5 participants