#1657 When decoding a file, try the encoding found, then UTF-8, otherwise bail
Merged by pingou. Opened by pingou.
decoding_fix  into  master

Download 1657.patch

This fixes displaying the URLs:
https://pagure.io/u-boot/blob/aaf098cfeed04595d4c5100ffd39095d79edbf90/f/MAINTAINERS
or
https://pagure.io/u-boot/blob/0c8721a466b5e0eca7e7fbe1007777fa82100541/f/CREDITS
(among others).

Basically, there is something in those files that does not please python.
When guessing the encoding, we find them both as: ISO-8859-8 but in
both cases we fail to decode them using this encoding.
As a fallback, we try decoding them as UTF-8, which also fails.

In this case, with this path, we just bail and return the string as bytes
as it was given to us.

What if we broke the part of guess_encoding that builds the sorted list of Guess objects into its own function and used it here. Then loop through the encodings in order of most confident to least confident and attempt to decode the data?

I'm actually quite surprised chardet is returning an encoding that isn't even capable of decoding the data.

Regardless of how we fall back, I would prefer we didn't do this. It breaks the API of this function, which promises a unicode string object.

It might be better to raise a Pagure exception here which informs the user that the file cannot be decoded.

rebased

1 new commit added

  • Create a guess_encodings() method returning all the possible encodings

1 new commit added

  • Place the methods in the order in which they are used for easier reading

1 new commit added

  • Add unit-tests for pagure.lib.encoding_utils.guess_encodings()

1 new commit added

  • Adjust docstrings in pagure.lib.encoding_utils

This doesn't seem to be an exceptional case to me. There are going to be files we can't decode and that's fine. Maybe info would be a better log level. It'd also be good if the log message included information about which file was problematic (repo, tree, and filename or something like that).

Same comment about exception vs info

I don't think 400 is appropriate here. There was nothing wrong with the request, it's just that the server was unable to satisfy it.

s/emtpy/empty

You could just do _log.debug('Trying encoding: ' + encoding) since encoding should be a string. I don't mind either way, though.

The advantage is that exception will send me an email with all the information (full url, user...) so I can then see if the situation can be improved.

It is actually such an email that started this PR.
So at least for now, I prefer keeping LOG.exception(). If it becomes too spammy, then I'll likely open a PR to change this :)

Would you prefer 500 then?

You could just do _log.debug('Trying encoding: ' + encoding) since encoding should be a string. I don't mind either way, though.

I dropped the call to str() but I was under the impression that using %s
in the log calls was the proper way. Is it wrong?

3 new commits added

  • Drop redundant call to str()
  • Fix typo in the docstring pointed out by @jcline :)
  • Decode the content of the README files when converting them

You could just do _log.debug('Trying encoding: ' + encoding) since encoding should be a string. I don't mind either way, though.

I dropped the call to str() but I was under the impression that using %s
in the log calls was the proper way. Is it wrong?

I'm actually not sure, I've never heard that before. I can't see how it would behave any different, but I'm fine leaving it as it is!

It's a tough call, since it's not really the client's fault so 4xx codes seem wrong, and there's no 5xx code that is right either. Maybe 415 is the least weird? I'm not sure.

Other than the HTTP return code this is fine with me. No HTTP code looks particularly right, so I don't mind if it's something like HTTP 500. At least that way the client doesn't think they're at fault.

Let's do 500 then :)

rebased

1 new commit added

  • Return a 500 error instead of a 400 when the server fails to decode the file

:thumbsup:

rebased

Pull-Request has been merged by pingou

Metadata