title: Flask Webapp on Google App Engine error only when accessed from PC: 'ascii' codec can't decode byte
body: <p>I have a Flask webapp hosted on Google App Engine that requires the user to upload a file. It's been working well for a few years now. The webapp is ad supported, so I won't link to the hosted version, but the source code is here: <a href="https://github.com/n8henrie/icw" rel="nofollow">https://github.com/n8henrie/icw</a></p>
  
  <p>A user recently notified me that he was getting a 500 error with a file: <code>'ascii' codec can't decode byte 0xc2 in position 108: ordinal not in range(128)</code></p>
  
  <p>He emailed me the file, and I was unable to replicate the error either locally or on the hosted webapp on OS X.</p>
  
  <p>Later, he emailed me several more files that were causing the error, and so I tried again, but this time from a PC. On the PC, I <em>did</em> get the error. Curious, I went back to my Mac, download the same file from my Gmail, and tried -- and did <em>not</em> get the error.</p>
  
  <p>Why would this be? I would <em>really</em> like to reproduce this error on my Mac so I can debug at home, but I'm only getting it from a PC at work -- where I don't have my code and can't debug.</p>
  
  <h2><a href="http://mattgemmell.com/what-have-you-tried/" rel="nofollow">WHYT</a></h2>
  
  <ul>
  <li>Thought it might be related to local file encoding after downloading from Gmail but prior to uploading to the webapp, so on my Mac I opened in TextWrangler and tried to change the encoding to ascii. Still no error</li>
  <li>Opened the file on PC in Notepad and changed encoding to UTF8. Still causes error.</li>
  <li>Added <code>from __future__ import unicode_literals</code> to webapp. Still passes all tests on OSX, still causes similar error on PC (<code>'ascii' codec can't decode byte 0xef in position 0: ordinal not in range(128)</code>).</li>
  </ul>
  
  <p>Why would the same webapp and the same uploaded file have an error on PC but not on my Mac? Does GAE somehow change the webapp version based on detecting the OS of the client?</p>
  
  <p>Many thanks for any help.</p>
  
  <ul>
  <li>Chrome 46.0.2490.80 on Windows 7 v6.1</li>
  <li>Chrome 46.0.2490.80 on OS X 10.11.1</li>
  <li>Python 2.7 hosted on GAE</li>
  <li>Flask==0.10.1</li>
  </ul>
  
  <h2>UPDATE 20151111</h2>
  
  <p>Was able to find the stack trace on GAE:</p>
  
  <pre><code>Exception on / [POST]
  Traceback (most recent call last):
    File "/base/data/home/apps/s~icw-flask/2.386023698597365904/lib/flask/app.py", line 1817, in wsgi_app
      response = self.full_dispatch_request()
    File "/base/data/home/apps/s~icw-flask/2.386023698597365904/lib/flask/app.py", line 1477, in full_dispatch_request
      rv = self.handle_user_exception(e)
    File "/base/data/home/apps/s~icw-flask/2.386023698597365904/lib/flask/app.py", line 1381, in handle_user_exception
      reraise(exc_type, exc_value, tb)
    File "/base/data/home/apps/s~icw-flask/2.386023698597365904/lib/flask/app.py", line 1475, in full_dispatch_request
      rv = self.dispatch_request()
    File "/base/data/home/apps/s~icw-flask/2.386023698597365904/lib/flask/app.py", line 1461, in dispatch_request
      return self.view_functions[rule.endpoint](**req.view_args)
    File "/base/data/home/apps/s~icw-flask/2.386023698597365904/icw/views.py", line 38, in index
      links_title=links_title)
    File "/base/data/home/apps/s~icw-flask/2.386023698597365904/lib/flask/templating.py", line 128, in render_template
      context, ctx.app)
    File "/base/data/home/apps/s~icw-flask/2.386023698597365904/lib/flask/templating.py", line 110, in _render
      rv = template.render(context)
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/jinja2-2.6/jinja2/environment.py", line 894, in render
      return self.environment.handle_exception(exc_info, True)
    File "/base/data/home/apps/s~icw-flask/2.386023698597365904/icw/templates/index.html", line 1, in top-level template code
      {% extends "base.html" %}
    File "/base/data/home/apps/s~icw-flask/2.386023698597365904/icw/templates/base.html", line 3, in top-level template code
      {% extends "bootstrap/base.html" %}
    File "/base/data/home/apps/s~icw-flask/2.386023698597365904/lib/flask_bootstrap/templates/bootstrap/base.html", line 1, in top-level template code
      {% block doc -%}
    File "/base/data/home/apps/s~icw-flask/2.386023698597365904/lib/flask_bootstrap/templates/bootstrap/base.html", line 4, in block "doc"
      {%- block html %}
    File "/base/data/home/apps/s~icw-flask/2.386023698597365904/lib/flask_bootstrap/templates/bootstrap/base.html", line 20, in block "html"
      {% block body -%}
    File "/base/data/home/apps/s~icw-flask/2.386023698597365904/icw/templates/base.html", line 40, in block "body"
      {{ utils.flashed_messages(messages=messages, container=False) }}
    File "/base/data/home/apps/s~icw-flask/2.386023698597365904/lib/flask_bootstrap/templates/bootstrap/utils.html", line 12, in template
      {% for cat, msg in messages %}      &lt;div class="alert alert-{{cat}}" role="alert"&gt;{{msg|safe}}&lt;/div&gt;{% endfor -%}
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/jinja2-2.6/jinja2/filters.py", line 705, in do_mark_safe
      return Markup(value)
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/markupsafe-0.15/markupsafe/__init__.py", line 71, in __new__
      return unicode.__new__(cls, base)
  UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 108: ordinal not in range(128)
  </code></pre>
  
  <p>I <em>think</em> the most relevant code from my project is reading the file, and the issue may be with <a href="http://werkzeug.pocoo.org/docs/0.10/unicode" rel="nofollow">werkzeug's handling of unicode</a>.</p>
  
  <p><code>icw/converter.py:139</code>:</p>
  
  <pre><code>def convert(upfile):
  
      reader_builder = csv.reader(upfile.read().splitlines(),
                                  skipinitialspace=True)
  
      reader_list = list(reader_builder)
  </code></pre>