lucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From va...@apache.org
Subject svn commit: r732916 [7/14] - in /lucene/pylucene/trunk: ./ java/ java/org/ java/org/osafoundation/ java/org/osafoundation/lucene/ java/org/osafoundation/lucene/analysis/ java/org/osafoundation/lucene/queryParser/ java/org/osafoundation/lucene/search/ j...
Date Fri, 09 Jan 2009 03:28:41 GMT
Added: lucene/pylucene/trunk/jcc/jcc/sources/functions.cpp
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/jcc/jcc/sources/functions.cpp?rev=732916&view=auto
==============================================================================
--- lucene/pylucene/trunk/jcc/jcc/sources/functions.cpp (added)
+++ lucene/pylucene/trunk/jcc/jcc/sources/functions.cpp Thu Jan  8 19:28:33 2009
@@ -0,0 +1,1092 @@
+/*
+ *   Copyright (c) 2007-2008 Open Source Applications Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ */
+
+#include <jni.h>
+#include <stdarg.h>
+
+#include "java/lang/Object.h"
+#include "java/lang/Class.h"
+#include "java/lang/String.h"
+#include "java/lang/Throwable.h"
+#include "java/lang/Boolean.h"
+#include "java/lang/Integer.h"
+#include "java/lang/Long.h"
+#include "java/lang/Double.h"
+#include "java/util/Iterator.h"
+#include "JArray.h"
+#include "functions.h"
+#include "macros.h"
+
+using namespace java::lang;
+using namespace java::util;
+
+PyObject *PyExc_JavaError = PyExc_ValueError;
+PyObject *PyExc_InvalidArgsError = PyExc_ValueError;
+
+PyObject *_setExceptionTypes(PyObject *self, PyObject *args)
+{
+    if (!PyArg_ParseTuple(args, "OO",
+                          &PyExc_JavaError, &PyExc_InvalidArgsError))
+        return NULL;
+
+    Py_RETURN_NONE;
+}
+
+PyObject *findClass(PyObject *self, PyObject *args)
+{
+    char *className;
+
+    if (!PyArg_ParseTuple(args, "s", &className))
+        return NULL;
+
+    try {
+        jclass cls = env->findClass(className);
+
+        if (cls)
+            return t_Class::wrap_Object(Class(cls));
+    } catch (JCCEnv::exception e) {
+        PyErr_SetJavaError(e.throwable);
+        return NULL;
+    }
+
+    Py_RETURN_NONE;
+}
+
+
+#if defined(_MSC_VER) || defined(__SUNPRO_CC)
+int __parseArgs(PyObject *args, char *types, ...)
+{
+    int count = ((PyTupleObject *)(args))->ob_size;
+    va_list list, check;
+
+    va_start(list, types);
+    va_start(check, types);
+
+    return _parseArgs(((PyTupleObject *)(args))->ob_item, count, types,
+		      list, check);
+}
+
+int __parseArg(PyObject *arg, char *types, ...)
+{
+    va_list list, check;
+
+    va_start(list, types);
+    va_start(check, types);
+
+    return _parseArgs(&arg, 1, types, list, check);
+}
+
+int _parseArgs(PyObject **args, unsigned int count, char *types,
+	       va_list list, va_list check)
+{
+    unsigned int typeCount = strlen(types);
+
+    if (count > typeCount)
+        return -1;
+#else
+
+int _parseArgs(PyObject **args, unsigned int count, char *types, ...)
+{
+    unsigned int typeCount = strlen(types);
+    va_list list, check;
+
+    if (count > typeCount)
+        return -1;
+
+    va_start(list, types);
+    va_start(check, types);
+#endif
+
+    unsigned int pos = 0;
+    int array = 0;
+
+    for (unsigned int a = 0; a < count; a++, pos++) {
+        PyObject *arg = args[a];
+
+        switch (types[pos]) {
+          case '[':
+          {
+              if (++array > 1)
+                  return -1;
+
+              a -= 1;
+              break;
+          }
+
+          case 'j':           /* Java object */
+          {
+              Class *cls = va_arg(list, Class *);
+              
+              if (arg == Py_None)
+                  break;
+
+              /* ensure that class Class is initialized (which may not be the
+               * case because of earlier recursion avoidance (JObject(cls)).
+               */
+              if (!Class::class$)
+                  Class::initializeClass();
+
+              if (array)
+              {
+                  if (PyObject_TypeCheck(arg, JArrayObjectType))
+                      break;
+
+                  if (PySequence_Check(arg) &&
+                      !PyString_Check(arg) && !PyUnicode_Check(arg))
+                  {
+                      if (PySequence_Length(arg) > 0)
+                      {
+                          PyObject *obj = PySequence_GetItem(arg, 0);
+                          int ok =
+                              (obj == Py_None ||
+                               PyObject_TypeCheck(obj, &ObjectType) &&
+                               cls->isInstance(((t_Object *) obj)->object));
+
+                          Py_DECREF(obj);
+                          if (ok)
+                              break;
+                      }
+                      else
+                          break;
+                  }
+              }
+              else if (PyObject_TypeCheck(arg, &ObjectType) &&
+                       cls->isInstance(((t_Object *) arg)->object))
+                  break;
+              else if (PyObject_TypeCheck(arg, &FinalizerProxyType))
+              {
+                  arg = ((t_fp *) arg)->object;
+                  if (PyObject_TypeCheck(arg, &ObjectType) &&
+                      cls->isInstance(((t_Object *) arg)->object))
+                      break;
+              }
+
+              return -1;
+          }
+
+          case 'Z':           /* boolean, strict */
+          {
+              if (array)
+              {
+                  if (arg == Py_None)
+                      break;
+
+                  if (PyObject_TypeCheck(arg, JArrayBoolType))
+                      break;
+
+                  if (PySequence_Check(arg))
+                  {
+                      if (PySequence_Length(arg) > 0)
+                      {
+                          PyObject *obj = PySequence_GetItem(arg, 0);
+                          int ok = obj == Py_True || obj == Py_False;
+
+                          Py_DECREF(obj);
+                          if (ok)
+                              break;
+                      }
+                      else
+                          break;
+                  }
+              }
+              else if (arg == Py_True || arg == Py_False)
+                  break;
+
+              return -1;
+          }
+
+          case 'B':           /* byte */
+          {
+              if (array)
+              {
+                  if (arg == Py_None)
+                      break;
+                  if (PyObject_TypeCheck(arg, JArrayByteType))
+                      break;
+                  if (arg == Py_None || PyString_Check(arg))
+                      break;
+              }
+              else if (PyString_Check(arg) && (PyString_Size(arg) == 1))
+                  break;
+              return -1;
+          }
+
+          case 'C':           /* char */
+          {
+              if (array)
+              {
+                  if (arg == Py_None)
+                      break;
+                  if (PyObject_TypeCheck(arg, JArrayCharType))
+                      break;
+                  if (arg == Py_None || PyUnicode_Check(arg))
+                      break;
+              }
+              else if (PyUnicode_Check(arg) && PyUnicode_GET_SIZE(arg) == 1)
+                  break;
+              return -1;
+          }
+
+          case 'I':           /* int */
+          {
+              if (array)
+              {
+                  if (arg == Py_None)
+                      break;
+
+                  if (PyObject_TypeCheck(arg, JArrayIntType))
+                      break;
+
+                  if (PySequence_Check(arg))
+                  {
+                      if (PySequence_Length(arg) > 0)
+                      {
+                          PyObject *obj = PySequence_GetItem(arg, 0);
+                          int ok = PyInt_CheckExact(obj);
+
+                          Py_DECREF(obj);
+                          if (ok)
+                              break;
+                      }
+                      else
+                          break;
+                  }
+              }
+              else if (PyInt_CheckExact(arg))
+                  break;
+
+              return -1;
+          }
+
+          case 'S':           /* short */
+          {
+              if (array)
+              {
+                  if (arg == Py_None)
+                      break;
+
+                  if (PyObject_TypeCheck(arg, JArrayShortType))
+                      break;
+
+                  if (PySequence_Check(arg))
+                  {
+                      if (PySequence_Length(arg) > 0)
+                      {
+                          PyObject *obj = PySequence_GetItem(arg, 0);
+                          int ok = PyInt_CheckExact(obj);
+
+                          Py_DECREF(obj);
+                          if (ok)
+                              break;
+                      }
+                      else
+                          break;
+                  }
+              }
+              else if (PyInt_CheckExact(arg))
+                  break;
+
+              return -1;
+          }
+
+          case 'D':           /* double */
+          {
+              if (array)
+              {
+                  if (arg == Py_None)
+                      break;
+
+                  if (PyObject_TypeCheck(arg, JArrayDoubleType))
+                      break;
+
+                  if (PySequence_Check(arg))
+                  {
+                      if (PySequence_Length(arg) > 0)
+                      {
+                          PyObject *obj = PySequence_GetItem(arg, 0);
+                          int ok = PyFloat_CheckExact(obj);
+
+                          Py_DECREF(obj);
+                          if (ok)
+                              break;
+                      }
+                      else
+                          break;
+                  }
+              }
+              else if (PyFloat_CheckExact(arg))
+                  break;
+
+              return -1;
+          }
+
+          case 'F':           /* float */
+          {
+              if (array)
+              {
+                  if (arg == Py_None)
+                      break;
+
+                  if (PyObject_TypeCheck(arg, JArrayFloatType))
+                      break;
+
+                  if (PySequence_Check(arg))
+                  {
+                      if (PySequence_Length(arg) > 0)
+                      {
+                          PyObject *obj = PySequence_GetItem(arg, 0);
+                          int ok = PyFloat_CheckExact(obj);
+
+                          Py_DECREF(obj);
+                          if (ok)
+                              break;
+                      }
+                      else
+                          break;
+                  }
+              }
+              else if (PyFloat_CheckExact(arg))
+                  break;
+
+              return -1;
+          }
+
+          case 'J':           /* long long */
+          {
+              if (array)
+              {
+                  if (arg == Py_None)
+                      break;
+
+                  if (PyObject_TypeCheck(arg, JArrayLongType))
+                      break;
+
+                  if (PySequence_Check(arg))
+                  {
+                      if (PySequence_Length(arg) > 0)
+                      {
+                          PyObject *obj = PySequence_GetItem(arg, 0);
+                          int ok = PyLong_CheckExact(obj);
+
+                          Py_DECREF(obj);
+                          if (ok)
+                              break;
+                      }
+                      else
+                          break;
+                  }
+              }
+              else if (PyLong_CheckExact(arg))
+                  break;
+
+              return -1;
+          }
+
+          case 's':           /* string  */
+          {
+              if (array)
+              {
+                  if (arg == Py_None)
+                      break;
+
+                  if (PyObject_TypeCheck(arg, JArrayStringType))
+                      break;
+
+                  if (PySequence_Check(arg) && 
+                      !PyString_Check(arg) && !PyUnicode_Check(arg))
+                  {
+                      if (PySequence_Length(arg) > 0)
+                      {
+                          PyObject *obj = PySequence_GetItem(arg, 0);
+                          int ok =
+                              (obj == Py_None ||
+                               PyString_Check(obj) || PyUnicode_Check(obj));
+
+                          Py_DECREF(obj);
+                          if (ok)
+                              break;
+                      }
+                      else
+                          break;
+                  }
+              }
+              else if (arg == Py_None ||
+                       PyString_Check(arg) || PyUnicode_Check(arg))
+                  break;
+
+              return -1;
+          }
+
+          case 'o':         /* java.lang.Object */
+            break;
+
+          default:
+            return -1;
+        }
+
+        if (types[pos] != '[')
+            array = 0;
+    }
+
+    if (array)
+        return -1;
+
+    pos = 0;
+
+    for (unsigned int a = 0; a < count; a++, pos++) {
+        PyObject *arg = args[a];
+        
+        switch (types[pos]) {
+          case '[':
+          {
+              if (++array > 1)
+                  return -1;
+
+              a -= 1;
+              break;
+          }
+
+          case 'j':           /* Java object except String and Object */
+          {
+              Class *cls = va_arg(check, Class *);
+
+              if (array)
+              {
+                  JArray<jobject> *array = va_arg(list, JArray<jobject> *);
+
+                  if (arg == Py_None)
+                      *array = JArray<jobject>((jobject) NULL);
+                  else if (PyObject_TypeCheck(arg, JArrayObjectType))
+                      *array = ((t_jarray<jobject> *) arg)->array;
+                  else 
+                      *array = JArray<jobject>((jclass) cls->this$, arg);
+
+                  if (PyErr_Occurred())
+                      return -1;
+              }
+              else
+              {
+                  Object *obj = va_arg(list, Object *);
+
+                  if (PyObject_TypeCheck(arg, &FinalizerProxyType))
+                      arg = ((t_fp *) arg)->object;
+
+                  *obj = arg == Py_None
+                      ? Object(NULL)
+                      : ((t_Object *) arg)->object;
+              }
+              break;
+          }
+
+          case 'Z':           /* boolean, strict */
+          {
+              if (array)
+              {
+                  JArray<jboolean> *array = va_arg(list, JArray<jboolean> *);
+
+                  if (arg == Py_None)
+                      *array = JArray<jboolean>((jobject) NULL);
+                  else if (PyObject_TypeCheck(arg, JArrayBoolType))
+                      *array = ((t_jarray<jboolean> *) arg)->array;
+                  else
+                      *array = JArray<jboolean>(arg);
+
+                  if (PyErr_Occurred())
+                      return -1;
+              }
+              else
+              {
+                  jboolean *b = va_arg(list, jboolean *);
+                  *b = arg == Py_True;
+              }
+              break;
+          }
+
+          case 'B':           /* byte */
+          {
+              if (array)
+              {
+                  JArray<jbyte> *array = va_arg(list, JArray<jbyte> *);
+
+                  if (arg == Py_None)
+                      *array = JArray<jbyte>((jobject) NULL);
+                  else if (PyObject_TypeCheck(arg, JArrayByteType))
+                      *array = ((t_jarray<jbyte> *) arg)->array;
+                  else 
+                      *array = JArray<jbyte>(arg);
+
+                  if (PyErr_Occurred())
+                      return -1;
+              }
+              else
+              {
+                  jbyte *a = va_arg(list, jbyte *);
+                  *a = (jbyte) PyString_AS_STRING(arg)[0];
+              }
+              break;
+          }
+
+          case 'C':           /* char */
+          {
+              if (array)
+              {
+                  JArray<jchar> *array = va_arg(list, JArray<jchar> *);
+
+                  if (arg == Py_None)
+                      *array = JArray<jchar>((jobject) NULL);
+                  else if (PyObject_TypeCheck(arg, JArrayCharType))
+                      *array = ((t_jarray<jchar> *) arg)->array;
+                  else 
+                      *array = JArray<jchar>(arg);
+
+                  if (PyErr_Occurred())
+                      return -1;
+              }
+              else
+              {
+                  jchar *c = va_arg(list, jchar *);
+                  *c = (jchar) PyUnicode_AS_UNICODE(arg)[0];
+              }
+              break;
+          }
+
+          case 'I':           /* int */
+          {
+              if (array)
+              {
+                  JArray<jint> *array = va_arg(list, JArray<jint> *);
+
+                  if (arg == Py_None)
+                      *array = JArray<jint>((jobject) NULL);
+                  else if (PyObject_TypeCheck(arg, JArrayIntType))
+                      *array = ((t_jarray<jint> *) arg)->array;
+                  else 
+                      *array = JArray<jint>(arg);
+
+                  if (PyErr_Occurred())
+                      return -1;
+              }
+              else
+              {
+                  jint *n = va_arg(list, jint *);
+                  *n = (jint) PyInt_AsLong(arg);
+              }
+              break;
+          }
+
+          case 'S':           /* short */
+          {
+              if (array)
+              {
+                  JArray<jshort> *array = va_arg(list, JArray<jshort> *);
+
+                  if (arg == Py_None)
+                      *array = JArray<jshort>((jobject) NULL);
+                  else if (PyObject_TypeCheck(arg, JArrayShortType))
+                      *array = ((t_jarray<jshort> *) arg)->array;
+                  else 
+                      *array = JArray<jshort>(arg);
+
+                  if (PyErr_Occurred())
+                      return -1;
+              }
+              else
+              {
+                  jshort *n = va_arg(list, jshort *);
+                  *n = (jshort) PyInt_AsLong(arg);
+              }
+              break;
+          }
+
+          case 'D':           /* double */
+          {
+              if (array)
+              {
+                  JArray<jdouble> *array = va_arg(list, JArray<jdouble> *);
+
+                  if (arg == Py_None)
+                      *array = JArray<jdouble>((jobject) NULL);
+                  else if (PyObject_TypeCheck(arg, JArrayDoubleType))
+                      *array = ((t_jarray<jdouble> *) arg)->array;
+                  else 
+                      *array = JArray<jdouble>(arg);
+
+                  if (PyErr_Occurred())
+                      return -1;
+              }
+              else
+              {
+                  jdouble *d = va_arg(list, jdouble *);
+                  *d = (jdouble) PyFloat_AsDouble(arg);
+              }
+              break;
+          }
+
+          case 'F':           /* float */
+          {
+              if (array)
+              {
+                  JArray<jfloat> *array = va_arg(list, JArray<jfloat> *);
+
+                  if (arg == Py_None)
+                      *array = JArray<jfloat>((jobject) NULL);
+                  else if (PyObject_TypeCheck(arg, JArrayFloatType))
+                      *array = ((t_jarray<jfloat> *) arg)->array;
+                  else 
+                      *array = JArray<jfloat>(arg);
+
+                  if (PyErr_Occurred())
+                      return -1;
+              }
+              else
+              {
+                  jfloat *d = va_arg(list, jfloat *);
+                  *d = (jfloat) (float) PyFloat_AsDouble(arg);
+              }
+              break;
+          }
+
+          case 'J':           /* long long */
+          {
+              if (array)
+              {
+                  JArray<jlong> *array = va_arg(list, JArray<jlong> *);
+
+                  if (arg == Py_None)
+                      *array = JArray<jlong>((jobject) NULL);
+                  else if (PyObject_TypeCheck(arg, JArrayLongType))
+                      *array = ((t_jarray<jlong> *) arg)->array;
+                  else 
+                      *array = JArray<jlong>(arg);
+
+                  if (PyErr_Occurred())
+                      return -1;
+              }
+              else
+              {
+                  jlong *l = va_arg(list, jlong *);
+                  *l = (jlong) PyLong_AsLongLong(arg);
+              }
+              break;
+          }
+
+          case 's':           /* string  */
+          {
+              if (array)
+              {
+                  JArray<jstring> *array = va_arg(list, JArray<jstring> *);
+
+                  if (arg == Py_None)
+                      *array = JArray<jstring>((jobject) NULL);
+                  else if (PyObject_TypeCheck(arg, JArrayStringType))
+                      *array = ((t_jarray<jstring> *) arg)->array;
+                  else 
+                      *array = JArray<jstring>(arg);
+
+                  if (PyErr_Occurred())
+                      return -1;
+              }
+              else
+              {
+                  String *str = va_arg(list, String *);
+
+                  if (arg == Py_None)
+                      *str = String(NULL);
+                  else
+                  {
+                      *str = p2j(arg);
+                      if (PyErr_Occurred())
+                          return -1;
+                  }
+              }
+              break;
+          }
+
+          case 'o':           /* java.lang.Object  */
+          {
+              if (array)
+              {
+                  JArray<Object> *array = va_arg(list, JArray<Object> *);
+
+                  if (arg == Py_None)
+                      *array = JArray<Object>((jobject) NULL);
+                  else 
+                      *array = JArray<Object>(arg);
+
+                  if (PyErr_Occurred())
+                      return -1;
+              }
+              else
+              {
+                  Object *obj = va_arg(list, Object *);
+
+                  if (arg == Py_None)
+                      *obj = Object(NULL);
+                  else if (PyObject_TypeCheck(arg, &ObjectType))
+                      *obj = ((t_Object *) arg)->object;
+                  else if (PyObject_TypeCheck(arg, &FinalizerProxyType))
+                  {
+                      arg = ((t_fp *) arg)->object;
+                      if (PyObject_TypeCheck(arg, &ObjectType))
+                          *obj = ((t_Object *) arg)->object;
+                      else
+                          return -1;
+                  }
+                  else if (PyString_Check(arg) || PyUnicode_Check(arg))
+                  {
+                      *obj = p2j(arg);
+                      if (PyErr_Occurred())
+                          return -1;
+                  }
+                  else if (arg == Py_True)
+                      *obj = *Boolean::TRUE;
+                  else if (arg == Py_False)
+                      *obj = *Boolean::FALSE;
+                  else if (PyInt_Check(arg))
+                  {
+                      long ln = PyInt_AS_LONG(arg);
+                      int n = (int) ln;
+
+                      if (ln != (long) n)
+                          *obj = Long((jlong) ln);
+                      else
+                          *obj = Integer((jint) n);
+                  }
+                  else if (PyLong_Check(arg))
+                      *obj = Long((jlong) PyLong_AsLongLong(arg));
+                  else if (PyFloat_Check(arg))
+                      *obj = Double((jdouble) PyFloat_AS_DOUBLE(arg));
+                  else
+                      return -1;
+              }
+              break;
+          }
+
+          default:
+            return -1;
+        }
+
+        if (types[pos] != '[')
+            array = 0;
+    }
+
+    if (pos == typeCount)
+        return 0;
+
+    return -1;
+}
+
+
+String p2j(PyObject *object)
+{
+    return String(env->fromPyString(object));
+}
+
+PyObject *j2p(const String& js)
+{
+    return env->fromJString((jstring) js.this$);
+}
+
+PyObject *PyErr_SetArgsError(char *name, PyObject *args)
+{
+    PyObject *err = Py_BuildValue("(sO)", name, args);
+
+    PyErr_SetObject(PyExc_InvalidArgsError, err);
+    Py_DECREF(err);
+
+    return NULL;
+}
+
+PyObject *PyErr_SetArgsError(PyObject *self, char *name, PyObject *args)
+{
+    PyObject *type = (PyObject *) self->ob_type;
+    PyObject *err = Py_BuildValue("(OsO)", type, name, args);
+
+    PyErr_SetObject(PyExc_InvalidArgsError, err);
+    Py_DECREF(err);
+
+    return NULL;
+}
+
+PyObject *PyErr_SetArgsError(PyTypeObject *type, char *name, PyObject *args)
+{
+    PyObject *err = Py_BuildValue("(OsO)", type, name, args);
+
+    PyErr_SetObject(PyExc_InvalidArgsError, err);
+    Py_DECREF(err);
+
+    return NULL;
+}
+
+PyObject *PyErr_SetJavaError(jthrowable throwable)
+{
+    PyObject *err = t_Throwable::wrap_Object(Throwable(throwable));
+
+    PyErr_SetObject(PyExc_JavaError, err);
+    Py_DECREF(err);
+
+    return NULL;
+}
+
+void throwPythonError(void)
+{
+    PyObject *exc = PyErr_Occurred();
+
+    if (exc && PyErr_GivenExceptionMatches(exc, PyExc_JavaError))
+    {
+        PyObject *value, *traceback;
+
+        PyErr_Fetch(&exc, &value, &traceback);
+        if (value)
+        {
+            PyObject *je = PyObject_CallMethod(value, "getJavaException", "");
+
+            if (!je)
+                PyErr_Restore(exc, value, traceback);
+            else
+            {
+                Py_DECREF(exc);
+                Py_DECREF(value);
+                Py_XDECREF(traceback);
+                exc = je;
+
+                if (exc && PyObject_TypeCheck(exc, &ThrowableType))
+                {
+                    jobject jobj = ((t_Throwable *) exc)->object.this$;
+
+                    env->get_vm_env()->Throw((jthrowable) jobj);
+                    Py_DECREF(exc);
+
+                    return;
+                }
+            }
+        }
+        else
+        {
+            Py_DECREF(exc);
+            Py_XDECREF(traceback);
+        }
+    }
+    else if (exc && PyErr_GivenExceptionMatches(exc, PyExc_StopIteration))
+    {
+        PyErr_Clear();
+        return;
+    }
+
+    if (exc)
+    {
+        PyObject *name = PyObject_GetAttrString(exc, "__name__");
+
+        env->get_vm_env()->ThrowNew(env->getPythonExceptionClass(),
+                                    PyString_AS_STRING(name));
+        Py_DECREF(name);
+    }
+    else
+        env->get_vm_env()->ThrowNew(env->getPythonExceptionClass(),
+                                    "python error");
+}
+
+void throwTypeError(const char *name, PyObject *object)
+{
+    PyObject *tuple = Py_BuildValue("(ssO)", "while calling", name, object);
+
+    PyErr_SetObject(PyExc_TypeError, tuple);
+    Py_DECREF(tuple);
+
+    env->get_vm_env()->ThrowNew(env->getPythonExceptionClass(), "type error");
+}
+
+int abstract_init(PyObject *self, PyObject *args, PyObject *kwds)
+{
+    PyObject *err =
+        Py_BuildValue("(sO)", "instantiating java class", self->ob_type);
+
+    PyErr_SetObject(PyExc_NotImplementedError, err);
+    Py_DECREF(err);
+
+    return -1;
+}
+
+PyObject *callSuper(PyTypeObject *type, const char *name, PyObject *args,
+                    int cardinality)
+{
+    PyObject *super = (PyObject *) type->tp_base;
+    PyObject *method =
+        PyObject_GetAttrString(super, (char *) name); // python 2.4 cast
+    PyObject *value;
+
+    if (!method)
+        return NULL;
+
+    if (cardinality > 1)
+        value = PyObject_Call(method, args, NULL);
+    else
+    {
+#if PY_VERSION_HEX < 0x02040000
+        PyObject *tuple = Py_BuildValue("(O)", args);
+#else
+        PyObject *tuple = PyTuple_Pack(1, args);
+#endif   
+        value = PyObject_Call(method, tuple, NULL);
+        Py_DECREF(tuple);
+    }
+
+    Py_DECREF(method);
+
+    return value;
+}
+
+PyObject *callSuper(PyTypeObject *type, PyObject *self,
+                    const char *name, PyObject *args, int cardinality)
+{
+#if PY_VERSION_HEX < 0x02040000
+    PyObject *tuple = Py_BuildValue("(OO)", type, self);
+#else
+    PyObject *tuple = PyTuple_Pack(2, type, self);
+#endif
+    PyObject *super = PyObject_Call((PyObject *) &PySuper_Type, tuple, NULL);
+    PyObject *method, *value;
+
+    Py_DECREF(tuple);
+    if (!super)
+        return NULL;
+
+    method = PyObject_GetAttrString(super, (char *) name); // python 2.4 cast
+    Py_DECREF(super);
+    if (!method)
+        return NULL;
+
+    if (cardinality > 1)
+        value = PyObject_Call(method, args, NULL);
+    else
+    {
+#if PY_VERSION_HEX < 0x02040000
+        tuple = Py_BuildValue("(O)", args);
+#else
+        tuple = PyTuple_Pack(1, args);
+#endif
+        value = PyObject_Call(method, tuple, NULL);
+        Py_DECREF(tuple);
+    }
+
+    Py_DECREF(method);
+
+    return value;
+}
+
+PyObject *castCheck(PyObject *obj, jclass cls, int reportError)
+{
+    if (PyObject_TypeCheck(obj, &FinalizerProxyType))
+        obj = ((t_fp *) obj)->object;
+
+    if (!PyObject_TypeCheck(obj, &ObjectType))
+    {
+        if (reportError)
+            PyErr_SetObject(PyExc_TypeError, obj);
+        return NULL;
+    }
+
+    jobject jobj = ((t_Object *) obj)->object.this$;
+    
+    if (jobj && !env->get_vm_env()->IsInstanceOf(jobj, cls))
+    {
+        if (reportError)
+            PyErr_SetObject(PyExc_TypeError, obj);
+        return NULL;
+    }
+
+    return obj;
+}
+
+PyObject *get_extension_iterator(PyObject *self)
+{
+    return PyObject_CallMethod(self, "iterator", "");
+}
+
+PyObject *get_extension_next(PyObject *self)
+{
+    return PyObject_CallMethod(self, "next", "");
+}
+
+PyObject *get_extension_nextElement(PyObject *self)
+{
+    return PyObject_CallMethod(self, "nextElement", "");
+}
+
+jobjectArray fromPySequence(jclass cls, PyObject *sequence)
+{
+    if (sequence == Py_None)
+        return NULL;
+
+    if (!PySequence_Check(sequence))
+    {
+        PyErr_SetObject(PyExc_TypeError, sequence);
+        return NULL;
+    }
+
+    int length = PySequence_Length(sequence);
+    jobjectArray array;
+
+    try {
+        array = env->newObjectArray(cls, length);
+    } catch (JCCEnv::pythonError) {
+        return NULL;
+    } catch (JCCEnv::exception e) {
+        PyErr_SetJavaError(e.throwable);
+        return NULL;
+    }
+
+    JNIEnv *vm_env = env->get_vm_env();
+
+    for (int i = 0; i < length; i++) {
+        PyObject *obj = PySequence_GetItem(sequence, i);
+        int fromString = 0;
+        jobject jobj;
+
+        if (!obj)
+            break;
+        else if (obj == Py_None)
+            jobj = NULL;
+        else if (PyString_Check(obj) || PyUnicode_Check(obj))
+        {
+            jobj = env->fromPyString(obj);
+            fromString = 1;
+        }
+        else if (PyObject_TypeCheck(obj, &JObjectType))
+            jobj = ((t_JObject *) obj)->object.this$;
+        else if (PyObject_TypeCheck(obj, &FinalizerProxyType))
+            jobj = ((t_JObject *) ((t_fp *) obj)->object)->object.this$;
+        else /* todo: add auto-boxing of primitive types */
+        {
+            PyErr_SetObject(PyExc_TypeError, obj);
+            Py_DECREF(obj);
+            return NULL;
+        }
+
+        Py_DECREF(obj);
+
+        try {
+            env->setObjectArrayElement(array, i, jobj);
+            if (fromString)
+                vm_env->DeleteLocalRef(jobj);
+        } catch (JCCEnv::exception e) {
+            PyErr_SetJavaError(e.throwable);
+            return NULL;
+        }
+    }
+
+    return array;
+}

Propchange: lucene/pylucene/trunk/jcc/jcc/sources/functions.cpp
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: lucene/pylucene/trunk/jcc/jcc/sources/functions.cpp
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: lucene/pylucene/trunk/jcc/jcc/sources/functions.h
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/jcc/jcc/sources/functions.h?rev=732916&view=auto
==============================================================================
--- lucene/pylucene/trunk/jcc/jcc/sources/functions.h (added)
+++ lucene/pylucene/trunk/jcc/jcc/sources/functions.h Thu Jan  8 19:28:33 2009
@@ -0,0 +1,182 @@
+/*
+ *   Copyright (c) 2007-2008 Open Source Applications Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ */
+
+#ifndef _functions_h
+#define _functions_h
+
+#include "java/util/Iterator.h"
+#include "java/util/Enumeration.h"
+#include "java/lang/String.h"
+#include "java/lang/Object.h"
+#include "macros.h"
+
+#if PY_VERSION_HEX < 0x02050000
+typedef int Py_ssize_t;
+typedef inquiry lenfunc;
+typedef intargfunc ssizeargfunc;
+typedef intintargfunc ssizessizeargfunc;
+typedef intobjargproc ssizeobjargproc;
+typedef intintobjargproc ssizessizeobjargproc;
+#endif
+
+PyObject *PyErr_SetArgsError(char *name, PyObject *args);
+PyObject *PyErr_SetArgsError(PyObject *self, char *name, PyObject *args);
+PyObject *PyErr_SetArgsError(PyTypeObject *type, char *name, PyObject *args);
+PyObject *PyErr_SetJavaError(jthrowable throwable);
+
+extern PyObject *PyExc_JavaError;
+extern PyObject *PyExc_InvalidArgsError;
+
+
+void throwPythonError(void);
+void throwTypeError(const char *name, PyObject *object);
+
+#if defined(_MSC_VER) || defined(__SUNPRO_CC)
+
+#define parseArgs __parseArgs
+#define parseArg __parseArg
+
+int __parseArgs(PyObject *args, char *types, ...);
+int __parseArg(PyObject *arg, char *types, ...);
+
+int _parseArgs(PyObject **args, unsigned int count, char *types,
+	       va_list list, va_list check);
+
+#else
+
+#define parseArgs(args, types, rest...) \
+    _parseArgs(((PyTupleObject *)(args))->ob_item, \
+               ((PyTupleObject *)(args))->ob_size, types, ##rest)
+
+#define parseArg(arg, types, rest...) \
+    _parseArgs(&(arg), 1, types, ##rest)
+
+int _parseArgs(PyObject **args, unsigned int count, char *types, ...);
+
+#endif
+
+int abstract_init(PyObject *self, PyObject *args, PyObject *kwds);
+
+PyObject *j2p(const java::lang::String& js);
+java::lang::String p2j(PyObject *object);
+
+PyObject *make_descriptor(PyTypeObject *value);
+PyObject *make_descriptor(jclass (*initializeClass)(void));
+PyObject *make_descriptor(PyObject *value);
+PyObject *make_descriptor(PyObject *(*wrapfn)(const jobject &));
+PyObject *make_descriptor(jboolean value);
+PyObject *make_descriptor(jbyte value);
+PyObject *make_descriptor(jchar value);
+PyObject *make_descriptor(jdouble value);
+PyObject *make_descriptor(jfloat value);
+PyObject *make_descriptor(jint value);
+PyObject *make_descriptor(jlong value);
+PyObject *make_descriptor(jshort value);
+
+jobjectArray make_array(jclass cls, PyObject *sequence);
+
+PyObject *callSuper(PyTypeObject *type,
+                    const char *name, PyObject *args, int cardinality);
+PyObject *callSuper(PyTypeObject *type, PyObject *self,
+                    const char *name, PyObject *args, int cardinality);
+
+template<class T> PyObject *get_iterator(PyObject *self)
+{
+    java::util::Iterator iterator((jobject) NULL);
+
+    OBJ_CALL(iterator = (((T *) self)->object.iterator()));
+    return java::util::t_Iterator::wrap_Object(iterator);
+}
+
+template<class U, class V> PyObject *get_iterator_next(PyObject *self)
+{
+    java::util::t_Iterator *iterator = (java::util::t_Iterator *) self;
+    jboolean hasNext;
+
+    OBJ_CALL(hasNext = iterator->object.hasNext());
+    if (!hasNext)
+    {
+        PyErr_SetNone(PyExc_StopIteration);
+        return NULL;
+    }
+
+    V next((jobject) NULL);
+    OBJ_CALL(next = iterator->object.next());
+
+    jclass cls = java::lang::String::initializeClass();
+    if (env->get_vm_env()->IsInstanceOf(next.this$, cls))
+        return env->fromJString((jstring) next.this$);
+
+    return U::wrap_Object(next);
+}
+
+template<class U, class V> PyObject *get_enumeration_nextElement(PyObject *self)
+{
+    java::util::t_Enumeration *enumeration = (java::util::t_Enumeration *) self;
+    jboolean hasMoreElements;
+
+    OBJ_CALL(hasMoreElements = enumeration->object.hasMoreElements());
+    if (!hasMoreElements)
+    {
+        PyErr_SetNone(PyExc_StopIteration);
+        return NULL;
+    }
+
+    V next((jobject) NULL);
+    OBJ_CALL(next = enumeration->object.nextElement());
+
+    jclass cls = java::lang::String::initializeClass();
+    if (env->get_vm_env()->IsInstanceOf(next.this$, cls))
+        return env->fromJString((jstring) next.this$);
+
+    return U::wrap_Object(next);
+}
+
+template<class T, class U, class V> PyObject *get_next(PyObject *self)
+{
+    T *iterator = (T *) self;
+    V next((jobject) NULL);
+
+    OBJ_CALL(next = iterator->object.next());
+    if (!next)
+    {
+        PyErr_SetNone(PyExc_StopIteration);
+        return NULL;
+    }
+        
+    jclass cls = java::lang::String::initializeClass();
+    if (env->get_vm_env()->IsInstanceOf(next.this$, cls))
+        return env->fromJString((jstring) next.this$);
+
+    return U::wrap_Object(next);
+}
+
+PyObject *get_extension_iterator(PyObject *self);
+PyObject *get_extension_next(PyObject *self);
+PyObject *get_extension_nextElement(PyObject *self);
+
+jobjectArray fromPySequence(jclass cls, PyObject *sequence);
+PyObject *castCheck(PyObject *obj, jclass cls, int reportError);
+
+extern PyTypeObject FinalizerClassType;
+extern PyTypeObject FinalizerProxyType;
+
+typedef struct {
+    PyObject_HEAD
+    PyObject *object;
+} t_fp;
+
+#endif /* _functions_h */

Propchange: lucene/pylucene/trunk/jcc/jcc/sources/functions.h
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: lucene/pylucene/trunk/jcc/jcc/sources/functions.h
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: lucene/pylucene/trunk/jcc/jcc/sources/jcc.cpp
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/jcc/jcc/sources/jcc.cpp?rev=732916&view=auto
==============================================================================
--- lucene/pylucene/trunk/jcc/jcc/sources/jcc.cpp (added)
+++ lucene/pylucene/trunk/jcc/jcc/sources/jcc.cpp Thu Jan  8 19:28:33 2009
@@ -0,0 +1,616 @@
+/*
+ *   Copyright (c) 2007-2008 Open Source Applications Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <jni.h>
+#ifdef linux
+#include <dlfcn.h>
+#endif
+
+#include <Python.h>
+#include "structmember.h"
+
+#include "JObject.h"
+#include "JCCEnv.h"
+
+
+_DLL_EXPORT JCCEnv *env;
+
+
+/* JCCEnv */
+
+class t_jccenv {
+public:
+    PyObject_HEAD
+    JCCEnv *env;
+};
+    
+static void t_jccenv_dealloc(t_jccenv *self);
+static PyObject *t_jccenv_attachCurrentThread(PyObject *self, PyObject *args);
+static PyObject *t_jccenv_detachCurrentThread(PyObject *self);
+static PyObject *t_jccenv_strhash(PyObject *self, PyObject *arg);
+static PyObject *t_jccenv__dumpRefs(PyObject *self,
+                                    PyObject *args, PyObject *kwds);
+
+static PyMemberDef t_jccenv_members[] = {
+    { NULL, 0, 0, 0, NULL }
+};
+
+static PyMethodDef t_jccenv_methods[] = {
+    { "attachCurrentThread", (PyCFunction) t_jccenv_attachCurrentThread,
+      METH_VARARGS, NULL },
+    { "detachCurrentThread", (PyCFunction) t_jccenv_detachCurrentThread,
+      METH_NOARGS, NULL },
+    { "strhash", (PyCFunction) t_jccenv_strhash,
+      METH_O, NULL },
+    { "_dumpRefs", (PyCFunction) t_jccenv__dumpRefs,
+      METH_VARARGS | METH_KEYWORDS, NULL },
+    { NULL, NULL, 0, NULL }
+};
+
+_DLL_EXPORT PyTypeObject JCCEnvType = {
+    PyObject_HEAD_INIT(NULL)
+    0,                                   /* ob_size */
+    "jcc.JCCEnv",                        /* tp_name */
+    sizeof(t_jccenv),                    /* tp_basicsize */
+    0,                                   /* tp_itemsize */
+    (destructor)t_jccenv_dealloc,        /* tp_dealloc */
+    0,                                   /* tp_print */
+    0,                                   /* tp_getattr */
+    0,                                   /* tp_setattr */
+    0,                                   /* tp_compare */
+    0,                                   /* tp_repr */
+    0,                                   /* tp_as_number */
+    0,                                   /* tp_as_sequence */
+    0,                                   /* tp_as_mapping */
+    0,                                   /* tp_hash  */
+    0,                                   /* tp_call */
+    0,                                   /* tp_str */
+    0,                                   /* tp_getattro */
+    0,                                   /* tp_setattro */
+    0,                                   /* tp_as_buffer */
+    Py_TPFLAGS_DEFAULT,                  /* tp_flags */
+    "JCCEnv",                            /* tp_doc */
+    0,                                   /* tp_traverse */
+    0,                                   /* tp_clear */
+    0,                                   /* tp_richcompare */
+    0,                                   /* tp_weaklistoffset */
+    0,                                   /* tp_iter */
+    0,                                   /* tp_iternext */
+    t_jccenv_methods,                    /* tp_methods */
+    t_jccenv_members,                    /* tp_members */
+    0,                                   /* tp_getset */
+    0,                                   /* tp_base */
+    0,                                   /* tp_dict */
+    0,                                   /* tp_descr_get */
+    0,                                   /* tp_descr_set */
+    0,                                   /* tp_dictoffset */
+    0,                                   /* tp_init */
+    0,                                   /* tp_alloc */
+    0,                                   /* tp_new */
+};
+
+static void t_jccenv_dealloc(t_jccenv *self)
+{
+    self->ob_type->tp_free((PyObject *) self);
+}
+
+static void add_option(char *name, char *value, JavaVMOption *option)
+{
+    char *buf = new char[strlen(name) + strlen(value) + 1];
+
+    sprintf(buf, "%s%s", name, value);
+    option->optionString = buf;
+}
+
+#ifdef _jcc_lib
+static void add_paths(char *name, char *p0, char *p1, JavaVMOption *option)
+{
+#if defined(_MSC_VER) || defined(__WIN32)
+    char pathsep = ';';
+#else
+    char pathsep = ':';
+#endif
+    char *buf = new char[strlen(name) + strlen(p0) + strlen(p1) + 4];
+
+    sprintf(buf, "%s%s%c%s", name, p0, pathsep, p1);
+    option->optionString = buf;
+}
+#endif
+
+
+static PyObject *t_jccenv_attachCurrentThread(PyObject *self, PyObject *args)
+{
+    char *name = NULL;
+    int asDaemon = 0, result;
+    JNIEnv *jenv = NULL;
+
+    if (!PyArg_ParseTuple(args, "|si", &name, &asDaemon))
+        return NULL;
+
+    JavaVMAttachArgs attach = {
+        JNI_VERSION_1_2, name, NULL
+    };
+
+    if (asDaemon)
+        result = env->vm->AttachCurrentThreadAsDaemon((void **) &jenv, &attach);
+    else
+        result = env->vm->AttachCurrentThread((void **) &jenv, &attach);
+
+    env->set_vm_env(jenv);
+        
+    return PyInt_FromLong(result);
+}
+
+static PyObject *t_jccenv_detachCurrentThread(PyObject *self)
+{
+    int result = env->vm->DetachCurrentThread();
+    return PyInt_FromLong(result);
+}
+
+static PyObject *t_jccenv_strhash(PyObject *self, PyObject *arg)
+{
+    int hash = PyObject_Hash(arg);
+    char buffer[10];
+
+    sprintf(buffer, "%08x", (unsigned int) hash);
+    return PyString_FromStringAndSize(buffer, 8);
+}
+
+static PyObject *t_jccenv__dumpRefs(PyObject *self,
+                                    PyObject *args, PyObject *kwds)
+{
+    static char *kwnames[] = {
+        "classes", "values", NULL
+    };
+    int classes = 0, values = 0;
+    PyObject *result;
+
+    if (!PyArg_ParseTupleAndKeywords(args, kwds, "|ii", kwnames,
+                                     &classes, &values))
+        return NULL;
+
+    if (classes)
+        result = PyDict_New();
+    else
+        result = PyList_New(env->refs.size());
+
+    int count = 0;
+
+    for (std::multimap<int, countedRef>::iterator iter = env->refs.begin();
+         iter != env->refs.end();
+         iter++) {
+        if (classes)  // return dict of { class name: instance count }
+        {
+            char *name = env->getClassName(iter->second.global);
+            PyObject *key = PyString_FromString(name);
+            PyObject *value = PyDict_GetItem(result, key);
+
+            if (value == NULL)
+                value = PyInt_FromLong(1);
+            else
+                value = PyInt_FromLong(PyInt_AS_LONG(value) + 1);
+
+            PyDict_SetItem(result, key, value);
+            Py_DECREF(key);
+            Py_DECREF(value);
+
+            delete name;
+        }
+        else if (values)  // return list of (value string, ref count)
+        {
+            char *str = env->toString(iter->second.global);
+            PyObject *key = PyString_FromString(str);
+            PyObject *value = PyInt_FromLong(iter->second.count);
+
+#if PY_VERSION_HEX < 0x02040000
+            PyList_SET_ITEM(result, count++, Py_BuildValue("(OO)", key, value));
+#else
+            PyList_SET_ITEM(result, count++, PyTuple_Pack(2, key, value));
+#endif
+            Py_DECREF(key);
+            Py_DECREF(value);
+
+            delete str;
+        }
+        else  // return list of (id hash code, ref count)
+        {
+            PyObject *key = PyInt_FromLong(iter->first);
+            PyObject *value = PyInt_FromLong(iter->second.count);
+
+#if PY_VERSION_HEX < 0x02040000
+            PyList_SET_ITEM(result, count++, Py_BuildValue("(OO)", key, value));
+#else
+            PyList_SET_ITEM(result, count++, PyTuple_Pack(2, key, value));
+#endif
+            Py_DECREF(key);
+            Py_DECREF(value);
+        }
+    }
+
+    return result;
+}
+
+
+_DLL_EXPORT PyObject *getVMEnv(PyObject *self)
+{
+    if (env)
+    {
+        t_jccenv *jccenv = (t_jccenv *) JCCEnvType.tp_alloc(&JCCEnvType, 0);
+        jccenv->env = env;
+
+        return (PyObject *) jccenv;
+    }
+
+    Py_RETURN_NONE;
+}
+
+#ifdef _jcc_lib
+static void registerNatives(JNIEnv *vm_env);
+#endif
+
+_DLL_EXPORT PyObject *initVM(PyObject *self, PyObject *args, PyObject *kwds)
+{
+    static char *kwnames[] = {
+        "classpath", "initialheap", "maxheap", "maxstack",
+        "vmargs", NULL
+    };
+    char *classpath = NULL;
+    char *initialheap = NULL, *maxheap = NULL, *maxstack = NULL;
+    char *vmargs = NULL;
+
+    if (!PyArg_ParseTupleAndKeywords(args, kwds, "|zzzzz", kwnames,
+                                     &classpath,
+                                     &initialheap, &maxheap, &maxstack,
+                                     &vmargs))
+        return NULL;
+
+    if (env)
+    {
+        if (initialheap || maxheap || maxstack || vmargs)
+        {
+            PyErr_SetString(PyExc_ValueError,
+                            "JVM is already running, options are ineffective");
+            return NULL;
+        }
+
+        if (classpath && classpath[0])
+            env->setClassPath(classpath);
+
+        return getVMEnv(self);
+    }
+    else
+    {
+        JavaVMInitArgs vm_args;
+        JavaVMOption vm_options[32];
+        JNIEnv *vm_env;
+        JavaVM *vm;
+        unsigned int nOptions = 0;
+
+        vm_args.version = JNI_VERSION_1_4;
+        JNI_GetDefaultJavaVMInitArgs(&vm_args);
+
+#ifdef _jcc_lib
+        PyObject *jcc = PyImport_ImportModule("jcc");
+        PyObject *cp = PyObject_GetAttrString(jcc, "CLASSPATH");
+
+        if (classpath)
+            add_paths("-Djava.class.path=", PyString_AsString(cp), classpath,
+                      &vm_options[nOptions++]);
+        else
+            add_option("-Djava.class.path=", PyString_AsString(cp),
+                       &vm_options[nOptions++]);
+            
+        Py_DECREF(cp);
+        Py_DECREF(jcc);
+#else
+        if (classpath)
+            add_option("-Djava.class.path=", classpath,
+                       &vm_options[nOptions++]);
+#endif
+        if (initialheap)
+            add_option("-Xms", initialheap, &vm_options[nOptions++]);
+        if (maxheap)
+            add_option("-Xmx", maxheap, &vm_options[nOptions++]);
+        if (maxstack)
+            add_option("-Xss", maxstack, &vm_options[nOptions++]);
+
+        if (vmargs)
+        {
+            char *buf = strdup(vmargs);
+            char *sep = ",";
+            char *option;
+
+            for (option = strtok(buf, sep); option; option = strtok(NULL, sep))
+            {
+                if (nOptions < sizeof(vm_options) / sizeof(JavaVMOption))
+                    add_option("", option, &vm_options[nOptions++]);
+                else
+                {
+                    free(buf);
+                    for (unsigned int i = 0; i < nOptions; i++)
+                        delete vm_options[i].optionString;
+                    PyErr_Format(PyExc_ValueError, "Too many options (> %d)",
+                                 nOptions);
+                    return NULL;
+                }
+            }
+            free(buf);
+        }
+
+        //vm_options[nOptions++].optionString = "-verbose:gc";
+        //vm_options[nOptions++].optionString = "-Xcheck:jni";
+
+        vm_args.nOptions = nOptions;
+        vm_args.ignoreUnrecognized = JNI_FALSE;
+        vm_args.options = vm_options;
+
+        if (JNI_CreateJavaVM(&vm, (void **) &vm_env, &vm_args) < 0)
+        {
+            for (unsigned int i = 0; i < nOptions; i++)
+                delete vm_options[i].optionString;
+
+            PyErr_Format(PyExc_ValueError,
+                         "An error occurred while creating Java VM");
+            return NULL;
+        }
+
+        for (unsigned int i = 0; i < nOptions; i++)
+            delete vm_options[i].optionString;
+
+        t_jccenv *jccenv = (t_jccenv *) JCCEnvType.tp_alloc(&JCCEnvType, 0);
+        jccenv->env = new JCCEnv(vm, vm_env);
+
+#ifdef _jcc_lib
+        registerNatives(vm_env);
+#endif
+
+        return (PyObject *) jccenv;
+    }
+}
+
+extern "C" {
+
+#ifdef _jcc_lib
+    static void raise_error(JNIEnv *vm_env, const char *message)
+    {
+        jclass cls = vm_env->FindClass("org/osafoundation/jcc/PythonException");
+        vm_env->ThrowNew(cls, message);
+    }
+
+    JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved)
+    {
+        JNIEnv *vm_env;
+
+        if (!vm->GetEnv((void **) &vm_env, JNI_VERSION_1_4))
+            env = new JCCEnv(vm, vm_env);
+
+        registerNatives(vm_env);
+
+        return JNI_VERSION_1_4;
+    }
+
+    JNIEXPORT void JNICALL Java_org_osafoundation_jcc_PythonVM_init(JNIEnv *vm_env, jobject self, jstring programName, jobjectArray args)
+    {
+        const char *str = vm_env->GetStringUTFChars(programName, JNI_FALSE);
+#ifdef linux
+        char buf[32];
+
+        // load python runtime for other .so modules to link (such as _time.so)
+        sprintf(buf, "libpython%d.%d.so", PY_MAJOR_VERSION, PY_MINOR_VERSION);
+        dlopen(buf, RTLD_NOW | RTLD_GLOBAL);
+#endif
+
+	Py_SetProgramName((char *) str);
+
+        PyEval_InitThreads();
+	Py_Initialize();
+
+        if (args)
+        {
+            int argc = vm_env->GetArrayLength(args);
+            char **argv = (char **) calloc(argc + 1, sizeof(char *));
+
+            argv[0] = (char *) str;
+            for (int i = 0; i < argc; i++) {
+                jstring arg = (jstring) vm_env->GetObjectArrayElement(args, i);
+                argv[i + 1] = (char *) vm_env->GetStringUTFChars(arg, JNI_FALSE);
+            }
+
+            PySys_SetArgv(argc + 1, argv);
+
+            for (int i = 0; i < argc; i++) {
+                jstring arg = (jstring) vm_env->GetObjectArrayElement(args, i);
+                vm_env->ReleaseStringUTFChars(arg, argv[i + 1]);
+            }
+            free(argv);
+        }
+        else
+            PySys_SetArgv(1, (char **) &str);
+
+        vm_env->ReleaseStringUTFChars(programName, str);
+        PyEval_ReleaseLock();
+    }
+
+    JNIEXPORT jobject JNICALL Java_org_osafoundation_jcc_PythonVM_instantiate(JNIEnv *vm_env, jobject self, jstring moduleName, jstring className)
+    {
+        PythonGIL gil(vm_env);
+
+        const char *modStr = vm_env->GetStringUTFChars(moduleName, JNI_FALSE);
+        PyObject *module =
+            PyImport_ImportModule((char *) modStr);  // python 2.4 cast
+
+        vm_env->ReleaseStringUTFChars(moduleName, modStr);
+
+        if (!module)
+        {
+            raise_error(vm_env, "import failed");
+            return NULL;
+        }
+
+        const char *clsStr = vm_env->GetStringUTFChars(className, JNI_FALSE);
+        PyObject *cls =
+            PyObject_GetAttrString(module, (char *) clsStr); // python 2.4 cast
+        PyObject *obj;
+        jobject jobj;
+
+        vm_env->ReleaseStringUTFChars(className, clsStr);
+        Py_DECREF(module);
+
+        if (!cls)
+        {
+            raise_error(vm_env, "class not found");
+            return NULL;
+        }
+
+        obj = PyObject_CallFunctionObjArgs(cls, NULL);
+        Py_DECREF(cls);
+
+        if (!obj)
+        {
+            raise_error(vm_env, "instantiation failed");
+            return NULL;
+        }
+
+        PyObject *cObj = PyObject_GetAttrString(obj, "_jobject");
+
+        if (!cObj)
+        {
+            raise_error(vm_env, "instance does not proxy a java object");
+            Py_DECREF(obj);
+
+            return NULL;
+        }
+
+        jobj = (jobject) PyCObject_AsVoidPtr(cObj);
+        Py_DECREF(cObj);
+
+        jobj = vm_env->NewLocalRef(jobj);
+        Py_DECREF(obj);
+
+        return jobj;
+    }
+#endif
+
+    void JNICALL PythonException_getErrorInfo(JNIEnv *vm_env, jobject self)
+    {
+        PythonGIL gil(vm_env);
+
+        if (!PyErr_Occurred())
+            return;
+
+        PyObject *type, *value, *tb, *errorName;
+        jclass jcls = vm_env->GetObjectClass(self);
+
+        PyErr_Fetch(&type, &value, &tb);
+
+        errorName = PyObject_GetAttrString(type, "__name__");
+        if (errorName != NULL)
+        {
+            jfieldID fid =
+                vm_env->GetFieldID(jcls, "errorName", "Ljava/lang/String;");
+            jstring str = env->fromPyString(errorName);
+
+            vm_env->SetObjectField(self, fid, str);
+            vm_env->DeleteLocalRef(str);
+            Py_DECREF(errorName);
+        }
+
+        if (value != NULL)
+        {
+            PyObject *message = PyObject_Str(value);
+
+            if (message != NULL)
+            {
+                jfieldID fid =
+                    vm_env->GetFieldID(jcls, "message", "Ljava/lang/String;");
+                jstring str = env->fromPyString(message);
+
+                vm_env->SetObjectField(self, fid, str);
+                vm_env->DeleteLocalRef(str);
+                Py_DECREF(message);
+            }
+        }
+
+        PyObject *module = NULL, *cls = NULL, *stringIO = NULL, *result = NULL;
+        PyObject *_stderr = PySys_GetObject("stderr");
+        if (!_stderr)
+            goto err;
+
+        module = PyImport_ImportModule("cStringIO");
+        if (!module)
+            goto err;
+
+        cls = PyObject_GetAttrString(module, "StringIO");
+        Py_DECREF(module);
+        if (!cls)
+            goto err;
+
+        stringIO = PyObject_CallObject(cls, NULL);
+        Py_DECREF(cls);
+        if (!stringIO)
+            goto err;
+
+        Py_INCREF(_stderr);
+        PySys_SetObject("stderr", stringIO);
+
+        PyErr_Restore(type, value, tb);
+        PyErr_Print();
+
+        result = PyObject_CallMethod(stringIO, "getvalue", NULL);
+        Py_DECREF(stringIO);
+
+        if (result != NULL)
+        {
+            jfieldID fid =
+                vm_env->GetFieldID(jcls, "traceback", "Ljava/lang/String;");
+            jstring str = env->fromPyString(result);
+
+            vm_env->SetObjectField(self, fid, str);
+            vm_env->DeleteLocalRef(str);
+            Py_DECREF(result);
+        }
+
+        PySys_SetObject("stderr", _stderr);
+        Py_DECREF(_stderr);
+
+        return;
+
+      err:
+        PyErr_Restore(type, value, tb);
+    }
+
+    void JNICALL PythonException_clear(JNIEnv *vm_env, jobject self)
+    {
+        PythonGIL gil(vm_env);
+        PyErr_Clear();
+    }
+};
+
+#ifdef _jcc_lib
+static void registerNatives(JNIEnv *vm_env)
+{
+    jclass cls = vm_env->FindClass("org/osafoundation/jcc/PythonException");
+    JNINativeMethod methods[] = {
+        { "getErrorInfo", "()V", (void *) PythonException_getErrorInfo },
+        { "clear", "()V", (void *) PythonException_clear },
+    };
+
+    vm_env->RegisterNatives(cls, methods, 2);
+}
+#endif

Propchange: lucene/pylucene/trunk/jcc/jcc/sources/jcc.cpp
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: lucene/pylucene/trunk/jcc/jcc/sources/jcc.cpp
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: lucene/pylucene/trunk/jcc/jcc/sources/jccfuncs.h
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/jcc/jcc/sources/jccfuncs.h?rev=732916&view=auto
==============================================================================
--- lucene/pylucene/trunk/jcc/jcc/sources/jccfuncs.h (added)
+++ lucene/pylucene/trunk/jcc/jcc/sources/jccfuncs.h Thu Jan  8 19:28:33 2009
@@ -0,0 +1,45 @@
+/*
+ *   Copyright (c) 2007-2008 Open Source Applications Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ */
+
+#ifndef _jccfuncs_H
+#define _jccfuncs_H
+
+#ifdef PYTHON
+
+PyObject *__initialize__(PyObject *module, PyObject *args, PyObject *kwds);
+PyObject *initVM(PyObject *self, PyObject *args, PyObject *kwds);
+PyObject *getVMEnv(PyObject *self);
+PyObject *_setExceptionTypes(PyObject *self, PyObject *args);
+PyObject *findClass(PyObject *self, PyObject *args);
+PyObject *JArray_Type(PyObject *self, PyObject *arg);
+
+PyMethodDef jcc_funcs[] = {
+    { "initVM", (PyCFunction) __initialize__,
+      METH_VARARGS | METH_KEYWORDS, NULL },
+    { "getVMEnv", (PyCFunction) getVMEnv,
+      METH_NOARGS, NULL },
+    { "findClass", (PyCFunction) findClass,
+      METH_VARARGS, NULL },
+    { "_setExceptionTypes", (PyCFunction) _setExceptionTypes,
+      METH_VARARGS, NULL },
+    { "JArray", (PyCFunction) JArray_Type,
+      METH_O, NULL },
+    { NULL, NULL, 0, NULL }
+};
+
+#endif
+
+#endif /* _jccfuncs_H */

Propchange: lucene/pylucene/trunk/jcc/jcc/sources/jccfuncs.h
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: lucene/pylucene/trunk/jcc/jcc/sources/jccfuncs.h
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: lucene/pylucene/trunk/jcc/jcc/sources/macros.h
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/jcc/jcc/sources/macros.h?rev=732916&view=auto
==============================================================================
--- lucene/pylucene/trunk/jcc/jcc/sources/macros.h (added)
+++ lucene/pylucene/trunk/jcc/jcc/sources/macros.h Thu Jan  8 19:28:33 2009
@@ -0,0 +1,179 @@
+/*
+ *   Copyright (c) 2007-2008 Open Source Applications Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ */
+
+#ifndef _macros_H
+#define _macros_H
+
+#define OBJ_CALL(action)                                                \
+    {                                                                   \
+        try {                                                           \
+            PythonThreadState state;                                    \
+            action;                                                     \
+        } catch (JCCEnv::pythonError) {                                 \
+            return NULL;                                                \
+        } catch (JCCEnv::exception e) {                                 \
+            return PyErr_SetJavaError(e.throwable);                     \
+        }                                                               \
+    }
+
+#define INT_CALL(action)                                                \
+    {                                                                   \
+        try {                                                           \
+            PythonThreadState state;                                    \
+            action;                                                     \
+        } catch (JCCEnv::pythonError) {                                 \
+            return -1;                                                  \
+        } catch (JCCEnv::exception e) {                                 \
+            PyErr_SetJavaError(e.throwable);                            \
+            return -1;                                                  \
+        }                                                               \
+    }
+
+
+#define DECLARE_METHOD(type, name, flags)               \
+    { #name, (PyCFunction) type##_##name, flags, "" }
+
+#define DECLARE_GET_FIELD(type, name)           \
+    { #name, (getter) type##_get__##name, NULL, "", NULL }
+
+#define DECLARE_SET_FIELD(type, name)           \
+    { #name, NULL, (setter) type##_set__##name, "", NULL }
+
+#define DECLARE_GETSET_FIELD(type, name)        \
+    { #name, (getter) type##_get__##name, (setter) type##_set__##name, "", NULL }
+
+
+#define DECLARE_TYPE(name, t_name, base, javaClass,                         \
+                     init, iter, iternext, getset, mapping, sequence)       \
+PyTypeObject name##Type = {                                                 \
+    PyObject_HEAD_INIT(NULL)                                                \
+    /* ob_size            */   0,                                           \
+    /* tp_name            */   #name,                                       \
+    /* tp_basicsize       */   sizeof(t_name),                              \
+    /* tp_itemsize        */   0,                                           \
+    /* tp_dealloc         */   0,                                           \
+    /* tp_print           */   0,                                           \
+    /* tp_getattr         */   0,                                           \
+    /* tp_setattr         */   0,                                           \
+    /* tp_compare         */   0,                                           \
+    /* tp_repr            */   0,                                           \
+    /* tp_as_number       */   0,                                           \
+    /* tp_as_sequence     */   sequence,                                    \
+    /* tp_as_mapping      */   mapping,                                     \
+    /* tp_hash            */   0,                                           \
+    /* tp_call            */   0,                                           \
+    /* tp_str             */   0,                                           \
+    /* tp_getattro        */   0,                                           \
+    /* tp_setattro        */   0,                                           \
+    /* tp_as_buffer       */   0,                                           \
+    /* tp_flags           */   Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,    \
+    /* tp_doc             */   #t_name" objects",                           \
+    /* tp_traverse        */   0,                                           \
+    /* tp_clear           */   0,                                           \
+    /* tp_richcompare     */   0,                                           \
+    /* tp_weaklistoffset  */   0,                                           \
+    /* tp_iter            */   (getiterfunc) iter,                          \
+    /* tp_iternext        */   (iternextfunc) iternext,                     \
+    /* tp_methods         */   t_name##__methods_,                          \
+    /* tp_members         */   0,                                           \
+    /* tp_getset          */   getset,                                      \
+    /* tp_base            */   &base##Type,                                 \
+    /* tp_dict            */   0,                                           \
+    /* tp_descr_get       */   0,                                           \
+    /* tp_descr_set       */   0,                                           \
+    /* tp_dictoffset      */   0,                                           \
+    /* tp_init            */   (initproc)init,                              \
+    /* tp_alloc           */   0,                                           \
+    /* tp_new             */   0,                                           \
+};                                                                          \
+PyObject *t_name::wrap_Object(const javaClass& object)                  \
+{                                                                       \
+    if (!!object)                                                       \
+    {                                                                   \
+        t_name *self = (t_name *) name##Type.tp_alloc(&name##Type, 0);  \
+        if (self)                                                       \
+            self->object = object;                                      \
+        return (PyObject *) self;                                       \
+    }                                                                   \
+    Py_RETURN_NONE;                                                     \
+}                                                                       \
+PyObject *t_name::wrap_jobject(const jobject& object)                   \
+{                                                                       \
+    if (!!object)                                                       \
+    {                                                                   \
+        t_name *self = (t_name *) name##Type.tp_alloc(&name##Type, 0);  \
+        if (self)                                                       \
+            self->object = javaClass(object);                           \
+        return (PyObject *) self;                                       \
+    }                                                                   \
+    Py_RETURN_NONE;                                                     \
+}                                                                       \
+
+
+
+#define INSTALL_TYPE(name, module)                                   \
+    if (PyType_Ready(&name##Type) == 0)                              \
+    {                                                                \
+        Py_INCREF(&name##Type);                                      \
+        PyModule_AddObject(module, #name, (PyObject *) &name##Type); \
+    }
+
+#define INSTALL_EXTENSION(name, module)                              \
+    if (PyType_Ready(&name##Type) == 0)                              \
+    {                                                                \
+        Py_INCREF(&name##Type);                                      \
+        name##Type.ob_type = &FinalizerClassType;                    \
+        PyModule_AddObject(module, #name, (PyObject *) &name##Type); \
+    }
+
+
+#define Py_RETURN_BOOL(b)                       \
+    {                                           \
+        if (b)                                  \
+            Py_RETURN_TRUE;                     \
+        else                                    \
+            Py_RETURN_FALSE;                    \
+    }
+
+
+#if PY_VERSION_HEX < 0x02040000
+
+#define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
+#define Py_RETURN_TRUE return Py_INCREF(Py_True), Py_True
+#define Py_RETURN_FALSE return Py_INCREF(Py_False), Py_False
+
+#define Py_CLEAR(op)                            \
+    do {                                        \
+        if (op) {                               \
+            PyObject *tmp = (PyObject *)(op);   \
+            (op) = NULL;                        \
+            Py_DECREF(tmp);                     \
+        }                                       \
+    } while (0)
+
+#define Py_VISIT(op)                                    \
+    do {                                                \
+        if (op) {                                       \
+            int vret = visit((PyObject *)(op), arg);    \
+            if (vret)                                   \
+                return vret;                            \
+        }                                               \
+    } while (0)
+          
+#endif /* Python 2.3.5 */
+
+
+#endif /* _macros_H */

Propchange: lucene/pylucene/trunk/jcc/jcc/sources/macros.h
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: lucene/pylucene/trunk/jcc/jcc/sources/macros.h
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: lucene/pylucene/trunk/jcc/jcc/sources/types.cpp
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/jcc/jcc/sources/types.cpp?rev=732916&view=auto
==============================================================================
--- lucene/pylucene/trunk/jcc/jcc/sources/types.cpp (added)
+++ lucene/pylucene/trunk/jcc/jcc/sources/types.cpp Thu Jan  8 19:28:33 2009
@@ -0,0 +1,435 @@
+/*
+ *   Copyright (c) 2007-2008 Open Source Applications Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ */
+
+#include <jni.h>
+#include <Python.h>
+#include "structmember.h"
+
+#include "java/lang/Object.h"
+#include "java/lang/Class.h"
+#include "functions.h"
+
+using namespace java::lang;
+
+
+/* FinalizerProxy */
+
+static PyObject *t_fc_call(PyObject *self, PyObject *args, PyObject *kwds);
+
+static void t_fp_dealloc(t_fp *self);
+static PyObject *t_fp_getattro(t_fp *self, PyObject *name);
+static int t_fp_setattro(t_fp *self, PyObject *name, PyObject *value);
+static int t_fp_traverse(t_fp *self, visitproc visit, void *arg);
+static int t_fp_clear(t_fp *self);
+static PyObject *t_fp_repr(t_fp *self);
+static PyObject *t_fp_iter(t_fp *self);
+
+PyTypeObject FinalizerClassType = {
+    PyObject_HEAD_INIT(NULL)
+    0,                                   /* ob_size */
+    "jcc.FinalizerClass",                /* tp_name */
+    PyType_Type.tp_basicsize,            /* tp_basicsize */
+    0,                                   /* tp_itemsize */
+    0,                                   /* tp_dealloc */
+    0,                                   /* tp_print */
+    0,                                   /* tp_getattr */
+    0,                                   /* tp_setattr */
+    0,                                   /* tp_compare */
+    0,                                   /* tp_repr */
+    0,                                   /* tp_as_number */
+    0,                                   /* tp_as_sequence */
+    0,                                   /* tp_as_mapping */
+    0,                                   /* tp_hash  */
+    (ternaryfunc) t_fc_call,             /* tp_call */
+    0,                                   /* tp_str */
+    0,                                   /* tp_getattro */
+    0,                                   /* tp_setattro */
+    0,                                   /* tp_as_buffer */
+    Py_TPFLAGS_DEFAULT,                  /* tp_flags */
+    "FinalizerClass",                    /* tp_doc */
+    0,                                   /* tp_traverse */
+    0,                                   /* tp_clear */
+    0,                                   /* tp_richcompare */
+    0,                                   /* tp_weaklistoffset */
+    0,                                   /* tp_iter */
+    0,                                   /* tp_iternext */
+    0,                                   /* tp_methods */
+    0,                                   /* tp_members */
+    0,                                   /* tp_getset */
+    &PyType_Type,                        /* tp_base */
+    0,                                   /* tp_dict */
+    0,                                   /* tp_descr_get */
+    0,                                   /* tp_descr_set */
+    0,                                   /* tp_dictoffset */
+    0,                                   /* tp_init */
+    0,                                   /* tp_alloc */
+    0,                                   /* tp_new */
+};
+
+PyTypeObject FinalizerProxyType = {
+    PyObject_HEAD_INIT(NULL)
+    0,                                   /* ob_size */
+    "jcc.FinalizerProxy",                /* tp_name */
+    sizeof(t_fp),                        /* tp_basicsize */
+    0,                                   /* tp_itemsize */
+    (destructor)t_fp_dealloc,            /* tp_dealloc */
+    0,                                   /* tp_print */
+    0,                                   /* tp_getattr */
+    0,                                   /* tp_setattr */
+    0,                                   /* tp_compare */
+    (reprfunc)t_fp_repr,                 /* tp_repr */
+    0,                                   /* tp_as_number */
+    0,                                   /* tp_as_sequence */
+    0,                                   /* tp_as_mapping */
+    0,                                   /* tp_hash  */
+    0,                                   /* tp_call */
+    0,                                   /* tp_str */
+    (getattrofunc)t_fp_getattro,         /* tp_getattro */
+    (setattrofunc)t_fp_setattro,         /* tp_setattro */
+    0,                                   /* tp_as_buffer */
+    (Py_TPFLAGS_DEFAULT |
+     Py_TPFLAGS_HAVE_GC),                /* tp_flags */
+    "FinalizerProxy",                    /* tp_doc */
+    (traverseproc)t_fp_traverse,         /* tp_traverse */
+    (inquiry)t_fp_clear,                 /* tp_clear */
+    0,                                   /* tp_richcompare */
+    0,                                   /* tp_weaklistoffset */
+    (getiterfunc)t_fp_iter,              /* tp_iter */
+    0,                                   /* tp_iternext */
+    0,                                   /* tp_methods */
+    0,                                   /* tp_members */
+    0,                                   /* tp_getset */
+    0,                                   /* tp_base */
+    0,                                   /* tp_dict */
+    0,                                   /* tp_descr_get */
+    0,                                   /* tp_descr_set */
+    0,                                   /* tp_dictoffset */
+    0,                                   /* tp_init */
+    0,                                   /* tp_alloc */
+    0,                                   /* tp_new */
+};
+
+static PyObject *t_fc_call(PyObject *self, PyObject *args, PyObject *kwds)
+{
+    PyObject *obj = PyType_Type.tp_call(self, args, kwds);
+
+    if (obj)
+    {
+        t_fp *fp = (t_fp *) FinalizerProxyType.tp_alloc(&FinalizerProxyType, 0);
+
+        fp->object = obj;
+        obj = (PyObject *) fp;
+    }
+
+    return obj;
+}
+
+static void t_fp_dealloc(t_fp *self)
+{
+    if (self->object)
+        ((t_JObject *) self->object)->object.weaken$();
+
+    t_fp_clear(self);
+    self->ob_type->tp_free((PyObject *) self);
+}
+
+static int t_fp_traverse(t_fp *self, visitproc visit, void *arg)
+{
+    Py_VISIT(self->object);
+    return 0;
+}
+
+static int t_fp_clear(t_fp *self)
+{
+    Py_CLEAR(self->object);
+    return 0;
+}
+
+static PyObject *t_fp_repr(t_fp *self)
+{
+    return PyObject_Repr(self->object);
+}
+
+static PyObject *t_fp_iter(t_fp *self)
+{
+    return PyObject_GetIter(self->object);
+}
+
+static PyObject *t_fp_getattro(t_fp *self, PyObject *name)
+{
+    return PyObject_GetAttr(self->object, name);
+}
+
+static int t_fp_setattro(t_fp *self, PyObject *name, PyObject *value)
+{
+    return PyObject_SetAttr(self->object, name, value);
+}
+
+
+/* const variable descriptor */
+
+class t_descriptor {
+public:
+    PyObject_HEAD
+    int flags;
+    union {
+        PyObject *value;
+        jclass (*initializeClass)(void);
+    } access;
+};
+    
+#define DESCRIPTOR_VALUE 0x1
+#define DESCRIPTOR_CLASS 0x2
+#define DESCRIPTOR_GETFN 0x4
+
+static void t_descriptor_dealloc(t_descriptor *self);
+static PyObject *t_descriptor___get__(t_descriptor *self,
+                                      PyObject *obj, PyObject *type);
+
+static PyMethodDef t_descriptor_methods[] = {
+    { NULL, NULL, 0, NULL }
+};
+
+
+PyTypeObject ConstVariableDescriptorType = {
+    PyObject_HEAD_INIT(NULL)
+    0,                                   /* ob_size */
+    "jcc.ConstVariableDescriptor",       /* tp_name */
+    sizeof(t_descriptor),                /* tp_basicsize */
+    0,                                   /* tp_itemsize */
+    (destructor)t_descriptor_dealloc,    /* tp_dealloc */
+    0,                                   /* tp_print */
+    0,                                   /* tp_getattr */
+    0,                                   /* tp_setattr */
+    0,                                   /* tp_compare */
+    0,                                   /* tp_repr */
+    0,                                   /* tp_as_number */
+    0,                                   /* tp_as_sequence */
+    0,                                   /* tp_as_mapping */
+    0,                                   /* tp_hash  */
+    0,                                   /* tp_call */
+    0,                                   /* tp_str */
+    0,                                   /* tp_getattro */
+    0,                                   /* tp_setattro */
+    0,                                   /* tp_as_buffer */
+    Py_TPFLAGS_DEFAULT,                  /* tp_flags */
+    "const variable descriptor",         /* tp_doc */
+    0,                                   /* tp_traverse */
+    0,                                   /* tp_clear */
+    0,                                   /* tp_richcompare */
+    0,                                   /* tp_weaklistoffset */
+    0,                                   /* tp_iter */
+    0,                                   /* tp_iternext */
+    t_descriptor_methods,                /* tp_methods */
+    0,                                   /* tp_members */
+    0,                                   /* tp_getset */
+    0,                                   /* tp_base */
+    0,                                   /* tp_dict */
+    (descrgetfunc)t_descriptor___get__,  /* tp_descr_get */
+    0,                                   /* tp_descr_set */
+    0,                                   /* tp_dictoffset */
+    0,                                   /* tp_init */
+    0,                                   /* tp_alloc */
+    0,                                   /* tp_new */
+};
+
+static void t_descriptor_dealloc(t_descriptor *self)
+{
+    if (self->flags & DESCRIPTOR_VALUE)
+        Py_DECREF(self->access.value);
+    self->ob_type->tp_free((PyObject *) self);
+}
+
+PyObject *make_descriptor(PyTypeObject *value)
+{
+    t_descriptor *self = (t_descriptor *)
+        ConstVariableDescriptorType.tp_alloc(&ConstVariableDescriptorType, 0);
+
+    if (self)
+    {
+        Py_INCREF(value);
+        self->access.value = (PyObject *) value;
+        self->flags = DESCRIPTOR_VALUE;
+    }
+
+    return (PyObject *) self;
+}
+
+PyObject *make_descriptor(jclass (*initializeClass)(void))
+{
+    t_descriptor *self = (t_descriptor *)
+        ConstVariableDescriptorType.tp_alloc(&ConstVariableDescriptorType, 0);
+
+    if (self)
+    {
+        self->access.initializeClass = initializeClass;
+        self->flags = DESCRIPTOR_CLASS;
+    }
+
+    return (PyObject *) self;
+}
+
+PyObject *make_descriptor(PyObject *value)
+{
+    t_descriptor *self = (t_descriptor *)
+        ConstVariableDescriptorType.tp_alloc(&ConstVariableDescriptorType, 0);
+
+    if (self)
+    {
+        self->access.value = value;
+        self->flags = DESCRIPTOR_VALUE;
+    }
+    else
+        Py_DECREF(value);
+
+    return (PyObject *) self;
+}
+
+PyObject *make_descriptor(PyObject *(*wrapfn)(const jobject &))
+{
+    return make_descriptor(PyCObject_FromVoidPtr((void *) wrapfn, NULL));
+}
+
+PyObject *make_descriptor(jboolean b)
+{
+    t_descriptor *self = (t_descriptor *)
+        ConstVariableDescriptorType.tp_alloc(&ConstVariableDescriptorType, 0);
+
+    if (self)
+    {
+        PyObject *value = b ? Py_True : Py_False;
+        self->access.value = (PyObject *) value; Py_INCREF(value);
+        self->flags = DESCRIPTOR_VALUE;
+    }
+
+    return (PyObject *) self;
+}
+
+PyObject *make_descriptor(jbyte value)
+{
+    t_descriptor *self = (t_descriptor *)
+        ConstVariableDescriptorType.tp_alloc(&ConstVariableDescriptorType, 0);
+
+    if (self)
+    {
+        self->access.value = PyString_FromStringAndSize((char *) &value, 1);
+        self->flags = DESCRIPTOR_VALUE;
+    }
+
+    return (PyObject *) self;
+}
+
+PyObject *make_descriptor(jchar value)
+{
+    t_descriptor *self = (t_descriptor *)
+        ConstVariableDescriptorType.tp_alloc(&ConstVariableDescriptorType, 0);
+
+    if (self)
+    {
+        Py_UNICODE pchar = (Py_UNICODE) value;
+
+        self->access.value = PyUnicode_FromUnicode(&pchar, 1);
+        self->flags = DESCRIPTOR_VALUE;
+    }
+
+    return (PyObject *) self;
+}
+
+PyObject *make_descriptor(jdouble value)
+{
+    t_descriptor *self = (t_descriptor *)
+        ConstVariableDescriptorType.tp_alloc(&ConstVariableDescriptorType, 0);
+
+    if (self)
+    {
+        self->access.value = PyFloat_FromDouble(value);
+        self->flags = DESCRIPTOR_VALUE;
+    }
+
+    return (PyObject *) self;
+}
+
+PyObject *make_descriptor(jfloat value)
+{
+    t_descriptor *self = (t_descriptor *)
+        ConstVariableDescriptorType.tp_alloc(&ConstVariableDescriptorType, 0);
+
+    if (self)
+    {
+        self->access.value = PyFloat_FromDouble((double) value);
+        self->flags = DESCRIPTOR_VALUE;
+    }
+
+    return (PyObject *) self;
+}
+
+PyObject *make_descriptor(jint value)
+{
+    t_descriptor *self = (t_descriptor *)
+        ConstVariableDescriptorType.tp_alloc(&ConstVariableDescriptorType, 0);
+
+    if (self)
+    {
+        self->access.value = PyInt_FromLong(value);
+        self->flags = DESCRIPTOR_VALUE;
+    }
+
+    return (PyObject *) self;
+}
+
+PyObject *make_descriptor(jlong value)
+{
+    t_descriptor *self = (t_descriptor *)
+        ConstVariableDescriptorType.tp_alloc(&ConstVariableDescriptorType, 0);
+
+    if (self)
+    {
+        self->access.value = PyLong_FromLongLong((long long) value);
+        self->flags = DESCRIPTOR_VALUE;
+    }
+
+    return (PyObject *) self;
+}
+
+PyObject *make_descriptor(jshort value)
+{
+    t_descriptor *self = (t_descriptor *)
+        ConstVariableDescriptorType.tp_alloc(&ConstVariableDescriptorType, 0);
+
+    if (self)
+    {
+        self->access.value = PyInt_FromLong((short) value);
+        self->flags = DESCRIPTOR_VALUE;
+    }
+
+    return (PyObject *) self;
+}
+
+static PyObject *t_descriptor___get__(t_descriptor *self,
+                                      PyObject *obj, PyObject *type)
+{
+    if (self->flags & DESCRIPTOR_VALUE)
+    {
+        Py_INCREF(self->access.value);
+        return self->access.value;
+    }
+
+    if (self->flags & DESCRIPTOR_CLASS)
+        return t_Class::wrap_Object(Class((*self->access.initializeClass)()));
+
+    Py_RETURN_NONE;
+}
+

Propchange: lucene/pylucene/trunk/jcc/jcc/sources/types.cpp
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: lucene/pylucene/trunk/jcc/jcc/sources/types.cpp
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message