Skip to content

Commit 97ee4dd

Browse files
committed
Fix some setsockopt error messages
1 parent 2ca926d commit 97ee4dd

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

Lib/test/test_socket.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1560,10 +1560,10 @@ def test_setsockopt_errors(self):
15601560
with self.assertRaisesRegex(TypeError, "socket option should be int, bytes-like object or None"):
15611561
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, dict())
15621562

1563-
with self.assertRaisesRegex(TypeError, "take 4 arguments when socket option is None"):
1563+
with self.assertRaisesRegex(TypeError, "requires 4 arguments when the third argument is None"):
15641564
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, None)
15651565

1566-
with self.assertRaisesRegex(TypeError, "argument 3 must be NoneType"):
1566+
with self.assertRaisesRegex(TypeError, "only takes 4 arguments when the third argument is None"):
15671567
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1, 2)
15681568

15691569
with self.assertRaisesRegex(TypeError, "takes at least 3 arguments"):

Modules/socketmodule.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3349,19 +3349,24 @@ sock_setsockopt(PyObject *self, PyObject *args)
33493349
arglen = PyTuple_Size(args);
33503350
if (arglen == 3 && optval == Py_None) {
33513351
PyErr_Format(PyExc_TypeError,
3352-
"setsockopt() take 4 arguments when socket option is None (%zd given)",
3352+
"setsockopt() requires 4 arguments when the third argument is None",
33533353
arglen);
33543354
return NULL;
33553355
}
33563356
if (arglen == 4 && optval != Py_None) {
33573357
PyErr_Format(PyExc_TypeError,
3358-
"setsockopt() argument 3 must be NoneType, not %T",
3358+
"setsockopt() only takes 4 arguments when the third argument is None (got %T)",
33593359
optval);
33603360
return NULL;
33613361
}
33623362

33633363
#ifdef AF_VSOCK
33643364
if (s->sock_family == AF_VSOCK) {
3365+
if (!PyIndex_Check(optval)) {
3366+
PyErr_Format(PyExc_TypeError,
3367+
"setsockopt() argument 3 for AF_VSOCK must be an int (got %T)",
3368+
optval);
3369+
}
33653370
uint64_t vflag; // Must be set width of 64 bits
33663371
/* setsockopt(level, opt, flag) */
33673372
if (!PyArg_Parse(optval, "K", &vflag)) {

0 commit comments

Comments
 (0)