Thanks so much, Bert. That should have been obvious!

As an interim solution, I'm just wrapping the inputs:

    if six.PY3:
        csvfile = BytesIO(csvfile.read().encode("utf-8"))
        csvfile.seek(0)



On Sunday, October 18, 2020 at 4:22:16 PM UTC-4 Bert JW Regeer wrote:

> Your body_file is not bytes, but str. You need to make sure that what you 
> pass to body_file returns bytes.
>
> On Oct 16, 2020, at 15:03, Jonathan Vanasco <jvan...@gmail.com> wrote:
>
> I discovered an issue with our code when generating dynamic CSVs under 
> Python3.  I'm hoping someone can point me in the right direction.  I 
> couldn't find the appropriate migration/changelog information.  This works 
> fine under Python2.
>
> The generic way we create/serve the CSV and check it in tests are below.
>
> The two problems:
>
> 1. Under Python3, an exception is thrown under waitress, because of 
> attempted string+byte concatenation. (
> https://github.com/Pylons/waitress/blob/master/src/waitress/task.py#L316)
>
>     >    towrite += data + b"\r\n"
>
>     > TypeError: can only concatenate str (not "bytes") to str
>
> 2. I picked this up in a unit test; i can't seem to access any sort of 
> body/text/content off the response.
>
>     from pyramid.request import Request
>     from pyramid import testing
>
>     self.config = config = testing.setUp()
>     app = self.config.make_wsgi_app()
>
>     req = Request.blank(csv_link)
>     req.remote_addr = "127.0.0.1"
>     resp = req.get_response(app)
>     self.assertEqual(resp.status_code, 200)
>     self.assertTrue(resp.text.startswith("User CPU time,"))
>
>
>
> ---
>
>  def view_csv(request):
>     csvfile = StringIO()
>     csvwriter = csv.writer(csvfile)
>     for row in ((1, "a"), (2, "b")):
>         csvwriter.writerow(row)
>     csvfile.seek(0)
>     as_csv = Response(content_type="text/csv", body_file=csvfile, 
> status=200)
>     as_csv.headers["Content-Disposition"] = str("attachment; 
> filename=example.csv")
>     return as_csv
>
> ---
>
>
>
>
>
>
>
> -- 
> You received this message because you are subscribed to the Google Groups 
> "pylons-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to pylons-discus...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/pylons-discuss/7d3c278f-8d20-4456-b05c-33c8d2cb6a67n%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/pylons-discuss/7d3c278f-8d20-4456-b05c-33c8d2cb6a67n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to pylons-discuss+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/pylons-discuss/e73aae83-0f7e-414c-a9c3-c20957793a0dn%40googlegroups.com.

Reply via email to