This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Irritating error message by socket's sendto method
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.1, Python 3.2, Python 3.3
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: anacrolix, ezio.melotti, helduel, lukknapen, nvetoshkin, pitrou, python-dev
Priority: normal Keywords: patch

Created on 2009-03-05 10:15 by helduel, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
issue5421.diff nvetoshkin, 2010-11-18 22:24 argument parsing patch agains py3k, no test review
Messages (9)
msg83187 - (view) Author: Manuel Hermann (helduel) Date: 2009-03-05 10:14
When sending unexpected data via a socket's sentdo method, a TypeError
is raised with the fallowing message: "sendto() takes exactly 3
arguments (2 given)". But two arguments are sufficient.

Examples for Python 2.x:
import socket
my_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

# this will succeed
my_socket.sendto("Foobar", ("localhost", 514))

# these will fail with above error message
my_socket.sendto(u"Umlaut ä", ("localhost", 514))
my_socket.sendto(1, ("localhost", 514))


Examples for Python 3:
import socket
my_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

# this will succeed
my_socket.sendto("No Umlaut".encode("ascii"), ("localhost", 514))

# these will fail with above error message
my_socket.sendto("No Umlaut", ("localhost", 514))
my_socket.sendto(1, ("localhost", 514))
msg83228 - (view) Author: Luk Knapen (lukknapen) Date: 2009-03-06 00:01
File $python/lib/python3.0/logging/handlers.py
Line 782 : a bytes object is required instead of a string.
As a consequence, encoding shall be specified : but which one ?

Is : 
   self.socket.sendto(msg, self.address)
Should look like :
   self.socket.sendto(bytes(msg,'ascii'), self.address)
msg121496 - (view) Author: Vetoshkin Nikita (nvetoshkin) Date: 2010-11-18 22:24
Here's a patch, which performs argument checking in a way to be able to provide better error message like that:

>>> my_socket.sendto("No Umlaut", ("localhost", 514))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' does not support the buffer interface
msg131279 - (view) Author: Vetoshkin Nikita (nvetoshkin) Date: 2011-03-17 19:32
ping?
msg131284 - (view) Author: Matt Joiner (anacrolix) Date: 2011-03-17 21:27
This bug is very misleading in Py3, as the TypeError makes one think that a string is being passed rather than bytes (how else do you get a 2 argument function call wrong?). Very difficult to determine that this is not in fact the bug in a dynamically typed language :P
msg131288 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2011-03-17 21:46
New changeset c61b72b0650d by Antoine Pitrou in branch '3.1':
Issue #5421: Fix misleading error message when one of socket.sendto()'s
http://hg.python.org/cpython/rev/c61b72b0650d

New changeset 2af7a6d765fd by Antoine Pitrou in branch '3.2':
Issue #5421: merge fix
http://hg.python.org/cpython/rev/2af7a6d765fd

New changeset 90cdc371a3b8 by Antoine Pitrou in branch 'default':
Issue #5421: merge fix
http://hg.python.org/cpython/rev/90cdc371a3b8
msg131289 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2011-03-17 21:47
Thank you for the patch! This is now fixed in 3.x.
msg135478 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2011-05-07 16:51
New changeset 9222c9d747c1 by Ezio Melotti in branch '3.1':
#5421: add tests.
http://hg.python.org/cpython/rev/9222c9d747c1

New changeset 4b3352b49483 by Ezio Melotti in branch '3.2':
#5421: merge with 3.1.
http://hg.python.org/cpython/rev/4b3352b49483

New changeset 20273f2195ba by Ezio Melotti in branch 'default':
#5421: merge with 3.2.
http://hg.python.org/cpython/rev/20273f2195ba
msg135482 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2011-05-07 16:55
I backported the patch to 2.7 in 7c3a20b5943a, and added tests to the 3.x branches.
History
Date User Action Args
2022-04-11 14:56:46adminsetgithub: 49671
2011-05-07 16:55:35ezio.melottisetnosy: + ezio.melotti
messages: + msg135482
2011-05-07 16:51:57python-devsetmessages: + msg135478
2011-03-17 21:47:59pitrousetstatus: open -> closed

versions: + Python 3.2, Python 3.3
nosy: + pitrou

messages: + msg131289
resolution: fixed
stage: needs patch -> resolved
2011-03-17 21:46:56python-devsetnosy: + python-dev
messages: + msg131288
2011-03-17 21:27:54anacrolixsetnosy: helduel, lukknapen, anacrolix, nvetoshkin
messages: + msg131284
2011-03-17 19:32:54nvetoshkinsetnosy: helduel, lukknapen, anacrolix, nvetoshkin
messages: + msg131279
2011-02-25 18:14:25anacrolixsetnosy: + anacrolix
2010-11-18 22:24:40nvetoshkinsetfiles: + issue5421.diff

nosy: + nvetoshkin
messages: + msg121496

keywords: + patch
2009-03-06 00:01:49lukknapensetnosy: + lukknapen
messages: + msg83228
versions: + Python 3.1, - Python 2.6, Python 2.5, Python 2.4, Python 3.0
2009-03-05 21:31:16benjamin.petersonsettype: behavior
stage: needs patch
2009-03-05 10:15:02helduelcreate