-
Notifications
You must be signed in to change notification settings - Fork 2
/
translate_python_value.c
79 lines (66 loc) · 2.24 KB
/
translate_python_value.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
#include <Python.h>
#include <sapi/embed/php_embed.h>
#include "translate_python_value.h"
#include "python_object_proxy.h"
zval *
pyhp_translate_python_value(PyObject *value) {
zval *var = NULL, *var2 = NULL;
Py_ssize_t pos = 0;
PyObject *key, *ivalue;
if (value == NULL) {
PyErr_SetString(PyExc_ValueError, "Cannot translate NULL value");
return NULL;
}
MAKE_STD_ZVAL(var);
if (value == Py_None) {
ZVAL_NULL(var);
} else if (PyBool_Check(value)) {
ZVAL_BOOL(var, (value == Py_True) ? 1 : 0);
} else if (PyFloat_Check(value)) {
ZVAL_DOUBLE(var, PyFloat_AsDouble(value));
} else if (PyInt_Check(value)) {
ZVAL_LONG(var, PyInt_AsLong(value));
} else if (PyString_Check(value)) {
ZVAL_STRINGL(var, PyString_AsString(value), PyString_Size(value), 1);
} else if (PyDict_Check(value)) {
array_init(var);
while (PyDict_Next(value, &pos, &key, &ivalue)) {
if (! PyString_Check(key)) {
zval_ptr_dtor(&var);
PyErr_SetString(PyExc_ValueError, "Unsupported key type");
return NULL;
}
var2 = pyhp_translate_python_value(ivalue);
if (var2 == NULL) {
zval_ptr_dtor(&var);
return NULL;
}
add_assoc_zval(var, PyString_AsString(key), var2);
}
} else if (PyTuple_Check(value)) {
array_init(var);
for (pos=0; pos < PyTuple_GET_SIZE(value); ++pos) {
ivalue = PyTuple_GetItem(value, pos);
var2 = pyhp_translate_python_value(ivalue);
if (var2 == NULL) {
zval_ptr_dtor(&var);
return NULL;
}
add_next_index_zval(var, var2);
}
} else if (PyList_Check(value)) {
array_init(var);
for (pos=0; pos < PyList_GET_SIZE(value); ++pos) {
ivalue = PyTuple_GetItem(value, pos);
var2 = pyhp_translate_python_value(ivalue);
if (var2 == NULL) {
zval_ptr_dtor(&var);
return NULL;
}
add_next_index_zval(var, var2);
}
} else {
pyhp_create_python_object_proxy(var, value);
}
return var;
}