-
Notifications
You must be signed in to change notification settings - Fork 58
/
demo.c
116 lines (93 loc) · 2.46 KB
/
demo.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
/*
Copyright 2011 David Malcolm <[email protected]>
Copyright 2011 Red Hat, Inc.
This 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 3 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, see
<http://www.gnu.org/licenses/>.
*/
/* Examples of mistakes made using the Python API */
#include <Python.h>
extern uint16_t htons(uint16_t hostshort);
#if PY_MAJOR_VERSION >= 3
#define PYINT_FROMLONG(l) (PyLong_FromLong(l))
#else
#define PYINT_FROMLONG(l) (PyInt_FromLong(l))
#endif
PyObject *
socket_htons(PyObject *self, PyObject *args)
{
unsigned long x1, x2;
if (!PyArg_ParseTuple(args, "i:htons", &x1)) {
return NULL;
}
x2 = (int)htons((short)x1);
return PYINT_FROMLONG(x2);
}
PyObject *
not_enough_varargs(PyObject *self, PyObject *args)
{
if (!PyArg_ParseTuple(args, "i")) {
return NULL;
}
Py_RETURN_NONE;
}
PyObject *
too_many_varargs(PyObject *self, PyObject *args)
{
int i, j;
if (!PyArg_ParseTuple(args, "i", &i, &j)) {
return NULL;
}
Py_RETURN_NONE;
}
PyObject *
kwargs_example(PyObject *self, PyObject *args, PyObject *kwargs)
{
double x, y;
char *keywords[] = {"x", "y"};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "(ff):kwargs_example", keywords, &x, &y)) {
return NULL;
}
Py_RETURN_NONE;
}
extern int convert_to_ssize(PyObject *, Py_ssize_t *);
PyObject *
buggy_converter(PyObject *self, PyObject *args)
{
int i;
if (!PyArg_ParseTuple(args, "O&", convert_to_ssize, &i)) {
return NULL;
}
Py_RETURN_NONE;
}
PyObject *
make_a_list_of_random_ints_badly(PyObject *self,
PyObject *args)
{
PyObject *list, *item;
long count, i;
if (!PyArg_ParseTuple(args, "i", &count)) {
return NULL;
}
list = PyList_New(0);
for (i = 0; i < count; i++) {
item = PyLong_FromLong(random());
PyList_Append(list, item);
}
return list;
}
/*
PEP-7
Local variables:
c-basic-offset: 4
indent-tabs-mode: nil
End:
*/