Pull requests

#10 Declined
Repository
olevinsky olevinsky
Branch
default
Repository
evzijst evzijst
Branch
default

fixed string concatenation

Author
  1. olevinsky avatarolevinsky
Reviewers
Description

Get this error, when stack includes encoded utf-8 symbols:

Dogslow failed

Types: UnicodeDecodeError
Value: 'ascii' codec can't decode byte 0xd0 in position 28432: ordinal not in range(128)
Location:   dogslow/__init__.py in peek , line 134

Stacktrace (most recent call last):

  File "dogslow/__init__.py", line 134, in peek
    output += stack(frame, with_locals=True)

Fix issue: https://bitbucket.org/evzijst/dogslow/issue/3/unicode-safety

Comments (3)

  1. Erik van Zijst repo owner

    I don't think we should just assume the stack information to be encoded in utf-8. The file names that appear in the backtrace for instance will be the raw strings as they are stored on the file system and most file systems do not bother with encoding.

    This means that a file name in a directory block could be written in utf-16 (meaning it will contain NULL bytes). When you then assume utf-8 on line 86, it would fail. Same for many machines in Asian countries.

    Maybe a better approach would be to decode the string based on the system's locale?

    1. Erik van Zijst repo owner

      I'm afraid it's actually more tricky than that.

      The stack function combines data from different sources:

      • the line of source code from the source file (line 51)
      • the module's filename (line 47)
      • plain ASCII (line 64)
      • whatever repr on the stack's variables returns (line 69)

      Since none of that uses unicode objects and the filename on the file system may be using a different encoding than the module's source file, we could be screwing ourselves over as we concatenate everything together as a single raw byte string and later trying to decode it to unicode might fail for any encoding we try.

      I don't know what the right way to handle this would be, but since each of the sources could be using a different character encoding, we might try to decode all the individual constituents independently, prior to concatenating them.

      Since we also don't know what encoding the individual parts might be in, we should maybe use something like chardet.

      This is all becoming fairly heavy-handed and it's still no guarantee for success. chardet will not always be able to unambiguously detect the right encoding for short strings and there may even be byte patterns that are technically valid in multiple encodings, but decode to different unicode code points. Likewise it's possible that a string cannot be decoded at all.

      A crude, quick workaround for now might be to force a certain encoding, replacing any byte that couldn't be decoded:

      s.decode('utf-8', 'replace')
      

      But of course that wouldn't make the output any more readable, or even guaranteed to be correct.

Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.