Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8a392d7

Browse files
committed
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.
1 parent b083400 commit 8a392d7

8 files changed

Lines changed: 42 additions & 43 deletions

File tree

Lib/SimpleXMLRPCServer.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,8 @@ def do_POST(self):
464464

465465
self.end_headers()
466466
else:
467-
# got a valid XML RPC response
467+
# Got a valid XML RPC response; convert to bytes first
468+
response = response.encode("utf-8")
468469
self.send_response(200)
469470
self.send_header("Content-type", "text/xml")
470471
self.send_header("Content-length", str(len(response)))

Lib/smtplib.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -295,12 +295,14 @@ def connect(self, host='localhost', port = 0):
295295
if self.debuglevel > 0: print("connect:", msg, file=stderr)
296296
return (code, msg)
297297

298-
def send(self, str):
299-
"""Send `str' to the server."""
300-
if self.debuglevel > 0: print('send:', repr(str), file=stderr)
298+
def send(self, s):
299+
"""Send `s' to the server."""
300+
if self.debuglevel > 0: print('send:', repr(s), file=stderr)
301301
if self.sock:
302+
if isinstance(s, str):
303+
s = s.encode("ascii")
302304
try:
303-
self.sock.sendall(str)
305+
self.sock.sendall(s)
304306
except socket.error:
305307
self.close()
306308
raise SMTPServerDisconnected('Server not connected')

Lib/test/test_ftplib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def server(evt, ready):
1818
except socket.timeout:
1919
pass
2020
else:
21-
conn.send("1 Hola mundo\n")
21+
conn.send(b"1 Hola mundo\n")
2222
conn.close()
2323
finally:
2424
serv.close()

Lib/test/test_poplib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def server(ready, evt):
1919
except socket.timeout:
2020
pass
2121
else:
22-
conn.send("+ Hola mundo\n")
22+
conn.send(b"+ Hola mundo\n")
2323
conn.close()
2424
finally:
2525
serv.close()

Lib/test/test_smtplib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class GeneralTests(TestCase):
5050

5151
def setUp(self):
5252
self.evt = threading.Event()
53-
servargs = (self.evt, "220 Hola mundo\n")
53+
servargs = (self.evt, b"220 Hola mundo\n")
5454
threading.Thread(target=server, args=servargs).start()
5555

5656
# wait until server thread has assigned a port number

Lib/test/test_socket.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ def testSendAfterClose(self):
498498
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
499499
sock.settimeout(1)
500500
sock.close()
501-
self.assertRaises(socket.error, sock.send, "spam")
501+
self.assertRaises(socket.error, sock.send, b"spam")
502502

503503
def testNewAttributes(self):
504504
# testing .family, .type and .protocol

Lib/test/test_urllib2_localnet.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def _return_auth_challenge(self, request_handler):
139139
# not.
140140
#request_handler.send_header('Connection', 'close')
141141
request_handler.end_headers()
142-
request_handler.wfile.write("Proxy Authentication Required.")
142+
request_handler.wfile.write(b"Proxy Authentication Required.")
143143
return False
144144

145145
def handle_request(self, request_handler):
@@ -210,9 +210,10 @@ def do_GET(self):
210210
self.send_response(200, "OK")
211211
self.send_header("Content-Type", "text/html")
212212
self.end_headers()
213-
self.wfile.write("You've reached %s!<BR>" % self.path)
214-
self.wfile.write("Our apologies, but our server is down due to "
215-
"a sudden zombie invasion.")
213+
self.wfile.write(bytes("You've reached %s!<BR>" % self.path,
214+
"ascii"))
215+
self.wfile.write(b"Our apologies, but our server is down due to "
216+
b"a sudden zombie invasion.")
216217

217218
# Test cases
218219

Modules/socketmodule.c

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,7 @@ makesockaddr(int sockfd, struct sockaddr *addr, int addrlen, int proto)
947947
#ifdef linux
948948
if (a->sun_path[0] == 0) { /* Linux abstract namespace */
949949
addrlen -= (sizeof(*a) - sizeof(a->sun_path));
950-
return PyBytes_FromStringAndSize(a->sun_path, addrlen);
950+
return PyString_FromStringAndSize(a->sun_path, addrlen);
951951
}
952952
else
953953
#endif /* linux */
@@ -1273,12 +1273,12 @@ getsockaddrarg(PySocketSockObject *s, PyObject *args,
12731273

12741274
addr = (struct sockaddr_sco *)addr_ret;
12751275
_BT_SCO_MEMB(addr, family) = AF_BLUETOOTH;
1276-
if (!PyBytes_Check(args)) {
1276+
if (!PyString_Check(args)) {
12771277
PyErr_SetString(socket_error, "getsockaddrarg: "
12781278
"wrong format");
12791279
return 0;
12801280
}
1281-
straddr = PyBytes_AS_STRING(args);
1281+
straddr = PyString_AS_STRING(args);
12821282
if (setbdaddr(straddr, &_BT_SCO_MEMB(addr, bdaddr)) < 0)
12831283
return 0;
12841284

@@ -1313,7 +1313,7 @@ getsockaddrarg(PySocketSockObject *s, PyObject *args,
13131313
Py_Type(args)->tp_name);
13141314
return 0;
13151315
}
1316-
if (!PyArg_ParseTuple(args, "si|iis#", &interfaceName,
1316+
if (!PyArg_ParseTuple(args, "si|iiy#", &interfaceName,
13171317
&protoNumber, &pkttype, &hatype,
13181318
&haddr, &halen))
13191319
return 0;
@@ -1606,7 +1606,7 @@ sock_setsockopt(PySocketSockObject *s, PyObject *args)
16061606
}
16071607
else {
16081608
PyErr_Clear();
1609-
if (!PyArg_ParseTuple(args, "iis#:setsockopt",
1609+
if (!PyArg_ParseTuple(args, "iiy#:setsockopt",
16101610
&level, &optname, &buf, &buflen))
16111611
return NULL;
16121612
}
@@ -1662,19 +1662,16 @@ sock_getsockopt(PySocketSockObject *s, PyObject *args)
16621662
"getsockopt buflen out of range");
16631663
return NULL;
16641664
}
1665-
buf = PyBytes_FromStringAndSize((char *)NULL, buflen);
1665+
buf = PyString_FromStringAndSize((char *)NULL, buflen);
16661666
if (buf == NULL)
16671667
return NULL;
16681668
res = getsockopt(s->sock_fd, level, optname,
1669-
(void *)PyBytes_AS_STRING(buf), &buflen);
1669+
(void *)PyString_AS_STRING(buf), &buflen);
16701670
if (res < 0) {
16711671
Py_DECREF(buf);
16721672
return s->errorhandler();
16731673
}
1674-
if (PyBytes_Resize(buf, buflen) < 0) {
1675-
Py_DECREF(buf);
1676-
return NULL;
1677-
}
1674+
_PyString_Resize(&buf, buflen);
16781675
return buf;
16791676
}
16801677

@@ -2097,12 +2094,12 @@ sock_recv(PySocketSockObject *s, PyObject *args)
20972094
}
20982095

20992096
/* Allocate a new string. */
2100-
buf = PyBytes_FromStringAndSize((char *) 0, recvlen);
2097+
buf = PyString_FromStringAndSize((char *) 0, recvlen);
21012098
if (buf == NULL)
21022099
return NULL;
21032100

21042101
/* Call the guts */
2105-
outlen = sock_recv_guts(s, PyBytes_AS_STRING(buf), recvlen, flags);
2102+
outlen = sock_recv_guts(s, PyString_AS_STRING(buf), recvlen, flags);
21062103
if (outlen < 0) {
21072104
/* An error occurred, release the string and return an
21082105
error. */
@@ -2112,9 +2109,7 @@ sock_recv(PySocketSockObject *s, PyObject *args)
21122109
if (outlen != recvlen) {
21132110
/* We did not read as many bytes as we anticipated, resize the
21142111
string if possible and be successful. */
2115-
if (PyBytes_Resize(buf, outlen) < 0)
2116-
/* Oopsy, not so successful after all. */
2117-
return NULL;
2112+
_PyString_Resize(&buf, outlen);
21182113
}
21192114

21202115
return buf;
@@ -2270,11 +2265,11 @@ sock_recvfrom(PySocketSockObject *s, PyObject *args)
22702265
return NULL;
22712266
}
22722267

2273-
buf = PyBytes_FromStringAndSize((char *) 0, recvlen);
2268+
buf = PyString_FromStringAndSize((char *) 0, recvlen);
22742269
if (buf == NULL)
22752270
return NULL;
22762271

2277-
outlen = sock_recvfrom_guts(s, PyBytes_AS_STRING(buf),
2272+
outlen = sock_recvfrom_guts(s, PyString_AS_STRING(buf),
22782273
recvlen, flags, &addr);
22792274
if (outlen < 0) {
22802275
goto finally;
@@ -2283,7 +2278,7 @@ sock_recvfrom(PySocketSockObject *s, PyObject *args)
22832278
if (outlen != recvlen) {
22842279
/* We did not read as many bytes as we anticipated, resize the
22852280
string if possible and be succesful. */
2286-
if (PyBytes_Resize(buf, outlen) < 0)
2281+
if (_PyString_Resize(&buf, outlen) < 0)
22872282
/* Oopsy, not so succesful after all. */
22882283
goto finally;
22892284
}
@@ -2358,7 +2353,7 @@ sock_send(PySocketSockObject *s, PyObject *args)
23582353
char *buf;
23592354
int len, n = -1, flags = 0, timeout;
23602355

2361-
if (!PyArg_ParseTuple(args, "s#|i:send", &buf, &len, &flags))
2356+
if (!PyArg_ParseTuple(args, "y#|i:send", &buf, &len, &flags))
23622357
return NULL;
23632358

23642359
if (!IS_SELECTABLE(s))
@@ -2399,7 +2394,7 @@ sock_sendall(PySocketSockObject *s, PyObject *args)
23992394
char *buf;
24002395
int len, n = -1, flags = 0, timeout;
24012396

2402-
if (!PyArg_ParseTuple(args, "s#|i:sendall", &buf, &len, &flags))
2397+
if (!PyArg_ParseTuple(args, "y#|i:sendall", &buf, &len, &flags))
24032398
return NULL;
24042399

24052400
if (!IS_SELECTABLE(s))
@@ -2454,9 +2449,9 @@ sock_sendto(PySocketSockObject *s, PyObject *args)
24542449
int addrlen, len, n = -1, flags, timeout;
24552450

24562451
flags = 0;
2457-
if (!PyArg_ParseTuple(args, "s#O:sendto", &buf, &len, &addro)) {
2452+
if (!PyArg_ParseTuple(args, "y#O:sendto", &buf, &len, &addro)) {
24582453
PyErr_Clear();
2459-
if (!PyArg_ParseTuple(args, "s#iO:sendto",
2454+
if (!PyArg_ParseTuple(args, "y#iO:sendto",
24602455
&buf, &len, &flags, &addro))
24612456
return NULL;
24622457
}
@@ -3382,7 +3377,7 @@ socket_inet_aton(PyObject *self, PyObject *args)
33823377
if (inet_aton != NULL) {
33833378
#endif
33843379
if (inet_aton(ip_addr, &buf))
3385-
return PyBytes_FromStringAndSize((char *)(&buf),
3380+
return PyString_FromStringAndSize((char *)(&buf),
33863381
sizeof(buf));
33873382

33883383
PyErr_SetString(socket_error,
@@ -3411,8 +3406,8 @@ socket_inet_aton(PyObject *self, PyObject *args)
34113406
return NULL;
34123407
}
34133408
}
3414-
return PyBytes_FromStringAndSize((char *) &packed_addr,
3415-
sizeof(packed_addr));
3409+
return PyString_FromStringAndSize((char *) &packed_addr,
3410+
sizeof(packed_addr));
34163411

34173412
#ifdef USE_INET_ATON_WEAKLINK
34183413
}
@@ -3488,12 +3483,12 @@ socket_inet_pton(PyObject *self, PyObject *args)
34883483
"illegal IP address string passed to inet_pton");
34893484
return NULL;
34903485
} else if (af == AF_INET) {
3491-
return PyBytes_FromStringAndSize(packed,
3492-
sizeof(struct in_addr));
3486+
return PyString_FromStringAndSize(packed,
3487+
sizeof(struct in_addr));
34933488
#ifdef ENABLE_IPV6
34943489
} else if (af == AF_INET6) {
3495-
return PyBytes_FromStringAndSize(packed,
3496-
sizeof(struct in6_addr));
3490+
return PyString_FromStringAndSize(packed,
3491+
sizeof(struct in6_addr));
34973492
#endif
34983493
} else {
34993494
PyErr_SetString(socket_error, "unknown address family");

0 commit comments

Comments
 (0)