static PyObject * create_filter(PyObject *category, _Py_Identifier *id) { PyObject *action_str = _PyUnicode_FromId(id); if (action_str == NULL) { return NULL; } /* This assumes the line number is zero for now. */ return PyTuple_Pack(5, action_str, Py_None, category, Py_None, _PyLong_Zero); }
PyObject * PyRun_StringFlags(const char *str, int start, PyObject *globals, PyObject *locals, PyCompilerFlags *flags) { PyObject *ret = NULL; mod_ty mod; PyArena *arena; PyObject *filename; filename = _PyUnicode_FromId(&PyId_string); /* borrowed */ if (filename == NULL) return NULL; arena = PyArena_New(); if (arena == NULL) return NULL; mod = PyParser_ASTFromStringObject(str, filename, start, flags, arena); if (mod != NULL) ret = run_mod(mod, filename, globals, locals, flags, arena); PyArena_Free(arena); return ret; }
/* Returns a new reference. A NULL return value can mean false or an error. */ static PyObject * get_warnings_attr(_Py_Identifier *attr_id, int try_import) { PyObject *warnings_str; PyObject *warnings_module, *obj; _Py_IDENTIFIER(warnings); warnings_str = _PyUnicode_FromId(&PyId_warnings); if (warnings_str == NULL) { return NULL; } /* don't try to import after the start of the Python finallization */ if (try_import && !_Py_IsFinalizing()) { warnings_module = PyImport_Import(warnings_str); if (warnings_module == NULL) { /* Fallback to the C implementation if we cannot get the Python implementation */ if (PyErr_ExceptionMatches(PyExc_ImportError)) { PyErr_Clear(); } return NULL; } } else { warnings_module = PyImport_GetModule(warnings_str); if (warnings_module == NULL) return NULL; } obj = _PyObject_GetAttrId(warnings_module, attr_id); Py_DECREF(warnings_module); if (obj == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) { PyErr_Clear(); } return obj; }
static PyObject * warnings_warn_explicit(PyObject *self, PyObject *args, PyObject *kwds) { static char *kwd_list[] = {"message", "category", "filename", "lineno", "module", "registry", "module_globals", "source", 0}; PyObject *message; PyObject *category; PyObject *filename; int lineno; PyObject *module = NULL; PyObject *registry = NULL; PyObject *module_globals = NULL; PyObject *sourceobj = NULL; if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOUi|OOOO:warn_explicit", kwd_list, &message, &category, &filename, &lineno, &module, ®istry, &module_globals, &sourceobj)) return NULL; if (module_globals) { _Py_IDENTIFIER(get_source); _Py_IDENTIFIER(splitlines); PyObject *tmp; PyObject *loader; PyObject *module_name; PyObject *source; PyObject *source_list; PyObject *source_line; PyObject *returned; if ((tmp = _PyUnicode_FromId(&PyId_get_source)) == NULL) return NULL; if ((tmp = _PyUnicode_FromId(&PyId_splitlines)) == NULL) return NULL; /* Check/get the requisite pieces needed for the loader. */ loader = PyDict_GetItemString(module_globals, "__loader__"); module_name = PyDict_GetItemString(module_globals, "__name__"); if (loader == NULL || module_name == NULL) goto standard_call; /* Make sure the loader implements the optional get_source() method. */ if (!_PyObject_HasAttrId(loader, &PyId_get_source)) goto standard_call; /* Call get_source() to get the source code. */ source = PyObject_CallMethodObjArgs(loader, PyId_get_source.object, module_name, NULL); if (!source) return NULL; else if (source == Py_None) { Py_DECREF(Py_None); goto standard_call; } /* Split the source into lines. */ source_list = PyObject_CallMethodObjArgs(source, PyId_splitlines.object, NULL); Py_DECREF(source); if (!source_list) return NULL; /* Get the source line. */ source_line = PyList_GetItem(source_list, lineno-1); if (!source_line) { Py_DECREF(source_list); return NULL; } /* Handle the warning. */ returned = warn_explicit(category, message, filename, lineno, module, registry, source_line, sourceobj); Py_DECREF(source_list); return returned; } standard_call: return warn_explicit(category, message, filename, lineno, module, registry, NULL, sourceobj); }
static int parse_syntax_error(PyObject *err, PyObject **message, PyObject **filename, int *lineno, int *offset, PyObject **text) { int hold; PyObject *v; _Py_IDENTIFIER(msg); _Py_IDENTIFIER(filename); _Py_IDENTIFIER(lineno); _Py_IDENTIFIER(offset); _Py_IDENTIFIER(text); *message = NULL; *filename = NULL; /* new style errors. `err' is an instance */ *message = _PyObject_GetAttrId(err, &PyId_msg); if (!*message) goto finally; v = _PyObject_GetAttrId(err, &PyId_filename); if (!v) goto finally; if (v == Py_None) { Py_DECREF(v); *filename = _PyUnicode_FromId(&PyId_string); if (*filename == NULL) goto finally; Py_INCREF(*filename); } else { *filename = v; } v = _PyObject_GetAttrId(err, &PyId_lineno); if (!v) goto finally; hold = _PyLong_AsInt(v); Py_DECREF(v); if (hold < 0 && PyErr_Occurred()) goto finally; *lineno = hold; v = _PyObject_GetAttrId(err, &PyId_offset); if (!v) goto finally; if (v == Py_None) { *offset = -1; Py_DECREF(v); } else { hold = _PyLong_AsInt(v); Py_DECREF(v); if (hold < 0 && PyErr_Occurred()) goto finally; *offset = hold; } v = _PyObject_GetAttrId(err, &PyId_text); if (!v) goto finally; if (v == Py_None) { Py_DECREF(v); *text = NULL; } else { *text = v; } return 1; finally: Py_XDECREF(*message); Py_XDECREF(*filename); return 0; }
/* A PyRun_InteractiveOneObject() auxiliary function that does not print the * error on failure. */ static int PyRun_InteractiveOneObjectEx(FILE *fp, PyObject *filename, PyCompilerFlags *flags) { PyObject *m, *d, *v, *w, *oenc = NULL, *mod_name; mod_ty mod; PyArena *arena; const char *ps1 = "", *ps2 = "", *enc = NULL; int errcode = 0; _Py_IDENTIFIER(encoding); _Py_IDENTIFIER(__main__); mod_name = _PyUnicode_FromId(&PyId___main__); /* borrowed */ if (mod_name == NULL) { return -1; } if (fp == stdin) { /* Fetch encoding from sys.stdin if possible. */ v = _PySys_GetObjectId(&PyId_stdin); if (v && v != Py_None) { oenc = _PyObject_GetAttrId(v, &PyId_encoding); if (oenc) enc = PyUnicode_AsUTF8(oenc); if (!enc) PyErr_Clear(); } } v = _PySys_GetObjectId(&PyId_ps1); if (v != NULL) { v = PyObject_Str(v); if (v == NULL) PyErr_Clear(); else if (PyUnicode_Check(v)) { ps1 = PyUnicode_AsUTF8(v); if (ps1 == NULL) { PyErr_Clear(); ps1 = ""; } } } w = _PySys_GetObjectId(&PyId_ps2); if (w != NULL) { w = PyObject_Str(w); if (w == NULL) PyErr_Clear(); else if (PyUnicode_Check(w)) { ps2 = PyUnicode_AsUTF8(w); if (ps2 == NULL) { PyErr_Clear(); ps2 = ""; } } } arena = PyArena_New(); if (arena == NULL) { Py_XDECREF(v); Py_XDECREF(w); Py_XDECREF(oenc); return -1; } mod = PyParser_ASTFromFileObject(fp, filename, enc, Py_single_input, ps1, ps2, flags, &errcode, arena); Py_XDECREF(v); Py_XDECREF(w); Py_XDECREF(oenc); if (mod == NULL) { PyArena_Free(arena); if (errcode == E_EOF) { PyErr_Clear(); return E_EOF; } return -1; } m = PyImport_AddModuleObject(mod_name); if (m == NULL) { PyArena_Free(arena); return -1; } d = PyModule_GetDict(m); v = run_mod(mod, filename, d, d, flags, arena); PyArena_Free(arena); if (v == NULL) { return -1; } Py_DECREF(v); flush_io(); return 0; }