Commit 8a392d73 authored by Guido van Rossum's avatar Guido van Rossum

Convert the socket module to insist on bytes for input, and to return bytes

(not bytearray) on output.  Discovered a bunch of places that were still
depending on it accepting text strings.
parent b0834005
...@@ -464,7 +464,8 @@ class SimpleXMLRPCRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): ...@@ -464,7 +464,8 @@ class SimpleXMLRPCRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
self.end_headers() self.end_headers()
else: else:
# got a valid XML RPC response # Got a valid XML RPC response; convert to bytes first
response = response.encode("utf-8")
self.send_response(200) self.send_response(200)
self.send_header("Content-type", "text/xml") self.send_header("Content-type", "text/xml")
self.send_header("Content-length", str(len(response))) self.send_header("Content-length", str(len(response)))
......
...@@ -295,12 +295,14 @@ class SMTP: ...@@ -295,12 +295,14 @@ class SMTP:
if self.debuglevel > 0: print("connect:", msg, file=stderr) if self.debuglevel > 0: print("connect:", msg, file=stderr)
return (code, msg) return (code, msg)
def send(self, str): def send(self, s):
"""Send `str' to the server.""" """Send `s' to the server."""
if self.debuglevel > 0: print('send:', repr(str), file=stderr) if self.debuglevel > 0: print('send:', repr(s), file=stderr)
if self.sock: if self.sock:
if isinstance(s, str):
s = s.encode("ascii")
try: try:
self.sock.sendall(str) self.sock.sendall(s)
except socket.error: except socket.error:
self.close() self.close()
raise SMTPServerDisconnected('Server not connected') raise SMTPServerDisconnected('Server not connected')
......
...@@ -18,7 +18,7 @@ def server(evt, ready): ...@@ -18,7 +18,7 @@ def server(evt, ready):
except socket.timeout: except socket.timeout:
pass pass
else: else:
conn.send("1 Hola mundo\n") conn.send(b"1 Hola mundo\n")
conn.close() conn.close()
finally: finally:
serv.close() serv.close()
......
...@@ -19,7 +19,7 @@ def server(ready, evt): ...@@ -19,7 +19,7 @@ def server(ready, evt):
except socket.timeout: except socket.timeout:
pass pass
else: else:
conn.send("+ Hola mundo\n") conn.send(b"+ Hola mundo\n")
conn.close() conn.close()
finally: finally:
serv.close() serv.close()
......
...@@ -50,7 +50,7 @@ class GeneralTests(TestCase): ...@@ -50,7 +50,7 @@ class GeneralTests(TestCase):
def setUp(self): def setUp(self):
self.evt = threading.Event() self.evt = threading.Event()
servargs = (self.evt, "220 Hola mundo\n") servargs = (self.evt, b"220 Hola mundo\n")
threading.Thread(target=server, args=servargs).start() threading.Thread(target=server, args=servargs).start()
# wait until server thread has assigned a port number # wait until server thread has assigned a port number
......
...@@ -498,7 +498,7 @@ class GeneralModuleTests(unittest.TestCase): ...@@ -498,7 +498,7 @@ class GeneralModuleTests(unittest.TestCase):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(1) sock.settimeout(1)
sock.close() sock.close()
self.assertRaises(socket.error, sock.send, "spam") self.assertRaises(socket.error, sock.send, b"spam")
def testNewAttributes(self): def testNewAttributes(self):
# testing .family, .type and .protocol # testing .family, .type and .protocol
......
...@@ -139,7 +139,7 @@ class DigestAuthHandler: ...@@ -139,7 +139,7 @@ class DigestAuthHandler:
# not. # not.
#request_handler.send_header('Connection', 'close') #request_handler.send_header('Connection', 'close')
request_handler.end_headers() request_handler.end_headers()
request_handler.wfile.write("Proxy Authentication Required.") request_handler.wfile.write(b"Proxy Authentication Required.")
return False return False
def handle_request(self, request_handler): def handle_request(self, request_handler):
...@@ -210,9 +210,10 @@ class FakeProxyHandler(BaseHTTPServer.BaseHTTPRequestHandler): ...@@ -210,9 +210,10 @@ class FakeProxyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
self.send_response(200, "OK") self.send_response(200, "OK")
self.send_header("Content-Type", "text/html") self.send_header("Content-Type", "text/html")
self.end_headers() self.end_headers()
self.wfile.write("You've reached %s!<BR>" % self.path) self.wfile.write(bytes("You've reached %s!<BR>" % self.path,
self.wfile.write("Our apologies, but our server is down due to " "ascii"))
"a sudden zombie invasion.") self.wfile.write(b"Our apologies, but our server is down due to "
b"a sudden zombie invasion.")
# Test cases # Test cases
......
...@@ -947,7 +947,7 @@ makesockaddr(int sockfd, struct sockaddr *addr, int addrlen, int proto) ...@@ -947,7 +947,7 @@ makesockaddr(int sockfd, struct sockaddr *addr, int addrlen, int proto)
#ifdef linux #ifdef linux
if (a->sun_path[0] == 0) { /* Linux abstract namespace */ if (a->sun_path[0] == 0) { /* Linux abstract namespace */
addrlen -= (sizeof(*a) - sizeof(a->sun_path)); addrlen -= (sizeof(*a) - sizeof(a->sun_path));
return PyBytes_FromStringAndSize(a->sun_path, addrlen); return PyString_FromStringAndSize(a->sun_path, addrlen);
} }
else else
#endif /* linux */ #endif /* linux */
...@@ -1273,12 +1273,12 @@ getsockaddrarg(PySocketSockObject *s, PyObject *args, ...@@ -1273,12 +1273,12 @@ getsockaddrarg(PySocketSockObject *s, PyObject *args,
addr = (struct sockaddr_sco *)addr_ret; addr = (struct sockaddr_sco *)addr_ret;
_BT_SCO_MEMB(addr, family) = AF_BLUETOOTH; _BT_SCO_MEMB(addr, family) = AF_BLUETOOTH;
if (!PyBytes_Check(args)) { if (!PyString_Check(args)) {
PyErr_SetString(socket_error, "getsockaddrarg: " PyErr_SetString(socket_error, "getsockaddrarg: "
"wrong format"); "wrong format");
return 0; return 0;
} }
straddr = PyBytes_AS_STRING(args); straddr = PyString_AS_STRING(args);
if (setbdaddr(straddr, &_BT_SCO_MEMB(addr, bdaddr)) < 0) if (setbdaddr(straddr, &_BT_SCO_MEMB(addr, bdaddr)) < 0)
return 0; return 0;
...@@ -1313,7 +1313,7 @@ getsockaddrarg(PySocketSockObject *s, PyObject *args, ...@@ -1313,7 +1313,7 @@ getsockaddrarg(PySocketSockObject *s, PyObject *args,
Py_Type(args)->tp_name); Py_Type(args)->tp_name);
return 0; return 0;
} }
if (!PyArg_ParseTuple(args, "si|iis#", &interfaceName, if (!PyArg_ParseTuple(args, "si|iiy#", &interfaceName,
&protoNumber, &pkttype, &hatype, &protoNumber, &pkttype, &hatype,
&haddr, &halen)) &haddr, &halen))
return 0; return 0;
...@@ -1606,7 +1606,7 @@ sock_setsockopt(PySocketSockObject *s, PyObject *args) ...@@ -1606,7 +1606,7 @@ sock_setsockopt(PySocketSockObject *s, PyObject *args)
} }
else { else {
PyErr_Clear(); PyErr_Clear();
if (!PyArg_ParseTuple(args, "iis#:setsockopt", if (!PyArg_ParseTuple(args, "iiy#:setsockopt",
&level, &optname, &buf, &buflen)) &level, &optname, &buf, &buflen))
return NULL; return NULL;
} }
...@@ -1662,19 +1662,16 @@ sock_getsockopt(PySocketSockObject *s, PyObject *args) ...@@ -1662,19 +1662,16 @@ sock_getsockopt(PySocketSockObject *s, PyObject *args)
"getsockopt buflen out of range"); "getsockopt buflen out of range");
return NULL; return NULL;
} }
buf = PyBytes_FromStringAndSize((char *)NULL, buflen); buf = PyString_FromStringAndSize((char *)NULL, buflen);
if (buf == NULL) if (buf == NULL)
return NULL; return NULL;
res = getsockopt(s->sock_fd, level, optname, res = getsockopt(s->sock_fd, level, optname,
(void *)PyBytes_AS_STRING(buf), &buflen); (void *)PyString_AS_STRING(buf), &buflen);
if (res < 0) { if (res < 0) {
Py_DECREF(buf); Py_DECREF(buf);
return s->errorhandler(); return s->errorhandler();
} }
if (PyBytes_Resize(buf, buflen) < 0) { _PyString_Resize(&buf, buflen);
Py_DECREF(buf);
return NULL;
}
return buf; return buf;
} }
...@@ -2097,12 +2094,12 @@ sock_recv(PySocketSockObject *s, PyObject *args) ...@@ -2097,12 +2094,12 @@ sock_recv(PySocketSockObject *s, PyObject *args)
} }
/* Allocate a new string. */ /* Allocate a new string. */
buf = PyBytes_FromStringAndSize((char *) 0, recvlen); buf = PyString_FromStringAndSize((char *) 0, recvlen);
if (buf == NULL) if (buf == NULL)
return NULL; return NULL;
/* Call the guts */ /* Call the guts */
outlen = sock_recv_guts(s, PyBytes_AS_STRING(buf), recvlen, flags); outlen = sock_recv_guts(s, PyString_AS_STRING(buf), recvlen, flags);
if (outlen < 0) { if (outlen < 0) {
/* An error occurred, release the string and return an /* An error occurred, release the string and return an
error. */ error. */
...@@ -2112,9 +2109,7 @@ sock_recv(PySocketSockObject *s, PyObject *args) ...@@ -2112,9 +2109,7 @@ sock_recv(PySocketSockObject *s, PyObject *args)
if (outlen != recvlen) { if (outlen != recvlen) {
/* We did not read as many bytes as we anticipated, resize the /* We did not read as many bytes as we anticipated, resize the
string if possible and be successful. */ string if possible and be successful. */
if (PyBytes_Resize(buf, outlen) < 0) _PyString_Resize(&buf, outlen);
/* Oopsy, not so successful after all. */
return NULL;
} }
return buf; return buf;
...@@ -2270,11 +2265,11 @@ sock_recvfrom(PySocketSockObject *s, PyObject *args) ...@@ -2270,11 +2265,11 @@ sock_recvfrom(PySocketSockObject *s, PyObject *args)
return NULL; return NULL;
} }
buf = PyBytes_FromStringAndSize((char *) 0, recvlen); buf = PyString_FromStringAndSize((char *) 0, recvlen);
if (buf == NULL) if (buf == NULL)
return NULL; return NULL;
outlen = sock_recvfrom_guts(s, PyBytes_AS_STRING(buf), outlen = sock_recvfrom_guts(s, PyString_AS_STRING(buf),
recvlen, flags, &addr); recvlen, flags, &addr);
if (outlen < 0) { if (outlen < 0) {
goto finally; goto finally;
...@@ -2283,7 +2278,7 @@ sock_recvfrom(PySocketSockObject *s, PyObject *args) ...@@ -2283,7 +2278,7 @@ sock_recvfrom(PySocketSockObject *s, PyObject *args)
if (outlen != recvlen) { if (outlen != recvlen) {
/* We did not read as many bytes as we anticipated, resize the /* We did not read as many bytes as we anticipated, resize the
string if possible and be succesful. */ string if possible and be succesful. */
if (PyBytes_Resize(buf, outlen) < 0) if (_PyString_Resize(&buf, outlen) < 0)
/* Oopsy, not so succesful after all. */ /* Oopsy, not so succesful after all. */
goto finally; goto finally;
} }
...@@ -2358,7 +2353,7 @@ sock_send(PySocketSockObject *s, PyObject *args) ...@@ -2358,7 +2353,7 @@ sock_send(PySocketSockObject *s, PyObject *args)
char *buf; char *buf;
int len, n = -1, flags = 0, timeout; int len, n = -1, flags = 0, timeout;
if (!PyArg_ParseTuple(args, "s#|i:send", &buf, &len, &flags)) if (!PyArg_ParseTuple(args, "y#|i:send", &buf, &len, &flags))
return NULL; return NULL;
if (!IS_SELECTABLE(s)) if (!IS_SELECTABLE(s))
...@@ -2399,7 +2394,7 @@ sock_sendall(PySocketSockObject *s, PyObject *args) ...@@ -2399,7 +2394,7 @@ sock_sendall(PySocketSockObject *s, PyObject *args)
char *buf; char *buf;
int len, n = -1, flags = 0, timeout; int len, n = -1, flags = 0, timeout;
if (!PyArg_ParseTuple(args, "s#|i:sendall", &buf, &len, &flags)) if (!PyArg_ParseTuple(args, "y#|i:sendall", &buf, &len, &flags))
return NULL; return NULL;
if (!IS_SELECTABLE(s)) if (!IS_SELECTABLE(s))
...@@ -2454,9 +2449,9 @@ sock_sendto(PySocketSockObject *s, PyObject *args) ...@@ -2454,9 +2449,9 @@ sock_sendto(PySocketSockObject *s, PyObject *args)
int addrlen, len, n = -1, flags, timeout; int addrlen, len, n = -1, flags, timeout;
flags = 0; flags = 0;
if (!PyArg_ParseTuple(args, "s#O:sendto", &buf, &len, &addro)) { if (!PyArg_ParseTuple(args, "y#O:sendto", &buf, &len, &addro)) {
PyErr_Clear(); PyErr_Clear();
if (!PyArg_ParseTuple(args, "s#iO:sendto", if (!PyArg_ParseTuple(args, "y#iO:sendto",
&buf, &len, &flags, &addro)) &buf, &len, &flags, &addro))
return NULL; return NULL;
} }
...@@ -3382,7 +3377,7 @@ socket_inet_aton(PyObject *self, PyObject *args) ...@@ -3382,7 +3377,7 @@ socket_inet_aton(PyObject *self, PyObject *args)
if (inet_aton != NULL) { if (inet_aton != NULL) {
#endif #endif
if (inet_aton(ip_addr, &buf)) if (inet_aton(ip_addr, &buf))
return PyBytes_FromStringAndSize((char *)(&buf), return PyString_FromStringAndSize((char *)(&buf),
sizeof(buf)); sizeof(buf));
PyErr_SetString(socket_error, PyErr_SetString(socket_error,
...@@ -3411,7 +3406,7 @@ socket_inet_aton(PyObject *self, PyObject *args) ...@@ -3411,7 +3406,7 @@ socket_inet_aton(PyObject *self, PyObject *args)
return NULL; return NULL;
} }
} }
return PyBytes_FromStringAndSize((char *) &packed_addr, return PyString_FromStringAndSize((char *) &packed_addr,
sizeof(packed_addr)); sizeof(packed_addr));
#ifdef USE_INET_ATON_WEAKLINK #ifdef USE_INET_ATON_WEAKLINK
...@@ -3488,11 +3483,11 @@ socket_inet_pton(PyObject *self, PyObject *args) ...@@ -3488,11 +3483,11 @@ socket_inet_pton(PyObject *self, PyObject *args)
"illegal IP address string passed to inet_pton"); "illegal IP address string passed to inet_pton");
return NULL; return NULL;
} else if (af == AF_INET) { } else if (af == AF_INET) {
return PyBytes_FromStringAndSize(packed, return PyString_FromStringAndSize(packed,
sizeof(struct in_addr)); sizeof(struct in_addr));
#ifdef ENABLE_IPV6 #ifdef ENABLE_IPV6
} else if (af == AF_INET6) { } else if (af == AF_INET6) {
return PyBytes_FromStringAndSize(packed, return PyString_FromStringAndSize(packed,
sizeof(struct in6_addr)); sizeof(struct in6_addr));
#endif #endif
} else { } else {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment