changeset 7:0d0ce6415b66

Added support for unicode strings.
author Atul Varma <varmaa@toolness.com>
date Sun, 28 Jun 2009 13:09:39 -0700
parents 42f57789f84f
children 6647870380cc
files pymonkey.c test_pymonkey.py
diffstat 2 files changed, 21 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/pymonkey.c	Sun Jun 28 12:48:29 2009 -0700
+++ b/pymonkey.c	Sun Jun 28 13:09:39 2009 -0700
@@ -29,6 +29,19 @@
   if (JSVAL_IS_NULL(value))
     Py_RETURN_NONE;
 
+  if (JSVAL_IS_STRING(value) && JS_CStringsAreUTF8()) {
+    // TODO: What to do if C strings aren't UTF-8?  The jschar *
+    // type isn't actually UTF-16, it's just "UTF-16-ish", so
+    // there doesn't seem to be any other lossless way of
+    // transferring the string other than perhaps by transmitting
+    // its JSON representation.
+
+    JSString *str = JSVAL_TO_STRING(value);
+    const char *bytes = JS_GetStringBytes(str);
+    const char *errors;
+    return PyUnicode_DecodeUTF8(bytes, strlen(bytes), errors);
+  }
+
   // TODO: Support more types.
   PyErr_SetString(PyExc_NotImplementedError,
                   "Data type conversion not implemented.");
@@ -46,6 +59,9 @@
                         &filename, &lineNo))
     return NULL;
 
+  if (!JS_CStringsAreUTF8())
+    JS_SetCStringsAreUTF8();
+
   JSRuntime *rt = JS_NewRuntime(8L * 1024L * 1024L);
   if (rt == NULL) {
     PyErr_SetString(PYM_error, "JS_NewRuntime() failed");
--- a/test_pymonkey.py	Sun Jun 28 12:48:29 2009 -0700
+++ b/test_pymonkey.py	Sun Jun 28 13:09:39 2009 -0700
@@ -2,6 +2,11 @@
 import pymonkey
 
 class PymonkeyTests(unittest.TestCase):
+    def testEvaluateReturnsUnicode(self):
+        retval = pymonkey.evaluate("'o hai\u2026'", '<string>', 1)
+        self.assertTrue(type(retval) == unicode)
+        self.assertEqual(retval, u'o hai\u2026')
+
     def testEvaluateReturnsTrue(self):
         self.assertTrue(pymonkey.evaluate('true', '<string>', 1) is True)