@@ -1934,178 +1934,3 @@ PyErr_ProgramTextObject(PyObject *filename, int lineno)
19341934{
19351935 return _PyErr_ProgramDecodedTextObject (filename , lineno , NULL );
19361936}
1937-
1938-
1939- /***********************/
1940- /* exception snapshots */
1941- /***********************/
1942-
1943- static const char *
1944- _copy_raw_string (const char * str )
1945- {
1946- char * copied = PyMem_RawMalloc (strlen (str )+ 1 );
1947- if (copied == NULL ) {
1948- return NULL ;
1949- }
1950- strcpy (copied , str );
1951- return copied ;
1952- }
1953-
1954- static int
1955- _exc_type_name_as_utf8 (PyObject * exc , const char * * p_typename )
1956- {
1957- // XXX Use PyObject_GetAttrString(Py_TYPE(exc), '__name__')?
1958- PyObject * nameobj = PyUnicode_FromString (Py_TYPE (exc )-> tp_name );
1959- if (nameobj == NULL ) {
1960- assert (PyErr_Occurred ());
1961- * p_typename = "unable to format exception type name" ;
1962- return -1 ;
1963- }
1964- const char * name = PyUnicode_AsUTF8 (nameobj );
1965- if (name == NULL ) {
1966- assert (PyErr_Occurred ());
1967- Py_DECREF (nameobj );
1968- * p_typename = "unable to encode exception type name" ;
1969- return -1 ;
1970- }
1971- name = _copy_raw_string (name );
1972- Py_DECREF (nameobj );
1973- if (name == NULL ) {
1974- * p_typename = "out of memory copying exception type name" ;
1975- return -1 ;
1976- }
1977- * p_typename = name ;
1978- return 0 ;
1979- }
1980-
1981- static int
1982- _exc_msg_as_utf8 (PyObject * exc , const char * * p_msg )
1983- {
1984- PyObject * msgobj = PyObject_Str (exc );
1985- if (msgobj == NULL ) {
1986- assert (PyErr_Occurred ());
1987- * p_msg = "unable to format exception message" ;
1988- return -1 ;
1989- }
1990- const char * msg = PyUnicode_AsUTF8 (msgobj );
1991- if (msg == NULL ) {
1992- assert (PyErr_Occurred ());
1993- Py_DECREF (msgobj );
1994- * p_msg = "unable to encode exception message" ;
1995- return -1 ;
1996- }
1997- msg = _copy_raw_string (msg );
1998- Py_DECREF (msgobj );
1999- if (msg == NULL ) {
2000- assert (PyErr_ExceptionMatches (PyExc_MemoryError ));
2001- * p_msg = "out of memory copying exception message" ;
2002- return -1 ;
2003- }
2004- * p_msg = msg ;
2005- return 0 ;
2006- }
2007-
2008- void
2009- _Py_excinfo_Clear (_Py_excinfo * info )
2010- {
2011- if (info -> type != NULL ) {
2012- PyMem_RawFree ((void * )info -> type );
2013- }
2014- if (info -> msg != NULL ) {
2015- PyMem_RawFree ((void * )info -> msg );
2016- }
2017- * info = (_Py_excinfo ){ NULL };
2018- }
2019-
2020- int
2021- _Py_excinfo_Copy (_Py_excinfo * dest , _Py_excinfo * src )
2022- {
2023- // XXX Clear dest first?
2024-
2025- if (src -> type == NULL ) {
2026- dest -> type = NULL ;
2027- }
2028- else {
2029- dest -> type = _copy_raw_string (src -> type );
2030- if (dest -> type == NULL ) {
2031- return -1 ;
2032- }
2033- }
2034-
2035- if (src -> msg == NULL ) {
2036- dest -> msg = NULL ;
2037- }
2038- else {
2039- dest -> msg = _copy_raw_string (src -> msg );
2040- if (dest -> msg == NULL ) {
2041- return -1 ;
2042- }
2043- }
2044-
2045- return 0 ;
2046- }
2047-
2048- const char *
2049- _Py_excinfo_InitFromException (_Py_excinfo * info , PyObject * exc )
2050- {
2051- assert (exc != NULL );
2052-
2053- // Extract the exception type name.
2054- const char * typename = NULL ;
2055- if (_exc_type_name_as_utf8 (exc , & typename ) < 0 ) {
2056- assert (typename != NULL );
2057- return typename ;
2058- }
2059-
2060- // Extract the exception message.
2061- const char * msg = NULL ;
2062- if (_exc_msg_as_utf8 (exc , & msg ) < 0 ) {
2063- assert (msg != NULL );
2064- return msg ;
2065- }
2066-
2067- info -> type = typename ;
2068- info -> msg = msg ;
2069- return NULL ;
2070- }
2071-
2072- void
2073- _Py_excinfo_Apply (_Py_excinfo * info , PyObject * exctype )
2074- {
2075- if (info -> type != NULL ) {
2076- if (info -> msg != NULL ) {
2077- PyErr_Format (exctype , "%s: %s" , info -> type , info -> msg );
2078- }
2079- else {
2080- PyErr_SetString (exctype , info -> type );
2081- }
2082- }
2083- else if (info -> msg != NULL ) {
2084- PyErr_SetString (exctype , info -> msg );
2085- }
2086- else {
2087- PyErr_SetNone (exctype );
2088- }
2089- }
2090-
2091- const char *
2092- _Py_excinfo_AsUTF8 (_Py_excinfo * info , char * buf , size_t bufsize )
2093- {
2094- // XXX Dynamically allocate if no buf provided?
2095- assert (buf != NULL );
2096- if (info -> type != NULL ) {
2097- if (info -> msg != NULL ) {
2098- snprintf (buf , bufsize , "%s: %s" , info -> type , info -> msg );
2099- return buf ;
2100- }
2101- else {
2102- return info -> type ;
2103- }
2104- }
2105- else if (info -> msg != NULL ) {
2106- return info -> msg ;
2107- }
2108- else {
2109- return NULL ;
2110- }
2111- }
0 commit comments