Thanks to visit codestin.com
Credit goes to github.com

Skip to content

don't close files... because that may be destructive #2

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

Merged
merged 1 commit into from
May 24, 2011

Conversation

stefanfoulis
Copy link
Contributor

do not presume the file needs to be closed (it may be a StringIO which is useless once closed). the framework user must decide when to close the file.

…h is useless once closed). the framework user must decide when to close the file.
@chrisglass
Copy link
Contributor

This was explicitely merged in to "fix a bug". I think your pull needs to override this behavior, since the previous addition of the close() call actually broke the default behavior of using a pisaTempfile (that raises an AttributeError right now).

Merging this in.

chrisglass added a commit that referenced this pull request May 24, 2011
don't close files... because that may be destructive
@chrisglass chrisglass merged commit 152d1ec into xhtml2pdf:master May 24, 2011
@jonknee
Copy link

jonknee commented Aug 18, 2011

This commit led to my application creating blank files (invoices as orders come in). I was able to get it working by manually closing the file (calling dest.close()). Perhaps I am doing it the wrong way, but even something this simple is not working:

from xhtml2pdf import pisa
pdf = pisa.CreatePDF("Hello World", file("test.pdf", "wb"))

That will work if called from the command line because Python finishes executing and it will close out the file, but if the PDF is generated from something like Django, you'll end up with an unclosed file that can't be used (my application was emailing a PDF invoice to sales people). I had to update the code to be:

from xhtml2pdf import pisa
pdf = pisa.CreatePDF("Hello World", file("test.pdf", "wb"))
pdf.dest.close()

There has to be a better way to handle this.

@stefanfoulis
Copy link
Contributor Author

That's precisely the thing to do for how it is implemented now.

The only thing I can think of is to add an parameter:

pdf = pisa.CreatePDF("Hello World", file("test.pdf", "wb"), close=True)

and if you are using a StringIO:

file_obj = StringIO.StringIO()
pdf = pisa.CreatePDF("Hello World", file_obj, close=False)

If the default for close would be True it would stay backwards compatible. But there definitely needs to be a way to use StringIO objects, so it's possible to do stuff in RAM without touching the filesystem.

@jonknee
Copy link

jonknee commented Aug 18, 2011

Why not just check if it's a file object and close it if so?

@stefanfoulis
Copy link
Contributor Author

Because of "DuckTyping" it is hard to tell what the thing that gets passed in really is. StringIO and File both have a file like signature. What if you use some alternative implementation of something like StringIO. That could never be detected in a reliable way. So I guess we could make it work automatically for StringIO, but the optional parameter would still have to exist for other cases.

@jonknee
Copy link

jonknee commented Aug 18, 2011

I was more thinking checking for if it was the file class and if so closing it, but you're right in that there are enough file like objects that explicitly checking is a bad idea. I'd be happy with the addition of a close argument (defaulting to true) then. It would make the examples function again at least.

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.

3 participants