iamrayw / python-spidermonkey

Automatically exported from code.google.com/p/python-spidermonkey
GNU General Public License v2.0
0 stars 0 forks source link

Can't bind objects from imported classes #7

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Hi,

thanks for reworking on this!

I found a bug I think trying to bind objects that are from imported classes.

{{{
import spidermonkey
from datetime import datetime

rt = spidermonkey.Runtime()
cx=rt.new_context()
cx.bind_class(datetime)
d = datetime(2008, 10, 31)
cx.bind_object('boo', d)
}}}

or
{{{
import spidermonkey
import datetime

rt = spidermonkey.Runtime()
cx=rt.new_context()
cx.bind_class(datetime.datetime)

d = datetime.datetime(2008, 10, 31)
cx.bind_object('boo', d)
}}}

borks with

{{{
Traceback (most recent call last):
  File "test2.py", line 12, in <module>
    cx.bind_object('boo', d)
  File "spidermonkey.pyx", line 591, in spidermonkey.Context.bind_object
    proxy_class = context_get_class(self, js_classname(obj))
  File "spidermonkey.pyx", line 688, in spidermonkey.context_get_class
    raise ValueError("no class named '%s' is bound" % name)
ValueError: no class named 'datetime' is bound
}}}

the name should be 'datetime.datetime'.  Ok not the best example, perhaps doing 
the same thing 
with 'import subprocess.Popen', etc, the error message says "no class named 
'Popen' is bound"

looks like the module name/scope is being dropped.

thanks again,

-nickg

Original issue reported on code.google.com by nickgsup...@gmail.com on 28 Oct 2008 at 3:04

GoogleCodeExporter commented 8 years ago
Wow this is old code.  So the issue isn't imports or paths or modules. The 
problem is the code doesn't 
understand new style class (e.g. class foo(object) vs. class foo()) which I 
think started in python 2.2??

Also there are some new PyRex helpers that are useful.

Here's the fix:

around line 746:
{{{
def js_classname(pobj):
    # class or instance?
    if inspect.isclass(pobj):
        klass = pobj
    else:
        klass = pobj.__class__
}}}

or in patch form:

{{{
Index: spidermonkey.pyx
===================================================================
--- spidermonkey.pyx    (revision 29)
+++ spidermonkey.pyx    (working copy)
@@ -745,10 +745,10 @@

 def js_classname(pobj):
     # class or instance?
-    try:
+    if inspect.isclass(pobj):
+        klass = pobj
+    else:
         klass = pobj.__class__
-    except AttributeError:
-        klass = pobj

     try:
         name = klass.js_name
}}}

You'll want to update the test to have a few new style classes too.  Not sure 
how you want to do this, so I 
punted.

Original comment by nickgsup...@gmail.com on 28 Oct 2008 at 5:11