* Compile a command and determine whether it is incomplete. * * The source string may contain line feeds and/or carriage returns. \n * Return value / exceptions raised: * - Return a code object if the command is complete and valid * - Return None if the command is incomplete * - Raise SyntaxError, ValueError or OverflowError if the command is a * syntax error (OverflowError and Value
| 177 | * malformed literals). |
| 178 | */ |
| 179 | PyObject* InteractiveInterpreter::compile(const char* source) const |
| 180 | { |
| 181 | Base::PyGILStateLocker lock; |
| 182 | PyObject* func = PyObject_GetAttrString(d->interpreter, "compile"); |
| 183 | PyObject* args = Py_BuildValue("(s)", source); |
| 184 | PyObject* eval = PyObject_CallObject(func, args); // must decref later |
| 185 | |
| 186 | Py_XDECREF(args); |
| 187 | Py_XDECREF(func); |
| 188 | |
| 189 | if (eval) { |
| 190 | return eval; |
| 191 | } |
| 192 | else { |
| 193 | // do not throw Base::PyException as this clears the error indicator |
| 194 | throw Base::RuntimeError("Code evaluation failed"); |
| 195 | } |
| 196 | |
| 197 | // can never happen |
| 198 | return nullptr; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Compile a command and determine whether it is incomplete. |
no outgoing calls