Skip to content

fix for #272 on SmartOS. Stripping whitespace from string.whitespace. - #2820

Closed
markmiscavage wants to merge 1 commit into
python-pillow:masterfrom
markmiscavage:master
Closed

markmiscavage wants to merge 1 commit into
python-pillow:masterfrom
markmiscavage:master

Conversation

@markmiscavage

Copy link
Copy Markdown
Contributor

Fixes #272
Strips the whitespace that comes through on the string.whitespace call on SmartOS systems.

Changes proposed in this pull request:

@hugovk

hugovk commented Nov 1, 2017

Copy link
Copy Markdown
Member

At least this breaks all the existing CI builds.


Examples on Mac:

Python 2.7.14 (default, Sep 25 2017, 09:53:22)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.37)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import string
>>> string.whitespace
'\t\n\x0b\x0c\r '
>>> string.whitespace.strip()
''
>>>
Python 3.6.3 (default, Oct  4 2017, 06:09:15)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.37)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import string
>>> string.whitespace
' \t\n\r\x0b\x0c'
>>> string.whitespace.strip()
''
>>>

What do you get for string.whitespace?


#272 (comment):

I appear to still be getting this error. I'm only seeing this currently on SmartOS base-64 17.3.0.

PIL/PpmImagePlugin.py line 33 is failing with UnicodeDecodeError: 'utf8' codec can't decode byte 0xa0 in position 6: invalid start byte, but the try/except is just skipping that error to fail with the same error seen above.

b_whitespace = string.whitespace.strip() seems to fix the problem for me.

try:
    import locale
    locale_lang, locale_enc = locale.getlocale()
    if locale_enc is None:
        locale_lang, locale_enc = locale.getdefaultlocale()
    b_whitespace = b_whitespace.decode(locale_enc)  # Line 33
except:
    pass

That bare except isn't helping much. Anyway, what do you get for locale.getdefaultlocale()? Example on Mac: ('en_GB', 'UTF-8').

@wiredfool

wiredfool commented Nov 1, 2017 •

Copy link
Copy Markdown
Member

The spec (http://netpbm.sourceforge.net/doc/ppm.html) defines whitespace as:

A "white space" character is space, CR, LF, TAB, VT, or FF (I.e. what the ANSI standard C isspace() function calls white space).

Which are essentially the old pre-locale default whitespace characters. So, we should probably just assign a fixed list, rather than pulling the locale dependent whitespace characters from the string module.

e.g.:

b_whitespace = (chr(32), chr(10), chr(13), chr(9), chr(11), chr(12))

@markmiscavage

markmiscavage commented Nov 1, 2017 •

Copy link
Copy Markdown
Contributor Author
Python 2.7.14 (default, Oct  3 2017, 16:34:09) 
[GCC 4.9.4] on sunos5
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import string
>>> string.whitespace
'\t\n\x0b\x0c\r \xa0'
>>> import locale
>>> locale.getdefaultlocale()
('en_US', 'UTF-8')`

Notice the space after the \r in string.whitespace, thats what was causing the problem for me. Switching it to a fixed list sounds sane to me.

@hugovk

hugovk commented Nov 1, 2017

Copy link
Copy Markdown
Member

I had a quick test with hugovk@5867ce0 but it's failing selftest.py on Python 3: https://travis-ci.org/hugovk/Pillow/builds/295933112

selftest and the full tests pass for me locally.

@wiredfool

Copy link
Copy Markdown
Member

Looks like it needs to be done as a bytes object: https://github.com/wiredfool/Pillow/blob/pr_2820/PIL/PpmImagePlugin.py#L27

@hugovk

hugovk commented Nov 5, 2017

Copy link
Copy Markdown
Member

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