diff -r 753955932f64 -r a24b02d77666 Doc/library/socket.rst --- a/Doc/library/socket.rst Sat Jan 08 04:35:36 2011 +0100 +++ b/Doc/library/socket.rst Sat Jan 08 09:54:44 2011 +0200 @@ -521,6 +521,15 @@ meanings. +.. function:: sethostname(name) + + Sets the machine's hostname to *name*. + + Availability: Unix. + + .. versionadded:: 3.3 + + .. data:: SocketType This is a Python type object that represents the socket object type. It is the diff -r 753955932f64 -r a24b02d77666 Lib/test/test_socket.py --- a/Lib/test/test_socket.py Sat Jan 08 04:35:36 2011 +0100 +++ b/Lib/test/test_socket.py Sat Jan 08 09:54:44 2011 +0200 @@ -325,6 +325,19 @@ if not fqhn in all_host_names: self.fail("Error testing host resolution mechanisms. (fqdn: %s, all: %s)" % (fqhn, repr(all_host_names))) + @unittest.skipUnless(hasattr(socket, 'sethostname'), "test needs socket.sethostname()") + @unittest.skipUnless(hasattr(socket, 'gethostname'), "test needs socket.gethostname()") + def test_sethostname(self): + oldhn = socket.gethostname() + try: + socket.sethostname('new') + except socket.error as e: + self.assertEqual(e.errno, errno.EPERM) + else: + # running test as root! + self.assertEqual(socket.gethostname(), 'new') + socket.sethostname(oldhn) + def testRefCountGetNameInfo(self): # Testing reference count for getnameinfo if hasattr(sys, "getrefcount"): diff -r 753955932f64 -r a24b02d77666 Modules/socketmodule.c --- a/Modules/socketmodule.c Sat Jan 08 04:35:36 2011 +0100 +++ b/Modules/socketmodule.c Sat Jan 08 09:54:44 2011 +0200 @@ -3135,6 +3135,47 @@ \n\ Return the current host name."); +#ifdef HAVE_SETHOSTNAME +PyDoc_STRVAR(sethostname_doc, +"sethostname(name)\n\n\ +Sets the hostname to name."); + +static PyObject * +socket_sethostname(PyObject *self, PyObject *args) +{ + PyObject *hnobj; + Py_buffer buf; + int res; + + if (PyTuple_Size(args) != 1) { + PyErr_SetString(PyExc_TypeError, + "sethostname() takes exactly 1 argument"); + return NULL; + } + hnobj = PyTuple_GetItem(args, 0); + if (hnobj == NULL) + return NULL; + /* PyUnicode_FSConverter chokes on embedded NULs so don't use if we + already have a bytes object. */ + if (PyBytes_Check(hnobj)) { + Py_INCREF(hnobj); + } + else { + if (!PyArg_ParseTuple(args, "O&:sethostname", + PyUnicode_FSConverter, &hnobj)) + return NULL; + } + PyObject_GetBuffer(hnobj, &buf, PyBUF_SIMPLE); + + res = sethostname(buf.buf, buf.len); + + PyBuffer_Release(&buf); + Py_DECREF(hnobj); + if (res == -1) + return set_error(); + Py_RETURN_NONE; +} +#endif /* Python interface to gethostbyname(name). */ @@ -4233,6 +4274,10 @@ METH_VARARGS, gethostbyaddr_doc}, {"gethostname", socket_gethostname, METH_NOARGS, gethostname_doc}, +#ifdef HAVE_SETHOSTNAME + {"sethostname", socket_sethostname, + METH_VARARGS, sethostname_doc}, +#endif {"getservbyname", socket_getservbyname, METH_VARARGS, getservbyname_doc}, {"getservbyport", socket_getservbyport, diff -r 753955932f64 -r a24b02d77666 configure.in --- a/configure.in Sat Jan 08 04:35:36 2011 +0100 +++ b/configure.in Sat Jan 08 09:54:44 2011 +0200 @@ -2542,7 +2542,7 @@ mremap nice pathconf pause plock poll pthread_init \ putenv readlink realpath \ select sem_open sem_timedwait sem_getvalue sem_unlink setegid seteuid \ - setgid \ + setgid sethostname \ setlocale setregid setreuid setresuid setresgid setsid setpgid setpgrp setuid setvbuf \ sigaction siginterrupt sigrelse snprintf strftime strlcpy \ sysconf tcgetpgrp tcsetpgrp tempnam timegm times tmpfile tmpnam tmpnam_r \