ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> pyxpcom
pyxpcom
Re: [pyxpcom] how to get a nsIWeakReference in pyxpcom?
by Mark Hammond other posts by this author
Jun 24 2007 4:56AM messages near this date
view in the new Beta List Site
Re: [pyxpcom] how to get a nsIWeakReference in pyxpcom? | [pyxpcom] failed to run pyxpcom+xul
>  Ok, thanks.
> 
>  weak_ref2._comobj_ looks a bit ugly and counter-intuitive. Perhaps
>  pyxpcom could do that implicitly if we pass a WeakReference
>  to a method
>  that accept a nsIWeakReference?

That is what is supposed to happen - but in this case we are indirecting
twice - ie, we ask your weakref object for its _comobj_, but what we get
back is the wrapped instance - which itself has the _comobj_ we are after.
PyISupports.cpp could probably grow a feature where it just loops until
_comobj_ fails - this would be a handy feature which would allow any user
wrappers to also be passed, so long as _comobj_ returns the 'wrapped'
object.

Something like the following (uncompiled/untested) code should do it...

Mark

--- PyISupports.cpp     11 Apr 2006 06:12:13 -0000      1.10
+++ PyISupports.cpp     24 Jun 2007 11:54:52 -0000
@@ -261,21 +261,28 @@
        // end of variant support.

        if (PyInstance_Check(ob)) {
-               // Get the _comobj_ attribute
-               PyObject *use_ob = PyObject_GetAttrString(ob, "_comobj_");
-               if (use_ob==NULL) {
-                       PyErr_Clear();
-                       if (bTryAutoWrap)
-                               // Try and auto-wrap it - errors will leave
Py exception set,
-                               return
PyXPCOM_XPTStub::AutoWrapPythonInstance(ob, iid, ppv);
-                       PyErr_SetString(PyExc_TypeError, "The Python
instance can not be converted to an XPCOM object");

-                       return PR_FALSE;
-               } else
-                       ob = use_ob;
-
+        // If a _comobj_ attribute exists then fetch it (repeating
+        // as required to allow wrappers of wrappers)
+        while (1) {
+            PyObject *use_ob = PyObject_GetAttrString(ob, "_comobj_");
+            if (use_ob == ob || use_ob == NULL) {
+                PyErr_Clear();
+                break;
+            }
+            ob = use_ob;
+        }
+        // If still an instance, try and wrap it if requested.
+        if (PyInstance_Check(ob)) {
+            if (bTryAutoWrap)
+                // Try and auto-wrap it - errors will leave Py exception
set,
+                return PyXPCOM_XPTStub::AutoWrapPythonInstance(ob, iid,
ppv);
+            PyErr_SetString(PyExc_TypeError, "The Python instance can not
be converted to an XPCOM object");
+            return PR_FALSE;
+        }
        } else {
                Py_INCREF(ob);
        }
+
        PRBool rc = InterfaceFromPyISupports(ob, iid, ppv);
        Py_DECREF(ob);
        return rc;

_______________________________________________
pyxpcom mailing list
pyxpcom@[...].com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Thread:
Tomeu Vizoso
Shane Caraveo
Tomeu Vizoso
Shane Caraveo
Tomeu Vizoso
Mark Hammond

Privacy Policy | Email Opt-out | Feedback | Syndication
© 2004 ActiveState, a division of Sophos All rights reserved