diff options
Diffstat (limited to 'include_server/c_extensions/distcc_pump_c_extensions_module.c')
-rw-r--r-- | include_server/c_extensions/distcc_pump_c_extensions_module.c | 128 |
1 files changed, 81 insertions, 47 deletions
diff --git a/include_server/c_extensions/distcc_pump_c_extensions_module.c b/include_server/c_extensions/distcc_pump_c_extensions_module.c index 549f922..2946df2 100644 --- a/include_server/c_extensions/distcc_pump_c_extensions_module.c +++ b/include_server/c_extensions/distcc_pump_c_extensions_module.c @@ -1,15 +1,15 @@ /* Copyright 2007 Google Inc. - * + * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, @@ -65,11 +65,11 @@ CompressLzo1xAlloc(PyObject *dummy, PyObject *args) { if (in_len < 0) return NULL; if (dcc_compress_lzo1x_alloc(in_buf, in_len, &out_buf, &out_len)) { - PyErr_SetString(distcc_pump_c_extensionsError, + PyErr_SetString(distcc_pump_c_extensionsError, "Couldn't compress that."); return NULL; } - string_object = PyString_FromStringAndSize(out_buf, out_len); + string_object = PyBytes_FromStringAndSize(out_buf, out_len); free(out_buf); return string_object; } @@ -101,7 +101,7 @@ RCwd(PyObject *dummy, PyObject *args) { "Couldn't read token string."); return NULL; } - return PyString_FromString(value_str); + return PyUnicode_FromString(value_str); } @@ -124,11 +124,12 @@ RTokenString(PyObject *dummy, PyObject *args) { if (!PyArg_ParseTuple(args, "is", &ifd, &expect_token)) return NULL; if (dcc_r_token_string(ifd, expect_token, &value_str)) { - PyErr_SetString(distcc_pump_c_extensionsError, + PyErr_SetString(distcc_pump_c_extensionsError, "Couldn't read token string."); return NULL; } - return PyString_FromString(value_str); + + return PyUnicode_FromString(value_str); } @@ -158,7 +159,7 @@ RArgv(PyObject *dummy, PyObject *args) { } if ((list_object = PyList_New(0)) == NULL) goto error; for (; argv[i]; i++) { - string_object = PyString_FromString(argv[i]); + string_object = PyUnicode_FromString(argv[i]); free(argv[i]); if (!string_object) { goto error; @@ -172,7 +173,7 @@ RArgv(PyObject *dummy, PyObject *args) { error: Py_XDECREF(list_object); Py_XDECREF(string_object); - for (i = i + 1; argv[i]; i++) + for (i = i + 1; argv[i]; i++) free(argv[i]); free(argv); return NULL; @@ -209,8 +210,9 @@ XArgv(PyObject *dummy, PyObject *args) { for (i = 0; i < len; i++) { PyObject *string_object; string_object = PyList_GetItem(list_object, i); /* borrowed ref */ - argv[i] = PyString_AsString(string_object); /* does not increase - ref count */ +/* TODO do it properly, catch exceptions for fancy Unicode symbols */ + argv[i] = PyUnicode_AsUTF8(string_object); /* does not increase + ref count */ } ret = dcc_x_argv(ifd, "ARGC", "ARGV", argv); free(argv); @@ -233,7 +235,7 @@ static /* const */ char OsPathExists_doc__[] = " Arguments:\n" " filepath: a string\n" " Returns:\n" -" True or False\n" +" True or False\n" ; static PyObject * @@ -241,20 +243,20 @@ OsPathExists(PyObject *dummy, PyObject *args) { const char *in; int len; int res; - + struct stat buf; - + UNUSED(dummy); if (!PyArg_ParseTuple(args, "s#", &in, &len)) return NULL; if (len < 0) return NULL; - res = stat(in, &buf); + res = stat(in, &buf); if (res == -1) Py_RETURN_FALSE; if (res == 0) Py_RETURN_TRUE; assert(0); return NULL; -} +} /*********************************************************************** OsPathIsFile @@ -267,7 +269,7 @@ static /* const */ char OsPathIsFile_doc__[] = " Arguments:\n" " filename: a string\n" " Returns:\n" -" True or False\n" +" True or False\n" ; static PyObject * @@ -275,20 +277,20 @@ OsPathIsFile(PyObject *dummy, PyObject *args) { const char *in; int len; int res; - + struct stat buf; - + UNUSED(dummy); if (!PyArg_ParseTuple(args, "s#", &in, &len)) return NULL; if (len < 0) return NULL; - res = stat(in, &buf); + res = stat(in, &buf); if (res == -1) Py_RETURN_FALSE; if ((res == 0) && S_ISREG(buf.st_mode)) Py_RETURN_TRUE; if ((res == 0) && !S_ISREG(buf.st_mode)) Py_RETURN_FALSE; return NULL; -} +} @@ -345,61 +347,93 @@ Realpath(PyObject *dummy, PyObject *args) { /* On Solaris this result may be a relative path, if the argument was relative. Fail hard if this happens. */ assert(res[0] == '/'); - result_str = PyString_FromStringAndSize(res, strlen(res)); + result_str = PyUnicode_FromStringAndSize(res, strlen(res)); if (result_str == NULL) return PyErr_NoMemory(); return result_str; - } + } else { - return PyString_FromStringAndSize(in, strlen(in)); + return PyUnicode_FromStringAndSize(in, strlen(in)); } } /*********************************************************************** -Bindings +Bindings; ************************************************************************/ -static PyMethodDef methods[] = { - {"OsPathExists", (PyCFunction)OsPathExists, METH_VARARGS, +struct module_state { + PyObject *error; +}; + +#define GETSTATE(m) ((struct module_state*)PyModule_GetState(m)) + +static const char module_documentation[]= +"Various utilities for distcc-pump.\n" +; + +static int module_traverse(PyObject *m, visitproc visit, void *arg) { + Py_VISIT(GETSTATE(m)->error); + return 0; +} + +static int module_clear(PyObject *m) { + Py_CLEAR(GETSTATE(m)->error); + return 0; +} + +static PyMethodDef module_methods[] = { + {"OsPathExists", (PyCFunction)OsPathExists, METH_VARARGS, OsPathExists_doc__}, - {"OsPathIsFile", (PyCFunction)OsPathIsFile, METH_VARARGS, + {"OsPathIsFile", (PyCFunction)OsPathIsFile, METH_VARARGS, OsPathIsFile_doc__}, {"Realpath", (PyCFunction)Realpath, METH_VARARGS, Realpath_doc__}, - {"RTokenString",(PyCFunction)RTokenString, METH_VARARGS, + {"RTokenString",(PyCFunction)RTokenString, METH_VARARGS, RTokenString_doc__}, {"RCwd", (PyCFunction)RCwd, METH_VARARGS, RCwd_doc__}, {"RArgv", (PyCFunction)RArgv, METH_VARARGS, RArgv_doc__}, {"XArgv", (PyCFunction)XArgv, METH_VARARGS, XArgv_doc__}, - {"CompressLzo1xAlloc", (PyCFunction)CompressLzo1xAlloc, METH_VARARGS, + {"CompressLzo1xAlloc", (PyCFunction)CompressLzo1xAlloc, METH_VARARGS, CompressLzo1xAlloc_doc__}, {NULL, NULL, 0, NULL} }; +static struct PyModuleDef moduledef = { + PyModuleDef_HEAD_INIT, + "distcc_pump_c_extensions", /* m_name */ + module_documentation, /* m_doc */ + sizeof(struct module_state),/* m_size */ + module_methods, /* m_methods */ + NULL, /* m_reload */ + module_traverse, /* m_traverse */ + module_clear, /* m_clear */ + NULL, /* m_free */ +}; -static /* const */ char module_documentation[]= -"Various utilities for distcc-pump.\n" -; - -void initdistcc_pump_c_extensions(void) { - PyObject *module; +PyObject * +PyInit_distcc_pump_c_extensions(void) { + PyObject *module = PyModule_Create(&moduledef); PyObject *py_str; distcc_pump_c_extensionsError = PyErr_NewException( (char *)"distcc_pump_c_extensions.Error", NULL, NULL); - - module = Py_InitModule4("distcc_pump_c_extensions", - methods, - module_documentation, - NULL, - PYTHON_API_VERSION); - - py_str = PyString_FromString("Nils Klarlund"); + + if (module == NULL) + return NULL; + + struct module_state *st = GETSTATE(module); + st->error = distcc_pump_c_extensionsError; + if (st->error == NULL) { + Py_DECREF(module); + return NULL; + } + + py_str = PyUnicode_FromString("Nils Klarlund"); + py_str = PyUnicode_FromString(version); PyModule_AddObject(module, "__author__", py_str); - py_str = PyString_FromString(version); PyModule_AddObject(module, "__version__", py_str); /* Make the exception class accessible */ PyModule_AddObject(module, "Error", distcc_pump_c_extensionsError); - + return module; } |