This repository has been archived by the owner on Mar 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 65
/
_verifytype.c
107 lines (89 loc) · 2.83 KB
/
_verifytype.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
/*
* C-version of the src/Utils/VerifyType.py module.
*
* (c) 2009, R. Tyler Ballance <tyler@slide.com>
*/
#include <Python.h>
#if __STDC_VERSION__ >= 199901L
#include <stdbool.h>
#else
typedef enum { false, true } bool;
#endif
#include "Cheetah.h"
#ifdef __cplusplus
extern "C" {
#endif
static PyObject *_errorMessage(char *arg, char *legalTypes, char *extra)
{
return PyString_FromFormat("Argument '%s' must be %s\n", arg, legalTypes);
}
static PyObject *py_verifytype(PyObject *self, PyObject *args, PyObject *kwargs)
{
PyObject *argument, *legalTypes;
char *arg_string, *types_string, *extra;
PyObject *iterator, *item;
bool rc = false;
char *kwlist[] = {"argument", "argument_name", "legalType",
"types_string", "errmsgExtra", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OsOs|s", kwlist, &argument,
&arg_string, &legalTypes, &types_string, &extra))
return NULL;
iterator = PyObject_GetIter(legalTypes);
if (iterator == NULL) {
return NULL;
}
while (item = PyIter_Next(iterator)) {
if ((PyObject *)argument->ob_type == item) {
rc = true;
Py_DECREF(item);
break;
}
Py_DECREF(item);
}
Py_DECREF(iterator);
if (rc)
Py_RETURN_TRUE;
PyErr_SetObject(PyExc_TypeError, _errorMessage(arg_string,
types_string, extra));
return NULL;
}
static PyObject *py_verifytypeclass(PyObject *self, PyObject *args, PyObject *kwargs)
{
PyObject *argument, *legalTypes, *klass;
PyObject *verifyTypeArgs, *v;
char *arg_string, *types_string, *extra;
bool rc = false;
char *kwlist[] = {"argument", "argument_name", "legalTypes",
"types_string", "klass", "errmsgExtra", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OsOsO|s", kwlist, &argument,
&arg_string, &legalTypes, &types_string, &klass, &extra))
return NULL;
verifyTypeArgs = Py_BuildValue("OsOs", argument, arg_string, legalTypes,
types_string);
v = py_verifytype(self, verifyTypeArgs, NULL);
if (v == NULL)
return NULL;
Py_DECREF(v);
if (PyClass_Check(argument) && (!PyClass_IsSubclass(argument, klass)) ) {
PyErr_SetObject(PyExc_TypeError, _errorMessage(arg_string,
types_string, extra));
return NULL;
}
Py_RETURN_TRUE;
}
static const char _verifytypedoc[] = "\
\n\
";
static struct PyMethodDef _verifytype_methods[] = {
{"verifyType", (PyCFunction)py_verifytype, METH_VARARGS | METH_KEYWORDS, NULL},
{"verifyTypeClass", (PyCFunction)py_verifytypeclass, METH_VARARGS | METH_KEYWORDS, NULL},
{NULL}
};
PyMODINIT_FUNC init_verifytype()
{
PyObject *module = Py_InitModule3("_verifytype", _verifytype_methods,
_verifytypedoc);
}
#ifdef __cplusplus
}
#endif