-
Notifications
You must be signed in to change notification settings - Fork 69
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
Don't throw exception when encoding text as UTF-8 bytes fails #40
Comments
That's a good suggestion. Would you like to make a PR? |
Sure, can do. |
phoerious
added a commit
to phoerious/selectolax
that referenced
this issue
Jul 9, 2021
rushter
pushed a commit
that referenced
this issue
Jul 10, 2021
* Apply `decode_errors` to encoding as well, fixes #40 * Fix __repr__() segfault and buggy items() on _Attributes When calling attrs on a temporary instance, the tree may already be deallocated when the tag name is being retrieved, which causes a null pointer dereference. This fix doesn't really solve the problem, but at least it prevents the crash. Also fixes a bug in items() that made the method useless. Fixes #39 * Add test for unencodable strings
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The following line throws an exception if the input HTML string cannot be encoded as UTF-8:
selectolax/selectolax/parser.pyx
Line 37 in 386392c
When using Selectolax for parsing arbitrary web data, this condition can be triggered easily when the server reports the wrong encoding or multiple encodings are mixed, particularly in combination with UTF-7.
Take the byte string
b'Roboto+Condensed'
, which (on its own) should have correctly been decoded as either ASCII or UTF-8. However, interpreted as UTF-7, the string contains an invalid shift sequence:Ignoring this error will produce a Unicode string which cannot be re-encoded as UTF-8 bytes without either replacing or ignoring invalid characters:
Obviously, the easy fix is to do the encoding step beforehand, pass the raw bytes to Selectolax and force it to use UTF-8 by setting
detect_encoding=False
, but that's a lot of stuff you have to figure out in order to get it right, because the error is not really intuitive and can occur without any apparent reason. Since Selectolax already has adecode_errors
parameter that defaults to"ignore"
, I would suggest to apply it also to this encoding step.selectolax/selectolax/parser.pyx
Line 26 in 386392c
The text was updated successfully, but these errors were encountered: