changeset 91:97d1faf02460

Got rid of Py_UNICODE_WIDE ifdefs.
author Atul Varma <varmaa@toolness.com>
date Sat, 15 Aug 2009 00:10:29 -0700
parents c41f1d2e8f9d
children d2891ca1b89c
files context.c utils.c
diffstat 2 files changed, 26 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/context.c	Fri Aug 14 20:26:40 2009 -0700
+++ b/context.c	Sat Aug 15 00:10:29 2009 -0700
@@ -212,43 +212,36 @@
 PYM_getProperty(PYM_JSContextObject *self, PyObject *args)
 {
   PYM_SANITY_CHECK(self->runtime);
-#ifndef Py_UNICODE_WIDE
   PYM_JSObject *object;
-  Py_UNICODE *string;
+  char *buffer = NULL;
+  int size;
 
-  if (!PyArg_ParseTuple(args, "O!u", &PYM_JSObjectType, &object,
-                        &string))
+  if (!PyArg_ParseTuple(args, "O!es#", &PYM_JSObjectType, &object,
+                        "utf-16", &buffer, &size))
     return NULL;
 
-  PYM_ENSURE_RUNTIME_MATCH(self->runtime, object->runtime);
-
-  JSString *jsString = JS_NewUCStringCopyZ(self->cx,
-                                           (const jschar *) string);
-  if (jsString == NULL) {
-    PyErr_SetString(PYM_error, "JS_NewStringCopyZ() failed");
-    return NULL;
+  if (self->runtime != object->runtime) {
+    PyMem_Free(buffer);
+    PYM_ENSURE_RUNTIME_MATCH(self->runtime, object->runtime);
   }
 
   jsval val;
   JSBool result;
   Py_BEGIN_ALLOW_THREADS;
-  result = JS_GetUCProperty(self->cx, object->obj,
-                            JS_GetStringChars(jsString),
-                            JS_GetStringLength(jsString), &val);
+  // Note that we're manipulating buffer and size here to get rid of
+  // the BOM.
+  result = JS_GetUCProperty(self->cx, object->obj, (jschar *) (buffer + 2),
+                            (size / 2) - 1, &val);
   Py_END_ALLOW_THREADS;
 
+  PyMem_Free(buffer);
+
   if (!result) {
     PYM_jsExceptionToPython(self);
     return NULL;
   }
 
   return PYM_jsvalToPyObject(self, val);
-#else
-  PyErr_SetString(PyExc_NotImplementedError,
-                  "This function is not yet implemented for wide "
-                  "unicode builds of Python.");
-  return NULL;
-#endif
 }
 
 static PyObject *
--- a/utils.c	Fri Aug 14 20:26:40 2009 -0700
+++ b/utils.c	Sat Aug 15 00:10:29 2009 -0700
@@ -59,11 +59,20 @@
                     PyObject *object,
                     jsval *rval)
 {
-#ifndef Py_UNICODE_WIDE
   if (PyUnicode_Check(object)) {
-    Py_UNICODE *string = PyUnicode_AsUnicode(object);
-    JSString *jsString = JS_NewUCStringCopyZ(context->cx,
-                                             (const jschar *) string);
+    PyObject *string = PyUnicode_AsUTF16String(object);
+    if (string == NULL)
+      return -1;
+
+    char *buffer = PyString_AS_STRING(string);
+    Py_ssize_t size = PyString_GET_SIZE(string);
+
+    // Note that we're manipulating buffer and size here to get rid of
+    // the BOM.
+    JSString *jsString = JS_NewUCStringCopyN(context->cx,
+                                             (const jschar *) (buffer + 2),
+                                             (size / 2) - 1);
+    Py_DECREF(string);
     if (jsString == NULL) {
       PyErr_SetString(PYM_error, "JS_NewUCStringCopyZ() failed");
       return -1;
@@ -72,7 +81,6 @@
     *rval = STRING_TO_JSVAL(jsString);
     return 0;
   }
-#endif
 
   if (PyInt_Check(object)) {
     long number = PyInt_AS_LONG(object);