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

Documentation about file_obj and url_fetcher #814

Closed
SebCorbin opened this issue Feb 27, 2019 · 4 comments
Closed

Documentation about file_obj and url_fetcher #814

SebCorbin opened this issue Feb 27, 2019 · 4 comments
Labels
documentation Problems or improvements needed on the documentation or on the website
Milestone

Comments

@SebCorbin
Copy link
Contributor

SebCorbin commented Feb 27, 2019

Seeing

If a ``file_obj`` key is given, it is the caller’s responsibility
to call ``file_obj.close()``.
and
@contextlib.contextmanager
def fetch(url_fetcher, url):
"""Call an url_fetcher, fill in optional data, and clean up."""
try:
result = url_fetcher(url)
except Exception as exc:
raise URLFetchingError('%s: %s' % (type(exc).__name__, str(exc)))
result.setdefault('redirected_url', url)
result.setdefault('mime_type', None)
if 'file_obj' in result:
try:
yield result
finally:
try:
result['file_obj'].close()
except Exception:
# May already be closed or something.
# This is just cleanup anyway: log but make it non-fatal.
LOGGER.warning('Error when closing stream for %s:\n%s',
url, traceback.format_exc())
else:
yield result

I'm guessing the documentation is either outdated or wrong, is that right?

@liZe
Copy link
Member

liZe commented Feb 27, 2019

is that right?

It is, thanks!

@liZe liZe added the documentation Problems or improvements needed on the documentation or on the website label Feb 27, 2019
@SebCorbin
Copy link
Contributor Author

In a way, it's not, because fetch() is actually the caller and is closing file_obj, but updating the special section about URL fetchers in the docs to explain that would be nice.

@liZe
Copy link
Member

liZe commented Feb 27, 2019

Yes, I'm trying to find the exact words to say this.

@liZe
Copy link
Member

liZe commented Feb 27, 2019

If a file_obj key is given, it is the caller’s responsibility to call file_obj.close(). The default function used internally to fetch data in WeasyPrint tries to close file_obj after retreiving; but if this URL fetcher is used elsewhere, the file object has to be closed manually.

Is that OK for you?

liZe added a commit that referenced this issue Feb 27, 2019
@liZe liZe closed this as completed in 97476aa Feb 28, 2019
liZe added a commit that referenced this issue Feb 28, 2019
Fix #814 Documentation about custom url_fetcher
@liZe liZe added this to the 46 milestone Mar 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Problems or improvements needed on the documentation or on the website
Projects
None yet
Development

No branches or pull requests

2 participants