# HG changeset patch # User Atul Varma # Date 1250322655 25200 # Node ID 0701aee1b0cd8addf691487938fedba7edea7d44 # Parent c66d7da09c95550acf65ef4ecea1a82d255cac13 JS-wrapped python functions can now return normal strings (not just unicode). diff -r c66d7da09c95 -r 0701aee1b0cd test_pymonkey.py --- a/test_pymonkey.py Sat Aug 15 00:25:48 2009 -0700 +++ b/test_pymonkey.py Sat Aug 15 00:50:55 2009 -0700 @@ -245,11 +245,17 @@ 'hai2u("blah\x00 ")'), u"blah\x00 o hai") + def testJsWrappedPythonFunctionReturnsString(self): + def hai2u(cx, this, args): + return "o hai" + self.assertEqual(self._evalJsWrappedPyFunc(hai2u, 'hai2u()'), + "o hai") + def testJsWrappedPythonFunctionReturnsUnicode(self): def hai2u(cx, this, args): - return u"o hai" + return u"o hai\u2026" self.assertEqual(self._evalJsWrappedPyFunc(hai2u, 'hai2u()'), - u"o hai") + u"o hai\u2026") def testJsWrappedPythonFunctionThrowsJsException(self): def hai2u(cx, this, args): diff -r c66d7da09c95 -r 0701aee1b0cd utils.c --- a/utils.c Sat Aug 15 00:25:48 2009 -0700 +++ b/utils.c Sat Aug 15 00:50:55 2009 -0700 @@ -59,8 +59,19 @@ PyObject *object, jsval *rval) { - if (PyUnicode_Check(object)) { - PyObject *string = PyUnicode_AsUTF16String(object); + if (PyString_Check(object) || PyUnicode_Check(object)) { + PyObject *unicode; + if (PyString_Check(object)) { + unicode = PyUnicode_FromObject(object); + if (unicode == NULL) + return -1; + } else { + unicode = object; + Py_INCREF(unicode); + } + + PyObject *string = PyUnicode_AsUTF16String(unicode); + Py_DECREF(unicode); if (string == NULL) return -1;