/
errno.c
137 lines (113 loc) · 4.75 KB
/
errno.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* Borrowed code from Python (Modules/errnomodule.c) */
static void
inscode(PyObject *module_dict, PyObject *other_dict, const char *name, int code)
{
#ifdef PYUV_PYTHON3
PyObject *error_name = PyUnicode_FromString(name);
#else
PyObject *error_name = PyString_FromString(name);
#endif
PyObject *error_code = PyInt_FromLong((long) code);
/* Don't bother checking for errors; they'll be caught at the end
* of the module initialization function by the caller of
* init_errno().
*/
if (error_name && error_code) {
PyDict_SetItem(module_dict, error_name, error_code);
PyDict_SetItem(other_dict, error_code, error_name);
}
Py_XDECREF(error_name);
Py_XDECREF(error_code);
}
static PyObject *
Errno_func_strerror(PyObject *obj, PyObject *args)
{
int errorno;
uv_err_t err;
UNUSED_ARG(obj);
if (!PyArg_ParseTuple(args, "i:strerror", &errorno)) {
return NULL;
}
err.code = errorno;
return PyString_FromString(uv_strerror(err));
}
static PyObject *
Errno_func_ares_strerror(PyObject *obj, PyObject *args)
{
int errorno;
UNUSED_ARG(obj);
if (!PyArg_ParseTuple(args, "i:ares_strerror", &errorno)) {
return NULL;
}
return PyString_FromString(ares_strerror(errorno));
}
static PyMethodDef
Errno_methods[] = {
{ "ares_strerror", (PyCFunction)Errno_func_ares_strerror, METH_VARARGS, "Get string representation of a c-ares error code." },
{ "strerror", (PyCFunction)Errno_func_strerror, METH_VARARGS, "Get string representation of an error code." },
{ NULL }
};
#ifdef PYUV_PYTHON3
static PyModuleDef pyuv_errorno_module = {
PyModuleDef_HEAD_INIT,
"pyuv.errno", /*m_name*/
NULL, /*m_doc*/
-1, /*m_size*/
Errno_methods, /*m_methods*/
};
#endif
PyObject *
init_errno(void)
{
PyObject *module;
PyObject *module_dict;
PyObject *errorcode_dict;
PyObject *ares_errorcode_dict;
#ifdef PYUV_PYTHON3
module = PyModule_Create(&pyuv_errorno_module);
#else
module = Py_InitModule("pyuv.errno", Errno_methods);
#endif
if (module == NULL) {
return NULL;
}
module_dict = PyModule_GetDict(module);
errorcode_dict = PyDict_New();
ares_errorcode_dict = PyDict_New();
if (!module_dict || !errorcode_dict ||
PyDict_SetItemString(module_dict, "errorcode", errorcode_dict) < 0 || PyDict_SetItemString(module_dict, "ares_errorcode", ares_errorcode_dict) < 0) {
return NULL;
}
#define XX(val, name, s) inscode(module_dict, errorcode_dict, __MSTR(UV_##name), UV_##name);
UV_ERRNO_MAP(XX)
#undef XX
/* TODO: also use some preprocessor trickery for c-ares errnos */
inscode(module_dict, ares_errorcode_dict, "ARES_SUCCESS", ARES_SUCCESS);
inscode(module_dict, ares_errorcode_dict, "ARES_ENODATA", ARES_ENODATA);
inscode(module_dict, ares_errorcode_dict, "ARES_EFORMERR", ARES_EFORMERR);
inscode(module_dict, ares_errorcode_dict, "ARES_ESERVFAIL", ARES_ESERVFAIL);
inscode(module_dict, ares_errorcode_dict, "ARES_ENOTFOUND", ARES_ENOTFOUND);
inscode(module_dict, ares_errorcode_dict, "ARES_ENOTIMP", ARES_ENOTIMP);
inscode(module_dict, ares_errorcode_dict, "ARES_EREFUSED", ARES_EREFUSED);
inscode(module_dict, ares_errorcode_dict, "ARES_EBADQUERY", ARES_EBADQUERY);
inscode(module_dict, ares_errorcode_dict, "ARES_EBADNAME", ARES_EBADNAME);
inscode(module_dict, ares_errorcode_dict, "ARES_EBADFAMILY", ARES_EBADFAMILY);
inscode(module_dict, ares_errorcode_dict, "ARES_EBADRESP", ARES_EBADRESP);
inscode(module_dict, ares_errorcode_dict, "ARES_ECONNREFUSED", ARES_ECONNREFUSED);
inscode(module_dict, ares_errorcode_dict, "ARES_ETIMEOUT", ARES_ETIMEOUT);
inscode(module_dict, ares_errorcode_dict, "ARES_EOF", ARES_EOF);
inscode(module_dict, ares_errorcode_dict, "ARES_EFILE", ARES_EFILE);
inscode(module_dict, ares_errorcode_dict, "ARES_ENOMEM", ARES_ENOMEM);
inscode(module_dict, ares_errorcode_dict, "ARES_EDESTRUCTION", ARES_EDESTRUCTION);
inscode(module_dict, ares_errorcode_dict, "ARES_EBADSTR", ARES_EBADSTR);
inscode(module_dict, ares_errorcode_dict, "ARES_EBADFLAGS", ARES_EBADFLAGS);
inscode(module_dict, ares_errorcode_dict, "ARES_ENONAME", ARES_ENONAME);
inscode(module_dict, ares_errorcode_dict, "ARES_EBADHINTS", ARES_EBADHINTS);
inscode(module_dict, ares_errorcode_dict, "ARES_ENOTINITIALIZED", ARES_ENOTINITIALIZED);
inscode(module_dict, ares_errorcode_dict, "ARES_ELOADIPHLPAPI", ARES_ELOADIPHLPAPI);
inscode(module_dict, ares_errorcode_dict, "ARES_EADDRGETNETWORKPARAMS", ARES_EADDRGETNETWORKPARAMS);
inscode(module_dict, ares_errorcode_dict, "ARES_ECANCELLED", ARES_ECANCELLED);
Py_DECREF(errorcode_dict);
Py_DECREF(ares_errorcode_dict);
return module;
}