static PyObject * try_complex_special_method(PyObject *op) { PyObject *f; _Py_IDENTIFIER(__complex__); f = _PyObject_LookupSpecial(op, &PyId___complex__); if (f) { PyObject *res = _PyObject_CallNoArg(f); Py_DECREF(f); if (!res || PyComplex_CheckExact(res)) { return res; } if (!PyComplex_Check(res)) { PyErr_Format(PyExc_TypeError, "__complex__ returned non-complex (type %.200s)", res->ob_type->tp_name); Py_DECREF(res); return NULL; } /* Issue #29894: warn if 'res' not of exact type complex. */ if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, "__complex__ returned non-complex (type %.200s). " "The ability to return an instance of a strict subclass of complex " "is deprecated, and may be removed in a future version of Python.", res->ob_type->tp_name)) { Py_DECREF(res); return NULL; } return res; } return NULL; }
void _PyGC_Fini(void) { if (!(debug & DEBUG_SAVEALL) && garbage != NULL && PyList_GET_SIZE(garbage) > 0) { char *message; if (debug & DEBUG_UNCOLLECTABLE) message = "gc: %zd uncollectable objects at " \ "shutdown"; else message = "gc: %zd uncollectable objects at " \ "shutdown; use gc.set_debug(gc.DEBUG_UNCOLLECTABLE) to list them"; if (PyErr_WarnFormat(PyExc_ResourceWarning, 0, message, PyList_GET_SIZE(garbage)) < 0) PyErr_WriteUnraisable(NULL); if (debug & DEBUG_UNCOLLECTABLE) { PyObject *repr = NULL, *bytes = NULL; repr = PyObject_Repr(garbage); if (!repr || !(bytes = PyUnicode_EncodeFSDefault(repr))) PyErr_WriteUnraisable(garbage); else { PySys_WriteStderr( " %s\n", PyBytes_AS_STRING(bytes) ); } Py_XDECREF(repr); Py_XDECREF(bytes); } } }
static void bpy_lib_exit_warn_type(BPy_Library *self, PyObject *item) { PyObject *exc, *val, *tb; PyErr_Fetch(&exc, &val, &tb); if (PyErr_WarnFormat(PyExc_UserWarning, 1, "load: '%s' expected a string type, not a %.200s", self->abspath, Py_TYPE(item)->tp_name)) { /* Spurious errors can appear at shutdown */ if (PyErr_ExceptionMatches(PyExc_Warning)) { PyErr_WriteUnraisable((PyObject *)self); } } PyErr_Restore(exc, val, tb); }
static void bpy_lib_exit_warn_idname(BPy_Library *self, const char *name_plural, const char *idname) { PyObject *exc, *val, *tb; PyErr_Fetch(&exc, &val, &tb); if (PyErr_WarnFormat(PyExc_UserWarning, 1, "load: '%s' does not contain %s[\"%s\"]", self->abspath, name_plural, idname)) { /* Spurious errors can appear at shutdown */ if (PyErr_ExceptionMatches(PyExc_Warning)) { PyErr_WriteUnraisable((PyObject *)self); } } PyErr_Restore(exc, val, tb); }
/* Check API/ABI version * Issues a warning on mismatch, which is usually not fatal. * Returns 0 if an exception is raised. */ static int check_api_version(const char *name, int module_api_version) { if (module_api_version != PYTHON_API_VERSION && module_api_version != PYTHON_ABI_VERSION) { int err; err = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, "Python C API version mismatch for module %.100s: " "This Python has API version %d, module %.100s has version %d.", name, PYTHON_API_VERSION, name, module_api_version); if (err) return 0; } return 1; }
/* Because this can call arbitrary code, it shouldn't be called when the refcount is 0 (that is, not directly from tp_dealloc unless the refcount has been temporarily re-incremented). */ static PyObject * fileio_dealloc_warn(fileio *self, PyObject *source) { if (self->fd >= 0 && self->closefd) { PyObject *exc, *val, *tb; PyErr_Fetch(&exc, &val, &tb); if (PyErr_WarnFormat(PyExc_ResourceWarning, 1, "unclosed file %R", source)) { /* Spurious errors can appear at shutdown */ if (PyErr_ExceptionMatches(PyExc_Warning)) PyErr_WriteUnraisable((PyObject *) self); } PyErr_Restore(exc, val, tb); } Py_RETURN_NONE; }
static PyObject * grp_getgrgid_impl(PyModuleDef *module, PyObject *id) /*[clinic end generated code: output=8a11f5fdeb8c78a0 input=15fa0e2ccf5cda25]*/ { PyObject *py_int_id; gid_t gid; struct group *p; if (!_Py_Gid_Converter(id, &gid)) { if (!PyErr_ExceptionMatches(PyExc_TypeError)) { return NULL; } PyErr_Clear(); if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, "group id must be int, not %.200", id->ob_type->tp_name) < 0) { return NULL; } py_int_id = PyNumber_Long(id); if (!py_int_id) return NULL; if (!_Py_Gid_Converter(py_int_id, &gid)) { Py_DECREF(py_int_id); return NULL; } Py_DECREF(py_int_id); } if ((p = getgrgid(gid)) == NULL) { PyObject *gid_obj = _PyLong_FromGid(gid); if (gid_obj == NULL) return NULL; PyErr_Format(PyExc_KeyError, "getgrgid(): gid not found: %S", gid_obj); Py_DECREF(gid_obj); return NULL; } return mkgrent(p); }
void _PyGen_Finalize(PyObject *self) { PyGenObject *gen = (PyGenObject *)self; PyObject *res; PyObject *error_type, *error_value, *error_traceback; /* If `gen` is a coroutine, and if it was never awaited on, issue a RuntimeWarning. */ if (gen->gi_code != NULL && ((PyCodeObject *)gen->gi_code)->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE) && gen->gi_frame != NULL && gen->gi_frame->f_lasti == -1 && !PyErr_Occurred() && PyErr_WarnFormat(PyExc_RuntimeWarning, 1, "coroutine '%.50S' was never awaited", gen->gi_qualname)) return; if (gen->gi_frame == NULL || gen->gi_frame->f_stacktop == NULL) /* Generator isn't paused, so no need to close */ return; /* Save the current exception, if any. */ PyErr_Fetch(&error_type, &error_value, &error_traceback); res = gen_close(gen, NULL); if (res == NULL) PyErr_WriteUnraisable(self); else Py_DECREF(res); /* Restore the saved exception. */ PyErr_Restore(error_type, error_value, error_traceback); }
static PyObject * gen_send_ex(PyGenObject *gen, PyObject *arg, int exc, int closing) { PyThreadState *tstate = PyThreadState_GET(); PyFrameObject *f = gen->gi_frame; PyObject *result; if (gen->gi_running) { char *msg = "generator already executing"; if (PyCoro_CheckExact(gen)) msg = "coroutine already executing"; PyErr_SetString(PyExc_ValueError, msg); return NULL; } if (f == NULL || f->f_stacktop == NULL) { if (PyCoro_CheckExact(gen) && !closing) { /* `gen` is an exhausted coroutine: raise an error, except when called from gen_close(), which should always be a silent method. */ PyErr_SetString( PyExc_RuntimeError, "cannot reuse already awaited coroutine"); } else if (arg && !exc) { /* `gen` is an exhausted generator: only set exception if called from send(). */ PyErr_SetNone(PyExc_StopIteration); } return NULL; } if (f->f_lasti == -1) { if (arg && arg != Py_None) { char *msg = "can't send non-None value to a " "just-started generator"; if (PyCoro_CheckExact(gen)) msg = "can't send non-None value to a " "just-started coroutine"; PyErr_SetString(PyExc_TypeError, msg); return NULL; } } else { /* Push arg onto the frame's value stack */ result = arg ? arg : Py_None; Py_INCREF(result); *(f->f_stacktop++) = result; } /* Generators always return to their most recent caller, not * necessarily their creator. */ Py_XINCREF(tstate->frame); assert(f->f_back == NULL); f->f_back = tstate->frame; gen->gi_running = 1; result = PyEval_EvalFrameEx(f, exc); gen->gi_running = 0; /* Don't keep the reference to f_back any longer than necessary. It * may keep a chain of frames alive or it could create a reference * cycle. */ assert(f->f_back == tstate->frame); Py_CLEAR(f->f_back); /* If the generator just returned (as opposed to yielding), signal * that the generator is exhausted. */ if (result && f->f_stacktop == NULL) { if (result == Py_None) { /* Delay exception instantiation if we can */ PyErr_SetNone(PyExc_StopIteration); } else { PyObject *e = PyObject_CallFunctionObjArgs( PyExc_StopIteration, result, NULL); if (e != NULL) { PyErr_SetObject(PyExc_StopIteration, e); Py_DECREF(e); } } Py_CLEAR(result); } else if (!result && PyErr_ExceptionMatches(PyExc_StopIteration)) { /* Check for __future__ generator_stop and conditionally turn * a leaking StopIteration into RuntimeError (with its cause * set appropriately). */ if (((PyCodeObject *)gen->gi_code)->co_flags & (CO_FUTURE_GENERATOR_STOP | CO_COROUTINE | CO_ITERABLE_COROUTINE)) { PyObject *exc, *val, *val2, *tb; char *msg = "generator raised StopIteration"; if (PyCoro_CheckExact(gen)) msg = "coroutine raised StopIteration"; PyErr_Fetch(&exc, &val, &tb); PyErr_NormalizeException(&exc, &val, &tb); if (tb != NULL) PyException_SetTraceback(val, tb); Py_DECREF(exc); Py_XDECREF(tb); PyErr_SetString(PyExc_RuntimeError, msg); PyErr_Fetch(&exc, &val2, &tb); PyErr_NormalizeException(&exc, &val2, &tb); Py_INCREF(val); PyException_SetCause(val2, val); PyException_SetContext(val2, val); PyErr_Restore(exc, val2, tb); } else { PyObject *exc, *val, *tb; /* Pop the exception before issuing a warning. */ PyErr_Fetch(&exc, &val, &tb); if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, "generator '%.50S' raised StopIteration", gen->gi_qualname)) { /* Warning was converted to an error. */ Py_XDECREF(exc); Py_XDECREF(val); Py_XDECREF(tb); } else { PyErr_Restore(exc, val, tb); } } } if (!result || f->f_stacktop == NULL) { /* generator can't be rerun, so release the frame */ /* first clean reference cycle through stored exception traceback */ PyObject *t, *v, *tb; t = f->f_exc_type; v = f->f_exc_value; tb = f->f_exc_traceback; f->f_exc_type = NULL; f->f_exc_value = NULL; f->f_exc_traceback = NULL; Py_XDECREF(t); Py_XDECREF(v); Py_XDECREF(tb); gen->gi_frame->f_gen = NULL; gen->gi_frame = NULL; Py_DECREF(f); } return result; }
PyObject * PyModule_Create2(struct PyModuleDef* module, int module_api_version) { PyObject *d, *v, *n; PyMethodDef *ml; const char* name; PyModuleObject *m; PyInterpreterState *interp = PyThreadState_Get()->interp; if (interp->modules == NULL) Py_FatalError("Python import machinery not initialized"); if (PyType_Ready(&moduledef_type) < 0) return NULL; if (module->m_base.m_index == 0) { max_module_number++; Py_REFCNT(module) = 1; Py_TYPE(module) = &moduledef_type; module->m_base.m_index = max_module_number; } name = module->m_name; if (module_api_version != PYTHON_API_VERSION && module_api_version != PYTHON_ABI_VERSION) { int err; err = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, "Python C API version mismatch for module %.100s: " "This Python has API version %d, module %.100s has version %d.", name, PYTHON_API_VERSION, name, module_api_version); if (err) return NULL; } /* Make sure name is fully qualified. This is a bit of a hack: when the shared library is loaded, the module name is "package.module", but the module calls PyModule_Create*() with just "module" for the name. The shared library loader squirrels away the true name of the module in _Py_PackageContext, and PyModule_Create*() will substitute this (if the name actually matches). */ if (_Py_PackageContext != NULL) { char *p = strrchr(_Py_PackageContext, '.'); if (p != NULL && strcmp(module->m_name, p+1) == 0) { name = _Py_PackageContext; _Py_PackageContext = NULL; } } if ((m = (PyModuleObject*)PyModule_New(name)) == NULL) return NULL; if (module->m_size > 0) { m->md_state = PyMem_MALLOC(module->m_size); if (!m->md_state) { PyErr_NoMemory(); Py_DECREF(m); return NULL; } memset(m->md_state, 0, module->m_size); } d = PyModule_GetDict((PyObject*)m); if (module->m_methods != NULL) { n = PyUnicode_FromString(name); if (n == NULL) { Py_DECREF(m); return NULL; } for (ml = module->m_methods; ml->ml_name != NULL; ml++) { if ((ml->ml_flags & METH_CLASS) || (ml->ml_flags & METH_STATIC)) { PyErr_SetString(PyExc_ValueError, "module functions cannot set" " METH_CLASS or METH_STATIC"); Py_DECREF(n); Py_DECREF(m); return NULL; } v = PyCFunction_NewEx(ml, (PyObject*)m, n); if (v == NULL) { Py_DECREF(n); Py_DECREF(m); return NULL; } if (PyDict_SetItemString(d, ml->ml_name, v) != 0) { Py_DECREF(v); Py_DECREF(n); Py_DECREF(m); return NULL; } Py_DECREF(v); } Py_DECREF(n); } if (module->m_doc != NULL) { v = PyUnicode_FromString(module->m_doc); if (v == NULL || PyDict_SetItemString(d, "__doc__", v) != 0) { Py_XDECREF(v); Py_DECREF(m); return NULL; } Py_DECREF(v); } m->md_def = module; return (PyObject*)m; }