@class PythonObject * This class defines an interface for calling PyObject from Singular. * * @note This class does not take care of the memory mangement, this is done in * the blackbox routines. **/
| 105 | * the blackbox routines. |
| 106 | **/ |
| 107 | class PythonObject |
| 108 | { |
| 109 | typedef PythonObject self; |
| 110 | |
| 111 | public: |
| 112 | typedef PyObject* ptr_type; |
| 113 | struct sequence_tag{}; |
| 114 | |
| 115 | PythonObject(): m_ptr(Py_None) { } |
| 116 | PythonObject(ptr_type ptr): m_ptr(ptr) { |
| 117 | if (!ptr && handle_exception()) m_ptr = Py_None; |
| 118 | } |
| 119 | |
| 120 | ptr_type check_context(ptr_type ptr) const { |
| 121 | if(ptr) sync_contexts(); |
| 122 | return ptr; |
| 123 | } |
| 124 | /// Unary operations |
| 125 | self operator()(int op) const |
| 126 | { |
| 127 | switch(op) |
| 128 | { |
| 129 | case '(': return check_context(PyObject_CallObject(*this, NULL)); |
| 130 | case ATTRIB_CMD: return PyObject_Dir(*this); |
| 131 | case PROC_CMD: return *this; |
| 132 | } |
| 133 | |
| 134 | if (op == PythonInterpreter::id()) |
| 135 | return *this; |
| 136 | |
| 137 | return self(NULL); |
| 138 | } |
| 139 | |
| 140 | /// Binary and n-ary operations |
| 141 | self operator()(int op, const self& arg) const { |
| 142 | |
| 143 | switch(op) |
| 144 | { |
| 145 | case '+': return PyNumber_Add(*this, arg); |
| 146 | case '-': return PyNumber_Subtract(*this, arg); |
| 147 | case '*': return PyNumber_Multiply(*this, arg); |
| 148 | case '/': return PyNumber_Divide(*this, arg); |
| 149 | case '^': return PyNumber_Power(*this, arg, Py_None); |
| 150 | case '(': return check_context(PyObject_CallObject(*this, arg)); |
| 151 | case '[': return operator[](arg); |
| 152 | case KILLATTR_CMD: return del_attr(arg); |
| 153 | case LIST_CMD: return args2list(arg); |
| 154 | case '.': case COLONCOLON: case ATTRIB_CMD: return attr(arg); |
| 155 | } |
| 156 | return self(NULL); |
| 157 | } |
| 158 | |
| 159 | /// Ternary operations |
| 160 | self operator()(int op, const self& arg1, const self& arg2) const |
| 161 | { |
| 162 | switch(op) |
| 163 | { |
| 164 | case ATTRIB_CMD: |