Update of /cvsroot/pywin32/pywin32/win32/src
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv6485
Modified Files:
Tag: py3k
PyWinTypes.h PyWinTypesmodule.cpp PythonService.cpp
Log Message:
Move GetPythonTraceback into pywintypes (used from win32com,
win32ui, and pythonservice)
Index: PyWinTypesmodule.cpp
===================================================================
RCS file: /cvsroot/pywin32/pywin32/win32/src/PyWinTypesmodule.cpp,v
retrieving revision 1.39.2.1
retrieving revision 1.39.2.2
diff -C2 -d -r1.39.2.1 -r1.39.2.2
*** PyWinTypesmodule.cpp 29 Aug 2008 04:59:26 -0000 1.39.2.1
--- PyWinTypesmodule.cpp 31 Aug 2008 02:11:22 -0000 1.39.2.2
***************
*** 1066,1067 ****
--- 1066,1162 ----
}
+ // Function to format a python traceback into a character string.
+ #define GPEM_ERROR(what) {errorMsg = "<Error getting traceback - " ## what ## ">";goto done;}
+ char *GetPythonTraceback(PyObject *exc_type, PyObject *exc_value, PyObject *exc_tb)
+ {
+ // Sleep (30000); // Time enough to attach the debugger (barely)
+ char *result = NULL;
+ char *errorMsg = NULL;
+ PyObject *modStringIO = NULL;
+ PyObject *modTB = NULL;
+ PyObject *obFuncStringIO = NULL;
+ PyObject *obStringIO = NULL;
+ PyObject *obFuncTB = NULL;
+ PyObject *argsTB = NULL;
+ PyObject *obResult = NULL;
+
+ /* Import the modules we need - cStringIO and traceback */
+ #if (PY_VERSION_HEX < 0x03000000)
+ modStringIO = PyImport_ImportModule("cStringIO");
+ #else
+ // In py3k, cStringIO is in "io"
+ modStringIO = PyImport_ImportModule("io");
+ #endif
+
+ if (modStringIO==NULL) GPEM_ERROR("cant import cStringIO");
+ modTB = PyImport_ImportModule("traceback");
+ if (modTB==NULL) GPEM_ERROR("cant import traceback");
+
+ /* Construct a cStringIO object */
+ obFuncStringIO = PyObject_GetAttrString(modStringIO, "StringIO");
+ if (obFuncStringIO==NULL) GPEM_ERROR("cant find cStringIO.StringIO");
+ obStringIO = PyObject_CallObject(obFuncStringIO, NULL);
+ if (obStringIO==NULL) GPEM_ERROR("cStringIO.StringIO() failed");
+
+ /* Get the traceback.print_exception function, and call it. */
+ obFuncTB = PyObject_GetAttrString(modTB, "print_exception");
+ if (obFuncTB==NULL) GPEM_ERROR("cant find traceback.print_exception");
+ argsTB = Py_BuildValue("OOOOO"
+ #if (PY_VERSION_HEX >= 0x03000000)
+ "i"
+ // Py3k has added an undocumented 'chain' argument which defaults to True
+ // and causes all kinds of exceptions while trying to print a goddam exception
+ #endif
+ ,
+ exc_type ? exc_type : Py_None,
+ exc_value ? exc_value : Py_None,
+ exc_tb ? exc_tb : Py_None,
+ Py_None, // limit
+ obStringIO
+ #if (PY_VERSION_HEX >= 0x03000000)
+ ,0 // Goddam undocumented 'chain' param, which defaults to True
+ #endif
+ );
+ if (argsTB==NULL) GPEM_ERROR("cant make print_exception arguments");
+
+ obResult = PyObject_CallObject(obFuncTB, argsTB);
+ if (obResult==NULL){
+ // Chain parameter when True causes traceback.print_exception to fail, leaving no
+ // way to see what the original problem is, or even what error print_exc raises
+ // PyObject *t, *v, *tb;
+ // PyErr_Fetch(&t, &v, &tb);
+ // PyUnicodeObject *uo=(PyUnicodeObject *)v;
+ // DebugBreak();
+ GPEM_ERROR("traceback.print_exception() failed");
+ }
+ /* Now call the getvalue() method in the StringIO instance */
+ Py_DECREF(obFuncStringIO);
+ obFuncStringIO = PyObject_GetAttrString(obStringIO, "getvalue");
+ if (obFuncStringIO==NULL) GPEM_ERROR("cant find getvalue function");
+ Py_DECREF(obResult);
+ obResult = PyObject_CallObject(obFuncStringIO, NULL);
+ if (obResult==NULL) GPEM_ERROR("getvalue() failed.");
+
+ /* And it should be a string all ready to go - duplicate it. */
+ if (PyString_Check(obResult))
+ result = strdup(PyString_AsString(obResult));
+ #if (PY_VERSION_HEX >= 0x03000000)
+ else if (PyUnicode_Check(obResult))
+ result = strdup(_PyUnicode_AsString(obResult));
+ #endif
+ else
+ GPEM_ERROR("getvalue() did not return a string");
+
+ done:
+ if (result==NULL && errorMsg != NULL)
+ result = strdup(errorMsg);
+ Py_XDECREF(modStringIO);
+ Py_XDECREF(modTB);
+ Py_XDECREF(obFuncStringIO);
+ Py_XDECREF(obStringIO);
+ Py_XDECREF(obFuncTB);
+ Py_XDECREF(argsTB);
+ Py_XDECREF(obResult);
+ return result;
+ }
+
Index: PyWinTypes.h
===================================================================
RCS file: /cvsroot/pywin32/pywin32/win32/src/PyWinTypes.h,v
retrieving revision 1.51.2.1
retrieving revision 1.51.2.2
diff -C2 -d -r1.51.2.1 -r1.51.2.2
*** PyWinTypes.h 29 Aug 2008 04:59:25 -0000 1.51.2.1
--- PyWinTypes.h 31 Aug 2008 02:11:22 -0000 1.51.2.2
***************
*** 112,115 ****
--- 112,118 ----
#endif
+ // Formats a python traceback into a character string - result must be free()ed
+ PYWINTYPES_EXPORT char *GetPythonTraceback(PyObject *exc_type, PyObject *exc_value, PyObject *exc_tb);
+
#include <tchar.h>
/*
Index: PythonService.cpp
===================================================================
RCS file: /cvsroot/pywin32/pywin32/win32/src/PythonService.cpp,v
retrieving revision 1.24.2.2
retrieving revision 1.24.2.3
diff -C2 -d -r1.24.2.2 -r1.24.2.3
*** PythonService.cpp 30 Aug 2008 22:33:34 -0000 1.24.2.2
--- PythonService.cpp 31 Aug 2008 02:11:22 -0000 1.24.2.3
***************
*** 1392,1490 ****
}
- #define GPEM_ERROR(what) {errorMsg = "<Error getting traceback - " ## what ## ">";goto done;}
- static char *GetPythonTraceback(PyObject *exc_type, PyObject *exc_value, PyObject *exc_tb)
- {
- // Sleep (30000); // Time enough to attach the debugger (barely)
- PyErr_Clear();
- char *result = NULL;
- char *errorMsg = NULL;
- PyObject *modStringIO = NULL;
- PyObject *modTB = NULL;
- PyObject *obFuncStringIO = NULL;
- PyObject *obStringIO = NULL;
- PyObject *obFuncTB = NULL;
- PyObject *argsTB = NULL;
- PyObject *obResult = NULL;
-
- /* Import the modules we need - cStringIO and traceback */
- #if (PY_VERSION_HEX < 0x03000000)
- modStringIO = PyImport_ImportModule("cStringIO");
- #else
- // In py3k, cStringIO is in "io"
- modStringIO = PyImport_ImportModule("io");
- #endif
-
- if (modStringIO==NULL) GPEM_ERROR("cant import cStringIO");
- modTB = PyImport_ImportModule("traceback");
- if (modTB==NULL) GPEM_ERROR("cant import traceback");
-
- /* Construct a cStringIO object */
- obFuncStringIO = PyObject_GetAttrString(modStringIO, "StringIO");
- if (obFuncStringIO==NULL) GPEM_ERROR("cant find cStringIO.StringIO");
- obStringIO = PyObject_CallObject(obFuncStringIO, NULL);
- if (obStringIO==NULL) GPEM_ERROR("cStringIO.StringIO() failed");
-
- /* Get the traceback.print_exception function, and call it. */
- obFuncTB = PyObject_GetAttrString(modTB, "print_exception");
- if (obFuncTB==NULL) GPEM_ERROR("cant find traceback.print_exception");
- argsTB = Py_BuildValue("OOOOO"
- #if (PY_VERSION_HEX >= 0x03000000)
- "i"
- // Py3k has added an undocumented 'chain' argument which defaults to True
- // and causes all kinds of exceptions while trying to print a goddam exception
- #endif
- ,
- exc_type ? exc_type : Py_None,
- exc_value ? exc_value : Py_None,
- exc_tb ? exc_tb : Py_None,
- Py_None, // limit
- obStringIO
- #if (PY_VERSION_HEX >= 0x03000000)
- ,0 // Goddam undocumented 'chain' param, which defaults to True
- #endif
- );
- if (argsTB==NULL) GPEM_ERROR("cant make print_exception arguments");
-
- obResult = PyObject_CallObject(obFuncTB, argsTB);
- if (obResult==NULL){
- // Chain parameter when True causes traceback.print_exception to fail, leaving no
- // way to see what the original problem is, or even what error print_exc raises
- // PyObject *t, *v, *tb;
- // PyErr_Fetch(&t, &v, &tb);
- // PyUnicodeObject *uo=(PyUnicodeObject *)v;
- // DebugBreak();
- GPEM_ERROR("traceback.print_exception() failed");
- }
- /* Now call the getvalue() method in the StringIO instance */
- Py_DECREF(obFuncStringIO);
- obFuncStringIO = PyObject_GetAttrString(obStringIO, "getvalue");
- if (obFuncStringIO==NULL) GPEM_ERROR("cant find getvalue function");
- Py_DECREF(obResult);
- obResult = PyObject_CallObject(obFuncStringIO, NULL);
- if (obResult==NULL) GPEM_ERROR("getvalue() failed.");
-
- /* And it should be a string all ready to go - duplicate it. */
- if (PyString_Check(obResult))
- result = strdup(PyString_AsString(obResult));
- #if (PY_VERSION_HEX >= 0x03000000)
- else if (PyUnicode_Check(obResult))
- result = strdup(_PyUnicode_AsString(obResult));
- #endif
- else
- GPEM_ERROR("getvalue() did not return a string");
-
- done:
- if (result==NULL && errorMsg != NULL)
- result = strdup(errorMsg);
- Py_XDECREF(modStringIO);
- Py_XDECREF(modTB);
- Py_XDECREF(obFuncStringIO);
- Py_XDECREF(obStringIO);
- Py_XDECREF(obFuncTB);
- Py_XDECREF(argsTB);
- Py_XDECREF(obResult);
- return result;
- }
-
static void ReportPythonError(DWORD code)
{
--- 1392,1395 ----
|