Back to index

lightning-sunbird  0.9+nobinonly
PyGStub.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is the Python XPCOM language bindings.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * ActiveState Tool Corp.
00018  * Portions created by the Initial Developer are Copyright (C) 2000
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Mark Hammond <mhammond@skippinet.com.au> (original author)
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 // PyXPTStub - the stub for implementing interfaces.
00039 //
00040 // This code is part of the XPCOM extensions for Python.
00041 //
00042 // Written May 2000 by Mark Hammond.
00043 //
00044 // Based heavily on the Python COM support, which is
00045 // (c) Mark Hammond and Greg Stein.
00046 //
00047 // (c) 2000, ActiveState corp.
00048 
00049 #include "PyXPCOM_std.h"
00050 #include <nsIInterfaceInfoManager.h>
00051 
00052 void *PyXPCOM_XPTStub::ThisAsIID(const nsIID &iid)
00053 {
00054        if (iid.Equals(NS_GET_IID(nsISupports)))
00055            return (nsISupports *)(nsXPTCStubBase *)this;
00056        else if (iid.Equals(m_iid))
00057               return (nsISupports *)(nsXPTCStubBase *)this;
00058        else
00059               return PyG_Base::ThisAsIID(iid);
00060 }
00061 
00062 
00063 NS_IMETHODIMP
00064 PyXPCOM_XPTStub::GetInterfaceInfo(nsIInterfaceInfo** info)
00065 {
00066        NS_PRECONDITION(info, "NULL pointer");
00067        if (info==nsnull)
00068               return NS_ERROR_NULL_POINTER;
00069        // Simply get the XPCOM runtime to provide this
00070        // (but there must be some reason why they dont get it themselves!?
00071        // Maybe because they dont know the IID?
00072        nsCOMPtr<nsIInterfaceInfoManager> iim = XPTI_GetInterfaceInfoManager();
00073        NS_ABORT_IF_FALSE(iim != nsnull, "Cant get interface from IIM!");
00074        if (iim==nsnull)
00075               return NS_ERROR_FAILURE;
00076 
00077        return iim->GetInfoForIID( &m_iid, info);
00078 }
00079 
00080 // call this method and return result
00081 NS_IMETHODIMP
00082 PyXPCOM_XPTStub::CallMethod(PRUint16 methodIndex,
00083                           const nsXPTMethodInfo* info,
00084                           nsXPTCMiniVariant* params)
00085 {
00086        nsresult rc = NS_ERROR_FAILURE;
00087        NS_PRECONDITION(info, "NULL methodinfo pointer");
00088        NS_PRECONDITION(params, "NULL variant pointer");
00089        CEnterLeavePython _celp;
00090        PyObject *obParams = NULL;
00091        PyObject *result = NULL;
00092        PyObject *obThisObject = NULL;
00093        PyObject *obMI = PyObject_FromXPTMethodDescriptor(info);
00094        PyXPCOM_GatewayVariantHelper arg_helper(this, methodIndex, info, params);
00095        if (obMI==NULL)
00096               goto done;
00097        // base object is passed raw.
00098        obThisObject = Py_nsISupports::PyObjectFromInterface((nsIInternalPython*)this, NS_GET_IID(nsISupports), PR_TRUE, PR_FALSE);
00099        obParams = arg_helper.MakePyArgs();
00100        if (obParams==NULL)
00101               goto done;
00102        result = PyObject_CallMethod(m_pPyObject, 
00103                                               "_CallMethod_",
00104                                           "OiOO",
00105                                           obThisObject,
00106                                           (int)methodIndex,
00107                                           obMI,
00108                                           obParams);
00109        if (result!=NULL) {
00110               rc = arg_helper.ProcessPythonResult(result);
00111               // Use an xor to check failure && pyerr, or !failure && !pyerr.
00112               NS_ABORT_IF_FALSE( ((NS_FAILED(rc)!=0)^(PyErr_Occurred()!=0)) == 0, "We must have failure with a Python error, or success without a Python error.");
00113        }
00114 done:
00115        if (PyErr_Occurred()) {
00116               // The error handling - fairly involved, but worth it as
00117               // good error reporting is critical for users to know WTF 
00118               // is going on - especially with TypeErrors etc in their
00119               // return values (ie, after the Python code has successfully
00120               // exited, but we encountered errors unpacking the
00121               // result values for the COM caller - there is literally no 
00122               // way to catch these exceptions from Python code, as their
00123               // is no Python function on the call-stack)
00124 
00125               // First line of attack in an error is to call-back on the policy.
00126               // If the callback of the error handler succeeds and returns an
00127               // integer (for the nsresult), we take no further action.
00128 
00129               // If this callback fails, we log _2_ exceptions - the error handler
00130               // error, and the original error.
00131 
00132               PRBool bProcessMainError = PR_TRUE; // set to false if our exception handler does its thing!
00133               PyObject *exc_typ, *exc_val, *exc_tb;
00134               PyErr_Fetch(&exc_typ, &exc_val, &exc_tb);
00135               PyErr_NormalizeException( &exc_typ, &exc_val, &exc_tb);
00136 
00137               PyObject *err_result = PyObject_CallMethod(m_pPyObject, 
00138                                               "_CallMethodException_",
00139                                           "OiOO(OOO)",
00140                                           obThisObject,
00141                                           (int)methodIndex,
00142                                           obMI,
00143                                           obParams,
00144                                              exc_typ ? exc_typ : Py_None, // should never be NULL, but defensive programming...
00145                                              exc_val ? exc_val : Py_None, // may well be NULL.
00146                                           exc_tb ? exc_tb : Py_None); // may well be NULL.
00147               if (err_result == NULL) {
00148                      PyXPCOM_LogError("The exception handler _CallMethodException_ failed!\n");
00149               } else if (err_result == Py_None) {
00150                      // The exception handler has chosen not to do anything with
00151                      // this error, so we still need to print it!
00152                      ;
00153               } else if (PyInt_Check(err_result)) {
00154                      // The exception handler has given us the nresult.
00155                      rc = PyInt_AsLong(err_result);
00156                      bProcessMainError = PR_FALSE;
00157               } else {
00158                      // The exception handler succeeded, but returned other than
00159                      // int or None.
00160                      PyXPCOM_LogError("The _CallMethodException_ handler returned object of type '%s' - None or an integer expected\n", err_result->ob_type->tp_name);
00161               }
00162               Py_XDECREF(err_result);
00163               PyErr_Restore(exc_typ, exc_val, exc_tb);
00164               if (bProcessMainError) {
00165                      PyXPCOM_LogError("The function '%s' failed\n", info->GetName());
00166                      rc = PyXPCOM_SetCOMErrorFromPyException();
00167               }
00168               // else everything is already setup,
00169               // just clear the Python error state.
00170               PyErr_Clear();
00171        }
00172 
00173        Py_XDECREF(obMI);
00174        Py_XDECREF(obParams);
00175        Py_XDECREF(obThisObject);
00176        Py_XDECREF(result);
00177        return rc;
00178 }