Repository: python-pillow/Pillow

Issue:
Image.open raise UnicodeDecodeError if locale setting modified
``` python
import locale
from PIL import Image

locale.setlocale(locale.LC_ALL, "polish")
im = Image.open('test.jpg')

Traceback (most recent call last):
  File "E:\DOWNLOADS\jpegtran\test.py", line 6, in <module>
    im = Image.open('test.jpg')
  File "G:\Python27\lib\site-packages\PIL\Image.py", line 1982, in open
    preinit()
  File "G:\Python27\lib\site-packages\PIL\Image.py", line 300, in preinit
    from PIL import PpmImagePlugin
  File "G:\Python27\lib\site-packages\PIL\PpmImagePlugin.py", line 27, in <module>
    b_whitespace = string.whitespace.encode()
UnicodeDecodeError: 'ascii' codec can't decode byte 0xa0 in position 6: ordinal
not in range(128)
```



Discussion:
Interesting, so what's the fix? Try/except to fallback to something else… looks like maybe the encode should check the locale for the proper encoding? Guessing here.

Does the image work fine if you don't change the language? Does it crash with every image once you changed the language? If it is the first, please upload the image!

What does locale.setlocale(locale.LC_ALL, "polish") do?  is "polish" a valid locale, as I can't get it to work here. [edit: I see this is probably windows, which may vary] I did generate the pl_PL.UTF-8 locale, and with that, this test passes:

```
from tester import *
from PIL import Image

import locale


path = "Images/lena.jpg"

def test_sanity():
    assert_no_exception(lambda: Image.open(path))
    locale.setlocale(locale.LC_ALL, "pl_PL.UTF-8")
    assert_no_exception(lambda: Image.open(path))
```

Windows 7, "polish" is valid locale.

```
import locale
print locale.setlocale(locale.LC_ALL, 'polish')
import string
print len(string.whitespace)
print ord(string.whitespace[6])
```

Polish_Poland.1250
7
160

string.whitespace has one addtional character: non-breaking space 0xa0 (160)
if string module imported after locale changed to "polish" and cause problems (not in 
ascii range)

Ok, so clearly in some locales, there's whitespace that's not a valid ascii character. I'm not sure what the specs are on PPM images, but I'm going to take a wild guess that it's ascii, since it's a really old spec. And that the tokens are separated by ordinary whitespace. In fact, I'd further say that the client locale really has nothing to do with the ppm spec. 

String.encode should probably specify the default ascii encoding, and specify some error parameter, or we should just get the ordinary whitespace and go with that. 

This should do it:

```
b_whitespace = string.whitespace.encode('ascii', 'ignore')
```


Resolving patch:
diff --git a/PIL/PpmImagePlugin.py b/PIL/PpmImagePlugin.py
--- a/PIL/PpmImagePlugin.py
+++ b/PIL/PpmImagePlugin.py
@@ -24,7 +24,7 @@
 #
 # --------------------------------------------------------------------
 
-b_whitespace = string.whitespace.encode()
+b_whitespace = string.whitespace.encode('ascii','ignore')
 
 MODES = {
     # standard


Test patch:
diff --git a/Tests/test_locale.py b/Tests/test_locale.py
new file mode 100644
--- /dev/null
+++ b/Tests/test_locale.py
@@ -0,0 +1,31 @@
+from tester import *
+from PIL import Image
+
+import locale
+
+# ref https://github.com/python-imaging/Pillow/issues/272
+## on windows, in polish locale:
+
+## import locale
+## print locale.setlocale(locale.LC_ALL, 'polish')
+## import string
+## print len(string.whitespace)
+## print ord(string.whitespace[6])
+
+## Polish_Poland.1250
+## 7
+## 160
+
+# one of string.whitespace is not freely convertable into ascii. 
+
+path = "Images/lena.jpg"
+
+def test_sanity():
+    assert_no_exception(lambda: Image.open(path))
+    try:
+        locale.setlocale(locale.LC_ALL, "polish")
+    except:
+        skip('polish locale not available')
+    import string
+    assert_no_exception(lambda: Image.open(path))
+