You are viewing a plain text version of this content. The canonical link for it is here.
Posted to pylucene-commits@lucene.apache.org by va...@apache.org on 2010/04/04 01:27:05 UTC

svn commit: r930603 - in /lucene/pylucene/trunk/jcc: CHANGES jcc/python.py jcc/sources/JCCEnv.cpp jcc/sources/JCCEnv.h jcc/sources/functions.cpp jcc/sources/macros.h

Author: vajda
Date: Sat Apr  3 23:27:05 2010
New Revision: 930603

URL: http://svn.apache.org/viewvc?rev=930603&view=rev
Log:
 - added support for of_() method to set instance type parameters

Modified:
    lucene/pylucene/trunk/jcc/CHANGES
    lucene/pylucene/trunk/jcc/jcc/python.py
    lucene/pylucene/trunk/jcc/jcc/sources/JCCEnv.cpp
    lucene/pylucene/trunk/jcc/jcc/sources/JCCEnv.h
    lucene/pylucene/trunk/jcc/jcc/sources/functions.cpp
    lucene/pylucene/trunk/jcc/jcc/sources/macros.h

Modified: lucene/pylucene/trunk/jcc/CHANGES
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/jcc/CHANGES?rev=930603&r1=930602&r2=930603&view=diff
==============================================================================
--- lucene/pylucene/trunk/jcc/CHANGES (original)
+++ lucene/pylucene/trunk/jcc/CHANGES Sat Apr  3 23:27:05 2010
@@ -6,8 +6,9 @@ Version 2.5 ->
  - added support for extracting JAVAHOME from Windows registry (Bill Janssen)
  - updated MANIFEST.in as sdist started creating incomplete source archives
  - improved support for building on Windows with mingw32 (Bill Janssen)
- - added support for --find-jvm-dll jvm.dll (Bill Janssen)
+ - added support for --find-jvm-dll parameter (Bill Janssen)
  - fixed bug with not inheriting type parameters to inner parameterized classes
+ - added support for of_() method to set instance type parameters
  - 
 
 Version 2.4 -> 2.5

Modified: lucene/pylucene/trunk/jcc/jcc/python.py
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/jcc/jcc/python.py?rev=930603&r1=930602&r2=930603&view=diff
==============================================================================
--- lucene/pylucene/trunk/jcc/jcc/python.py (original)
+++ lucene/pylucene/trunk/jcc/jcc/python.py Sat Apr  3 23:27:05 2010
@@ -563,6 +563,10 @@ def python(env, out_h, out, cls, superCl
 
     line(out, indent, 'static PyObject *t_%s_cast_(PyTypeObject *type, PyObject *arg);', names[-1])
     line(out, indent, 'static PyObject *t_%s_instance_(PyTypeObject *type, PyObject *arg);', names[-1])
+    if clsParams:
+        line(out, indent,
+             'static PyObject *t_%s_of_(t_%s *self, PyObject *args);',
+             names[-1], names[-1])
 
     if constructors:
         line(out, indent, 'static int t_%s_init_(t_%s *self, PyObject *args, PyObject *kwds);', names[-1], names[-1])
@@ -799,6 +803,9 @@ def python(env, out_h, out, cls, superCl
          'DECLARE_METHOD(t_%s, cast_, METH_O | METH_CLASS),', names[-1])
     line(out, indent + 1,
          'DECLARE_METHOD(t_%s, instance_, METH_O | METH_CLASS),', names[-1])
+    if clsParams:
+        line(out, indent + 1,
+             'DECLARE_METHOD(t_%s, of_, METH_VARARGS),', names[-1])
 
     for name, methods in allMethods:
         modifiers = methods[0].getModifiers()
@@ -1009,6 +1016,20 @@ def python(env, out_h, out, cls, superCl
     line(out, indent + 1, 'Py_RETURN_TRUE;')
     line(out, indent, '}')
 
+    if clsParams:
+        line(out)
+        line(out, indent,
+             'static PyObject *t_%s_of_(t_%s *self, PyObject *args)',
+             names[-1], names[-1])
+        line(out, indent, '{')
+        line(out, indent + 1,
+             'if (!parseArg(args, "T", %d, &(self->parameters)))',
+             len(clsParams))
+        line(out, indent + 2, 'Py_RETURN_SELF;');
+        line(out, indent + 1,
+             'return PyErr_SetArgsError((PyObject *) self, "of_", args);')
+        line(out, indent, '}')
+
     if constructors:
         line(out)
         line(out, indent, 'static int t_%s_init_(t_%s *self, PyObject *args, PyObject *kwds)', names[-1], names[-1])

Modified: lucene/pylucene/trunk/jcc/jcc/sources/JCCEnv.cpp
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/jcc/jcc/sources/JCCEnv.cpp?rev=930603&r1=930602&r2=930603&view=diff
==============================================================================
--- lucene/pylucene/trunk/jcc/jcc/sources/JCCEnv.cpp (original)
+++ lucene/pylucene/trunk/jcc/jcc/sources/JCCEnv.cpp Sat Apr  3 23:27:05 2010
@@ -160,6 +160,11 @@ jobject JCCEnv::enumerationNext(jobject 
     return callObjectMethod(obj, _mids[mid_enumeration_nextElement]);
 }
 
+jboolean JCCEnv::isInstanceOf(jobject obj, jclass (*initializeClass)())
+{
+    return get_vm_env()->IsInstanceOf(obj, (*initializeClass)());
+}
+
 jclass JCCEnv::findClass(const char *className) const
 {
     jclass cls = NULL;

Modified: lucene/pylucene/trunk/jcc/jcc/sources/JCCEnv.h
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/jcc/jcc/sources/JCCEnv.h?rev=930603&r1=930602&r2=930603&view=diff
==============================================================================
--- lucene/pylucene/trunk/jcc/jcc/sources/JCCEnv.h (original)
+++ lucene/pylucene/trunk/jcc/jcc/sources/JCCEnv.h Sat Apr  3 23:27:05 2010
@@ -137,6 +137,8 @@ public:
     virtual jstring getJavaVersion() const;
 
     virtual jclass findClass(const char *className) const;
+    virtual jboolean isInstanceOf(jobject obj, jclass (*initializeClass)());
+
     virtual void registerNatives(jclass cls, JNINativeMethod *methods,
                                  int n) const;
 

Modified: lucene/pylucene/trunk/jcc/jcc/sources/functions.cpp
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/jcc/jcc/sources/functions.cpp?rev=930603&r1=930602&r2=930603&view=diff
==============================================================================
--- lucene/pylucene/trunk/jcc/jcc/sources/functions.cpp (original)
+++ lucene/pylucene/trunk/jcc/jcc/sources/functions.cpp Sat Apr  3 23:27:05 2010
@@ -499,6 +499,29 @@ int _parseArgs(PyObject **args, unsigned
           case 'o':         /* java.lang.Object */
             break;
 
+          case 'T':         /* tuple of python types with wrapfn_ */
+          {
+              static PyObject *wrapfn_ = PyString_FromString("wrapfn_");
+              int len = va_arg(list, int);
+
+              if (PyTuple_Check(arg))
+              {
+                  if (PyTuple_GET_SIZE(arg) != len)
+                      return -1;
+
+                  for (int i = 0; i < len; i++) {
+                      PyObject *type = PyTuple_GET_ITEM(arg, i);
+
+                      if (!(type == Py_None ||
+                            (PyType_Check(type) &&
+                             PyObject_HasAttr(type, wrapfn_))))
+                          return -1;
+                  }
+                  break;
+              }
+              return -1;
+          }
+
           default:
             return -1;
         }
@@ -881,6 +904,22 @@ int _parseArgs(PyObject **args, unsigned
               break;
           }
 
+          case 'T':         /* tuple of python types with wrapfn_ */
+          {
+              int len = va_arg(check, int);
+              PyTypeObject **types = va_arg(list, PyTypeObject **);
+
+              for (int i = 0; i < len; i++) {
+                  PyObject *type = PyTuple_GET_ITEM(arg, i);
+
+                  if (type == Py_None)
+                      types[i] = NULL;
+                  else
+                      types[i] = (PyTypeObject *) type;
+              }
+              break;
+          }
+
           default:
             return -1;
         }
@@ -1239,7 +1278,8 @@ void installType(PyTypeObject *type, PyO
 
 PyObject *wrapType(PyTypeObject *type, const jobject& obj)
 {
-    PyObject *cobj = PyObject_GetAttrString((PyObject *) type, "wrapfn_");
+    static PyObject *wrapfn_ = PyString_FromString("wrapfn_");
+    PyObject *cobj = PyObject_GetAttr((PyObject *) type, wrapfn_);
     PyObject *(*wrapfn)(const jobject&);
     
     if (cobj == NULL)

Modified: lucene/pylucene/trunk/jcc/jcc/sources/macros.h
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/jcc/jcc/sources/macros.h?rev=930603&r1=930602&r2=930603&view=diff
==============================================================================
--- lucene/pylucene/trunk/jcc/jcc/sources/macros.h (original)
+++ lucene/pylucene/trunk/jcc/jcc/sources/macros.h Sat Apr  3 23:27:05 2010
@@ -113,8 +113,14 @@ PyObject *t_name::wrap_jobject(const job
 {                                                                       \
     if (!!object)                                                       \
     {                                                                   \
-        t_name *self =                                                  \
-            (t_name *) name##$$Type.tp_alloc(&name##$$Type, 0);         \
+        if (!env->isInstanceOf(object, javaClass::initializeClass))     \
+        {                                                               \
+            PyErr_SetObject(PyExc_TypeError,                            \
+                            (PyObject *) &name##$$Type);                \
+            return NULL;                                                \
+        }                                                               \
+        t_name *self = (t_name *)                                       \
+            name##$$Type.tp_alloc(&name##$$Type, 0);                    \
         if (self)                                                       \
             self->object = javaClass(object);                           \
         return (PyObject *) self;                                       \
@@ -139,6 +145,12 @@ PyObject *t_name::wrap_jobject(const job
             Py_RETURN_FALSE;                    \
     }
 
+#define Py_RETURN_SELF                                      \
+    {                                                       \
+        Py_INCREF(self);                                    \
+        return (PyObject *) self;                           \
+    }
+
 
 #if PY_VERSION_HEX < 0x02040000