[issue43535] Make str.join auto-convert inputs to strings.

2021-03-20 Thread Emil Stenström

Emil Stenström  added the comment:

Terry, Gregory: The suggestion is not to change what 1 + "2" does, I fully 
agree that it behaves at it should. The suggestion is to change what 
",".join(1, "2") does. There's no doubt that the intended result is "1, 2". 
That's why it's possible to coerce.

About the example with a list with mixed types: If the reason that example is 
buggy is "this list should only have strings", a better way to enforce that is 
to add types to enforces it.

--

___
Python tracker 
<https://bugs.python.org/issue43535>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43535] Make str.join auto-convert inputs to strings.

2021-03-20 Thread Emil Stenström

Emil Stenström  added the comment:

Since the proposal is fully backwards compatible I don’t think preferring the 
old version is a reason against this nicer API. After all, people that like the 
current version can continue using it as they do today. 

Teaching Python to beginners is a great way to find the warts of a language 
(I’ve done it too). In the beginning people struggle with arrays and if-blocks, 
and having to go into how map and the str constructor work together to get a 
comma separated list of ints is just too much. Beginners are an important group 
of programmers that this proposal will clearly benefit.

I’m sure there will be some “None”-strings that will slip through this, but I 
think the upside far outweighs the downside in this case.

Big +1 from me.

--
nosy: +EmilStenstrom

___
Python tracker 
<https://bugs.python.org/issue43535>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue26045] Improve error message for http.client when posting unicode string

2016-01-08 Thread Emil Stenström

Emil Stenström added the comment:

I think changing the error message is enough for the short term, and 
deprecation of automatic encoding is the correct way in the long term.

A text that mention "utf-8" which will likely be the correct solution 
definitely gets my vote, so Guidos suggestion sounds good to me:

UnicodeEncodeError("Use data.encode('utf-8') if you want the data to be encoded 
in UTF-8")

Andrew's and Pauls suggestions doesn't point to a solution to the problem, 
which I think is a great think for something this basic. Also, the error 
message only gets shown when latin-1 fails, so we can't use text that speaks 
about "no encoding" in general.

--

___
Python tracker 
<http://bugs.python.org/issue26045>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue26045] Improve error message for http.client when posting unicode string

2016-01-07 Thread Emil Stenström

New submission from Emil Stenström:

This issue is in response to this thread on python-ideas: 
https://mail.python.org/pipermail/python-ideas/2016-January/037678.html

Note that Cory did a lot of encoding background work here:
https://mail.python.org/pipermail/python-ideas/2016-January/037680.html

---
Bug description:

When posting an unencoded unicode string directly with python-requests you get 
the following stacktrace:

import requests
r = requests.post("http://example.com";, data="Celebrate 🎉") 
...
  File "../lib/python3.4/http/client.py", line 1127, in _send_request
body = body.encode('iso-8859-1')
UnicodeEncodeError: 'latin-1' codec can't encode characters in position 14-15: 
ordinal not in range(256) 

This is because requests uses http.client, and http.client assumes the encoding 
to be latin-1 if given a unicode string. This is a very common source of bugs 
for beginners who assume sending in unicode would automatically encode it in 
utf-8, like in the libraries of many other languages.

The simplest fix here is to catch the UnicodeEncodeError and improve the error 
message to something that points beginners in the right direction.

Another option would be to:
- Keep encoding in latin-1 first, and if that fails try utf-8

Other possible solutions (that would be backwards incompatible) includes:
- Changing the default encoding to utf-8 instead of latin-1
- Detect an unencoded unicode string and fail without encoding it with a 
descriptive error message

---

Just to show that this is a problem that exists in the wild, here are a few 
examples that all crashes on the same line in http.client (not all going 
through the requests library:

- https://github.com/kennethreitz/requests/issues/2838
- https://github.com/kennethreitz/requests/issues/1822
- 
http://stackoverflow.com/questions/34618149/post-unicode-string-to-web-service-using-python-requests-library
- 
https://www.reddit.com/r/learnpython/comments/3violw/unicodeencodeerror_when_searching_ebay_with/
- https://github.com/codecov/codecov-python/issues/35
- https://github.com/google/google-api-python-client/issues/145
- https://bugs.launchpad.net/ubuntu/+source/lazr.restfulclient/+bug/1414063

------
components: Unicode
messages: 257721
nosy: Emil Stenström, ezio.melotti, haypo
priority: normal
severity: normal
status: open
title: Improve error message for http.client when posting unicode string
type: enhancement
versions: Python 3.2, Python 3.3, Python 3.4, Python 3.5, Python 3.6

___
Python tracker 
<http://bugs.python.org/issue26045>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com