amend bash.h; add example_builtin.c
This commit is contained in:
47
bash.h
47
bash.h
@ -34,32 +34,29 @@ extern char **make_builtin_argv(WORD_LIST*, int*);
|
||||
PyImport_AppendInittab(#modname, PyInit_##modname); \
|
||||
Py_Initialize(); \
|
||||
} \
|
||||
int _##bash_name(int argc, char **argv) { \
|
||||
int ret = 1; \
|
||||
PyObject *mod = NULL, *func = NULL, *result; \
|
||||
\
|
||||
mod = PyImport_ImportModule(#modname); \
|
||||
func = PyObject_GetAttrString(mod, #function); \
|
||||
\
|
||||
PyObject *py_argv = PyTuple_New(argc - 1); \
|
||||
\
|
||||
for (int i = 1; i < argc; i++) { \
|
||||
PyObject *arg_as_str = PyUnicode_FromString(argv[i]); \
|
||||
PyTuple_SetItem(py_argv, i-1, arg_as_str); \
|
||||
} \
|
||||
\
|
||||
result = PyObject_CallObject(func, py_argv); \
|
||||
if (!result) { PyErr_Print(); goto finally; } \
|
||||
ret = (int) PyFloat_AsDouble(result); \
|
||||
\
|
||||
finally: \
|
||||
Py_XDECREF(result); \
|
||||
Py_XDECREF(py_argv); \
|
||||
Py_XDECREF(func); \
|
||||
Py_XDECREF(mod); \
|
||||
return ret; \
|
||||
int ret = 1; \
|
||||
PyObject *mod = NULL, *func = NULL, *result; \
|
||||
\
|
||||
mod = PyImport_ImportModule(#modname); \
|
||||
func = PyObject_GetAttrString(mod, #function); \
|
||||
\
|
||||
PyObject *py_argv = PyTuple_New(argc - 1); \
|
||||
\
|
||||
for (int i = 1; i < argc; i++) { \
|
||||
PyObject *arg_as_str = PyUnicode_FromString(argv[i]); \
|
||||
PyTuple_SetItem(py_argv, i-1, arg_as_str); \
|
||||
} \
|
||||
return _##bash_name(argc, argv); \
|
||||
\
|
||||
result = PyObject_CallObject(func, py_argv); \
|
||||
if (!result) { PyErr_Print(); goto finally; } \
|
||||
ret = (int) PyFloat_AsDouble(result); \
|
||||
\
|
||||
finally: \
|
||||
Py_XDECREF(result); \
|
||||
Py_XDECREF(py_argv); \
|
||||
Py_XDECREF(func); \
|
||||
Py_XDECREF(mod); \
|
||||
return ret; \
|
||||
} \
|
||||
WRAP_FUNC_WITH_BUILTIN(bash_name); \
|
||||
DEFINE_BUILTIN(bash_name);
|
||||
|
@ -3,4 +3,4 @@
|
||||
|
||||
PY_FUNC(foo, foo, _foo);
|
||||
PY_FUNC(bar, foo, _bar);
|
||||
PY_FUNC(graph, foo, _complicated_function);
|
||||
PY_FUNC(graph, bar, _complicated_function);
|
||||
|
Reference in New Issue
Block a user