[11746] | 1 | /* ***** BEGIN LICENSE BLOCK *****
|
---|
| 2 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
---|
| 3 | *
|
---|
| 4 | * The contents of this file are subject to the Mozilla Public License Version
|
---|
| 5 | * 1.1 (the "License"); you may not use this file except in compliance with
|
---|
| 6 | * the License. You may obtain a copy of the License at
|
---|
| 7 | * http://www.mozilla.org/MPL/
|
---|
| 8 | *
|
---|
| 9 | * Software distributed under the License is distributed on an "AS IS" basis,
|
---|
| 10 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
---|
| 11 | * for the specific language governing rights and limitations under the
|
---|
| 12 | * License.
|
---|
| 13 | *
|
---|
| 14 | * The Original Code is the Python XPCOM language bindings.
|
---|
| 15 | *
|
---|
| 16 | * The Initial Developer of the Original Code is
|
---|
| 17 | * ActiveState Tool Corp.
|
---|
| 18 | * Portions created by the Initial Developer are Copyright (C) 2000
|
---|
| 19 | * the Initial Developer. All Rights Reserved.
|
---|
| 20 | *
|
---|
| 21 | * Contributor(s):
|
---|
| 22 | * Mark Hammond <MarkH@ActiveState.com> (original author)
|
---|
| 23 | *
|
---|
| 24 | * Alternatively, the contents of this file may be used under the terms of
|
---|
| 25 | * either the GNU General Public License Version 2 or later (the "GPL"), or
|
---|
| 26 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
---|
| 27 | * in which case the provisions of the GPL or the LGPL are applicable instead
|
---|
| 28 | * of those above. If you wish to allow use of your version of this file only
|
---|
| 29 | * under the terms of either the GPL or the LGPL, and not to allow others to
|
---|
| 30 | * use your version of this file under the terms of the MPL, indicate your
|
---|
| 31 | * decision by deleting the provisions above and replace them with the notice
|
---|
| 32 | * and other provisions required by the GPL or the LGPL. If you do not delete
|
---|
| 33 | * the provisions above, a recipient may use your version of this file under
|
---|
| 34 | * the terms of any one of the MPL, the GPL or the LGPL.
|
---|
| 35 | *
|
---|
| 36 | * ***** END LICENSE BLOCK ***** */
|
---|
| 37 |
|
---|
| 38 | //
|
---|
| 39 | // This code is part of the XPCOM extensions for Python.
|
---|
| 40 | //
|
---|
| 41 | // Written May 2000 by Mark Hammond.
|
---|
| 42 | //
|
---|
| 43 | // Based heavily on the Python COM support, which is
|
---|
| 44 | // (c) Mark Hammond and Greg Stein.
|
---|
| 45 | //
|
---|
| 46 | // (c) 2000, ActiveState corp.
|
---|
| 47 |
|
---|
| 48 | #include "PyXPCOM_std.h"
|
---|
| 49 | #include "nsReadableUtils.h"
|
---|
[12752] | 50 | #ifdef VBOX
|
---|
[39535] | 51 | # include <nsIExceptionService.h>
|
---|
| 52 | # include <iprt/err.h>
|
---|
| 53 | # include <iprt/string.h>
|
---|
[101955] | 54 | # include <iprt/stream.h>
|
---|
[12752] | 55 | #endif
|
---|
[11746] | 56 |
|
---|
| 57 | static char *PyTraceback_AsString(PyObject *exc_tb);
|
---|
| 58 |
|
---|
| 59 | // The internal helper that actually moves the
|
---|
| 60 | // formatted string to the target!
|
---|
| 61 |
|
---|
| 62 | // Only used in really bad situations!
|
---|
| 63 | static void _PanicErrorWrite(const char *msg)
|
---|
| 64 | {
|
---|
[101955] | 65 | RTStrmPrintf(g_pStdErr,"%s\n", msg);
|
---|
[11746] | 66 | }
|
---|
| 67 |
|
---|
| 68 | // Called when our "normal" error logger fails.
|
---|
| 69 | static void HandleLogError(const char *pszMessageText)
|
---|
| 70 | {
|
---|
| 71 | nsCAutoString streamout;
|
---|
| 72 |
|
---|
| 73 | _PanicErrorWrite("Failed to log an error record");
|
---|
| 74 | if (PyXPCOM_FormatCurrentException(streamout))
|
---|
| 75 | _PanicErrorWrite(streamout.get());
|
---|
| 76 | _PanicErrorWrite("Original error follows:");
|
---|
| 77 | _PanicErrorWrite(pszMessageText);
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | static const char *LOGGER_WARNING = "warning";
|
---|
| 81 | static const char *LOGGER_ERROR = "error";
|
---|
[103505] | 82 | #ifdef DEBUG
|
---|
[11746] | 83 | static const char *LOGGER_DEBUG = "debug";
|
---|
[103505] | 84 | #endif
|
---|
[11746] | 85 |
|
---|
| 86 | // Our "normal" error logger - calls back to the logging module.
|
---|
[103176] | 87 | static void DoLogMessage(const char *methodName, const char *pszMessageText)
|
---|
[11746] | 88 | {
|
---|
| 89 | // We use the logging module now. Originally this code called
|
---|
| 90 | // the logging module directly by way of the C API's
|
---|
| 91 | // PyImport_ImportModule/PyObject_CallMethod etc. However, this
|
---|
| 92 | // causes problems when there is no Python caller on the stack -
|
---|
| 93 | // the logging module's findCaller method fails with a None frame.
|
---|
| 94 | // We now work around this by calling PyRun_SimpleString - this
|
---|
| 95 | // causes a new frame to be created for executing the compiled
|
---|
| 96 | // string, and the logging module no longer fails.
|
---|
| 97 | // XXX - this implementation is less than ideal - findCaller now
|
---|
| 98 | // returns ("<string>", 2). Ideally we would compile with a
|
---|
| 99 | // filename something similar to "<pydom error reporter>".
|
---|
| 100 |
|
---|
| 101 | // But this also means we need a clear error state...
|
---|
| 102 | PyObject *exc_typ = NULL, *exc_val = NULL, *exc_tb = NULL;
|
---|
| 103 | PyErr_Fetch(&exc_typ, &exc_val, &exc_tb);
|
---|
| 104 | // We will execute:
|
---|
| 105 | // import logging
|
---|
| 106 | // logging.getLogger('xpcom').{warning/error/etc}("%s", {msg_text})
|
---|
| 107 | nsCAutoString c("import logging\nlogging.getLogger('xpcom').");
|
---|
| 108 | c += methodName;
|
---|
| 109 | c += "('%s', ";
|
---|
| 110 | // Pull a trick to ensure a valid string - use Python repr!
|
---|
[59809] | 111 | #if PY_MAJOR_VERSION <= 2
|
---|
[11746] | 112 | PyObject *obMessage = PyString_FromString(pszMessageText);
|
---|
[59809] | 113 | #else
|
---|
| 114 | PyObject *obMessage = PyUnicode_FromString(pszMessageText);
|
---|
| 115 | #endif
|
---|
[11746] | 116 | if (obMessage) {
|
---|
| 117 | PyObject *repr = PyObject_Repr(obMessage);
|
---|
| 118 | if (repr) {
|
---|
[59809] | 119 | #if PY_MAJOR_VERSION <= 2
|
---|
[11746] | 120 | c += PyString_AsString(repr);
|
---|
[59809] | 121 | #else
|
---|
| 122 | c += PyUnicode_AsUTF8(repr);
|
---|
| 123 | #endif
|
---|
[11746] | 124 | Py_DECREF(repr);
|
---|
| 125 | }
|
---|
| 126 | Py_DECREF(obMessage);
|
---|
| 127 | }
|
---|
| 128 | c += ")\n";
|
---|
| 129 | if (PyRun_SimpleString(c.get()) != 0) {
|
---|
| 130 | HandleLogError(pszMessageText);
|
---|
| 131 | }
|
---|
| 132 | PyErr_Restore(exc_typ, exc_val, exc_tb);
|
---|
| 133 | }
|
---|
| 134 |
|
---|
[103176] | 135 | static void LogMessage(const char *methodName, const char *pszMessageText)
|
---|
[11746] | 136 | {
|
---|
| 137 | // Be careful to save and restore the Python exception state
|
---|
| 138 | // before calling back to Python, or we lose the original error.
|
---|
| 139 | PyObject *exc_typ = NULL, *exc_val = NULL, *exc_tb = NULL;
|
---|
| 140 | PyErr_Fetch( &exc_typ, &exc_val, &exc_tb);
|
---|
| 141 | DoLogMessage(methodName, pszMessageText);
|
---|
| 142 | PyErr_Restore(exc_typ, exc_val, exc_tb);
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 |
|
---|
[103176] | 146 | static void LogMessage(const char *methodName, nsACString &text)
|
---|
[11746] | 147 | {
|
---|
| 148 | char *c = ToNewCString(text);
|
---|
| 149 | LogMessage(methodName, c);
|
---|
| 150 | nsCRT::free(c);
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | // A helper for the various logging routines.
|
---|
| 154 | static void VLogF(const char *methodName, const char *fmt, va_list argptr)
|
---|
| 155 | {
|
---|
[101955] | 156 | char buff[512];
|
---|
| 157 | RTStrPrintfV(buff, sizeof(buff), fmt, argptr);
|
---|
[11746] | 158 |
|
---|
[101955] | 159 | LogMessage(methodName, buff);
|
---|
[11746] | 160 | }
|
---|
| 161 |
|
---|
[101955] | 162 | bool PyXPCOM_FormatCurrentException(nsCString &streamout)
|
---|
[11746] | 163 | {
|
---|
[101955] | 164 | bool ok = false;
|
---|
[11746] | 165 | PyObject *exc_typ = NULL, *exc_val = NULL, *exc_tb = NULL;
|
---|
| 166 | PyErr_Fetch( &exc_typ, &exc_val, &exc_tb);
|
---|
| 167 | PyErr_NormalizeException( &exc_typ, &exc_val, &exc_tb);
|
---|
| 168 | if (exc_typ) {
|
---|
| 169 | ok = PyXPCOM_FormatGivenException(streamout, exc_typ, exc_val,
|
---|
| 170 | exc_tb);
|
---|
| 171 | }
|
---|
| 172 | PyErr_Restore(exc_typ, exc_val, exc_tb);
|
---|
| 173 | return ok;
|
---|
| 174 | }
|
---|
| 175 |
|
---|
[101955] | 176 | bool PyXPCOM_FormatGivenException(nsCString &streamout,
|
---|
[11746] | 177 | PyObject *exc_typ, PyObject *exc_val,
|
---|
| 178 | PyObject *exc_tb)
|
---|
| 179 | {
|
---|
| 180 | if (!exc_typ)
|
---|
[101955] | 181 | return false;
|
---|
[11746] | 182 | streamout += "\n";
|
---|
| 183 |
|
---|
| 184 | if (exc_tb) {
|
---|
| 185 | const char *szTraceback = PyTraceback_AsString(exc_tb);
|
---|
| 186 | if (szTraceback == NULL)
|
---|
| 187 | streamout += "Can't get the traceback info!";
|
---|
| 188 | else {
|
---|
| 189 | streamout += "Traceback (most recent call last):\n";
|
---|
| 190 | streamout += szTraceback;
|
---|
| 191 | PyMem_Free((void *)szTraceback);
|
---|
| 192 | }
|
---|
| 193 | }
|
---|
| 194 | PyObject *temp = PyObject_Str(exc_typ);
|
---|
| 195 | if (temp) {
|
---|
[59809] | 196 | #if PY_MAJOR_VERSION <= 2
|
---|
[11746] | 197 | streamout += PyString_AsString(temp);
|
---|
[59809] | 198 | #else
|
---|
| 199 | streamout += PyUnicode_AsUTF8(temp);
|
---|
| 200 | #endif
|
---|
[11746] | 201 | Py_DECREF(temp);
|
---|
| 202 | } else
|
---|
| 203 | streamout += "Can't convert exception to a string!";
|
---|
| 204 | streamout += ": ";
|
---|
| 205 | if (exc_val != NULL) {
|
---|
| 206 | temp = PyObject_Str(exc_val);
|
---|
| 207 | if (temp) {
|
---|
[59809] | 208 | #if PY_MAJOR_VERSION <= 2
|
---|
[11746] | 209 | streamout += PyString_AsString(temp);
|
---|
[59809] | 210 | #else
|
---|
| 211 | streamout += PyUnicode_AsUTF8(temp);
|
---|
| 212 | #endif
|
---|
[11746] | 213 | Py_DECREF(temp);
|
---|
| 214 | } else
|
---|
| 215 | streamout += "Can't convert exception value to a string!";
|
---|
| 216 | }
|
---|
[101955] | 217 | return true;
|
---|
[11746] | 218 | }
|
---|
| 219 |
|
---|
| 220 | void PyXPCOM_LogError(const char *fmt, ...)
|
---|
| 221 | {
|
---|
| 222 | va_list marker;
|
---|
| 223 | va_start(marker, fmt);
|
---|
| 224 | // NOTE: It is tricky to use logger.exception here - the exception
|
---|
| 225 | // state when called back from the C code is clear. Only Python 2.4
|
---|
| 226 | // and later allows an explicit exc_info tuple().
|
---|
[16903] | 227 |
|
---|
[11746] | 228 | // Don't use VLogF here, instead arrange for exception info and
|
---|
| 229 | // traceback to be in the same buffer.
|
---|
| 230 | char buff[512];
|
---|
[101955] | 231 | RTStrPrintf2V(buff, sizeof(buff), fmt, marker);
|
---|
[11746] | 232 | // If we have a Python exception, also log that:
|
---|
| 233 | nsCAutoString streamout(buff);
|
---|
| 234 | if (PyXPCOM_FormatCurrentException(streamout)) {
|
---|
| 235 | LogMessage(LOGGER_ERROR, streamout);
|
---|
| 236 | }
|
---|
[58104] | 237 | va_end(marker);
|
---|
[11746] | 238 | }
|
---|
| 239 |
|
---|
| 240 | void PyXPCOM_LogWarning(const char *fmt, ...)
|
---|
| 241 | {
|
---|
| 242 | va_list marker;
|
---|
| 243 | va_start(marker, fmt);
|
---|
| 244 | VLogF(LOGGER_WARNING, fmt, marker);
|
---|
[58104] | 245 | va_end(marker);
|
---|
[11746] | 246 | }
|
---|
| 247 |
|
---|
| 248 | void PyXPCOM_Log(const char *level, const nsCString &msg)
|
---|
| 249 | {
|
---|
| 250 | DoLogMessage(level, msg.get());
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 253 | #ifdef DEBUG
|
---|
| 254 | void PyXPCOM_LogDebug(const char *fmt, ...)
|
---|
| 255 | {
|
---|
| 256 | va_list marker;
|
---|
| 257 | va_start(marker, fmt);
|
---|
| 258 | VLogF(LOGGER_DEBUG, fmt, marker);
|
---|
[58104] | 259 | va_end(marker);
|
---|
[11746] | 260 | }
|
---|
| 261 | #endif
|
---|
| 262 |
|
---|
[16903] | 263 | #ifdef VBOX
|
---|
| 264 | PyObject *PyXPCOM_BuildErrorMessage(nsresult r)
|
---|
| 265 | {
|
---|
| 266 | char msg[512];
|
---|
| 267 | bool gotMsg = false;
|
---|
[11746] | 268 |
|
---|
[16909] | 269 | if (!gotMsg)
|
---|
[16903] | 270 | {
|
---|
[16909] | 271 | nsresult rc;
|
---|
| 272 | nsCOMPtr <nsIExceptionService> es;
|
---|
| 273 | es = do_GetService (NS_EXCEPTIONSERVICE_CONTRACTID, &rc);
|
---|
[12752] | 274 | if (NS_SUCCEEDED (rc))
|
---|
| 275 | {
|
---|
[16909] | 276 | nsCOMPtr <nsIExceptionManager> em;
|
---|
| 277 | rc = es->GetCurrentExceptionManager (getter_AddRefs (em));
|
---|
| 278 | if (NS_SUCCEEDED (rc))
|
---|
[12752] | 279 | {
|
---|
[16909] | 280 | nsCOMPtr <nsIException> ex;
|
---|
| 281 | rc = em->GetExceptionFromProvider(r, NULL, getter_AddRefs (ex));
|
---|
| 282 | if (NS_SUCCEEDED (rc) && ex)
|
---|
| 283 | {
|
---|
| 284 | nsXPIDLCString emsg;
|
---|
| 285 | ex->GetMessage(getter_Copies(emsg));
|
---|
[101955] | 286 | RTStrPrintf2(msg, sizeof(msg), "%s",
|
---|
| 287 | emsg.get());
|
---|
[16909] | 288 | gotMsg = true;
|
---|
| 289 | }
|
---|
[12752] | 290 | }
|
---|
| 291 | }
|
---|
[16903] | 292 | }
|
---|
[12752] | 293 |
|
---|
[16903] | 294 | if (!gotMsg)
|
---|
| 295 | {
|
---|
[16909] | 296 | const RTCOMERRMSG* pMsg = RTErrCOMGet(r);
|
---|
| 297 | if (strncmp(pMsg->pszMsgFull, "Unknown", 7) != 0)
|
---|
| 298 | {
|
---|
[101955] | 299 | RTStrPrintf2(msg, sizeof(msg), "%s (%s)",
|
---|
| 300 | pMsg->pszMsgFull, pMsg->pszDefine);
|
---|
[16909] | 301 | gotMsg = true;
|
---|
| 302 | }
|
---|
| 303 | }
|
---|
| 304 |
|
---|
| 305 | if (!gotMsg)
|
---|
| 306 | {
|
---|
[101955] | 307 | RTStrPrintf2(msg, sizeof(msg), "Error 0x%x in module 0x%x",
|
---|
| 308 | NS_ERROR_GET_CODE(r), NS_ERROR_GET_MODULE(r));
|
---|
[16903] | 309 | }
|
---|
| 310 | PyObject *evalue = Py_BuildValue("is", r, msg);
|
---|
| 311 | return evalue;
|
---|
| 312 | }
|
---|
| 313 | #endif
|
---|
| 314 |
|
---|
| 315 | PyObject *PyXPCOM_BuildPyException(nsresult r)
|
---|
| 316 | {
|
---|
| 317 | #ifndef VBOX
|
---|
| 318 | // Need the message etc.
|
---|
| 319 | PyObject *evalue = Py_BuildValue("i", r);
|
---|
| 320 | #else
|
---|
| 321 | PyObject *evalue = PyXPCOM_BuildErrorMessage(r);
|
---|
| 322 | #endif
|
---|
[12752] | 323 | PyErr_SetObject(PyXPCOM_Error, evalue);
|
---|
| 324 | Py_XDECREF(evalue);
|
---|
| 325 | return NULL;
|
---|
[11746] | 326 | }
|
---|
| 327 |
|
---|
| 328 | nsresult PyXPCOM_SetCOMErrorFromPyException()
|
---|
| 329 | {
|
---|
| 330 | if (!PyErr_Occurred())
|
---|
| 331 | // No error occurred
|
---|
| 332 | return NS_OK;
|
---|
| 333 | nsresult rv = NS_ERROR_FAILURE;
|
---|
| 334 | if (PyErr_ExceptionMatches(PyExc_MemoryError))
|
---|
| 335 | rv = NS_ERROR_OUT_OF_MEMORY;
|
---|
| 336 | // todo:
|
---|
| 337 | // * Set an exception using the exception service.
|
---|
| 338 |
|
---|
| 339 | // Once we have returned to the xpcom caller, we don't want to leave a
|
---|
| 340 | // Python exception pending - it may get noticed when the next call
|
---|
| 341 | // is made on the same thread.
|
---|
| 342 | PyErr_Clear();
|
---|
| 343 | return rv;
|
---|
| 344 | }
|
---|
| 345 |
|
---|
| 346 | /* Obtains a string from a Python traceback.
|
---|
| 347 | This is the exact same string as "traceback.print_exc" would return.
|
---|
| 348 |
|
---|
| 349 | Pass in a Python traceback object (probably obtained from PyErr_Fetch())
|
---|
| 350 | Result is a string which must be free'd using PyMem_Free()
|
---|
| 351 | */
|
---|
| 352 | #define TRACEBACK_FETCH_ERROR(what) {errMsg = what; goto done;}
|
---|
| 353 |
|
---|
| 354 | char *PyTraceback_AsString(PyObject *exc_tb)
|
---|
| 355 | {
|
---|
| 356 | const char *errMsg = NULL; /* holds a local error message */
|
---|
| 357 | char *result = NULL; /* a valid, allocated result. */
|
---|
| 358 | PyObject *modStringIO = NULL;
|
---|
| 359 | PyObject *modTB = NULL;
|
---|
| 360 | PyObject *obFuncStringIO = NULL;
|
---|
| 361 | PyObject *obStringIO = NULL;
|
---|
| 362 | PyObject *obFuncTB = NULL;
|
---|
| 363 | PyObject *argsTB = NULL;
|
---|
| 364 | PyObject *obResult = NULL;
|
---|
| 365 |
|
---|
[59809] | 366 | #if PY_MAJOR_VERSION <= 2
|
---|
[11746] | 367 | /* Import the modules we need - cStringIO and traceback */
|
---|
| 368 | modStringIO = PyImport_ImportModule("cStringIO");
|
---|
| 369 | if (modStringIO==NULL)
|
---|
| 370 | TRACEBACK_FETCH_ERROR("cant import cStringIO\n");
|
---|
| 371 |
|
---|
| 372 | modTB = PyImport_ImportModule("traceback");
|
---|
| 373 | if (modTB==NULL)
|
---|
| 374 | TRACEBACK_FETCH_ERROR("cant import traceback\n");
|
---|
| 375 | /* Construct a cStringIO object */
|
---|
| 376 | obFuncStringIO = PyObject_GetAttrString(modStringIO, "StringIO");
|
---|
| 377 | if (obFuncStringIO==NULL)
|
---|
| 378 | TRACEBACK_FETCH_ERROR("cant find cStringIO.StringIO\n");
|
---|
| 379 | obStringIO = PyObject_CallObject(obFuncStringIO, NULL);
|
---|
| 380 | if (obStringIO==NULL)
|
---|
| 381 | TRACEBACK_FETCH_ERROR("cStringIO.StringIO() failed\n");
|
---|
[59809] | 382 | #else
|
---|
| 383 | /* Import the modules we need - io and traceback */
|
---|
| 384 | modStringIO = PyImport_ImportModule("io");
|
---|
| 385 | if (modStringIO==NULL)
|
---|
| 386 | TRACEBACK_FETCH_ERROR("cant import io\n");
|
---|
| 387 |
|
---|
| 388 | modTB = PyImport_ImportModule("traceback");
|
---|
| 389 | if (modTB==NULL)
|
---|
| 390 | TRACEBACK_FETCH_ERROR("cant import traceback\n");
|
---|
| 391 | /* Construct a StringIO object */
|
---|
| 392 | obFuncStringIO = PyObject_GetAttrString(modStringIO, "StringIO");
|
---|
| 393 | if (obFuncStringIO==NULL)
|
---|
| 394 | TRACEBACK_FETCH_ERROR("cant find io.StringIO\n");
|
---|
| 395 | obStringIO = PyObject_CallObject(obFuncStringIO, NULL);
|
---|
| 396 | if (obStringIO==NULL)
|
---|
| 397 | TRACEBACK_FETCH_ERROR("io.StringIO() failed\n");
|
---|
| 398 | #endif
|
---|
[11746] | 399 | /* Get the traceback.print_exception function, and call it. */
|
---|
| 400 | obFuncTB = PyObject_GetAttrString(modTB, "print_tb");
|
---|
| 401 | if (obFuncTB==NULL)
|
---|
| 402 | TRACEBACK_FETCH_ERROR("cant find traceback.print_tb\n");
|
---|
| 403 |
|
---|
[16903] | 404 | argsTB = Py_BuildValue("OOO",
|
---|
[11746] | 405 | exc_tb ? exc_tb : Py_None,
|
---|
[16903] | 406 | Py_None,
|
---|
[11746] | 407 | obStringIO);
|
---|
[16903] | 408 | if (argsTB==NULL)
|
---|
[11746] | 409 | TRACEBACK_FETCH_ERROR("cant make print_tb arguments\n");
|
---|
| 410 |
|
---|
| 411 | obResult = PyObject_CallObject(obFuncTB, argsTB);
|
---|
[16903] | 412 | if (obResult==NULL)
|
---|
[11746] | 413 | TRACEBACK_FETCH_ERROR("traceback.print_tb() failed\n");
|
---|
| 414 | /* Now call the getvalue() method in the StringIO instance */
|
---|
| 415 | Py_DECREF(obFuncStringIO);
|
---|
| 416 | obFuncStringIO = PyObject_GetAttrString(obStringIO, "getvalue");
|
---|
| 417 | if (obFuncStringIO==NULL)
|
---|
| 418 | TRACEBACK_FETCH_ERROR("cant find getvalue function\n");
|
---|
| 419 | Py_DECREF(obResult);
|
---|
| 420 | obResult = PyObject_CallObject(obFuncStringIO, NULL);
|
---|
[16903] | 421 | if (obResult==NULL)
|
---|
[11746] | 422 | TRACEBACK_FETCH_ERROR("getvalue() failed.\n");
|
---|
| 423 |
|
---|
| 424 | /* And it should be a string all ready to go - duplicate it. */
|
---|
[59809] | 425 | #if PY_MAJOR_VERSION <= 2
|
---|
[11746] | 426 | if (!PyString_Check(obResult))
|
---|
[59809] | 427 | #else
|
---|
| 428 | if (!PyUnicode_Check(obResult))
|
---|
| 429 | #endif
|
---|
[11746] | 430 | TRACEBACK_FETCH_ERROR("getvalue() did not return a string\n");
|
---|
| 431 |
|
---|
| 432 | { // a temp scope so I can use temp locals.
|
---|
[59809] | 433 | #if PY_MAJOR_VERSION <= 2
|
---|
[11746] | 434 | char *tempResult = PyString_AsString(obResult);
|
---|
[59809] | 435 | #else
|
---|
[73153] | 436 | /* PyUnicode_AsUTF8() is const char * as of Python 3.7, char * earlier. */
|
---|
| 437 | const char *tempResult = (const char *)PyUnicode_AsUTF8(obResult);
|
---|
[59809] | 438 | #endif
|
---|
[11746] | 439 | result = (char *)PyMem_Malloc(strlen(tempResult)+1);
|
---|
| 440 | if (result==NULL)
|
---|
| 441 | TRACEBACK_FETCH_ERROR("memory error duplicating the traceback string\n");
|
---|
| 442 |
|
---|
| 443 | strcpy(result, tempResult);
|
---|
| 444 | } // end of temp scope.
|
---|
| 445 | done:
|
---|
| 446 | /* All finished - first see if we encountered an error */
|
---|
| 447 | if (result==NULL && errMsg != NULL) {
|
---|
| 448 | result = (char *)PyMem_Malloc(strlen(errMsg)+1);
|
---|
| 449 | if (result != NULL)
|
---|
| 450 | /* if it does, not much we can do! */
|
---|
| 451 | strcpy(result, errMsg);
|
---|
| 452 | }
|
---|
| 453 | Py_XDECREF(modStringIO);
|
---|
| 454 | Py_XDECREF(modTB);
|
---|
| 455 | Py_XDECREF(obFuncStringIO);
|
---|
| 456 | Py_XDECREF(obStringIO);
|
---|
| 457 | Py_XDECREF(obFuncTB);
|
---|
| 458 | Py_XDECREF(argsTB);
|
---|
| 459 | Py_XDECREF(obResult);
|
---|
| 460 | return result;
|
---|
| 461 | }
|
---|
| 462 |
|
---|
| 463 | // See comments in PyXPCOM.h for why we need this!
|
---|
| 464 | void PyXPCOM_MakePendingCalls()
|
---|
| 465 | {
|
---|
| 466 | while (1) {
|
---|
| 467 | int rc = Py_MakePendingCalls();
|
---|
| 468 | if (rc == 0)
|
---|
| 469 | break;
|
---|
| 470 | // An exception - just report it as normal.
|
---|
| 471 | // Note that a traceback is very unlikely!
|
---|
| 472 | PyXPCOM_LogError("Unhandled exception detected before entering Python.\n");
|
---|
| 473 | PyErr_Clear();
|
---|
| 474 | // And loop around again until we are told everything is done!
|
---|
| 475 | }
|
---|
| 476 | }
|
---|