summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2011-02-28 22:25:22 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2011-02-28 22:25:22 (GMT)
commit061cfb5258f27c895a070502d4f83a8c1a394561 (patch)
tree8c44a58404bd44a248eb2b4405c93e0faf75e834 /Modules
parent8d0f257211186de3475dd996c3a32772d2e34456 (diff)
downloadcpython-061cfb5258f27c895a070502d4f83a8c1a394561.zip
cpython-061cfb5258f27c895a070502d4f83a8c1a394561.tar.gz
cpython-061cfb5258f27c895a070502d4f83a8c1a394561.tar.bz2
Issue #10866: Add socket.sethostname(). Initial patch by Ross Lagerwall.
Diffstat (limited to 'Modules')
-rw-r--r--Modules/socketmodule.c35
1 files changed, 35 insertions, 0 deletions
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c
index 563395a..687d964 100644
--- a/Modules/socketmodule.c
+++ b/Modules/socketmodule.c
@@ -3135,6 +3135,37 @@ PyDoc_STRVAR(gethostname_doc,
\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, flag = 0;
+
+ if (!PyArg_ParseTuple(args, "S:sethostname", &hnobj)) {
+ PyErr_Clear();
+ if (!PyArg_ParseTuple(args, "O&:sethostname",
+ PyUnicode_FSConverter, &hnobj))
+ return NULL;
+ flag = 1;
+ }
+ res = PyObject_GetBuffer(hnobj, &buf, PyBUF_SIMPLE);
+ if (!res) {
+ res = sethostname(buf.buf, buf.len);
+ PyBuffer_Release(&buf);
+ }
+ if (flag)
+ Py_DECREF(hnobj);
+ if (res)
+ return set_error();
+ Py_RETURN_NONE;
+}
+#endif
/* Python interface to gethostbyname(name). */
@@ -4233,6 +4264,10 @@ static PyMethodDef socket_methods[] = {
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,