* Compile a command and determine whether it is incomplete. * * The source string may contain line feeds and/or carriage returns. \n * Return value: * - Return 1 if the command is incomplete * - Return 0 if the command is complete and valid * - Return -1 if the command is a syntax error * . * (OverflowError and ValueError can be produced by malformed literals). */
| 210 | * (OverflowError and ValueError can be produced by malformed literals). |
| 211 | */ |
| 212 | int InteractiveInterpreter::compileCommand(const char* source) const |
| 213 | { |
| 214 | Base::PyGILStateLocker lock; |
| 215 | PyObject* func = PyObject_GetAttrString(d->interpreter, "compile"); |
| 216 | PyObject* args = Py_BuildValue("(s)", source); |
| 217 | PyObject* eval = PyObject_CallObject(func, args); // must decref later |
| 218 | |
| 219 | Py_DECREF(args); |
| 220 | Py_DECREF(func); |
| 221 | |
| 222 | int ret = 0; |
| 223 | if (eval) { |
| 224 | if (PyObject_TypeCheck(Py_None, eval->ob_type)) { |
| 225 | ret = 1; // incomplete |
| 226 | } |
| 227 | else { |
| 228 | ret = 0; // complete |
| 229 | } |
| 230 | Py_DECREF(eval); |
| 231 | } |
| 232 | else { |
| 233 | ret = -1; // invalid |
| 234 | } |
| 235 | |
| 236 | return ret; |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Compile and run some source in the interpreter. |
no outgoing calls
no test coverage detected