annotate test_pymonkey.py @ 88:0b2970e8cd67

Added a test.
author Atul Varma <varmaa@toolness.com>
date Sun, 09 Aug 2009 22:26:45 -0700
parents 345d4c0e3dd3
children e77bc7c799e8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
46
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
1 import sys
0
21aa6e3abb49 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
2 import unittest
59
fb97bed55789 Added a test that fails b/c of a known bug in pymonkey.
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
3 import weakref
66
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 64
diff changeset
4 import time
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 64
diff changeset
5 import threading
46
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
6
0
21aa6e3abb49 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
7 import pymonkey
21aa6e3abb49 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
8
21aa6e3abb49 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
9 class PymonkeyTests(unittest.TestCase):
16
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
10 def _evaljs(self, code):
9
032cfc448079 Added pymonkey.Runtime and pymonkey.Context as new types.
Atul Varma <varmaa@toolness.com>
parents: 8
diff changeset
11 rt = pymonkey.Runtime()
032cfc448079 Added pymonkey.Runtime and pymonkey.Context as new types.
Atul Varma <varmaa@toolness.com>
parents: 8
diff changeset
12 cx = rt.new_context()
16
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
13 obj = cx.new_object()
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
14 cx.init_standard_classes(obj)
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
15 return cx.evaluate_script(obj, code, '<string>', 1)
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
16
31
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
17 def _evalJsWrappedPyFunc(self, func, code):
24
74b7ad049542 Added very primitive support for calling python functions from JS.
Atul Varma <varmaa@toolness.com>
parents: 22
diff changeset
18 cx = pymonkey.Runtime().new_context()
74b7ad049542 Added very primitive support for calling python functions from JS.
Atul Varma <varmaa@toolness.com>
parents: 22
diff changeset
19 obj = cx.new_object()
74b7ad049542 Added very primitive support for calling python functions from JS.
Atul Varma <varmaa@toolness.com>
parents: 22
diff changeset
20 cx.init_standard_classes(obj)
37
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
21 jsfunc = cx.new_function(func, func.__name__)
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
22 cx.define_property(obj, func.__name__, jsfunc)
31
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
23 return cx.evaluate_script(obj, code, '<string>', 1)
24
74b7ad049542 Added very primitive support for calling python functions from JS.
Atul Varma <varmaa@toolness.com>
parents: 22
diff changeset
24
46
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
25 def assertRaises(self, exctype, func, *args):
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
26 was_raised = False
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
27 try:
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
28 func(*args)
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
29 except exctype, e:
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
30 self.last_exception = e
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
31 was_raised = True
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
32 self.assertTrue(was_raised)
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
33
87
345d4c0e3dd3 Thread safety exceptions are now properly raised by all relevant pymonkey functions.
Atul Varma <varmaa@toolness.com>
parents: 86
diff changeset
34 def testThreadSafetyExceptionIsRaised(self):
345d4c0e3dd3 Thread safety exceptions are now properly raised by all relevant pymonkey functions.
Atul Varma <varmaa@toolness.com>
parents: 86
diff changeset
35 stuff = {}
345d4c0e3dd3 Thread safety exceptions are now properly raised by all relevant pymonkey functions.
Atul Varma <varmaa@toolness.com>
parents: 86
diff changeset
36 def make_runtime():
345d4c0e3dd3 Thread safety exceptions are now properly raised by all relevant pymonkey functions.
Atul Varma <varmaa@toolness.com>
parents: 86
diff changeset
37 stuff['rt'] = pymonkey.Runtime()
345d4c0e3dd3 Thread safety exceptions are now properly raised by all relevant pymonkey functions.
Atul Varma <varmaa@toolness.com>
parents: 86
diff changeset
38 thread = threading.Thread(target = make_runtime)
345d4c0e3dd3 Thread safety exceptions are now properly raised by all relevant pymonkey functions.
Atul Varma <varmaa@toolness.com>
parents: 86
diff changeset
39 thread.start()
345d4c0e3dd3 Thread safety exceptions are now properly raised by all relevant pymonkey functions.
Atul Varma <varmaa@toolness.com>
parents: 86
diff changeset
40 thread.join()
345d4c0e3dd3 Thread safety exceptions are now properly raised by all relevant pymonkey functions.
Atul Varma <varmaa@toolness.com>
parents: 86
diff changeset
41 self.assertRaises(pymonkey.error,
345d4c0e3dd3 Thread safety exceptions are now properly raised by all relevant pymonkey functions.
Atul Varma <varmaa@toolness.com>
parents: 86
diff changeset
42 stuff['rt'].new_context)
345d4c0e3dd3 Thread safety exceptions are now properly raised by all relevant pymonkey functions.
Atul Varma <varmaa@toolness.com>
parents: 86
diff changeset
43 self.assertEqual(self.last_exception.message,
345d4c0e3dd3 Thread safety exceptions are now properly raised by all relevant pymonkey functions.
Atul Varma <varmaa@toolness.com>
parents: 86
diff changeset
44 'Function called from wrong thread')
345d4c0e3dd3 Thread safety exceptions are now properly raised by all relevant pymonkey functions.
Atul Varma <varmaa@toolness.com>
parents: 86
diff changeset
45
81
99138265d4b9 Added a context.clear_object_private() function.
Atul Varma <varmaa@toolness.com>
parents: 77
diff changeset
46 def testClearObjectPrivateWorks(self):
99138265d4b9 Added a context.clear_object_private() function.
Atul Varma <varmaa@toolness.com>
parents: 77
diff changeset
47 class Foo(object):
99138265d4b9 Added a context.clear_object_private() function.
Atul Varma <varmaa@toolness.com>
parents: 77
diff changeset
48 pass
99138265d4b9 Added a context.clear_object_private() function.
Atul Varma <varmaa@toolness.com>
parents: 77
diff changeset
49 pyobj = Foo()
99138265d4b9 Added a context.clear_object_private() function.
Atul Varma <varmaa@toolness.com>
parents: 77
diff changeset
50 cx = pymonkey.Runtime().new_context()
99138265d4b9 Added a context.clear_object_private() function.
Atul Varma <varmaa@toolness.com>
parents: 77
diff changeset
51 obj = cx.new_object(pyobj)
99138265d4b9 Added a context.clear_object_private() function.
Atul Varma <varmaa@toolness.com>
parents: 77
diff changeset
52 pyobj = weakref.ref(pyobj)
99138265d4b9 Added a context.clear_object_private() function.
Atul Varma <varmaa@toolness.com>
parents: 77
diff changeset
53 self.assertEqual(pyobj(), cx.get_object_private(obj))
99138265d4b9 Added a context.clear_object_private() function.
Atul Varma <varmaa@toolness.com>
parents: 77
diff changeset
54 cx.clear_object_private(obj)
99138265d4b9 Added a context.clear_object_private() function.
Atul Varma <varmaa@toolness.com>
parents: 77
diff changeset
55 self.assertEqual(cx.get_object_private(obj), None)
99138265d4b9 Added a context.clear_object_private() function.
Atul Varma <varmaa@toolness.com>
parents: 77
diff changeset
56 self.assertEqual(pyobj(), None)
99138265d4b9 Added a context.clear_object_private() function.
Atul Varma <varmaa@toolness.com>
parents: 77
diff changeset
57
71
9b3f4e53e365 Added context.get_object_private() and an optional private object parameter to context.new_object().
Atul Varma <varmaa@toolness.com>
parents: 66
diff changeset
58 def testGetObjectPrivateWorks(self):
9b3f4e53e365 Added context.get_object_private() and an optional private object parameter to context.new_object().
Atul Varma <varmaa@toolness.com>
parents: 66
diff changeset
59 class Foo(object):
9b3f4e53e365 Added context.get_object_private() and an optional private object parameter to context.new_object().
Atul Varma <varmaa@toolness.com>
parents: 66
diff changeset
60 pass
9b3f4e53e365 Added context.get_object_private() and an optional private object parameter to context.new_object().
Atul Varma <varmaa@toolness.com>
parents: 66
diff changeset
61 pyobj = Foo()
9b3f4e53e365 Added context.get_object_private() and an optional private object parameter to context.new_object().
Atul Varma <varmaa@toolness.com>
parents: 66
diff changeset
62 cx = pymonkey.Runtime().new_context()
9b3f4e53e365 Added context.get_object_private() and an optional private object parameter to context.new_object().
Atul Varma <varmaa@toolness.com>
parents: 66
diff changeset
63 obj = cx.new_object(pyobj)
9b3f4e53e365 Added context.get_object_private() and an optional private object parameter to context.new_object().
Atul Varma <varmaa@toolness.com>
parents: 66
diff changeset
64 pyobj = weakref.ref(pyobj)
9b3f4e53e365 Added context.get_object_private() and an optional private object parameter to context.new_object().
Atul Varma <varmaa@toolness.com>
parents: 66
diff changeset
65 self.assertEqual(pyobj(), cx.get_object_private(obj))
9b3f4e53e365 Added context.get_object_private() and an optional private object parameter to context.new_object().
Atul Varma <varmaa@toolness.com>
parents: 66
diff changeset
66 del obj
9b3f4e53e365 Added context.get_object_private() and an optional private object parameter to context.new_object().
Atul Varma <varmaa@toolness.com>
parents: 66
diff changeset
67 del cx
9b3f4e53e365 Added context.get_object_private() and an optional private object parameter to context.new_object().
Atul Varma <varmaa@toolness.com>
parents: 66
diff changeset
68 self.assertEqual(pyobj(), None)
9b3f4e53e365 Added context.get_object_private() and an optional private object parameter to context.new_object().
Atul Varma <varmaa@toolness.com>
parents: 66
diff changeset
69
64
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 62
diff changeset
70 def testOperationCallbackIsCalled(self):
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 62
diff changeset
71 def opcb(cx):
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 62
diff changeset
72 raise Exception("stop eet!")
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 62
diff changeset
73
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 62
diff changeset
74 cx = pymonkey.Runtime().new_context()
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 62
diff changeset
75 cx.set_operation_callback(opcb)
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 62
diff changeset
76 obj = cx.new_object()
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 62
diff changeset
77 cx.init_standard_classes(obj)
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 62
diff changeset
78
66
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 64
diff changeset
79 def watchdog():
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 64
diff changeset
80 time.sleep(0.1)
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 64
diff changeset
81 cx.trigger_operation_callback()
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 64
diff changeset
82
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 64
diff changeset
83 thread = threading.Thread(target = watchdog)
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 64
diff changeset
84 thread.start()
64
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 62
diff changeset
85
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 62
diff changeset
86 self.assertRaises(
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 62
diff changeset
87 pymonkey.error,
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 62
diff changeset
88 cx.evaluate_script,
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 62
diff changeset
89 obj, 'while (1) {}', '<string>', 1
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 62
diff changeset
90 )
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 62
diff changeset
91
57
a2b617731398 pymonkey.undefined now has a 'falsy' value.
Atul Varma <varmaa@toolness.com>
parents: 52
diff changeset
92 def testUndefinedStrIsUndefined(self):
a2b617731398 pymonkey.undefined now has a 'falsy' value.
Atul Varma <varmaa@toolness.com>
parents: 52
diff changeset
93 self.assertEqual(str(pymonkey.undefined),
a2b617731398 pymonkey.undefined now has a 'falsy' value.
Atul Varma <varmaa@toolness.com>
parents: 52
diff changeset
94 "pymonkey.undefined")
a2b617731398 pymonkey.undefined now has a 'falsy' value.
Atul Varma <varmaa@toolness.com>
parents: 52
diff changeset
95
84
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
96 def testJsWrappedPythonFuncHasPrivate(self):
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
97 def foo(cx, this, args):
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
98 pass
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
99
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
100 cx = pymonkey.Runtime().new_context()
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
101 jsfunc = cx.new_function(foo, foo.__name__)
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
102 self.assertEqual(cx.get_object_private(jsfunc), foo)
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
103
59
fb97bed55789 Added a test that fails b/c of a known bug in pymonkey.
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
104 def testJsWrappedPythonFuncIsNotGCd(self):
fb97bed55789 Added a test that fails b/c of a known bug in pymonkey.
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
105 def define(cx, obj):
fb97bed55789 Added a test that fails b/c of a known bug in pymonkey.
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
106 def func(cx, this, args):
fb97bed55789 Added a test that fails b/c of a known bug in pymonkey.
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
107 return u'func was called'
fb97bed55789 Added a test that fails b/c of a known bug in pymonkey.
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
108 jsfunc = cx.new_function(func, func.__name__)
fb97bed55789 Added a test that fails b/c of a known bug in pymonkey.
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
109 cx.define_property(obj, func.__name__, jsfunc)
fb97bed55789 Added a test that fails b/c of a known bug in pymonkey.
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
110 return weakref.ref(func)
61
1506350991d4 JS Wrapped python functions are now only GC'd by python once they've first been GC'd by JS.
Atul Varma <varmaa@toolness.com>
parents: 59
diff changeset
111 rt = pymonkey.Runtime()
1506350991d4 JS Wrapped python functions are now only GC'd by python once they've first been GC'd by JS.
Atul Varma <varmaa@toolness.com>
parents: 59
diff changeset
112 cx = rt.new_context()
59
fb97bed55789 Added a test that fails b/c of a known bug in pymonkey.
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
113 obj = cx.new_object()
fb97bed55789 Added a test that fails b/c of a known bug in pymonkey.
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
114 cx.init_standard_classes(obj)
fb97bed55789 Added a test that fails b/c of a known bug in pymonkey.
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
115 ref = define(cx, obj)
61
1506350991d4 JS Wrapped python functions are now only GC'd by python once they've first been GC'd by JS.
Atul Varma <varmaa@toolness.com>
parents: 59
diff changeset
116 cx.gc()
59
fb97bed55789 Added a test that fails b/c of a known bug in pymonkey.
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
117 self.assertNotEqual(ref(), None)
fb97bed55789 Added a test that fails b/c of a known bug in pymonkey.
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
118 result = cx.evaluate_script(obj, 'func()', '<string>', 1)
fb97bed55789 Added a test that fails b/c of a known bug in pymonkey.
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
119 self.assertEqual(result, u'func was called')
fb97bed55789 Added a test that fails b/c of a known bug in pymonkey.
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
120
61
1506350991d4 JS Wrapped python functions are now only GC'd by python once they've first been GC'd by JS.
Atul Varma <varmaa@toolness.com>
parents: 59
diff changeset
121 # Now ensure that the wrapped function is GC'd when it's
1506350991d4 JS Wrapped python functions are now only GC'd by python once they've first been GC'd by JS.
Atul Varma <varmaa@toolness.com>
parents: 59
diff changeset
122 # no longer reachable from JS space.
1506350991d4 JS Wrapped python functions are now only GC'd by python once they've first been GC'd by JS.
Atul Varma <varmaa@toolness.com>
parents: 59
diff changeset
123 cx.define_property(obj, 'func', 0)
1506350991d4 JS Wrapped python functions are now only GC'd by python once they've first been GC'd by JS.
Atul Varma <varmaa@toolness.com>
parents: 59
diff changeset
124 cx.gc()
1506350991d4 JS Wrapped python functions are now only GC'd by python once they've first been GC'd by JS.
Atul Varma <varmaa@toolness.com>
parents: 59
diff changeset
125 self.assertEqual(ref(), None)
1506350991d4 JS Wrapped python functions are now only GC'd by python once they've first been GC'd by JS.
Atul Varma <varmaa@toolness.com>
parents: 59
diff changeset
126
84
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
127 def testCircularJsWrappedPythonFuncIsGCdIfPrivateCleared(self):
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
128 def define(cx, obj):
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
129 rt = cx.get_runtime()
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
130 def func(cx, this, args):
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
131 # Oh noes, a circular reference is born!
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
132 rt
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
133 jsfunc = cx.new_function(func, func.__name__)
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
134 cx.define_property(obj, func.__name__, jsfunc)
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
135 return (jsfunc, weakref.ref(func))
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
136 rt = pymonkey.Runtime()
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
137 cx = rt.new_context()
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
138 obj = cx.new_object()
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
139 cx.init_standard_classes(obj)
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
140 jsfunc, ref = define(cx, obj)
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
141
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
142 # This will break the circular reference.
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
143 cx.clear_object_private(jsfunc)
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
144
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
145 del jsfunc
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
146 del rt
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
147 del cx
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
148 del obj
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
149 self.assertEqual(ref(), None)
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
150
62
2b5696b91b01 Fixed a bug whereby some objects wouldn't be finalized when a runtime was destroyed.
Atul Varma <varmaa@toolness.com>
parents: 61
diff changeset
151 def testJsWrappedPythonFuncIsGCdAtRuntimeDestruction(self):
2b5696b91b01 Fixed a bug whereby some objects wouldn't be finalized when a runtime was destroyed.
Atul Varma <varmaa@toolness.com>
parents: 61
diff changeset
152 def define(cx, obj):
2b5696b91b01 Fixed a bug whereby some objects wouldn't be finalized when a runtime was destroyed.
Atul Varma <varmaa@toolness.com>
parents: 61
diff changeset
153 def func(cx, this, args):
2b5696b91b01 Fixed a bug whereby some objects wouldn't be finalized when a runtime was destroyed.
Atul Varma <varmaa@toolness.com>
parents: 61
diff changeset
154 return u'func was called'
2b5696b91b01 Fixed a bug whereby some objects wouldn't be finalized when a runtime was destroyed.
Atul Varma <varmaa@toolness.com>
parents: 61
diff changeset
155 jsfunc = cx.new_function(func, func.__name__)
2b5696b91b01 Fixed a bug whereby some objects wouldn't be finalized when a runtime was destroyed.
Atul Varma <varmaa@toolness.com>
parents: 61
diff changeset
156 cx.define_property(obj, func.__name__, jsfunc)
2b5696b91b01 Fixed a bug whereby some objects wouldn't be finalized when a runtime was destroyed.
Atul Varma <varmaa@toolness.com>
parents: 61
diff changeset
157 return weakref.ref(func)
2b5696b91b01 Fixed a bug whereby some objects wouldn't be finalized when a runtime was destroyed.
Atul Varma <varmaa@toolness.com>
parents: 61
diff changeset
158 rt = pymonkey.Runtime()
2b5696b91b01 Fixed a bug whereby some objects wouldn't be finalized when a runtime was destroyed.
Atul Varma <varmaa@toolness.com>
parents: 61
diff changeset
159 cx = rt.new_context()
2b5696b91b01 Fixed a bug whereby some objects wouldn't be finalized when a runtime was destroyed.
Atul Varma <varmaa@toolness.com>
parents: 61
diff changeset
160 obj = cx.new_object()
2b5696b91b01 Fixed a bug whereby some objects wouldn't be finalized when a runtime was destroyed.
Atul Varma <varmaa@toolness.com>
parents: 61
diff changeset
161 cx.init_standard_classes(obj)
2b5696b91b01 Fixed a bug whereby some objects wouldn't be finalized when a runtime was destroyed.
Atul Varma <varmaa@toolness.com>
parents: 61
diff changeset
162 ref = define(cx, obj)
2b5696b91b01 Fixed a bug whereby some objects wouldn't be finalized when a runtime was destroyed.
Atul Varma <varmaa@toolness.com>
parents: 61
diff changeset
163 del rt
2b5696b91b01 Fixed a bug whereby some objects wouldn't be finalized when a runtime was destroyed.
Atul Varma <varmaa@toolness.com>
parents: 61
diff changeset
164 del cx
2b5696b91b01 Fixed a bug whereby some objects wouldn't be finalized when a runtime was destroyed.
Atul Varma <varmaa@toolness.com>
parents: 61
diff changeset
165 del obj
2b5696b91b01 Fixed a bug whereby some objects wouldn't be finalized when a runtime was destroyed.
Atul Varma <varmaa@toolness.com>
parents: 61
diff changeset
166 self.assertEqual(ref(), None)
2b5696b91b01 Fixed a bug whereby some objects wouldn't be finalized when a runtime was destroyed.
Atul Varma <varmaa@toolness.com>
parents: 61
diff changeset
167
86
16a3e99e9b77 JS-wrapped Python functions that have had their Python functions cleared by context.clear_object_private() now throw appropriate JS exceptions when called.
Atul Varma <varmaa@toolness.com>
parents: 84
diff changeset
168 def testJsWrappedPythonFuncThrowsExcIfPrivateCleared(self):
16a3e99e9b77 JS-wrapped Python functions that have had their Python functions cleared by context.clear_object_private() now throw appropriate JS exceptions when called.
Atul Varma <varmaa@toolness.com>
parents: 84
diff changeset
169 def func(cx, this, args):
16a3e99e9b77 JS-wrapped Python functions that have had their Python functions cleared by context.clear_object_private() now throw appropriate JS exceptions when called.
Atul Varma <varmaa@toolness.com>
parents: 84
diff changeset
170 return True
16a3e99e9b77 JS-wrapped Python functions that have had their Python functions cleared by context.clear_object_private() now throw appropriate JS exceptions when called.
Atul Varma <varmaa@toolness.com>
parents: 84
diff changeset
171
16a3e99e9b77 JS-wrapped Python functions that have had their Python functions cleared by context.clear_object_private() now throw appropriate JS exceptions when called.
Atul Varma <varmaa@toolness.com>
parents: 84
diff changeset
172 code = "func()"
16a3e99e9b77 JS-wrapped Python functions that have had their Python functions cleared by context.clear_object_private() now throw appropriate JS exceptions when called.
Atul Varma <varmaa@toolness.com>
parents: 84
diff changeset
173 cx = pymonkey.Runtime().new_context()
16a3e99e9b77 JS-wrapped Python functions that have had their Python functions cleared by context.clear_object_private() now throw appropriate JS exceptions when called.
Atul Varma <varmaa@toolness.com>
parents: 84
diff changeset
174 obj = cx.new_object()
16a3e99e9b77 JS-wrapped Python functions that have had their Python functions cleared by context.clear_object_private() now throw appropriate JS exceptions when called.
Atul Varma <varmaa@toolness.com>
parents: 84
diff changeset
175 cx.init_standard_classes(obj)
16a3e99e9b77 JS-wrapped Python functions that have had their Python functions cleared by context.clear_object_private() now throw appropriate JS exceptions when called.
Atul Varma <varmaa@toolness.com>
parents: 84
diff changeset
176 jsfunc = cx.new_function(func, func.__name__)
16a3e99e9b77 JS-wrapped Python functions that have had their Python functions cleared by context.clear_object_private() now throw appropriate JS exceptions when called.
Atul Varma <varmaa@toolness.com>
parents: 84
diff changeset
177 cx.define_property(obj, func.__name__, jsfunc)
16a3e99e9b77 JS-wrapped Python functions that have had their Python functions cleared by context.clear_object_private() now throw appropriate JS exceptions when called.
Atul Varma <varmaa@toolness.com>
parents: 84
diff changeset
178 cx.clear_object_private(jsfunc)
16a3e99e9b77 JS-wrapped Python functions that have had their Python functions cleared by context.clear_object_private() now throw appropriate JS exceptions when called.
Atul Varma <varmaa@toolness.com>
parents: 84
diff changeset
179 self.assertRaises(pymonkey.error,
16a3e99e9b77 JS-wrapped Python functions that have had their Python functions cleared by context.clear_object_private() now throw appropriate JS exceptions when called.
Atul Varma <varmaa@toolness.com>
parents: 84
diff changeset
180 cx.evaluate_script,
16a3e99e9b77 JS-wrapped Python functions that have had their Python functions cleared by context.clear_object_private() now throw appropriate JS exceptions when called.
Atul Varma <varmaa@toolness.com>
parents: 84
diff changeset
181 obj, code, '<string>', 1)
16a3e99e9b77 JS-wrapped Python functions that have had their Python functions cleared by context.clear_object_private() now throw appropriate JS exceptions when called.
Atul Varma <varmaa@toolness.com>
parents: 84
diff changeset
182 self.assertEqual(
16a3e99e9b77 JS-wrapped Python functions that have had their Python functions cleared by context.clear_object_private() now throw appropriate JS exceptions when called.
Atul Varma <varmaa@toolness.com>
parents: 84
diff changeset
183 self._tostring(cx, self.last_exception.message),
16a3e99e9b77 JS-wrapped Python functions that have had their Python functions cleared by context.clear_object_private() now throw appropriate JS exceptions when called.
Atul Varma <varmaa@toolness.com>
parents: 84
diff changeset
184 "Error: Wrapped Python function no longer exists"
16a3e99e9b77 JS-wrapped Python functions that have had their Python functions cleared by context.clear_object_private() now throw appropriate JS exceptions when called.
Atul Varma <varmaa@toolness.com>
parents: 84
diff changeset
185 )
16a3e99e9b77 JS-wrapped Python functions that have had their Python functions cleared by context.clear_object_private() now throw appropriate JS exceptions when called.
Atul Varma <varmaa@toolness.com>
parents: 84
diff changeset
186
43
5727675b1bcb JS-wrapped python functions now take a context object as their first parameter.
Atul Varma <varmaa@toolness.com>
parents: 41
diff changeset
187 def testJsWrappedPythonFuncPassesContext(self):
5727675b1bcb JS-wrapped python functions now take a context object as their first parameter.
Atul Varma <varmaa@toolness.com>
parents: 41
diff changeset
188 contexts = []
5727675b1bcb JS-wrapped python functions now take a context object as their first parameter.
Atul Varma <varmaa@toolness.com>
parents: 41
diff changeset
189
52
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
190 def func(cx, this, args):
43
5727675b1bcb JS-wrapped python functions now take a context object as their first parameter.
Atul Varma <varmaa@toolness.com>
parents: 41
diff changeset
191 contexts.append(cx)
5727675b1bcb JS-wrapped python functions now take a context object as their first parameter.
Atul Varma <varmaa@toolness.com>
parents: 41
diff changeset
192 return True
5727675b1bcb JS-wrapped python functions now take a context object as their first parameter.
Atul Varma <varmaa@toolness.com>
parents: 41
diff changeset
193
5727675b1bcb JS-wrapped python functions now take a context object as their first parameter.
Atul Varma <varmaa@toolness.com>
parents: 41
diff changeset
194 code = "func()"
5727675b1bcb JS-wrapped python functions now take a context object as their first parameter.
Atul Varma <varmaa@toolness.com>
parents: 41
diff changeset
195 cx = pymonkey.Runtime().new_context()
5727675b1bcb JS-wrapped python functions now take a context object as their first parameter.
Atul Varma <varmaa@toolness.com>
parents: 41
diff changeset
196 obj = cx.new_object()
5727675b1bcb JS-wrapped python functions now take a context object as their first parameter.
Atul Varma <varmaa@toolness.com>
parents: 41
diff changeset
197 cx.init_standard_classes(obj)
5727675b1bcb JS-wrapped python functions now take a context object as their first parameter.
Atul Varma <varmaa@toolness.com>
parents: 41
diff changeset
198 jsfunc = cx.new_function(func, func.__name__)
5727675b1bcb JS-wrapped python functions now take a context object as their first parameter.
Atul Varma <varmaa@toolness.com>
parents: 41
diff changeset
199 cx.define_property(obj, func.__name__, jsfunc)
5727675b1bcb JS-wrapped python functions now take a context object as their first parameter.
Atul Varma <varmaa@toolness.com>
parents: 41
diff changeset
200 cx.evaluate_script(obj, code, '<string>', 1)
5727675b1bcb JS-wrapped python functions now take a context object as their first parameter.
Atul Varma <varmaa@toolness.com>
parents: 41
diff changeset
201 self.assertEqual(contexts[0], cx)
5727675b1bcb JS-wrapped python functions now take a context object as their first parameter.
Atul Varma <varmaa@toolness.com>
parents: 41
diff changeset
202
52
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
203 def testJsWrappedPythonFuncPassesThisArg(self):
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
204 thisObjs = []
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
205
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
206 def func(cx, this, args):
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
207 thisObjs.append(this)
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
208 return True
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
209
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
210 code = "func()"
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
211 cx = pymonkey.Runtime().new_context()
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
212 obj = cx.new_object()
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
213 cx.init_standard_classes(obj)
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
214 jsfunc = cx.new_function(func, func.__name__)
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
215 cx.define_property(obj, func.__name__, jsfunc)
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
216 cx.evaluate_script(obj, code, '<string>', 1)
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
217 self.assertEqual(thisObjs[0], obj)
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
218
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
219 def testJsWrappedPythonFuncPassesFuncArgs(self):
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
220 funcArgs = []
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
221
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
222 def func(cx, this, args):
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
223 funcArgs.append(args)
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
224 return True
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
225
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
226 cx = pymonkey.Runtime().new_context()
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
227 obj = cx.new_object()
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
228 cx.init_standard_classes(obj)
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
229 jsfunc = cx.new_function(func, func.__name__)
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
230 cx.define_property(obj, func.__name__, jsfunc)
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
231
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
232 cx.evaluate_script(obj, "func()", '<string>', 1)
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
233 self.assertEqual(len(funcArgs[0]), 0)
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
234 self.assertTrue(isinstance(funcArgs[0], tuple))
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
235
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
236 cx.evaluate_script(obj, "func(1, 'foo')", '<string>', 1)
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
237 self.assertEqual(len(funcArgs[1]), 2)
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
238 self.assertEqual(funcArgs[1][0], 1)
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
239 self.assertEqual(funcArgs[1][1], u'foo')
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
240
31
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
241 def testJsWrappedPythonFunctionReturnsUnicode(self):
52
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
242 def hai2u(cx, this, args):
29
608d086d12e3 Added a new PYM_pyObjectToJsval() function that only supports unicode for the moment. Also, whereever we're assuming that Py_UNICODE is UCS-2, we're surrounding such code with #ifndef Py_UNICODE_WIDE.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
243 return u"o hai"
31
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
244 self.assertEqual(self._evalJsWrappedPyFunc(hai2u, 'hai2u()'),
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
245 u"o hai")
24
74b7ad049542 Added very primitive support for calling python functions from JS.
Atul Varma <varmaa@toolness.com>
parents: 22
diff changeset
246
75
4b1149d818e8 Exceptions work a bit more securely now.
Atul Varma <varmaa@toolness.com>
parents: 74
diff changeset
247 def testJsWrappedPythonFunctionThrowsJsException(self):
52
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
248 def hai2u(cx, this, args):
75
4b1149d818e8 Exceptions work a bit more securely now.
Atul Varma <varmaa@toolness.com>
parents: 74
diff changeset
249 raise pymonkey.error(u"blarg")
4b1149d818e8 Exceptions work a bit more securely now.
Atul Varma <varmaa@toolness.com>
parents: 74
diff changeset
250 self.assertRaises(pymonkey.error,
4b1149d818e8 Exceptions work a bit more securely now.
Atul Varma <varmaa@toolness.com>
parents: 74
diff changeset
251 self._evalJsWrappedPyFunc,
4b1149d818e8 Exceptions work a bit more securely now.
Atul Varma <varmaa@toolness.com>
parents: 74
diff changeset
252 hai2u, 'hai2u()')
4b1149d818e8 Exceptions work a bit more securely now.
Atul Varma <varmaa@toolness.com>
parents: 74
diff changeset
253 self.assertEqual(self.last_exception.message, u"blarg")
4b1149d818e8 Exceptions work a bit more securely now.
Atul Varma <varmaa@toolness.com>
parents: 74
diff changeset
254
4b1149d818e8 Exceptions work a bit more securely now.
Atul Varma <varmaa@toolness.com>
parents: 74
diff changeset
255 def testJsWrappedPythonFunctionThrowsPyException(self):
4b1149d818e8 Exceptions work a bit more securely now.
Atul Varma <varmaa@toolness.com>
parents: 74
diff changeset
256 thecx = []
4b1149d818e8 Exceptions work a bit more securely now.
Atul Varma <varmaa@toolness.com>
parents: 74
diff changeset
257 def hai2u(cx, this, args):
4b1149d818e8 Exceptions work a bit more securely now.
Atul Varma <varmaa@toolness.com>
parents: 74
diff changeset
258 thecx.append(cx)
46
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
259 raise Exception("hello")
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
260 self.assertRaises(pymonkey.error,
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
261 self._evalJsWrappedPyFunc,
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
262 hai2u, 'hai2u()')
75
4b1149d818e8 Exceptions work a bit more securely now.
Atul Varma <varmaa@toolness.com>
parents: 74
diff changeset
263 exc = thecx[0].get_object_private(self.last_exception.message)
4b1149d818e8 Exceptions work a bit more securely now.
Atul Varma <varmaa@toolness.com>
parents: 74
diff changeset
264 self.assertEqual(exc.message, "hello")
46
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
265
45
03aec8572461 Python-wrapped JS functions can now return null/None.
Atul Varma <varmaa@toolness.com>
parents: 43
diff changeset
266 def testJsWrappedPythonFunctionReturnsNone(self):
52
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
267 def hai2u(cx, this, args):
45
03aec8572461 Python-wrapped JS functions can now return null/None.
Atul Varma <varmaa@toolness.com>
parents: 43
diff changeset
268 pass
03aec8572461 Python-wrapped JS functions can now return null/None.
Atul Varma <varmaa@toolness.com>
parents: 43
diff changeset
269 self.assertEqual(self._evalJsWrappedPyFunc(hai2u, 'hai2u()'),
03aec8572461 Python-wrapped JS functions can now return null/None.
Atul Varma <varmaa@toolness.com>
parents: 43
diff changeset
270 None)
03aec8572461 Python-wrapped JS functions can now return null/None.
Atul Varma <varmaa@toolness.com>
parents: 43
diff changeset
271
34
5d3d3b25f23f JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents: 32
diff changeset
272 def testJsWrappedPythonFunctionReturnsTrue(self):
52
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
273 def hai2u(cx, this, args):
34
5d3d3b25f23f JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents: 32
diff changeset
274 return True
5d3d3b25f23f JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents: 32
diff changeset
275 self.assertEqual(self._evalJsWrappedPyFunc(hai2u, 'hai2u()'),
5d3d3b25f23f JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents: 32
diff changeset
276 True)
5d3d3b25f23f JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents: 32
diff changeset
277
5d3d3b25f23f JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents: 32
diff changeset
278 def testJsWrappedPythonFunctionReturnsFalse(self):
52
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
279 def hai2u(cx, this, args):
34
5d3d3b25f23f JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents: 32
diff changeset
280 return False
5d3d3b25f23f JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents: 32
diff changeset
281 self.assertEqual(self._evalJsWrappedPyFunc(hai2u, 'hai2u()'),
5d3d3b25f23f JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents: 32
diff changeset
282 False)
5d3d3b25f23f JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents: 32
diff changeset
283
31
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
284 def testJsWrappedPythonFunctionReturnsSmallInt(self):
52
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
285 def hai2u(cx, this, args):
31
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
286 return 5
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
287 self.assertEqual(self._evalJsWrappedPyFunc(hai2u, 'hai2u()'),
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
288 5)
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
289
32
abf14cba9ef5 JS wrapped Python functions can now return floats.
Atul Varma <varmaa@toolness.com>
parents: 31
diff changeset
290 def testJsWrappedPythonFunctionReturnsFloat(self):
52
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
291 def hai2u(cx, this, args):
32
abf14cba9ef5 JS wrapped Python functions can now return floats.
Atul Varma <varmaa@toolness.com>
parents: 31
diff changeset
292 return 5.1
abf14cba9ef5 JS wrapped Python functions can now return floats.
Atul Varma <varmaa@toolness.com>
parents: 31
diff changeset
293 self.assertEqual(self._evalJsWrappedPyFunc(hai2u, 'hai2u()'),
abf14cba9ef5 JS wrapped Python functions can now return floats.
Atul Varma <varmaa@toolness.com>
parents: 31
diff changeset
294 5.1)
abf14cba9ef5 JS wrapped Python functions can now return floats.
Atul Varma <varmaa@toolness.com>
parents: 31
diff changeset
295
31
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
296 def testJsWrappedPythonFunctionReturnsNegativeInt(self):
52
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
297 def hai2u(cx, this, args):
31
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
298 return -5
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
299 self.assertEqual(self._evalJsWrappedPyFunc(hai2u, 'hai2u()'),
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
300 -5)
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
301
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
302 def testJsWrappedPythonFunctionReturnsBigInt(self):
52
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
303 def hai2u(cx, this, args):
31
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
304 return 2147483647
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
305 self.assertEqual(self._evalJsWrappedPyFunc(hai2u, 'hai2u()'),
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
306 2147483647)
24
74b7ad049542 Added very primitive support for calling python functions from JS.
Atul Varma <varmaa@toolness.com>
parents: 22
diff changeset
307
37
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
308 def testDefinePropertyWorksWithObject(self):
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
309 cx = pymonkey.Runtime().new_context()
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
310 obj = cx.new_object()
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
311 cx.init_standard_classes(obj)
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
312 foo = cx.new_object()
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
313 cx.define_property(obj, "foo", foo)
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
314 self.assertEqual(
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
315 cx.evaluate_script(obj, 'foo', '<string>', 1),
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
316 foo
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
317 )
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
318
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
319 def testDefinePropertyWorksWithString(self):
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
320 cx = pymonkey.Runtime().new_context()
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
321 obj = cx.new_object()
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
322 cx.init_standard_classes(obj)
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
323 foo = cx.new_object()
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
324 cx.define_property(obj, "foo", u"hello")
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
325 self.assertEqual(
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
326 cx.evaluate_script(obj, 'foo', '<string>', 1),
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
327 u"hello"
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
328 )
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
329
22
988a8998c75f JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents: 20
diff changeset
330 def testObjectIsIdentityPreserving(self):
988a8998c75f JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents: 20
diff changeset
331 cx = pymonkey.Runtime().new_context()
988a8998c75f JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents: 20
diff changeset
332 obj = cx.new_object()
988a8998c75f JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents: 20
diff changeset
333 cx.init_standard_classes(obj)
47
3f4982759e55 Converting JS exceptions into Python exceptions is now doable, albeit not yet implemented, thanks to the discovery of JSOPTION_DONT_REPORT_UNCAUGHT. Also, JS warnings are now converted into Python warnings.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
334 cx.evaluate_script(obj, 'var foo = {bar: 1}', '<string>', 1)
27
21045074139f Based on my new understanding of JSString */jschar * thanks to folks on #jsapi, I've removed the requirement that SpiderMonkey be in UTF-8 mode to translate strings between Python and SpiderMonkey.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
335 self.assertTrue(isinstance(cx.get_property(obj, u"foo"),
22
988a8998c75f JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents: 20
diff changeset
336 pymonkey.Object))
27
21045074139f Based on my new understanding of JSString */jschar * thanks to folks on #jsapi, I've removed the requirement that SpiderMonkey be in UTF-8 mode to translate strings between Python and SpiderMonkey.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
337 self.assertTrue(cx.get_property(obj, u"foo") is
21045074139f Based on my new understanding of JSString */jschar * thanks to folks on #jsapi, I've removed the requirement that SpiderMonkey be in UTF-8 mode to translate strings between Python and SpiderMonkey.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
338 cx.get_property(obj, u"foo"))
22
988a8998c75f JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents: 20
diff changeset
339
73
efa0cfe6fc03 Resolved two TODOs.
Atul Varma <varmaa@toolness.com>
parents: 71
diff changeset
340 def testObjectGetattrThrowsException(self):
efa0cfe6fc03 Resolved two TODOs.
Atul Varma <varmaa@toolness.com>
parents: 71
diff changeset
341 cx = pymonkey.Runtime().new_context()
efa0cfe6fc03 Resolved two TODOs.
Atul Varma <varmaa@toolness.com>
parents: 71
diff changeset
342 obj = cx.new_object()
efa0cfe6fc03 Resolved two TODOs.
Atul Varma <varmaa@toolness.com>
parents: 71
diff changeset
343 cx.init_standard_classes(obj)
efa0cfe6fc03 Resolved two TODOs.
Atul Varma <varmaa@toolness.com>
parents: 71
diff changeset
344 result = cx.evaluate_script(obj, '({get foo() { throw "blah"; }})',
efa0cfe6fc03 Resolved two TODOs.
Atul Varma <varmaa@toolness.com>
parents: 71
diff changeset
345 '<string>', 1)
efa0cfe6fc03 Resolved two TODOs.
Atul Varma <varmaa@toolness.com>
parents: 71
diff changeset
346 self.assertRaises(pymonkey.error,
efa0cfe6fc03 Resolved two TODOs.
Atul Varma <varmaa@toolness.com>
parents: 71
diff changeset
347 cx.get_property,
efa0cfe6fc03 Resolved two TODOs.
Atul Varma <varmaa@toolness.com>
parents: 71
diff changeset
348 result,
efa0cfe6fc03 Resolved two TODOs.
Atul Varma <varmaa@toolness.com>
parents: 71
diff changeset
349 u"foo")
efa0cfe6fc03 Resolved two TODOs.
Atul Varma <varmaa@toolness.com>
parents: 71
diff changeset
350 self.assertEqual(self.last_exception.message, u"blah")
efa0cfe6fc03 Resolved two TODOs.
Atul Varma <varmaa@toolness.com>
parents: 71
diff changeset
351
76
4cb89d48b068 Added test for infinite recursion.
Atul Varma <varmaa@toolness.com>
parents: 75
diff changeset
352 def testInfiniteRecursionRaisesError(self):
4cb89d48b068 Added test for infinite recursion.
Atul Varma <varmaa@toolness.com>
parents: 75
diff changeset
353 cx = pymonkey.Runtime().new_context()
4cb89d48b068 Added test for infinite recursion.
Atul Varma <varmaa@toolness.com>
parents: 75
diff changeset
354 obj = cx.new_object()
4cb89d48b068 Added test for infinite recursion.
Atul Varma <varmaa@toolness.com>
parents: 75
diff changeset
355 cx.init_standard_classes(obj)
4cb89d48b068 Added test for infinite recursion.
Atul Varma <varmaa@toolness.com>
parents: 75
diff changeset
356 self.assertRaises(
4cb89d48b068 Added test for infinite recursion.
Atul Varma <varmaa@toolness.com>
parents: 75
diff changeset
357 pymonkey.error,
4cb89d48b068 Added test for infinite recursion.
Atul Varma <varmaa@toolness.com>
parents: 75
diff changeset
358 cx.evaluate_script,
4cb89d48b068 Added test for infinite recursion.
Atul Varma <varmaa@toolness.com>
parents: 75
diff changeset
359 obj, '(function foo() { foo(); })();', '<string>', 1
4cb89d48b068 Added test for infinite recursion.
Atul Varma <varmaa@toolness.com>
parents: 75
diff changeset
360 )
4cb89d48b068 Added test for infinite recursion.
Atul Varma <varmaa@toolness.com>
parents: 75
diff changeset
361 self.assertEqual(
4cb89d48b068 Added test for infinite recursion.
Atul Varma <varmaa@toolness.com>
parents: 75
diff changeset
362 self._tostring(cx, self.last_exception.message),
4cb89d48b068 Added test for infinite recursion.
Atul Varma <varmaa@toolness.com>
parents: 75
diff changeset
363 "InternalError: too much recursion"
4cb89d48b068 Added test for infinite recursion.
Atul Varma <varmaa@toolness.com>
parents: 75
diff changeset
364 )
4cb89d48b068 Added test for infinite recursion.
Atul Varma <varmaa@toolness.com>
parents: 75
diff changeset
365
17
0812422ec99e Added a context.get_property() method.
Atul Varma <varmaa@toolness.com>
parents: 16
diff changeset
366 def testObjectGetattrWorks(self):
0812422ec99e Added a context.get_property() method.
Atul Varma <varmaa@toolness.com>
parents: 16
diff changeset
367 cx = pymonkey.Runtime().new_context()
0812422ec99e Added a context.get_property() method.
Atul Varma <varmaa@toolness.com>
parents: 16
diff changeset
368 obj = cx.new_object()
0812422ec99e Added a context.get_property() method.
Atul Varma <varmaa@toolness.com>
parents: 16
diff changeset
369 cx.init_standard_classes(obj)
47
3f4982759e55 Converting JS exceptions into Python exceptions is now doable, albeit not yet implemented, thanks to the discovery of JSOPTION_DONT_REPORT_UNCAUGHT. Also, JS warnings are now converted into Python warnings.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
370 cx.evaluate_script(obj, 'var boop = 5', '<string>', 1)
17
0812422ec99e Added a context.get_property() method.
Atul Varma <varmaa@toolness.com>
parents: 16
diff changeset
371 cx.evaluate_script(obj, 'this["blarg\u2026"] = 5', '<string>', 1)
27
21045074139f Based on my new understanding of JSString */jschar * thanks to folks on #jsapi, I've removed the requirement that SpiderMonkey be in UTF-8 mode to translate strings between Python and SpiderMonkey.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
372 self.assertEqual(cx.get_property(obj, u"beans"),
17
0812422ec99e Added a context.get_property() method.
Atul Varma <varmaa@toolness.com>
parents: 16
diff changeset
373 pymonkey.undefined)
0812422ec99e Added a context.get_property() method.
Atul Varma <varmaa@toolness.com>
parents: 16
diff changeset
374 self.assertEqual(cx.get_property(obj, u"blarg\u2026"), 5)
27
21045074139f Based on my new understanding of JSString */jschar * thanks to folks on #jsapi, I've removed the requirement that SpiderMonkey be in UTF-8 mode to translate strings between Python and SpiderMonkey.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
375 self.assertEqual(cx.get_property(obj, u"boop"), 5)
17
0812422ec99e Added a context.get_property() method.
Atul Varma <varmaa@toolness.com>
parents: 16
diff changeset
376
16
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
377 def testContextIsInstance(self):
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
378 cx = pymonkey.Runtime().new_context()
9
032cfc448079 Added pymonkey.Runtime and pymonkey.Context as new types.
Atul Varma <varmaa@toolness.com>
parents: 8
diff changeset
379 self.assertTrue(isinstance(cx, pymonkey.Context))
032cfc448079 Added pymonkey.Runtime and pymonkey.Context as new types.
Atul Varma <varmaa@toolness.com>
parents: 8
diff changeset
380
16
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
381 def testContextTypeCannotBeInstantiated(self):
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
382 self.assertRaises(TypeError, pymonkey.Context)
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
383
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
384 def testObjectIsInstance(self):
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
385 obj = pymonkey.Runtime().new_context().new_object()
13
ca17531e8c81 Added an object class.
Atul Varma <varmaa@toolness.com>
parents: 9
diff changeset
386 self.assertTrue(isinstance(obj, pymonkey.Object))
37
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
387 self.assertFalse(isinstance(obj, pymonkey.Function))
13
ca17531e8c81 Added an object class.
Atul Varma <varmaa@toolness.com>
parents: 9
diff changeset
388
16
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
389 def testObjectTypeCannotBeInstantiated(self):
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
390 self.assertRaises(TypeError, pymonkey.Object)
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
391
37
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
392 def testFunctionIsInstance(self):
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
393 def boop():
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
394 pass
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
395 obj = pymonkey.Runtime().new_context().new_function(boop, "boop")
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
396 self.assertTrue(isinstance(obj, pymonkey.Object))
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
397 self.assertTrue(isinstance(obj, pymonkey.Function))
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
398
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
399 def testFunctionTypeCannotBeInstantiated(self):
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
400 self.assertRaises(TypeError, pymonkey.Function)
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
401
77
755af0a94c94 Added a pymonkey.Object.get_runtime() method.
Atul Varma <varmaa@toolness.com>
parents: 76
diff changeset
402 def testObjectGetRuntimeWorks(self):
755af0a94c94 Added a pymonkey.Object.get_runtime() method.
Atul Varma <varmaa@toolness.com>
parents: 76
diff changeset
403 rt = pymonkey.Runtime()
755af0a94c94 Added a pymonkey.Object.get_runtime() method.
Atul Varma <varmaa@toolness.com>
parents: 76
diff changeset
404 obj = rt.new_context().new_object()
755af0a94c94 Added a pymonkey.Object.get_runtime() method.
Atul Varma <varmaa@toolness.com>
parents: 76
diff changeset
405 self.assertEqual(obj.get_runtime(), rt)
755af0a94c94 Added a pymonkey.Object.get_runtime() method.
Atul Varma <varmaa@toolness.com>
parents: 76
diff changeset
406
755af0a94c94 Added a pymonkey.Object.get_runtime() method.
Atul Varma <varmaa@toolness.com>
parents: 76
diff changeset
407 def testContextGetRuntimeWorks(self):
16
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
408 rt = pymonkey.Runtime()
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
409 cx = rt.new_context()
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
410 self.assertEqual(cx.get_runtime(), rt)
14
9edcdb4ab12d added an init_standard_classes() method to context objects.
Atul Varma <varmaa@toolness.com>
parents: 13
diff changeset
411
8
6647870380cc Added support for undefined.
Atul Varma <varmaa@toolness.com>
parents: 7
diff changeset
412 def testUndefinedCannotBeInstantiated(self):
6647870380cc Added support for undefined.
Atul Varma <varmaa@toolness.com>
parents: 7
diff changeset
413 self.assertRaises(TypeError, pymonkey.undefined)
6647870380cc Added support for undefined.
Atul Varma <varmaa@toolness.com>
parents: 7
diff changeset
414
46
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
415 def testEvaluateThrowsException(self):
74
e06376295170 JS exceptions thrown out to Python now include the wrapped original exception. This fixes a TODO.
Atul Varma <varmaa@toolness.com>
parents: 73
diff changeset
416 cx = pymonkey.Runtime().new_context()
e06376295170 JS exceptions thrown out to Python now include the wrapped original exception. This fixes a TODO.
Atul Varma <varmaa@toolness.com>
parents: 73
diff changeset
417 obj = cx.new_object()
46
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
418 self.assertRaises(pymonkey.error,
74
e06376295170 JS exceptions thrown out to Python now include the wrapped original exception. This fixes a TODO.
Atul Varma <varmaa@toolness.com>
parents: 73
diff changeset
419 cx.evaluate_script,
e06376295170 JS exceptions thrown out to Python now include the wrapped original exception. This fixes a TODO.
Atul Varma <varmaa@toolness.com>
parents: 73
diff changeset
420 obj, 'hai2u()', '<string>', 1)
e06376295170 JS exceptions thrown out to Python now include the wrapped original exception. This fixes a TODO.
Atul Varma <varmaa@toolness.com>
parents: 73
diff changeset
421 self.assertEqual(self._tostring(cx,
e06376295170 JS exceptions thrown out to Python now include the wrapped original exception. This fixes a TODO.
Atul Varma <varmaa@toolness.com>
parents: 73
diff changeset
422 self.last_exception.message),
47
3f4982759e55 Converting JS exceptions into Python exceptions is now doable, albeit not yet implemented, thanks to the discovery of JSOPTION_DONT_REPORT_UNCAUGHT. Also, JS warnings are now converted into Python warnings.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
423 'ReferenceError: hai2u is not defined')
46
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
424
8
6647870380cc Added support for undefined.
Atul Varma <varmaa@toolness.com>
parents: 7
diff changeset
425 def testEvaluateReturnsUndefined(self):
16
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
426 retval = self._evaljs("")
8
6647870380cc Added support for undefined.
Atul Varma <varmaa@toolness.com>
parents: 7
diff changeset
427 self.assertTrue(retval is pymonkey.undefined)
6647870380cc Added support for undefined.
Atul Varma <varmaa@toolness.com>
parents: 7
diff changeset
428
28
bd30f5c02fc3 Added a new test for supplementary multilingual plane unicode.
Atul Varma <varmaa@toolness.com>
parents: 27
diff changeset
429 def testEvaluateReturnsSMPUnicode(self):
bd30f5c02fc3 Added a new test for supplementary multilingual plane unicode.
Atul Varma <varmaa@toolness.com>
parents: 27
diff changeset
430 # This is 'LINEAR B SYLLABLE B008 A', in the supplementary
bd30f5c02fc3 Added a new test for supplementary multilingual plane unicode.
Atul Varma <varmaa@toolness.com>
parents: 27
diff changeset
431 # multilingual plane (SMP).
bd30f5c02fc3 Added a new test for supplementary multilingual plane unicode.
Atul Varma <varmaa@toolness.com>
parents: 27
diff changeset
432 retval = self._evaljs("'\uD800\uDC00'")
bd30f5c02fc3 Added a new test for supplementary multilingual plane unicode.
Atul Varma <varmaa@toolness.com>
parents: 27
diff changeset
433 self.assertEqual(retval, u'\U00010000')
bd30f5c02fc3 Added a new test for supplementary multilingual plane unicode.
Atul Varma <varmaa@toolness.com>
parents: 27
diff changeset
434 self.assertEqual(retval.encode('utf-16'),
bd30f5c02fc3 Added a new test for supplementary multilingual plane unicode.
Atul Varma <varmaa@toolness.com>
parents: 27
diff changeset
435 '\xff\xfe\x00\xd8\x00\xdc')
bd30f5c02fc3 Added a new test for supplementary multilingual plane unicode.
Atul Varma <varmaa@toolness.com>
parents: 27
diff changeset
436
bd30f5c02fc3 Added a new test for supplementary multilingual plane unicode.
Atul Varma <varmaa@toolness.com>
parents: 27
diff changeset
437 def testEvaluateReturnsBMPUnicode(self):
16
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
438 retval = self._evaljs("'o hai\u2026'")
7
0d0ce6415b66 Added support for unicode strings.
Atul Varma <varmaa@toolness.com>
parents: 5
diff changeset
439 self.assertTrue(type(retval) == unicode)
0d0ce6415b66 Added support for unicode strings.
Atul Varma <varmaa@toolness.com>
parents: 5
diff changeset
440 self.assertEqual(retval, u'o hai\u2026')
0d0ce6415b66 Added support for unicode strings.
Atul Varma <varmaa@toolness.com>
parents: 5
diff changeset
441
20
abede8af8cf5 PYM_jsvalToPyObject() can now deal with JSObjects.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
442 def testEvaluateReturnsObject(self):
41
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
443 cx = pymonkey.Runtime().new_context()
20
abede8af8cf5 PYM_jsvalToPyObject() can now deal with JSObjects.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
444 obj = cx.new_object()
abede8af8cf5 PYM_jsvalToPyObject() can now deal with JSObjects.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
445 cx.init_standard_classes(obj)
abede8af8cf5 PYM_jsvalToPyObject() can now deal with JSObjects.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
446 obj = cx.evaluate_script(obj, '({boop: 1})', '<string>', 1)
abede8af8cf5 PYM_jsvalToPyObject() can now deal with JSObjects.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
447 self.assertTrue(isinstance(obj, pymonkey.Object))
27
21045074139f Based on my new understanding of JSString */jschar * thanks to folks on #jsapi, I've removed the requirement that SpiderMonkey be in UTF-8 mode to translate strings between Python and SpiderMonkey.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
448 self.assertEqual(cx.get_property(obj, u"boop"), 1)
20
abede8af8cf5 PYM_jsvalToPyObject() can now deal with JSObjects.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
449
40
8a7abd0bb48d Renamed PYM_newJSFunction() to PYM_newJSFunctionFromCallable(). PYM_newJSObject() now returns objects of type PYM_JSFunctionType as needed.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
450 def testEvaluateReturnsFunction(self):
41
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
451 cx = pymonkey.Runtime().new_context()
40
8a7abd0bb48d Renamed PYM_newJSFunction() to PYM_newJSFunctionFromCallable(). PYM_newJSObject() now returns objects of type PYM_JSFunctionType as needed.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
452 obj = cx.new_object()
8a7abd0bb48d Renamed PYM_newJSFunction() to PYM_newJSFunctionFromCallable(). PYM_newJSObject() now returns objects of type PYM_JSFunctionType as needed.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
453 cx.init_standard_classes(obj)
8a7abd0bb48d Renamed PYM_newJSFunction() to PYM_newJSFunctionFromCallable(). PYM_newJSObject() now returns objects of type PYM_JSFunctionType as needed.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
454 obj = cx.evaluate_script(obj, '(function boop() { return 1; })',
8a7abd0bb48d Renamed PYM_newJSFunction() to PYM_newJSFunctionFromCallable(). PYM_newJSObject() now returns objects of type PYM_JSFunctionType as needed.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
455 '<string>', 1)
8a7abd0bb48d Renamed PYM_newJSFunction() to PYM_newJSFunctionFromCallable(). PYM_newJSObject() now returns objects of type PYM_JSFunctionType as needed.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
456 self.assertTrue(isinstance(obj, pymonkey.Function))
8a7abd0bb48d Renamed PYM_newJSFunction() to PYM_newJSFunctionFromCallable(). PYM_newJSObject() now returns objects of type PYM_JSFunctionType as needed.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
457
88
0b2970e8cd67 Added a test.
Atul Varma <varmaa@toolness.com>
parents: 87
diff changeset
458 def testJsExceptionStateIsClearedAfterExceptionIsCaught(self):
0b2970e8cd67 Added a test.
Atul Varma <varmaa@toolness.com>
parents: 87
diff changeset
459 cx = pymonkey.Runtime().new_context()
0b2970e8cd67 Added a test.
Atul Varma <varmaa@toolness.com>
parents: 87
diff changeset
460 obj = cx.new_object()
0b2970e8cd67 Added a test.
Atul Varma <varmaa@toolness.com>
parents: 87
diff changeset
461 self.assertRaises(pymonkey.error,
0b2970e8cd67 Added a test.
Atul Varma <varmaa@toolness.com>
parents: 87
diff changeset
462 cx.evaluate_script,
0b2970e8cd67 Added a test.
Atul Varma <varmaa@toolness.com>
parents: 87
diff changeset
463 obj, 'blah()', '<string>', 1)
0b2970e8cd67 Added a test.
Atul Varma <varmaa@toolness.com>
parents: 87
diff changeset
464 self.assertEqual(cx.evaluate_script(obj, '5+3', '<string>', 1),
0b2970e8cd67 Added a test.
Atul Varma <varmaa@toolness.com>
parents: 87
diff changeset
465 8)
0b2970e8cd67 Added a test.
Atul Varma <varmaa@toolness.com>
parents: 87
diff changeset
466
41
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
467 def testCallFunctionRaisesErrorOnBadFuncArgs(self):
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
468 cx = pymonkey.Runtime().new_context()
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
469 obj = cx.new_object()
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
470 obj = cx.evaluate_script(
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
471 obj,
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
472 '(function boop(a, b) { return a+b+this.c; })',
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
473 '<string>', 1
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
474 )
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
475 self.assertRaises(
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
476 NotImplementedError,
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
477 cx.call_function,
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
478 obj, obj, (1, self)
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
479 )
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
480
74
e06376295170 JS exceptions thrown out to Python now include the wrapped original exception. This fixes a TODO.
Atul Varma <varmaa@toolness.com>
parents: 73
diff changeset
481 def _tostring(self, cx, obj):
e06376295170 JS exceptions thrown out to Python now include the wrapped original exception. This fixes a TODO.
Atul Varma <varmaa@toolness.com>
parents: 73
diff changeset
482 return cx.call_function(obj,
e06376295170 JS exceptions thrown out to Python now include the wrapped original exception. This fixes a TODO.
Atul Varma <varmaa@toolness.com>
parents: 73
diff changeset
483 cx.get_property(obj, u"toString"),
e06376295170 JS exceptions thrown out to Python now include the wrapped original exception. This fixes a TODO.
Atul Varma <varmaa@toolness.com>
parents: 73
diff changeset
484 ())
e06376295170 JS exceptions thrown out to Python now include the wrapped original exception. This fixes a TODO.
Atul Varma <varmaa@toolness.com>
parents: 73
diff changeset
485
41
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
486 def testCallFunctionRaisesErrorFromJS(self):
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
487 cx = pymonkey.Runtime().new_context()
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
488 obj = cx.new_object()
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
489 obj = cx.evaluate_script(
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
490 obj,
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
491 '(function boop(a, b) { blarg(); })',
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
492 '<string>', 1
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
493 )
46
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
494 self.assertRaises(pymonkey.error,
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
495 cx.call_function,
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
496 obj, obj, (1,))
74
e06376295170 JS exceptions thrown out to Python now include the wrapped original exception. This fixes a TODO.
Atul Varma <varmaa@toolness.com>
parents: 73
diff changeset
497 self.assertEqual(self._tostring(cx,
e06376295170 JS exceptions thrown out to Python now include the wrapped original exception. This fixes a TODO.
Atul Varma <varmaa@toolness.com>
parents: 73
diff changeset
498 self.last_exception.message),
47
3f4982759e55 Converting JS exceptions into Python exceptions is now doable, albeit not yet implemented, thanks to the discovery of JSOPTION_DONT_REPORT_UNCAUGHT. Also, JS warnings are now converted into Python warnings.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
499 'ReferenceError: blarg is not defined')
41
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
500
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
501 def testCallFunctionWorks(self):
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
502 cx = pymonkey.Runtime().new_context()
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
503 obj = cx.new_object()
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
504 thisArg = cx.new_object()
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
505 cx.define_property(thisArg, "c", 3)
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
506 cx.init_standard_classes(obj)
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
507 obj = cx.evaluate_script(
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
508 obj,
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
509 '(function boop(a, b) { return a+b+this.c; })',
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
510 '<string>', 1
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
511 )
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
512 self.assertEqual(cx.call_function(thisArg, obj, (1,2)), 6)
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
513
5
aae78eac86d6 Added support for booleans.
Atul Varma <varmaa@toolness.com>
parents: 4
diff changeset
514 def testEvaluateReturnsTrue(self):
16
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
515 self.assertTrue(self._evaljs('true') is True)
5
aae78eac86d6 Added support for booleans.
Atul Varma <varmaa@toolness.com>
parents: 4
diff changeset
516
aae78eac86d6 Added support for booleans.
Atul Varma <varmaa@toolness.com>
parents: 4
diff changeset
517 def testEvaluateReturnsFalse(self):
16
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
518 self.assertTrue(self._evaljs('false') is False)
5
aae78eac86d6 Added support for booleans.
Atul Varma <varmaa@toolness.com>
parents: 4
diff changeset
519
4
2711b636f8e6 Added support for NULL return values.
Atul Varma <varmaa@toolness.com>
parents: 3
diff changeset
520 def testEvaluateReturnsNone(self):
16
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
521 self.assertTrue(self._evaljs('null') is None)
4
2711b636f8e6 Added support for NULL return values.
Atul Varma <varmaa@toolness.com>
parents: 3
diff changeset
522
3
d6a0819ca6ca Added return value support for doubles.
Atul Varma <varmaa@toolness.com>
parents: 2
diff changeset
523 def testEvaluateReturnsIntegers(self):
16
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
524 self.assertEqual(self._evaljs('1+3'), 4)
0
21aa6e3abb49 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
525
31
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
526 def testEvaluateReturnsNegativeIntegers(self):
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
527 self.assertEqual(self._evaljs('-5'), -5)
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
528
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
529 def testEvaluateReturnsBigIntegers(self):
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
530 self.assertEqual(self._evaljs('2147483647*2'),
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
531 2147483647*2)
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
532
3
d6a0819ca6ca Added return value support for doubles.
Atul Varma <varmaa@toolness.com>
parents: 2
diff changeset
533 def testEvaluateReturnsFloats(self):
16
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
534 self.assertEqual(self._evaljs('1.1+3'), 4.1)
3
d6a0819ca6ca Added return value support for doubles.
Atul Varma <varmaa@toolness.com>
parents: 2
diff changeset
535
0
21aa6e3abb49 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
536 if __name__ == '__main__':
21aa6e3abb49 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
537 unittest.main()