15 #include "torch/csrc/utils/structseq.h" 16 #include "torch/csrc/utils/six.h" 17 #include "structmember.h" 23 #if PY_MAJOR_VERSION == 2 24 PyObject *structseq_slice(PyStructSequence *obj, Py_ssize_t low, Py_ssize_t high)
32 if (high > Py_SIZE(obj)) {
38 np = (PyTupleObject *)PyTuple_New(high-low);
42 for(i = low; i < high; ++i) {
43 PyObject *v = obj->ob_item[i];
45 PyTuple_SET_ITEM(np, i-low, v);
47 return (PyObject *) np;
50 #define PyUnicode_AsUTF8 PyString_AsString 53 PyObject *returned_structseq_repr(PyStructSequence *obj) {
54 PyTypeObject *typ = Py_TYPE(obj);
61 ss << typ->tp_name <<
"(\n";
62 size_t num_elements = Py_SIZE(obj);
64 for (
int i=0; i < num_elements; i++) {
66 const char *cname, *crepr;
68 cname = typ->tp_members[i].name;
69 if (cname ==
nullptr) {
70 PyErr_Format(PyExc_SystemError,
"In structseq_repr(), member %d name is nullptr" 71 " for type %.500s", i, typ->tp_name);
75 val = PyTuple_GetItem(tup.get(), i);
80 repr = PyObject_Repr(val);
81 if (repr ==
nullptr) {
85 crepr = PyUnicode_AsUTF8(repr);
87 if (crepr ==
nullptr) {
91 ss << cname <<
'=' << crepr;
92 if (i < num_elements - 1) {
98 return PyUnicode_FromString(ss.str().c_str());