pyhelper.c 657 Bytes
Newer Older
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
#include "pyhelper.h"

bool is_pytable(PyObject *pyobj) {
	return PyList_Check(pyobj) || PyTuple_Check(pyobj);
}

Py_ssize_t pytable_size(PyObject *pyobj) {
	if (PyList_Check(pyobj))
		return PyList_Size(pyobj);
	else if (PyTuple_Check(pyobj))
		return PyTuple_Size(pyobj);
	PyErr_SetNone(PyExc_NotImplementedError);
	return 0;
}

const char *pytable_string(PyObject *pyobj, int index) {
	PyObject *str = NULL;
	if (PyList_Check(pyobj))
		str =  PyList_GetItem(pyobj, index);
	else if (PyTuple_Check(pyobj))
		str = PyTuple_GetItem(pyobj, index);
	else
		PyErr_SetNone(PyExc_NotImplementedError);

	if (!str)
		return NULL;
Karel Koci's avatar
Karel Koci committed
27
	return PyUnicode_AsUTF8(str);
28
}