lucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From va...@apache.org
Subject svn commit: r732916 [5/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/python.py
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/jcc/jcc/python.py?rev=732916&view=auto
==============================================================================
--- lucene/pylucene/trunk/jcc/jcc/python.py (added)
+++ lucene/pylucene/trunk/jcc/jcc/python.py Thu Jan  8 19:28:33 2009
@@ -0,0 +1,1426 @@
+#   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.
+
+import os, sys, platform, shutil, _jcc, py_compile
+
+from cpp import PRIMITIVES, RESERVED, INDENT, HALF_INDENT
+from cpp import typename, line, signature, find_method, split_pkg, sort
+from cpp import Modifier
+from _jcc import findClass
+from config import INCLUDES, CFLAGS, DEBUG_CFLAGS, LFLAGS, SHARED
+
+python_ver = '%d.%d.%d' %(sys.version_info[0:3])
+if python_ver < '2.4':
+    from sets import Set as set
+
+
+RESULTS = { 'boolean': 'Py_RETURN_BOOL(%s);',
+            'byte': 'return PyString_FromStringAndSize((char *) &%s, 1);',
+            'char': 'return PyUnicode_FromUnicode((Py_UNICODE *) &%s, 1);',
+            'double': 'return PyFloat_FromDouble((double) %s);',
+            'float': 'return PyFloat_FromDouble((double) %s);',
+            'int': 'return PyInt_FromLong((long) %s);',
+            'long': 'return PyLong_FromLongLong((PY_LONG_LONG) %s);',
+            'short': 'return PyInt_FromLong((long) %s);',
+            'java.lang.String': 'return j2p(%s);' }
+
+CALLARGS = { 'boolean': ('O', '(%s ? Py_True : Py_False)', False),
+             'byte': ('O', 'PyString_FromStringAndSize((char *) &%s, 1)', True),
+             'char': ('O', 'PyUnicode_FromUnicode((Py_UNICODE *) &%s, 1)', True),
+             'double': ('d', '(double) %s', False),
+             'float': ('f', '(float) %s', False),
+             'int': ('i', '(int) %s', False),
+             'long': ('L', '(long long) %s', False),
+             'short': ('i', '(int) %s', False),
+             'java.lang.String': ('O', 'env->fromJString((jstring) %s)', True) }
+
+
+def parseArgs(params, current):
+
+    def signature(cls):
+        array = ''
+        while cls.isArray():
+            array += '['
+            cls = cls.getComponentType()
+        clsName = cls.getName()
+        if cls.isPrimitive():
+            return array + PRIMITIVES[clsName]
+        if clsName == 'java.lang.String':
+            return array + 's'
+        if clsName == 'java.lang.Object':
+            return array + 'o'
+        return array + 'j'
+
+    def checkarg(cls):
+        while cls.isArray():
+            cls = cls.getComponentType()
+        if (cls.isPrimitive() or
+            cls.getName() in ('java.lang.String', 'java.lang.Object')):
+            return ''
+        return ', %s::getclass$()' %(typename(cls, current, False))
+
+    def callarg(cls, i):
+        return ', &a%d' %(i)
+
+    return (''.join([signature(param) for param in params]),
+            ''.join([checkarg(param) for param in params]),
+            ''.join([callarg(params[i], i) for i in xrange(len(params))]))
+
+
+def construct(out, indent, cls, inCase, constructor, names):
+
+    if inCase:
+        line(out, indent, '{')
+        indent += 1
+
+    params = constructor.getParameterTypes()
+    count = len(params)
+
+    for i in xrange(count):
+        line(out, indent, '%s a%d%s;',
+             typename(params[i], cls, False), i,
+             not params[i].isPrimitive() and '((jobject) NULL)' or '')
+    line(out, indent, '%s object((jobject) NULL);', names[-1])
+
+    line(out)
+    if count:
+        line(out, indent, 'if (!parseArgs(args, "%s"%s%s))',
+             *parseArgs(params, cls))
+        line(out, indent, '{')
+        indent += 1
+
+    line(out, indent, 'INT_CALL(object = %s(%s));',
+         names[-1], ', '.join(['a%d' %(i) for i in xrange(count)]))
+    line(out, indent, 'self->object = object;')
+    if inCase:
+        line(out, indent, 'break;')
+
+    if count:
+        indent -= 1
+        line(out, indent, '}')
+
+    if inCase:
+        indent -= 1
+        line(out, indent, '}')
+
+
+def rpartition(string, sep):
+
+    if python_ver >= '2.5.0':
+        return string.rpartition(sep)
+    else:
+        parts = split_pkg(string, sep)
+        if len(parts) == 1:
+            return ('', '', parts[0])
+        return (parts[0], sep, parts[1])
+
+
+def fieldValue(cls, value, fieldType):
+
+    if fieldType.isArray():
+        fieldType = fieldType.getComponentType()
+        if fieldType.isArray():
+            result = 'JArray<jobject>(%s->this$).wrap(NULL)'
+        elif fieldType.isPrimitive():
+            result = '%s->wrap()'
+        elif fieldType.getName() == 'java.lang.String':
+            result = 'JArray<jstring>(%s->this$).wrap()'
+        else:
+            parts = rpartition(typename(fieldType, cls, False), '::')
+            result = 'JArray<jobject>(%%s->this$).wrap(%s%st_%s::wrap_jobject)' %(parts)
+
+    elif fieldType.getName() == 'java.lang.String':
+        result = 'j2p(*%s)'
+
+    elif not fieldType.isPrimitive():
+        parts = rpartition(typename(fieldType, cls, False), '::')
+        result = '%s%st_%s::wrap_Object(*%%s)' %(parts)
+
+    else:
+        return value
+
+    return result %(value)
+
+
+def returnValue(cls, returnType, value):
+
+    result = RESULTS.get(returnType.getName())
+    if not result:
+        if returnType.isArray():
+            returnType = returnType.getComponentType()
+            depth = 1
+            while returnType.isArray():
+                returnType = returnType.getComponentType()
+                depth += 1
+            if depth > 1:
+                result = 'return JArray<jobject>(%s.this$).wrap(NULL);'
+            elif returnType.isPrimitive():
+                result = 'return %s.wrap();'
+            elif returnType.getName() == 'java.lang.String':
+                result = 'return JArray<jstring>(%s.this$).wrap();'
+            else:
+                returnName = typename(returnType, cls, False)
+                parts = rpartition(returnName, '::')
+                result = 'return JArray<jobject>(%%s.this$).wrap(%s%st_%s::wrap_jobject);' %(parts)
+        else:
+            returnName = typename(returnType, cls, False)
+            parts = rpartition(returnName, '::')
+            result = 'return %s%st_%s::wrap_Object(%%s);' %(parts)
+
+    return result %(value)
+
+
+def call(out, indent, cls, inCase, method, names, cardinality, isExtension):
+
+    if inCase:
+        line(out, indent, '{')
+        indent += 1
+
+    name = method.getName()
+    modifiers = method.getModifiers()
+    params = method.getParameterTypes()
+    returnType = method.getReturnType()
+    count = len(params)
+
+    for i in xrange(count):
+        line(out, indent, '%s a%d%s;',
+             typename(params[i], cls, False), i,
+             not params[i].isPrimitive() and '((jobject) NULL)' or '')
+    returnName = returnType.getName()
+    if returnName != 'void':
+        line(out, indent, '%s result%s;',
+             typename(returnType, cls, False),
+             not returnType.isPrimitive() and '((jobject) NULL)' or '')
+        result = 'result = '
+    else:
+        result = ''
+
+    if cardinality and (count or not inCase):
+        s = cardinality > 1 and 's' or ''
+        line(out)
+        if isExtension and name == 'clone' and Modifier.isNative(modifiers):
+            line(out, indent, 'if (arg)')
+        else:
+            line(out, indent, 'if (!parseArg%s(arg%s, "%s"%s%s))',
+                 s, s, *parseArgs(params, cls))
+        line(out, indent, '{')
+        indent += 1
+
+    if name in RESERVED:
+        name += '$'
+    if Modifier.isStatic(modifiers):
+        line(out, indent, 'OBJ_CALL(%s%s::%s(%s));',
+             result, '::'.join(names), name,
+             ', '.join(['a%d' %(i) for i in xrange(count)]))
+    else:
+        line(out, indent, 'OBJ_CALL(%sself->object.%s(%s));',
+             result, name, ', '.join(['a%d' %(i) for i in xrange(count)]))
+
+    if isExtension and name == 'clone' and Modifier.isNative(modifiers):
+        line(out)
+        line(out, indent, '%s object(result.this$);', typename(cls, cls, False))
+        line(out, indent, 'if (PyObject_TypeCheck(arg, &FinalizerProxyType) &&')
+        line(out, indent, '    PyObject_TypeCheck(((t_fp *) arg)->object, self->ob_type))')
+        line(out, indent, '{')
+        line(out, indent + 1, 'PyObject *_arg = ((t_fp *) arg)->object;')
+        line(out, indent + 1, '((t_JObject *) _arg)->object = object;')
+        line(out, indent + 1, 'Py_INCREF(_arg);')
+        line(out, indent + 1, 'object.pythonExtension((jlong) (Py_intptr_t) (void *) _arg);')
+        line(out, indent + 1, 'Py_INCREF(arg);')
+        line(out, indent + 1, 'return arg;')
+        line(out, indent, '}')
+        line(out, indent, 'return PyErr_SetArgsError("%s", arg);' %(name))
+    elif returnName != 'void':
+        line(out, indent, returnValue(cls, returnType, 'result'))
+    else:
+        line(out, indent, 'Py_RETURN_NONE;')
+    if cardinality and (count or not inCase):
+        indent -= 1
+        line(out, indent, '}')
+
+    if inCase:
+        indent -= 1
+        line(out, indent, '}')
+
+
+def methodargs(methods, superMethods):
+        
+    if len(methods) == 1 and methods[0].getName() not in superMethods:
+        count = len(methods[0].getParameterTypes())
+        if count == 0:
+            return '', '', 0
+        elif count == 1:
+            return ', PyObject *arg', ', arg', 1
+
+    return ', PyObject *args', ', args', 2
+
+
+def jniname(cls):
+    
+    if cls.isPrimitive():
+        name = cls.getName()
+        if name != 'void':
+            name = 'j' + name
+    else:
+        name = 'jobject'
+
+    return name
+
+
+def jniargs(params):
+
+    count = len(params)
+    decls = ', '.join(['%s a%d' %(jniname(params[i]), i)
+                       for i in xrange(count)])
+    if decls:
+        return ', ' + decls
+
+    return ''
+
+
+def extension(env, out, indent, cls, names, name, count, method):
+
+    line(out, indent, 'jlong ptr = jenv->CallLongMethod(jobj, %s::mids$[%s::mid_pythonExtension_%s]);',
+         names[-1], names[-1], env.strhash('()J'))
+    line(out, indent, 'PyObject *obj = (PyObject *) (Py_intptr_t) ptr;')
+
+    if name == 'pythonDecRef':
+        line(out)
+        line(out, indent, 'if (obj != NULL)')
+        line(out, indent, '{')
+        line(out, indent + 1, 'jenv->CallVoidMethod(jobj, %s::mids$[%s::mid_pythonExtension_%s], (jlong) 0);',
+             names[-1], names[-1], env.strhash('(J)V'))
+        line(out, indent + 1, 'env->finalizeObject(jenv, obj);')
+        line(out, indent, '}')
+        return
+
+    returnType = method.getReturnType()
+    returnName = returnType.getName()
+    if returnName != 'void':
+        line(out, indent, '%s value%s;',
+             typename(returnType, cls, False),
+             not returnType.isPrimitive() and '((jobject) NULL)' or '')
+
+    line(out, indent, 'PythonGIL gil(jenv);')
+
+    sigs = []
+    decrefs = []
+    args = []
+    i = 0
+    for param in method.getParameterTypes():
+        typeName = param.getName()
+        if typeName in CALLARGS:
+            sig, code, decref = CALLARGS[typeName]
+        elif param.isArray():
+            param = param.getComponentType()
+            if param.isPrimitive():
+                code = 'JArray<j%s>(%%s).wrap()' %(param.getName())
+            elif param.isArray():
+                code = 'JArray<jobject>(%s).wrap(NULL)'
+            elif param.getName() == 'java.lang.String':
+                code = 'JArray<jstring>(%s).wrap()'
+            else:
+                parts = rpartition(typename(param, cls, False), '::')
+                code = 'JArray<jobject>(%%s).wrap(%s%st_%s::wrap_jobject)' %(parts)
+            sig, decref = 'O', True
+        elif param.getName() == 'java.lang.String':
+            sig, code, decref = 'O', 'j2p(%%s))', True
+        else:
+            parts = rpartition(typename(param, cls, False), '::')
+            sig, code, decref = 'O', '%s%st_%s::wrap_Object(%s%s%s(%%s))' %(parts*2), True
+        if sig == 'O':
+            line(out, indent, 'PyObject *o%d = %s;', i, code %('a%d' %(i)))
+            args.append('o%d' %(i))
+        else:
+            args.append(code %('a%d' %(i)))
+        sigs.append(sig)
+        decrefs.append(decref)
+        i += 1
+
+    args = ', '.join(args)
+    if args:
+        args = ', ' + args
+    line(out, indent, 'PyObject *result = PyObject_CallMethod(obj, "%s", "%s"%s);',
+         name, ''.join(sigs), args)
+    i = 0
+    for decref in decrefs:
+        if decref:
+            line(out, indent, 'Py_DECREF(o%d);', i)
+        i += 1
+    line(out, indent, 'if (!result)')
+    line(out, indent + 1, 'throwPythonError();')
+    if returnName == 'void':
+        line(out, indent, 'else')
+        line(out, indent + 1, 'Py_DECREF(result);')
+    else:
+        signature, check, x = parseArgs([returnType], cls)
+        line(out, indent, 'else if (parseArg(result, "%s"%s, &value))',
+             signature, check)
+        line(out, indent, '{')
+        line(out, indent + 1, 'throwTypeError("%s", result);', name)
+        line(out, indent + 1, 'Py_DECREF(result);')
+        line(out, indent, '}')
+        line(out, indent, 'else')
+        line(out, indent, '{')
+        if not returnType.isPrimitive():
+            line(out, indent + 1, 'jobj = jenv->NewLocalRef(value.this$);')
+        line(out, indent + 1, 'Py_DECREF(result);')
+        if returnType.isPrimitive():
+            line(out, indent + 1, 'return value;')
+        else:
+            line(out, indent + 1, 'return jobj;')
+        line(out, indent, '}')
+        line(out)
+        if returnType.isPrimitive():
+            line(out, indent, 'return (j%s) 0;', returnName)
+        else:
+            line(out, indent, 'return (jobject) NULL;')
+
+
+def python(env, out_h, out, cls, superCls, names, superNames,
+           constructors, methods, protectedMethods, fields, instanceFields,
+           mapping, sequence, declares, typeset, excludes, moduleName):
+
+    line(out_h)
+    line(out_h, 0, '#include <Python.h>')
+    line(out_h)
+
+    indent = 0
+    for name in names[:-1]:
+        line(out_h, indent, 'namespace %s {', name)
+        indent += 1
+    line(out_h, indent, 'extern PyTypeObject %sType;', names[-1])
+
+    line(out_h)
+    line(out_h, indent, 'class t_%s {', names[-1])
+    line(out_h, indent, 'public:')
+    line(out_h, indent + 1, 'PyObject_HEAD')
+    line(out_h, indent + 1, '%s object;', names[-1])
+    line(out_h, indent + 1, 'static PyObject *wrap_Object(const %s&);',
+         names[-1])
+    line(out_h, indent + 1, 'static PyObject *wrap_jobject(const jobject&);')
+    line(out_h, indent + 1, 'static void install(PyObject *module);')
+    line(out_h, indent + 1, 'static void initialize(PyObject *module);')
+    line(out_h, indent, '};')
+
+    iterator = findClass('java/util/Iterator')
+    enumeration = findClass('java/util/Enumeration')
+
+    while indent:
+        indent -= 1
+        line(out_h, indent, '}')
+
+    line(out)
+    line(out, 0, '#include "structmember.h"')
+    line(out, 0, '#include "functions.h"')
+    line(out, 0, '#include "macros.h"')
+
+    for inner in cls.getDeclaredClasses():
+        if inner in typeset and not inner in declares:
+            if Modifier.isStatic(inner.getModifiers()):
+                line(out, 0, '#include "%s.h"',
+                     inner.getName().replace('.', '/'))
+
+    for method in methods:
+        if method.getName() == 'pythonExtension':
+            isExtension = True
+            break
+    else:
+        isExtension = False
+                
+    line(out)
+    indent = 0
+    for name in names[:-1]:
+        line(out, indent, 'namespace %s {', name)
+        indent += 1
+
+    if not isExtension:
+        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 constructors:
+        line(out, indent, 'static int t_%s_init_(t_%s *self, PyObject *args, PyObject *kwds);', names[-1], names[-1])
+        constructorName = 't_%s_init_' %(names[-1])
+    else:
+        constructorName = 'abstract_init'
+
+    if superCls:
+        superMethods = set([method.getName()
+                            for method in superCls.getMethods()])
+    else:
+        superMethods = ()
+
+    allMethods = {}
+    extMethods = {}
+    propMethods = {}
+
+    if methods:
+        for method in methods:
+            modifiers = method.getModifiers()
+            name = method.getName()
+            params = method.getParameterTypes()
+            superMethod = None
+            isNative = Modifier.isNative(modifiers)
+            isStatic = Modifier.isStatic(modifiers)
+
+            if (isExtension and not isStatic and superCls and isNative):
+                superMethod = find_method(superCls, name, params)
+
+            if isExtension and isNative and not isStatic:
+                extMethods.setdefault(name, []).append(method)
+
+            if superMethod or not (isExtension and isNative and not isStatic):
+                allMethods.setdefault(name, []).append(method)
+
+            if not (isExtension and isNative):
+                nameLen = len(name)
+                paramsLen = len(params)
+                if nameLen > 3 and paramsLen == 0 and name.startswith('get'):
+                    propMethods.setdefault(name[3].lower() + name[4:],
+                                           []).append(method)
+                elif nameLen > 3 and paramsLen == 1 and name.startswith('set'):
+                    propMethods.setdefault(name[3].lower() + name[4:],
+                                           []).append(method)
+                elif nameLen > 2 and paramsLen == 0 and name.startswith('is'):
+                    propMethods.setdefault(name[2].lower() + name[3:],
+                                           []).append(method)
+
+    properties = set([name for name in propMethods.iterkeys()
+                      if name not in allMethods])
+    propMethods = [(name, propMethods[name]) for name in properties]
+    sort(propMethods, key=lambda x: x[0])
+
+    extMethods = extMethods.items()
+    sort(extMethods, key=lambda x: x[0])
+    allMethods = allMethods.items()
+    sort(allMethods, key=lambda x: x[0])
+
+    iteratorMethod = None
+    iteratorExt = False
+    nextMethod = None
+    nextExt = False
+    nextElementMethod = None
+    nextElementExt = False
+
+    mappingMethod = None
+    if mapping:
+        mappingName, mappingSig = mapping.split(':')
+
+    sequenceLenMethod = None
+    sequenceGetMethod = None
+    if sequence:
+        sequenceLenName, sequenceLenSig = sequence[0].split(':')
+        sequenceGetName, sequenceGetSig = sequence[1].split(':')
+        
+    for name, methods in allMethods:
+        args, x, cardinality = methodargs(methods, superMethods)
+        sort(methods, key=lambda x: len(x.getParameterTypes()))
+        method = methods[0]
+        modifiers = method.getModifiers()
+        if name == 'iterator' and iteratorMethod is None:
+            if (not method.getParameterTypes() and
+                iterator.isAssignableFrom(method.getReturnType())):
+                iteratorMethod = method
+        elif name == 'next' and nextMethod is None:
+            if (not method.getParameterTypes() and
+                not method.getReturnType().isPrimitive()):
+                nextMethod = method
+        elif name == 'nextElement' and nextElementMethod is None:
+            if (not method.getParameterTypes() and
+                not method.getReturnType().isPrimitive()):
+                nextElementMethod = method
+        elif mapping and name == mappingName and mappingMethod is None:
+            if signature(method) == mappingSig:
+                mappingMethod = (method, cardinality)
+        elif sequence and name == sequenceLenName and sequenceLenMethod is None:
+            if signature(method) == sequenceLenSig:
+                sequenceLenMethod = (method, cardinality)
+        elif sequence and name == sequenceGetName and sequenceGetMethod is None:
+            if signature(method) == sequenceGetSig:
+                sequenceGetMethod = (method, cardinality)
+        elif isExtension and name == 'clone' and Modifier.isNative(modifiers):
+            args, x, cardinality = ', PyObject *arg', ', arg', 1
+
+        if Modifier.isStatic(modifiers):
+            line(out, indent, 'static PyObject *t_%s_%s(PyTypeObject *type%s);',
+                 names[-1], name, args)
+        else:
+            line(out, indent, 'static PyObject *t_%s_%s(t_%s *self%s);',
+                 names[-1], name, names[-1], args)
+
+    for name, methods in extMethods:
+        args, x, cardinality = methodargs(methods, superMethods)
+        sort(methods, key=lambda x: len(x.getParameterTypes()))
+        method = methods[0]
+        modifiers = method.getModifiers()
+        if name == 'iterator' and iteratorMethod is None:
+            if (not method.getParameterTypes() and
+                iterator.isAssignableFrom(method.getReturnType())):
+                iteratorMethod = method
+                iteratorExt = True
+        elif name == 'next' and nextMethod is None:
+            if (not method.getParameterTypes() and
+                not method.getReturnType().isPrimitive()):
+                nextMethod = method
+                nextExt = True
+        elif name == 'nextElement' and nextElementMethod is None:
+            if (not method.getParameterTypes() and
+                not method.getReturnType().isPrimitive()):
+                nextElementMethod = method
+                nextElementExt = True
+
+    if isExtension:
+        count = 0
+        for name, methods in extMethods:
+            for method in methods:
+                line(out, indent,
+                     'static %s JNICALL t_%s_%s%d(JNIEnv *jenv, jobject jobj%s);',
+                     jniname(method.getReturnType()), names[-1], name, count,
+                     jniargs(method.getParameterTypes()))
+                count += 1
+        line(out, indent, 'static PyObject *t_%s_get__self(t_%s *self, void *data);', names[-1], names[-1])
+
+    if instanceFields:
+        for field in instanceFields:
+            fieldName = field.getName()
+            if fieldName not in properties:
+                line(out, indent, 'static PyObject *t_%s_get__%s(t_%s *self, void *data);',
+                     names[-1], fieldName, names[-1])
+                if not Modifier.isFinal(field.getModifiers()):
+                    line(out, indent, 'static int t_%s_set__%s(t_%s *self, PyObject *arg, void *data);',
+                         names[-1], field.getName(), names[-1])
+        line(out)
+
+    for fieldName, methods in propMethods:
+        getter = False
+        setter = False
+        for method in methods:
+            methodName = method.getName()
+            if not getter and (methodName.startswith('get') or
+                               methodName.startswith('is')):
+                getter = True
+                line(out, indent, 'static PyObject *t_%s_get__%s(t_%s *self, void *data);',
+                     names[-1], fieldName, names[-1])
+            elif not setter and methodName.startswith('set'):
+                setter = True
+                line(out, indent, 'static int t_%s_set__%s(t_%s *self, PyObject *arg, void *data);',
+                     names[-1], fieldName, names[-1])
+
+    if instanceFields or propMethods or isExtension:
+        line(out, indent, 'static PyGetSetDef t_%s__fields_[] = {', names[-1])
+        for field in instanceFields:
+            fieldName = field.getName()
+            if fieldName not in properties:
+                if Modifier.isFinal(field.getModifiers()):
+                    line(out, indent + 1, 'DECLARE_GET_FIELD(t_%s, %s),',
+                         names[-1], fieldName)
+                else:
+                    line(out, indent + 1, 'DECLARE_GETSET_FIELD(t_%s, %s),',
+                         names[-1], fieldName)
+        for fieldName, methods in propMethods:
+            getter = False
+            setter = False
+            for method in methods:
+                methodName = method.getName()
+                if not getter and (methodName.startswith('get') or
+                                   methodName.startswith('is')):
+                    getter = True
+                elif not setter and methodName.startswith('set'):
+                    setter = True
+                if getter and setter:
+                    op = 'GETSET'
+                elif getter:
+                    op = 'GET'
+                elif setter:
+                    op = 'SET'
+            line(out, indent + 1, 'DECLARE_%s_FIELD(t_%s, %s),',
+                 op, names[-1], fieldName)
+        if isExtension:
+            line(out, indent + 1, 'DECLARE_GET_FIELD(t_%s, self),', names[-1])
+        line(out, indent + 1, '{ NULL, NULL, NULL, NULL, NULL }')
+        line(out, indent, '};')
+
+    line(out)
+    line(out, indent, 'static PyMethodDef t_%s__methods_[] = {', names[-1])
+
+    if not isExtension:
+        line(out, indent + 1,
+             '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])
+
+    for name, methods in allMethods:
+        modifiers = methods[0].getModifiers()
+        if len(methods) == 1 and not name in superMethods:
+            count = len(methods[0].getParameterTypes())
+            if count == 0:
+                args = 'METH_NOARGS'
+            elif count == 1:
+                args = 'METH_O'
+            else:
+                args = 'METH_VARARGS'
+        elif isExtension and name == 'clone' and Modifier.isNative(modifiers):
+            args = 'METH_O'
+        else:
+            args = 'METH_VARARGS'
+        if Modifier.isStatic(modifiers):
+            args += ' | METH_CLASS'
+
+        line(out, indent + 1, 'DECLARE_METHOD(t_%s, %s, %s),',
+             names[-1], name, args)
+    line(out, indent + 1, '{ NULL, NULL, 0, NULL }')
+    line(out, indent, '};')
+
+    if instanceFields or propMethods or isExtension:
+        tp_getset = 't_%s__fields_' %(names[-1])
+    else:
+        tp_getset = '0'
+
+    if iteratorMethod:
+        if iteratorExt:
+            tp_iter = 'get_extension_iterator'
+        else:
+            tp_iter = 'get_iterator<t_%s>' %(names[-1])
+        tp_iternext = '0'
+    elif nextMethod and iterator.isAssignableFrom(cls):
+        tp_iter = 'PyObject_SelfIter'
+        returnName = typename(nextMethod.getReturnType(), cls, False)
+        ns, sep, n = rpartition(returnName, '::')
+        if nextExt:
+            tp_iternext = 'get_extension_next'
+        else:
+            tp_iternext = '(get_iterator_next<%s%st_%s,%s>)' %(ns, sep, n, returnName)
+    elif nextElementMethod and enumeration.isAssignableFrom(cls):
+        tp_iter = 'PyObject_SelfIter'
+        returnName = typename(nextElementMethod.getReturnType(), cls, False)
+        ns, sep, n = rpartition(returnName, '::')
+        if nextElementExt:
+            tp_iternext = 'get_extension_nextElement'
+        else:
+            tp_iternext = '(get_enumeration_nextElement<%s%st_%s,%s>)' %(ns, sep, n, returnName)
+    elif nextMethod:
+        tp_iter = 'PyObject_SelfIter'
+        returnName = typename(nextMethod.getReturnType(), cls, False)
+        ns, sep, n = rpartition(returnName, '::')
+        if nextExt:
+            tp_iternext = 'get_extension_next'
+        else:
+            tp_iternext = '(get_next<t_%s,%s%st_%s,%s>)' %(names[-1], ns, sep, n, returnName)
+    else:
+        tp_iter = '0'
+        tp_iternext = '0'
+
+    if mappingMethod:
+        method, cardinality = mappingMethod
+        if cardinality > 1:
+            getName = 't_%s_%s_map_' %(names[-1], method.getName())
+            line(out, indent, 'static PyObject *%s(t_%s *self, PyObject *key);',
+                 getName, names[-1])
+        else:
+            getName = 't_%s_%s' %(names[-1], method.getName())
+        line(out)
+        line(out, indent, 'static PyMappingMethods t_%s_as_mapping = {',
+             names[-1])
+        line(out, indent + 1, '0,')
+        line(out, indent + 1, '(binaryfunc) %s,', getName)
+        line(out, indent + 1, '0,')
+        line(out, indent, '};')
+        tp_as_mapping = '&t_%s_as_mapping' %(names[-1])
+    else:
+        tp_as_mapping = '0'
+
+    if sequenceLenMethod or sequenceGetMethod:
+        if sequenceLenMethod:
+            method, cardinality = sequenceLenMethod
+            lenName = 't_%s_%s_seq_' %(names[-1], method.getName())
+            line(out, indent, 'static int %s(t_%s *self);', lenName, names[-1])
+        else:
+            lenName = '0'
+
+        if sequenceGetMethod:
+            method, cardinality = sequenceGetMethod
+            getName = 't_%s_%s_seq_' %(names[-1], method.getName())
+            line(out, indent, 'static PyObject *%s(t_%s *self, int n);',
+                 getName, names[-1])
+        else:
+            getName = '0'
+
+        line(out)
+        line(out, indent, 'static PySequenceMethods t_%s_as_sequence = {',
+             names[-1])
+        if python_ver < '2.5.0':
+            line(out, indent + 1, '(inquiry) %s,', lenName)
+            line(out, indent + 1, '0,')
+            line(out, indent + 1, '0,')
+            line(out, indent + 1, '(intargfunc) %s', getName)
+            line(out, indent, '};')
+        else:
+            line(out, indent + 1, '(lenfunc) %s,', lenName)
+            line(out, indent + 1, '0,')
+            line(out, indent + 1, '0,')
+            line(out, indent + 1, '(ssizeargfunc) %s', getName)
+            line(out, indent, '};')
+        tp_as_sequence = '&t_%s_as_sequence' %(names[-1])
+    else:
+        tp_as_sequence = '0'
+
+    line(out)
+    line(out, indent, 'DECLARE_TYPE(%s, t_%s, %s, %s, %s, %s, %s, %s, %s, %s);',
+         names[-1], names[-1], '::'.join(superNames), names[-1],
+         constructorName, tp_iter, tp_iternext, tp_getset, tp_as_mapping,
+         tp_as_sequence)
+
+    line(out)
+    line(out, indent, 'void t_%s::install(PyObject *module)', names[-1])
+    line(out, indent, '{')
+    if isExtension:
+        line(out, indent + 1, 'INSTALL_EXTENSION(%s, module);', names[-1])
+    else:
+        line(out, indent + 1, 'INSTALL_TYPE(%s, module);', names[-1])
+    for inner in cls.getDeclaredClasses():
+        if inner in typeset:
+            if Modifier.isStatic(inner.getModifiers()):
+                innerName = inner.getName().split('.')[-1]
+                line(out, indent + 1, 'PyDict_SetItemString(%sType.tp_dict, "%s", make_descriptor(&%sType));',
+                     names[-1], innerName[len(names[-1])+1:], innerName)
+    line(out, indent, '}')
+
+    line(out)
+    line(out, indent, 'void t_%s::initialize(PyObject *module)', names[-1])
+    line(out, indent, '{')
+    line(out, indent + 1, 'PyDict_SetItemString(%sType.tp_dict, "class_", make_descriptor(%s::initializeClass));',
+         names[-1], names[-1])
+    line(out, indent + 1, 'PyDict_SetItemString(%sType.tp_dict, "wrapfn_", make_descriptor(t_%s::wrap_jobject));',
+         names[-1], names[-1])
+
+    if isExtension:
+        line(out, indent + 1, 'jclass cls = %s::initializeClass();', names[-1]);
+    elif fields:
+        line(out, indent + 1, '%s::initializeClass();', names[-1]);
+
+    if isExtension:
+        count = 0
+        line(out, indent + 1, 'JNINativeMethod methods[] = {')
+        for name, methods in extMethods:
+            for method in methods:
+                line(out, indent + 2, '{ "%s", "%s", (void *) t_%s_%s%d },',
+                     name, signature(method), names[-1], name, count)
+                count += 1
+        line(out, indent + 1, '};')
+        line(out, indent + 1, 'env->registerNatives(cls, methods, %d);',
+             count)
+
+    for field in fields:
+        fieldType = field.getType()
+        fieldName = field.getName()
+        value = '%s::%s' %(names[-1], fieldName)
+        if fieldName in RESERVED:
+            value += '$'
+        value = fieldValue(cls, value, fieldType)
+        line(out, indent + 1, 'PyDict_SetItemString(%sType.tp_dict, "%s", make_descriptor(%s));',
+             names[-1], fieldName, value)
+    line(out, indent, '}')
+
+    if not isExtension:
+        line(out)
+        line(out, indent, 'static PyObject *t_%s_cast_(PyTypeObject *type, PyObject *arg)', names[-1])
+        line(out, indent, '{')
+        line(out, indent + 1, 'if (!(arg = castCheck(arg, %s::initializeClass(), 1)))', names[-1])
+        line(out, indent + 2, 'return NULL;')
+        line(out, indent + 1, 'return t_%s::wrap_Object(%s(((t_%s *) arg)->object.this$));', names[-1], names[-1], names[-1])
+        line(out, indent, '}')
+
+        line(out, indent, 'static PyObject *t_%s_instance_(PyTypeObject *type, PyObject *arg)', names[-1])
+        line(out, indent, '{')
+        line(out, indent + 1, 'if (!castCheck(arg, %s::initializeClass(), 0))', names[-1])
+        line(out, indent + 2, 'Py_RETURN_FALSE;')
+        line(out, indent + 1, 'Py_RETURN_TRUE;')
+        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])
+        line(out, indent, '{')
+        if len(constructors) > 1:
+            currLen = -1
+            line(out, indent + 1, 'switch (PyTuple_GET_SIZE(args)) {')
+            withErr = False
+            for constructor in constructors:
+                params = constructor.getParameterTypes()
+                if len(params) != currLen:
+                    if currLen >= 0:
+                        withErr = True
+                        line(out, indent + 2, 'goto err;')
+                    currLen = len(params)
+                    line(out, indent + 1, '%scase %d:', HALF_INDENT, currLen)
+                construct(out, indent + 2, cls, True, constructor, names)
+            line(out, indent + 1, '%sdefault:', HALF_INDENT)
+            if withErr:
+                line(out, indent + 1, '%serr:', HALF_INDENT)
+            line(out, indent + 2, 'PyErr_SetArgsError((PyObject *) self, "__init__", args);')
+            line(out, indent + 2, 'return -1;')
+            line(out, indent + 1, '}')
+        else:
+            construct(out, indent + 1, cls, False, constructors[0], names)
+            if constructors[0].getParameterTypes():
+                line(out, indent + 1, 'else')
+                line(out, indent + 1, '{')
+                line(out, indent + 2, 'PyErr_SetArgsError((PyObject *) self, "__init__", args);')
+                line(out, indent + 2, 'return -1;')
+                line(out, indent + 1, '}')
+
+        if isExtension:
+            line(out)
+            line(out, indent + 1, 'Py_INCREF((PyObject *) self);')
+            line(out, indent + 1, 'self->object.pythonExtension((jlong) (Py_intptr_t) (void *) self);')
+
+        line(out)
+        line(out, indent + 1, 'return 0;')
+        line(out, indent , '}')
+
+    for name, methods in allMethods:
+        line(out)
+        modifiers = methods[0].getModifiers()
+        if isExtension and name == 'clone' and Modifier.isNative(modifiers):
+            declargs, args, cardinality = ', PyObject *arg', ', arg', 1
+        else:
+            declargs, args, cardinality = methodargs(methods, superMethods)
+        static = Modifier.isStatic(modifiers)
+        if static:
+            line(out, indent, 'static PyObject *t_%s_%s(PyTypeObject *type%s)',
+                 names[-1], name, declargs)
+        else:
+            line(out, indent, 'static PyObject *t_%s_%s(t_%s *self%s)',
+                 names[-1], name, names[-1], declargs)
+
+        line(out, indent, '{')
+        if len(methods) > 1:
+            currLen = -1
+            line(out, indent + 1, 'switch (PyTuple_GET_SIZE(args)) {')
+            for method in methods:
+                params = method.getParameterTypes()
+                if len(params) != currLen:
+                    if currLen >= 0:
+                        line(out, indent + 2, 'break;')
+                    currLen = len(params)
+                    line(out, indent + 1, '%scase %d:', HALF_INDENT, currLen)
+                call(out, indent + 2, cls, True, method, names, cardinality,
+                     isExtension)
+            line(out, indent + 1, '}')
+        else:
+            call(out, indent + 1, cls, False, methods[0], names, cardinality,
+                 isExtension)
+
+        if args:
+            line(out)
+            if name in superMethods:
+                if static:
+                    line(out, indent + 1, 'return callSuper(type, "%s"%s, %d);',
+                         name, args, cardinality)
+                else:
+                    line(out, indent + 1, 'return callSuper(&%sType, (PyObject *) self, "%s"%s, %d);',
+                         names[-1], name, args, cardinality)
+            else:
+                line(out, indent + 1, 'PyErr_SetArgsError(%s, "%s"%s);',
+                     static and 'type' or '(PyObject *) self', name, args)
+                line(out, indent + 1, 'return NULL;')
+
+        line(out, indent, '}')
+
+    if isExtension:
+        count = 0
+        for name, methods in extMethods:
+            for method in methods:
+                line(out)
+                line(out, indent,
+                     'static %s JNICALL t_%s_%s%d(JNIEnv *jenv, jobject jobj%s)',
+                     jniname(method.getReturnType()), names[-1], name, count,
+                     jniargs(method.getParameterTypes()))
+                count += 1
+                line(out, indent, '{')
+                extension(env, out, indent + 1, cls, names, name, count, method)
+                line(out, indent, '}')
+        line(out)
+        line(out, indent, 'static PyObject *t_%s_get__self(t_%s *self, void *data)',
+             names[-1], names[-1])
+        line(out, indent, '{')
+        indent += 1
+        line(out, indent, 'jlong ptr;')
+        line(out, indent, 'OBJ_CALL(ptr = self->object.pythonExtension());')
+        line(out, indent, 'PyObject *obj = (PyObject *) (Py_intptr_t) ptr;')
+        line(out)
+        line(out, indent, 'if (obj != NULL)')
+        line(out, indent, '{')
+        line(out, indent + 1, 'Py_INCREF(obj);')
+        line(out, indent + 1, 'return obj;')
+        line(out, indent, '}')
+        line(out, indent, 'else')
+        line(out, indent + 1, 'Py_RETURN_NONE;')
+        indent -= 1
+        line(out, indent, '}')
+
+    if instanceFields:
+        for field in instanceFields:
+            fieldName = field.getName()
+            if fieldName not in properties:
+                line(out)
+                fieldType = field.getType()
+                typeName = typename(fieldType, cls, False)
+                line(out, indent, 'static PyObject *t_%s_get__%s(t_%s *self, void *data)',
+                     names[-1], fieldName, names[-1])
+                line(out, indent, '{')
+                line(out, indent + 1, '%s value%s;', typeName,
+                     not fieldType.isPrimitive() and '((jobject) NULL)' or '')
+                line(out, indent + 1, 'OBJ_CALL(value = self->object._get_%s());',
+                     fieldName)
+                line(out, indent + 1, returnValue(cls, fieldType, 'value'))
+                line(out, indent, '}')
+
+                if not Modifier.isFinal(field.getModifiers()):
+                    line(out, indent, 'static int t_%s_set__%s(t_%s *self, PyObject *arg, void *data)',
+                         names[-1], fieldName, names[-1])
+                    line(out, indent, '{')
+                    line(out, indent + 1, '%s value%s;', typeName,
+                         not fieldType.isPrimitive() and '((jobject) NULL)' or '')
+                    sig, check, x = parseArgs([fieldType], cls)
+                    line(out, indent + 1, 'if (!parseArg(arg, "%s"%s, &value))',
+                         sig, check)
+                    line(out, indent + 1, '{')
+                    line(out, indent + 2, 'INT_CALL(self->object._set_%s(value));',
+                         fieldName)
+                    line(out, indent + 2, 'return 0;')
+                    line(out, indent + 1, '}')
+                    line(out, indent + 1, 'PyErr_SetArgsError((PyObject *) self, "%s", arg);',
+                         fieldName)
+                    line(out, indent + 1, 'return -1;')
+                    line(out, indent, '}')
+
+    if propMethods:
+        for fieldName, methods in propMethods:
+            line(out)
+            getter = None
+            setters = []
+            sort(methods, key=lambda x: x.getName())
+            for method in methods:
+                methodName = method.getName()
+                if not getter and (methodName.startswith('get') or
+                                   methodName.startswith('is')):
+                    getter = method
+                elif methodName.startswith('set'):
+                    setters.append(method)
+
+            if getter:
+                methodName = getter.getName()
+                returnType = getter.getReturnType()
+                typeName = typename(returnType, cls, False)
+                line(out, indent, 'static PyObject *t_%s_get__%s(t_%s *self, void *data)',
+                     names[-1], fieldName, names[-1])
+                line(out, indent, '{')
+                line(out, indent + 1, '%s value%s;', typeName,
+                     not returnType.isPrimitive() and '((jobject) NULL)' or '')
+                line(out, indent + 1, 'OBJ_CALL(value = self->object.%s());',
+                     methodName)
+                line(out, indent + 1, returnValue(cls, returnType, 'value'))
+                line(out, indent, '}')
+
+            if setters:
+                line(out, indent, 'static int t_%s_set__%s(t_%s *self, PyObject *arg, void *data)',
+                     names[-1], fieldName, names[-1])
+                line(out, indent, '{')
+                methodName = setters[0].getName()
+                for method in setters:
+                    argType = method.getParameterTypes()[0]
+                    typeName = typename(argType, cls, False)
+                    line(out, indent + 1, '{')
+                    line(out, indent + 2, '%s value%s;', typeName,
+                         not argType.isPrimitive() and '((jobject) NULL)' or '')
+                    sig, check, x = parseArgs([argType], cls)
+                    line(out, indent + 2, 'if (!parseArg(arg, "%s"%s, &value))',
+                         sig, check)
+                    line(out, indent + 2, '{')
+                    line(out, indent + 3, 'INT_CALL(self->object.%s(value));',
+                         methodName)
+                    line(out, indent + 3, 'return 0;')
+                    line(out, indent + 2, '}')
+                    line(out, indent + 1, '}')
+                line(out, indent + 1, 'PyErr_SetArgsError((PyObject *) self, "%s", arg);',
+                     fieldName)
+                line(out, indent + 1, 'return -1;')
+                line(out, indent, '}')
+
+    if mappingMethod:
+        method, cardinality = mappingMethod
+        if cardinality > 1:
+            methodName = method.getName()
+            getName = 't_%s_%s_map_' %(names[-1], methodName)
+            line(out)
+            line(out, indent, 'static PyObject *%s(t_%s *self, PyObject *arg)',
+                 getName, names[-1])
+            line(out, indent, '{')
+            call(out, indent + 1, cls, False, method, names, 1, isExtension)
+            line(out)
+            line(out, indent + 1, 'PyErr_SetArgsError((PyObject *) self, "%s", arg);',
+                 methodName)
+            line(out, indent + 1, 'return NULL;')
+            line(out, indent, '}')
+
+    if sequenceLenMethod:
+        method, cardinality = sequenceLenMethod
+        methodName = method.getName()
+        lenName = 't_%s_%s_seq_' %(names[-1], methodName)
+        line(out)
+        line(out, indent, 'static int %s(t_%s *self)', lenName, names[-1])
+        line(out, indent, '{')
+        line(out, indent + 1, '%s len;',
+             typename(method.getReturnType(), cls, False))
+        line(out, indent + 1, 'INT_CALL(len = self->object.%s());', methodName)
+        line(out, indent + 1, 'return (int) len;')
+        line(out, indent, '}')
+
+    if sequenceGetMethod:
+        method, cardinality = sequenceGetMethod
+        methodName = method.getName()
+        returnType = method.getReturnType()
+        getName = 't_%s_%s_seq_' %(names[-1], methodName)
+        line(out)
+        line(out, indent, 'static PyObject *%s(t_%s *self, int n)', getName, names[-1])
+        line(out, indent, '{')
+        line(out, indent + 1, '%s result%s;',
+             typename(returnType, cls, False),
+             not returnType.isPrimitive() and '((jobject) NULL)' or '')
+        line(out, indent + 1, 'OBJ_CALL(result = self->object.%s((%s) n));',
+             methodName, typename(method.getParameterTypes()[0], cls, False))
+        line(out, indent + 1, returnValue(cls, returnType, 'result'))
+        line(out, indent, '}')
+
+    while indent:
+        indent -= 1
+        line(out, indent, '}')
+
+
+def package(out, allInOne, cppdir, namespace, names):
+
+    if not allInOne:
+        out = file(os.path.join(os.path.join(cppdir, *names),
+                                '__init__.cpp'), 'w')
+
+    if allInOne and not names or not allInOne:
+        line(out, 0, '#include <jni.h>')
+        line(out, 0, '#include <Python.h>')
+        line(out, 0, '#include "macros.h"')
+
+    if not names:
+        line(out)
+        line(out, 0, 'PyObject *initVM(PyObject *module, PyObject *args, PyObject *kwds);')
+
+    packages = []
+    types = []
+
+    namespaces = namespace.items()
+    sort(namespaces, key=lambda x: x[0])
+    for name, entries in namespaces:
+        if entries is True:
+            if names:
+                line(out, 0, '#include "%s/%s.h"', '/'.join(names), name)
+            else:
+                line(out, 0, '#include "%s.h"', name)
+            types.append(name)
+        else:
+            packages.append((name, entries))
+
+    indent = 0
+    if names:
+        line(out)
+        for name in names:
+            line(out, indent, 'namespace %s {', name)
+            indent += 1
+
+    line(out);
+    for name, entries in packages:
+        line(out, indent, 'namespace %s {', name)
+        line(out, indent + 1, 'void __install__(PyObject *module);')
+        line(out, indent + 1, 'void __initialize__(PyObject *module);')
+        line(out, indent, '}')
+
+    line(out)
+    line(out, indent, 'void __install__(PyObject *module)')
+    line(out, indent, '{')
+    for name in types:
+        line(out, indent + 1, 't_%s::install(module);', name)
+    for name, entries in packages:
+        line(out, indent + 1, '%s::__install__(module);', name)
+    line(out, indent, '}')
+
+    line(out)
+    if not names:
+        line(out, indent, 'PyObject *__initialize__(PyObject *module, PyObject *args, PyObject *kwds)')
+        line(out, indent, '{')
+        line(out, indent + 1, 'PyObject *env = initVM(module, args, kwds);')
+        line(out)
+        line(out, indent + 1, 'if (env == NULL)')
+        line(out, indent + 2, 'return NULL;')
+        line(out)
+    else:
+        line(out, indent, 'void __initialize__(PyObject *module)')
+        line(out, indent, '{')
+    for name in types:
+        line(out, indent + 1, 't_%s::initialize(module);', name)
+    for name, entries in packages:
+        line(out, indent + 1, '%s::__initialize__(module);', name)
+    if not names:
+        line(out)
+        line(out, indent + 1, 'return env;')
+    line(out, indent, '}')
+
+    while indent:
+        indent -= 1
+        line(out, indent, '}')
+
+    if not allInOne:
+        out.close()
+    else:
+        line(out)
+
+    for name, entries in packages:
+        package(out, allInOne, cppdir, entries, names + (name,))
+
+
+def module(out, allInOne, classes, cppdir, moduleName, shared):
+
+    extname = '_%s' %(moduleName)
+    line(out, 0, '#include <Python.h>')
+    line(out, 0, '#include "macros.h"')
+    line(out, 0, '#include "jccfuncs.h"')
+
+    if allInOne:
+        out_init = file(os.path.join(cppdir, '__init__.cpp'), 'w')
+    namespaces = {}
+    for cls in classes:
+        namespace = namespaces
+        classNames = cls.getName().split('.')
+        for className in classNames[:-1]:
+            namespace = namespace.setdefault(className, {})
+        namespace[classNames[-1]] = True
+    if allInOne:
+        package(out_init, True, cppdir, namespaces, ())
+        out_init.close()
+    else:
+        package(None, False, cppdir, namespaces, ())
+
+    line(out)
+    line(out, 0, 'void __install__(PyObject *module);')
+    line(out, 0, 'extern PyTypeObject JObjectType, ConstVariableDescriptorType, FinalizerClassType, FinalizerProxyType;')
+    line(out, 0, 'extern void _install_jarray(PyObject *);')
+    line(out)
+    line(out, 0, '#if defined(_jcc_shared) && (defined(_MSC_VER) || defined(__WIN32))')
+    line(out, 0, '#define _DLL_IMPORT __declspec(dllimport)')
+    line(out, 0, '#else')
+    line(out, 0, '#define _DLL_IMPORT')
+    line(out, 0, '#endif')
+    line(out, 0, '_DLL_IMPORT extern PyTypeObject JCCEnvType;')
+
+    line(out)
+    line(out, 0, 'extern "C" {')
+
+    line(out)
+    line(out, 1, 'void init%s(void)', extname)
+    line(out, 1, '{')
+    line(out, 2, 'PyObject *module = Py_InitModule3("%s", jcc_funcs, "");',
+         extname);
+    line(out)
+    line(out, 2, 'PyEval_InitThreads();')
+    line(out)
+    line(out, 2, 'INSTALL_TYPE(JObject, module);')
+    line(out, 2, 'INSTALL_TYPE(JCCEnv, module);')
+    line(out, 2, 'INSTALL_TYPE(ConstVariableDescriptor, module);')
+    line(out, 2, 'INSTALL_TYPE(FinalizerClass, module);')
+    line(out, 2, 'INSTALL_TYPE(FinalizerProxy, module);')
+    line(out, 2, '_install_jarray(module);')
+    line(out, 2, '__install__(module);')
+    line(out, 1, '}')
+    line(out, 0, '}')
+
+
+def compile(env, jccPath, output, moduleName, install, dist, debug, jars,
+            version, prefix, root, install_dir, use_distutils,
+            shared, compiler, modules):
+
+    try:
+        if use_distutils:
+            raise ImportError
+        from setuptools import setup, Extension
+        with_setuptools = True
+        if shared and not SHARED:
+            raise NotImplementedError, "JCC was not built with --shared mode support, see JCC's INSTALL file for more information"
+    except ImportError:
+        if python_ver < '2.4':
+            raise ImportError, 'setuptools is required when using Python 2.3'
+        if shared:
+            raise ImportError, 'setuptools is required when using --shared'
+        from distutils.core import setup, Extension
+        with_setuptools = False
+
+    extname = '_%s' %(moduleName)
+
+    modulePath = os.path.join(output, moduleName)
+    if not os.path.isdir(modulePath):
+        os.makedirs(modulePath)
+
+    out = file(os.path.join(modulePath, '__init__.py'), 'w')
+    line(out)
+    if shared:
+        line(out, 0, "import os, sys")
+        line(out)
+        line(out, 0, "if sys.platform == 'win32':")
+        line(out, 1, "import jcc, %s", extname)
+        line(out, 0, "else:")
+        line(out, 1, "import %s", extname)
+    else:
+        line(out, 0, 'import os, %s', extname)
+    line(out)
+    line(out, 0, '__dir__ = os.path.abspath(os.path.dirname(__file__))')
+
+    package_data = []
+    for jar in jars:
+        shutil.copy2(jar, modulePath)
+        package_data.append(os.path.basename(jar))
+
+    if modules:
+        for module in modules:
+            pfile = module.split('.')[0] + '.py'
+            shutil.copy2(pfile, modulePath)
+            pfile = os.path.basename(pfile)
+            cfile = pfile + (__debug__ and 'c' or 'o')
+            py_compile.compile(os.path.join(modulePath, pfile),
+                               os.path.join(modulePath, cfile),
+                               doraise=True)
+
+    line(out)
+    line(out, 0, 'class JavaError(Exception):')
+    line(out, 1, 'def getJavaException(self):')
+    line(out, 2, 'return self.args[0]')
+    line(out)
+    line(out, 0, 'class InvalidArgsError(Exception):')
+    line(out, 1, 'pass')
+    line(out)
+    line(out, 0, '%s._setExceptionTypes(JavaError, InvalidArgsError)', extname)
+
+    if version:
+        line(out)
+        line(out, 0, 'VERSION = "%s"', version)
+        
+    line(out, 0, 'CLASSPATH = [%s]' %(', '.join(['os.path.join(__dir__, "%s")' %(os.path.basename(jar)) for jar in jars])))
+    line(out, 0, 'CLASSPATH = os.pathsep.join(CLASSPATH)')
+
+    line(out)
+    line(out, 0, 'from %s import *', extname)
+    out.close()
+
+    includes = [os.path.join(output, extname),
+                os.path.join(jccPath, 'sources')]
+
+    sources = ['JObject.cpp', 'JArray.cpp', 'functions.cpp', 'types.cpp']
+    if not shared:
+        sources.append('jcc.cpp')
+        sources.append('JCCEnv.cpp')
+    for source in sources:
+	shutil.copy2(os.path.join(jccPath, 'sources', source),
+                     os.path.join(output, extname))
+
+    sources = []
+    for path, dirs, names in os.walk(os.path.join(output, extname)):
+        for name in names:
+            if name.endswith('.cpp'):
+                sources.append(os.path.join(path, name))
+
+    script_args = ['build_ext']
+
+    includes[0:0] = INCLUDES
+    compile_args = CFLAGS
+    link_args = LFLAGS
+    defines=['PYTHON']
+
+    if compiler:
+        script_args.append('--compiler=%s' %(compiler))
+
+    if shared:
+        defines.append('_jcc_shared')
+    script_args.append('--define=%s' %(','.join(defines)))
+
+    if debug:
+        script_args.append('--debug')
+        compile_args += DEBUG_CFLAGS
+    elif sys.platform == 'win32':
+        pass
+    elif sys.platform == 'sunos5':
+        link_args.append('-Wl,-s')
+    else:
+        link_args.append('-Wl,-S')
+
+    if install:
+        script_args.append('install')
+    if prefix:
+        script_args.append('--prefix=%s' % prefix)
+    if root:
+        script_args.append('--root=%s' % root)
+    if install_dir:
+        script_args.append('--install-lib=%s' % install_dir)
+
+    if dist:
+        if with_setuptools:
+            script_args.append('bdist_egg')
+        else:
+            script_args.append('bdist')
+
+    args = {
+        'extra_compile_args': compile_args,
+        'extra_link_args': link_args,
+        'include_dirs': includes,
+        'sources': sources
+    }
+    if shared:
+        shlibdir = os.path.dirname(os.path.dirname(_jcc.__file__))
+        if sys.platform == 'darwin':   # distutils no good with -R
+            machine = platform.machine()
+            if machine.startswith('iPod') or machine.startswith('iPhone'):
+                args['extra_link_args'] += ['-L' + shlibdir]
+            else:
+                args['extra_link_args'] += ['-Wl,-rpath', shlibdir]
+            args['library_dirs'] = [shlibdir]
+            args['libraries'] = ['jcc']
+        elif sys.platform == 'linux2': # distutils no good with -R
+            args['extra_link_args'] += ['-Wl,-rpath', shlibdir]
+            args['library_dirs'] = [shlibdir]
+            args['libraries'] = ['jcc']
+        elif sys.platform == 'win32':
+            jcclib = 'jcc%s.lib' %(debug and '_d' or '')
+            args['extra_link_args'] += [os.path.join(shlibdir, 'jcc', jcclib)]
+        else:
+            raise NotImplementedError, "shared mode on %s" %(sys.platform)
+
+    extensions = [Extension('.'.join([moduleName, extname]), **args)]
+
+    args = {
+        'name': moduleName,
+        'packages': [moduleName],
+        'package_dir': {moduleName: modulePath},
+        'package_data': {moduleName: package_data},
+        'version': version,
+        'ext_modules': extensions,
+        'script_args': script_args
+    }
+    if with_setuptools:
+        args['zip_safe'] = False
+
+    setup(**args)

Propchange: lucene/pylucene/trunk/jcc/jcc/python.py
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: lucene/pylucene/trunk/jcc/jcc/python.py
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: lucene/pylucene/trunk/jcc/jcc/sources/JArray.cpp
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/jcc/jcc/sources/JArray.cpp?rev=732916&view=auto
==============================================================================
--- lucene/pylucene/trunk/jcc/jcc/sources/JArray.cpp (added)
+++ lucene/pylucene/trunk/jcc/jcc/sources/JArray.cpp Thu Jan  8 19:28:33 2009
@@ -0,0 +1,1148 @@
+/*
+ *   Copyright (c) 2008-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.
+ */
+
+#ifdef PYTHON
+
+#include <jni.h>
+#include <Python.h>
+#include "structmember.h"
+
+#include "JArray.h"
+#include "functions.h"
+#include "java/lang/Class.h"
+
+using namespace java::lang;
+
+
+template<typename T> class _t_jarray : public t_jarray<T> {
+public:
+    static PyObject *format;
+};
+
+template<typename U>
+static PyObject *get(U *self, int n)
+{
+    return self->array.get(n);
+}
+
+template<typename U>
+static PyObject *toSequence(U *self)
+{
+    return self->array.toSequence();
+}
+
+template<typename U>
+static PyObject *toSequence(U *self, int lo, int hi)
+{
+    return self->array.toSequence(lo, hi);
+}
+
+template<typename U> class _t_iterator {
+public:
+    PyObject_HEAD
+    U *obj;
+    Py_ssize_t position;
+
+    static void dealloc(_t_iterator *self)
+    {
+        Py_XDECREF(self->obj);
+        self->ob_type->tp_free((PyObject *) self);
+    }
+
+    static PyObject *iternext(_t_iterator *self)
+    {
+        if (self->position < (Py_ssize_t) self->obj->array.length)
+            return get<U>(self->obj, self->position++);
+
+        PyErr_SetNone(PyExc_StopIteration);
+        return NULL;
+    }
+
+    static PyTypeObject *JArrayIterator;
+};
+
+template<typename T, typename U>
+static int init(U *self, PyObject *args, PyObject *kwds)
+{
+    PyObject *obj;
+
+    if (!PyArg_ParseTuple(args, "O", &obj))
+        return -1;
+
+    if (PySequence_Check(obj))
+    {
+        self->array = JArray<T>(obj);
+        if (PyErr_Occurred())
+            return -1;
+    }
+    else if (PyGen_Check(obj))
+    {
+        PyObject *tuple =
+            PyObject_CallFunctionObjArgs((PyObject *) &PyTuple_Type, obj, NULL);
+
+        if (!tuple)
+            return -1;
+
+        self->array = JArray<T>(tuple);
+        Py_DECREF(tuple);
+        if (PyErr_Occurred())
+            return -1;
+    }
+    else if (PyInt_Check(obj))
+    {
+        int n = PyInt_AsLong(obj);
+
+        if (n < 0)
+        {
+            PyErr_SetObject(PyExc_ValueError, obj);
+            return -1;
+        }
+
+        self->array = JArray<T>(n);
+    }
+    else
+    {
+        PyErr_SetObject(PyExc_TypeError, obj);
+        return -1;
+    }
+
+    return 0;
+}
+
+template<typename T, typename U>
+static void dealloc(U *self)
+{
+    self->array = JArray<T>((jobject) NULL);
+    self->ob_type->tp_free((PyObject *) self);
+}
+
+template<typename U>
+static PyObject *_format(U *self, PyObject *(*fn)(PyObject *))
+{
+    if (self->array.this$)
+    {
+        PyObject *list = toSequence<U>(self);
+            
+        if (list)
+        {
+            PyObject *result = (*fn)(list);
+
+            Py_DECREF(list);
+            if (result)
+            {
+                PyObject *args = PyTuple_New(1);
+
+                PyTuple_SET_ITEM(args, 0, result);
+                result = PyString_Format(U::format, args);
+                Py_DECREF(args);
+
+                return result;
+            }
+        }
+
+        return NULL;
+    }
+
+    return PyString_FromString("<null>");
+}
+
+template<typename U>
+static PyObject *repr(U *self)
+{
+    return _format(self, PyObject_Repr);
+}
+
+template<typename U>
+static PyObject *str(U *self)
+{
+    return _format(self, PyObject_Str);
+}
+
+template<typename U>
+static int _compare(U *self, PyObject *value, int i0, int i1, int op, int *cmp)
+{
+    PyObject *v0 = get<U>(self, i0);
+    PyObject *v1 = PySequence_Fast_GET_ITEM(value, i1);  /* borrowed */
+
+    if (!v0)
+        return -1;
+
+    if (!v1)
+    {
+        Py_DECREF(v0);
+        return -1;
+    }
+
+    *cmp = PyObject_RichCompareBool(v0, v1, op);
+    Py_DECREF(v0);
+
+    if (*cmp < 0)
+        return -1;
+
+    return 0;
+}
+
+template<typename U>
+static PyObject *richcompare(U *self, PyObject *value, int op)
+{
+    PyObject *result = NULL;
+    int s0, s1;
+
+    if (!PySequence_Check(value))
+    {
+        Py_INCREF(Py_NotImplemented);
+        return Py_NotImplemented;
+    }
+
+    value = PySequence_Fast(value, "not a sequence");
+    if (!value)
+        return NULL;
+
+    s0 = PySequence_Fast_GET_SIZE(value);
+    s1 = self->array.length;
+
+    if (s1 < 0)
+    {
+        Py_DECREF(value);
+        return NULL;
+    }
+
+    if (s0 != s1)
+    {
+        switch (op) {
+          case Py_EQ: result = Py_False; break;
+          case Py_NE: result = Py_True; break;
+        }
+    }
+
+    if (!result)
+    {
+        int i0, i1, cmp = 1;
+
+        for (i0 = 0, i1 = 0; i0 < s0 && i1 < s1 && cmp; i0++, i1++) {
+            if (_compare(self, value, i0, i1, Py_EQ, &cmp) < 0)
+            {
+                Py_DECREF(value);
+                return NULL;
+            }                
+        }
+
+        if (cmp)
+        {
+            switch (op) {
+              case Py_LT: cmp = s0 < s1; break;
+              case Py_LE: cmp = s0 <= s1; break;
+              case Py_EQ: cmp = s0 == s1; break;
+              case Py_NE: cmp = s0 != s1; break;
+              case Py_GT: cmp = s0 > s1; break;
+              case Py_GE: cmp = s0 >= s1; break;
+              default: cmp = 0;
+            }
+
+            result = cmp ? Py_True : Py_False;
+        }
+        else if (op == Py_EQ)
+            result = Py_False;
+        else if (op == Py_NE)
+            result = Py_True;
+        else if (_compare(self, value, i0, i1, op, &cmp) < 0)
+        {
+            Py_DECREF(value);
+            return NULL;
+        }
+        else
+            result = cmp ? Py_True : Py_False;
+    }
+    Py_DECREF(value);
+
+    Py_INCREF(result);
+    return result;
+}
+
+template<typename U>
+static PyObject *iter(U *self)
+{
+    _t_iterator<U> *it =
+        PyObject_New(_t_iterator<U>, _t_iterator<U>::JArrayIterator);
+
+    if (it)
+    {
+        it->position = 0;
+        it->obj = self; Py_INCREF((PyObject *) self);
+    }
+
+    return (PyObject *) it;
+}
+
+template<typename U>
+static Py_ssize_t seq_length(U *self)
+{
+    if (self->array.this$)
+        return self->array.length;
+
+    return -1;
+}
+
+template<typename U>
+static PyObject *seq_get(U *self, Py_ssize_t n)
+{
+    return get<U>(self, n);
+}
+
+template<typename U>
+static int seq_contains(U *self, PyObject *value)
+{
+    return 0;
+}
+
+template<typename U>
+static PyObject *seq_concat(U *self, PyObject *arg)
+{
+    PyObject *list = toSequence<U>(self);
+
+    if (list != NULL &&
+        PyList_Type.tp_as_sequence->sq_inplace_concat(list, arg) < 0)
+    {
+        Py_DECREF(list);
+        return NULL;
+    }
+
+    return list;
+}
+
+template<typename U>
+static PyObject *seq_repeat(U *self, Py_ssize_t n)
+{
+    PyObject *list = toSequence<U>(self);
+
+    if (list != NULL &&
+        PyList_Type.tp_as_sequence->sq_inplace_repeat(list, n) < 0)
+    {
+        Py_DECREF(list);
+        return NULL;
+    }
+
+    return list;
+}
+
+template<typename U>
+static PyObject *seq_getslice(U *self, Py_ssize_t lo, Py_ssize_t hi)
+{
+    return toSequence<U>(self, lo, hi);
+}
+
+template<typename U>
+static int seq_set(U *self, Py_ssize_t n, PyObject *value)
+{
+    return self->array.set(n, value);
+}
+
+template<typename U>
+static int seq_setslice(U *self, Py_ssize_t lo, Py_ssize_t hi, PyObject *values)
+{
+    int length = self->array.length;
+
+    if (values == NULL)
+    {
+        PyErr_SetString(PyExc_ValueError, "array size cannot change");
+        return -1;
+    }
+            
+    if (lo < 0) lo = length + lo;
+    if (lo < 0) lo = 0;
+    else if (lo > length) lo = length;
+    if (hi < 0) hi = length + hi;
+    if (hi < 0) hi = 0;
+    else if (hi > length) hi = length;
+    if (lo > hi) lo = hi;
+
+    PyObject *sequence = PySequence_Fast(values, "not a sequence");
+    if (!sequence)
+        return -1;
+
+    int size = PySequence_Fast_GET_SIZE(sequence);
+    if (size < 0)
+        goto error;
+
+    if (size != hi - lo)
+    {
+        PyErr_SetString(PyExc_ValueError, "array size cannot change");
+        goto error;
+    }
+
+    for (int i = lo; i < hi; i++) {
+        PyObject *value = PySequence_Fast_GET_ITEM(sequence, i - lo);
+
+        if (value == NULL)
+            goto error;
+
+        if (self->array.set(i, value) < 0)
+            goto error;
+    }
+
+    Py_DECREF(sequence);
+    return 0;
+
+  error:
+    Py_DECREF(sequence);
+    return -1;
+}
+
+template<typename T> 
+static jclass initializeClass(void)
+{
+    return env->get_vm_env()->GetObjectClass(JArray<T>(0).this$);
+}
+
+template<typename T> 
+static PyObject *cast_(PyTypeObject *type, PyObject *args, PyObject *kwds)
+{
+    PyObject *arg, *clsObj;
+
+    if (!PyArg_ParseTuple(args, "O", &arg))
+        return NULL;
+
+    if (!PyObject_TypeCheck(arg, &ObjectType))
+    {
+        PyErr_SetObject(PyExc_TypeError, arg);
+        return NULL;
+    }
+
+    Class argCls = ((t_Object *) arg)->object.getClass();
+
+    if (!argCls.isArray())
+    {
+        PyErr_SetObject(PyExc_TypeError, arg);
+        return NULL;
+    }
+
+    clsObj = PyObject_GetAttrString((PyObject *) type, "class_");
+    if (!clsObj)
+        return NULL;
+
+    Class arrayCls = ((t_Class *) clsObj)->object;
+
+    if (!arrayCls.isAssignableFrom(argCls))
+    {
+        PyErr_SetObject(PyExc_TypeError, arg);
+        return NULL;
+    }
+
+    return JArray<T>(((t_JObject *) arg)->object.this$).wrap();
+}
+
+template<typename T> 
+static PyObject *instance_(PyTypeObject *type, PyObject *args, PyObject *kwds)
+{
+    PyObject *arg, *clsObj;
+
+    if (!PyArg_ParseTuple(args, "O", &arg))
+        return NULL;
+
+    if (!PyObject_TypeCheck(arg, &ObjectType))
+        Py_RETURN_FALSE;
+
+    Class argCls = ((t_Object *) arg)->object.getClass();
+
+    if (!argCls.isArray())
+        Py_RETURN_FALSE;
+
+    clsObj = PyObject_GetAttrString((PyObject *) type, "class_");
+    if (!clsObj)
+        return NULL;
+
+    Class arrayCls = ((t_Class *) clsObj)->object;
+
+    if (!arrayCls.isAssignableFrom(argCls))
+        Py_RETURN_FALSE;
+
+    Py_RETURN_TRUE;
+}
+
+template<typename T> 
+static PyObject *assignable_(PyTypeObject *type, PyObject *args, PyObject *kwds)
+{
+    return instance_<T>(type, args, kwds);
+}
+
+template< typename T, typename U = _t_jarray<T> > class jarray_type {
+public:
+    PySequenceMethods seq_methods;
+    PyTypeObject type_object;
+
+    class iterator_type {
+    public:
+        PyTypeObject type_object;
+
+        void install(char *name, PyObject *module)
+        {
+            type_object.tp_name = name;
+
+            if (PyType_Ready(&type_object) == 0)
+            {
+                Py_INCREF((PyObject *) &type_object);
+                PyModule_AddObject(module, name, (PyObject *) &type_object);
+            }
+
+            _t_iterator<U>::JArrayIterator = &type_object;
+        }
+
+        iterator_type()
+        {
+            memset(&type_object, 0, sizeof(type_object));
+
+            type_object.ob_refcnt = 1;
+            type_object.ob_type = NULL;
+            type_object.tp_basicsize = sizeof(_t_iterator<U>);
+            type_object.tp_dealloc = (destructor) _t_iterator<U>::dealloc;
+            type_object.tp_flags = Py_TPFLAGS_DEFAULT;
+            type_object.tp_doc = "JArrayIterator<T> wrapper type";
+            type_object.tp_iter = (getiterfunc) PyObject_SelfIter;
+            type_object.tp_iternext = (iternextfunc) _t_iterator<U>::iternext;
+        }
+    };
+
+    iterator_type iterator_type_object;
+
+    void install(char *name, char *type_name, char *iterator_name,
+                 PyObject *module)
+    {
+        type_object.tp_name = name;
+
+        if (PyType_Ready(&type_object) == 0)
+        {
+            Py_INCREF((PyObject *) &type_object);
+            PyDict_SetItemString(type_object.tp_dict, "class_",
+                                 make_descriptor(initializeClass<T>));
+            
+            PyModule_AddObject(module, name, (PyObject *) &type_object);
+        }
+
+        U::format = PyString_FromFormat("JArray<%s>%%s", type_name);
+        iterator_type_object.install(iterator_name, module);
+    }
+
+    static PyObject *_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
+    {
+        U *self = (U *) type->tp_alloc(type, 0);
+
+        if (self)
+            self->array = JArray<T>((jobject) NULL);
+
+        return (PyObject *) self;
+    }
+
+    jarray_type()
+    {
+        memset(&seq_methods, 0, sizeof(seq_methods));
+        memset(&type_object, 0, sizeof(type_object));
+
+        static PyMethodDef methods[] = {
+            { "cast_",
+              (PyCFunction) (PyObject *(*)(PyTypeObject *,
+                                           PyObject *, PyObject *))
+              cast_<T>,
+              METH_VARARGS | METH_CLASS, NULL },
+            { "instance_",
+              (PyCFunction) (PyObject *(*)(PyTypeObject *,
+                                           PyObject *, PyObject *))
+              instance_<T>,
+              METH_VARARGS | METH_CLASS, NULL },
+            { "assignable_",
+              (PyCFunction) (PyObject *(*)(PyTypeObject *,
+                                           PyObject *, PyObject *))
+              assignable_<T>,
+              METH_VARARGS | METH_CLASS, NULL },
+            { NULL, NULL, 0, NULL }
+        };
+
+        seq_methods.sq_length =
+            (lenfunc) (Py_ssize_t (*)(U *)) seq_length<U>;
+        seq_methods.sq_concat =
+            (binaryfunc) (PyObject *(*)(U *, PyObject *)) seq_concat<U>;
+        seq_methods.sq_repeat =
+            (ssizeargfunc) (PyObject *(*)(U *, Py_ssize_t)) seq_repeat<U>;
+        seq_methods.sq_item =
+            (ssizeargfunc) (PyObject *(*)(U *, Py_ssize_t)) seq_get<U>;
+        seq_methods.sq_slice =
+            (ssizessizeargfunc) (PyObject *(*)(U *, Py_ssize_t, Py_ssize_t))
+            seq_getslice<U>;
+        seq_methods.sq_ass_item =
+            (ssizeobjargproc) (int (*)(U *, Py_ssize_t, PyObject *)) seq_set<U>;
+        seq_methods.sq_ass_slice =
+            (ssizessizeobjargproc) (int (*)(U *, Py_ssize_t, Py_ssize_t,
+                                            PyObject *)) seq_setslice<U>;
+        seq_methods.sq_contains =
+            (objobjproc) (int (*)(U *, PyObject *)) seq_contains<U>;
+        seq_methods.sq_inplace_concat = NULL;
+        seq_methods.sq_inplace_repeat = NULL;
+
+        type_object.ob_refcnt = 1;
+        type_object.tp_basicsize = sizeof(U);
+        type_object.tp_dealloc = (destructor) (void (*)(U *)) dealloc<T,U>;
+        type_object.tp_repr = (reprfunc) (PyObject *(*)(U *)) repr<U>;
+        type_object.tp_as_sequence = &seq_methods;
+        type_object.tp_str = (reprfunc) (PyObject *(*)(U *)) str<U>;
+        type_object.tp_flags = Py_TPFLAGS_DEFAULT;
+        type_object.tp_doc = "JArray<T> wrapper type";
+        type_object.tp_richcompare =
+            (richcmpfunc) (PyObject *(*)(U *, PyObject *, int)) richcompare<U>;
+        type_object.tp_iter = (getiterfunc) (PyObject *(*)(U *)) iter<U>;
+        type_object.tp_methods = methods;
+        type_object.tp_base = &ObjectType;
+        type_object.tp_init =
+            (initproc) (int (*)(U *, PyObject *, PyObject *)) init<T,U>;
+        type_object.tp_new = (newfunc) _new;
+    }
+};
+
+template<typename T> class _t_jobjectarray : public _t_jarray<T> {
+public:
+    PyObject *(*wrapfn)(const T&);
+};
+
+template<> PyObject *get(_t_jobjectarray<jobject> *self, int n)
+{
+    return self->array.get(n, self->wrapfn);
+}
+
+template<> PyObject *toSequence(_t_jobjectarray<jobject> *self)
+{
+    return self->array.toSequence(self->wrapfn);
+}
+
+template<> PyObject *toSequence(_t_jobjectarray<jobject> *self, int lo, int hi)
+{
+    return self->array.toSequence(lo, hi, self->wrapfn);
+}
+
+template<> int init< jobject,_t_jobjectarray<jobject> >(_t_jobjectarray<jobject> *self, PyObject *args, PyObject *kwds)
+{
+    PyObject *obj, *clsObj = NULL;
+    PyObject *(*wrapfn)(const jobject &) = NULL;
+    jclass cls;
+
+    if (!PyArg_ParseTuple(args, "O|O", &obj, &clsObj))
+        return -1;
+
+    if (clsObj == NULL)
+        cls = env->findClass("java/lang/Object");
+    else if (PyObject_TypeCheck(clsObj, &ClassType))
+        cls = (jclass) ((t_Class *) clsObj)->object.this$;
+    else if (PyType_Check(clsObj))
+    {
+        if (PyType_IsSubtype((PyTypeObject *) clsObj, &JObjectType))
+        {
+            PyObject *cobj = PyObject_GetAttrString(clsObj, "wrapfn_");
+
+            if (cobj == NULL)
+                PyErr_Clear();
+            else
+            {
+                wrapfn = (PyObject *(*)(const jobject &))
+                    PyCObject_AsVoidPtr(cobj);
+                Py_DECREF(cobj);
+            }
+
+            clsObj = PyObject_GetAttrString(clsObj, "class_");
+            if (clsObj == NULL)
+                return -1;
+
+            cls = (jclass) ((t_Class *) clsObj)->object.this$;
+            Py_DECREF(clsObj);
+        }
+        else
+        {
+            PyErr_SetObject(PyExc_ValueError, clsObj);
+            return -1;
+        }
+    }
+    else
+    {
+        PyErr_SetObject(PyExc_TypeError, clsObj);
+        return -1;
+    }
+
+    if (PySequence_Check(obj))
+    {
+        self->array = JArray<jobject>(cls, obj);
+        if (PyErr_Occurred())
+            return -1;
+    }
+    else if (PyGen_Check(obj))
+    {
+        PyObject *tuple =
+            PyObject_CallFunctionObjArgs((PyObject *) &PyTuple_Type, obj, NULL);
+
+        if (!tuple)
+            return -1;
+
+        self->array = JArray<jobject>(cls, tuple);
+        Py_DECREF(tuple);
+        if (PyErr_Occurred())
+            return -1;
+    }
+    else if (PyInt_Check(obj))
+    {
+        int n = PyInt_AsLong(obj);
+
+        if (n < 0)
+        {
+            PyErr_SetObject(PyExc_ValueError, obj);
+            return -1;
+        }
+
+        self->array = JArray<jobject>(cls, n);
+    }
+    else
+    {
+        PyErr_SetObject(PyExc_TypeError, obj);
+        return -1;
+    }
+
+    self->wrapfn = wrapfn;
+
+    return 0;
+}
+
+template<> jclass initializeClass<jobject>(void)
+{
+    jclass cls = env->findClass("java/lang/Object");
+    return env->get_vm_env()->GetObjectClass(JArray<jobject>(cls, 0).this$);
+}
+
+template<> PyObject *cast_<jobject>(PyTypeObject *type,
+				    PyObject *args, PyObject *kwds)
+{
+    PyObject *arg, *clsArg = NULL;
+    PyObject *(*wrapfn)(const jobject&) = NULL;
+    jclass elementCls;
+
+    if (!PyArg_ParseTuple(args, "O|O", &arg, &clsArg))
+        return NULL;
+
+    if (!PyObject_TypeCheck(arg, &ObjectType))
+    {
+        PyErr_SetObject(PyExc_TypeError, arg);
+        return NULL;
+    }
+
+    Class argCls = ((t_Object *) arg)->object.getClass();
+
+    if (!argCls.isArray())
+    {
+        PyErr_SetObject(PyExc_TypeError, arg);
+        return NULL;
+    }
+
+    if (clsArg != NULL)
+    {
+        if (!PyType_Check(clsArg))
+        {
+            PyErr_SetObject(PyExc_TypeError, clsArg);
+            return NULL;
+        }
+        else if (!PyType_IsSubtype((PyTypeObject *) clsArg, &JObjectType))
+        {
+            PyErr_SetObject(PyExc_ValueError, clsArg);
+            return NULL;
+        }
+
+        PyObject *cobj = PyObject_GetAttrString(clsArg, "wrapfn_");
+
+        if (cobj == NULL)
+            PyErr_Clear();
+        else
+        {
+            wrapfn = (PyObject *(*)(const jobject &)) PyCObject_AsVoidPtr(cobj);
+            Py_DECREF(cobj);
+        }
+
+        clsArg = PyObject_GetAttrString(clsArg, "class_");
+        if (clsArg == NULL)
+            return NULL;
+
+        elementCls = (jclass) ((t_Class *) clsArg)->object.this$;
+        Py_DECREF(clsArg);
+    }
+    else
+        elementCls = env->findClass("java/lang/Object");
+
+    JNIEnv *vm_env = env->get_vm_env();
+    jobjectArray array = vm_env->NewObjectArray(0, elementCls, NULL);
+    Class arrayCls(vm_env->GetObjectClass((jobject) array));
+
+    if (!arrayCls.isAssignableFrom(argCls))
+    {
+        PyErr_SetObject(PyExc_TypeError, arg);
+        return NULL;
+    }
+
+    return JArray<jobject>(((t_JObject *) arg)->object.this$).wrap(wrapfn);
+}
+
+template<> PyObject *instance_<jobject>(PyTypeObject *type,
+					PyObject *args, PyObject *kwds)
+{
+    PyObject *arg, *clsArg = NULL;
+    jclass elementCls;
+
+    if (!PyArg_ParseTuple(args, "O|O", &arg, &clsArg))
+        return NULL;
+
+    if (!PyObject_TypeCheck(arg, &ObjectType))
+        Py_RETURN_FALSE;
+
+    Class argCls = ((t_Object *) arg)->object.getClass();
+
+    if (!argCls.isArray())
+        Py_RETURN_FALSE;
+
+    if (clsArg != NULL)
+    {
+        if (!PyType_Check(clsArg))
+        {
+            PyErr_SetObject(PyExc_TypeError, clsArg);
+            return NULL;
+        }
+        else if (!PyType_IsSubtype((PyTypeObject *) clsArg, &JObjectType))
+        {
+            PyErr_SetObject(PyExc_ValueError, clsArg);
+            return NULL;
+        }
+
+        clsArg = PyObject_GetAttrString(clsArg, "class_");
+        if (clsArg == NULL)
+            return NULL;
+
+        elementCls = (jclass) ((t_Class *) clsArg)->object.this$;
+        Py_DECREF(clsArg);
+    }
+    else
+        elementCls = env->findClass("java/lang/Object");
+
+    JNIEnv *vm_env = env->get_vm_env();
+    jobjectArray array = vm_env->NewObjectArray(0, elementCls, NULL);
+    Class arrayCls(vm_env->GetObjectClass((jobject) array));
+
+    if (!arrayCls.isAssignableFrom(argCls))
+        Py_RETURN_FALSE;
+
+    Py_RETURN_TRUE;
+}
+
+template<> PyObject *assignable_<jobject>(PyTypeObject *type,
+					  PyObject *args, PyObject *kwds)
+{
+    PyObject *arg, *clsArg = NULL;
+    jclass elementCls;
+
+    if (!PyArg_ParseTuple(args, "O|O", &arg, &clsArg))
+        return NULL;
+
+    if (!PyObject_TypeCheck(arg, &ObjectType))
+        Py_RETURN_FALSE;
+
+    Class argCls = ((t_Object *) arg)->object.getClass();
+
+    if (!argCls.isArray())
+        Py_RETURN_FALSE;
+
+    if (clsArg != NULL)
+    {
+        if (!PyType_Check(clsArg))
+        {
+            PyErr_SetObject(PyExc_TypeError, clsArg);
+            return NULL;
+        }
+        else if (!PyType_IsSubtype((PyTypeObject *) clsArg, &JObjectType))
+        {
+            PyErr_SetObject(PyExc_ValueError, clsArg);
+            return NULL;
+        }
+
+        clsArg = PyObject_GetAttrString(clsArg, "class_");
+        if (clsArg == NULL)
+            return NULL;
+
+        elementCls = (jclass) ((t_Class *) clsArg)->object.this$;
+        Py_DECREF(clsArg);
+    }
+    else
+        elementCls = env->findClass("java/lang/Object");
+
+    JNIEnv *vm_env = env->get_vm_env();
+    jobjectArray array = vm_env->NewObjectArray(0, elementCls, NULL);
+    Class arrayCls(vm_env->GetObjectClass((jobject) array));
+
+    if (!argCls.isAssignableFrom(arrayCls))
+        Py_RETURN_FALSE;
+
+    Py_RETURN_TRUE;
+}
+
+
+template<typename T> PyTypeObject *_t_iterator<T>::JArrayIterator;
+template<typename T> PyObject *_t_jarray<T>::format;
+
+static jarray_type< jobject, _t_jobjectarray<jobject> > jarray_jobject;
+
+static jarray_type<jstring> jarray_jstring;
+static jarray_type<jboolean> jarray_jboolean;
+static jarray_type<jbyte> jarray_jbyte;
+static jarray_type<jchar> jarray_jchar;
+static jarray_type<jdouble> jarray_jdouble;
+static jarray_type<jfloat> jarray_jfloat;
+static jarray_type<jint> jarray_jint;
+static jarray_type<jlong> jarray_jlong;
+static jarray_type<jshort> jarray_jshort;
+
+
+PyObject *JArray<jobject>::wrap(PyObject *(*wrapfn)(const jobject&))
+{
+    _t_jobjectarray<jobject> *obj =
+        PyObject_New(_t_jobjectarray<jobject>, &jarray_jobject.type_object);
+
+    memset(&(obj->array), 0, sizeof(JArray<jobject>));
+    obj->array = *this;
+    obj->wrapfn = wrapfn;
+
+    return (PyObject *) obj;
+}
+
+PyObject *JArray<jstring>::wrap()
+{
+    _t_jarray<jstring> *obj =
+        PyObject_New(_t_jarray<jstring>, &jarray_jstring.type_object);
+
+    memset(&(obj->array), 0, sizeof(JArray<jstring>));
+    obj->array = *this;
+
+    return (PyObject *) obj;
+}
+
+PyObject *JArray<jboolean>::wrap()
+{
+    _t_jarray<jboolean> *obj =
+        PyObject_New(_t_jarray<jboolean>, &jarray_jboolean.type_object);
+
+    memset(&(obj->array), 0, sizeof(JArray<jboolean>));
+    obj->array = *this;
+
+    return (PyObject *) obj;
+}
+
+PyObject *JArray<jbyte>::wrap()
+{
+    _t_jarray<jbyte> *obj =
+        PyObject_New(_t_jarray<jbyte>, &jarray_jbyte.type_object);
+
+    memset(&(obj->array), 0, sizeof(JArray<jbyte>));
+    obj->array = *this;
+
+    return (PyObject *) obj;
+}
+
+PyObject *JArray<jchar>::wrap()
+{
+    _t_jarray<jchar> *obj =
+        PyObject_New(_t_jarray<jchar>, &jarray_jchar.type_object);
+
+    memset(&(obj->array), 0, sizeof(JArray<jchar>));
+    obj->array = *this;
+
+    return (PyObject *) obj;
+}
+
+PyObject *JArray<jdouble>::wrap()
+{
+    _t_jarray<jdouble> *obj =
+        PyObject_New(_t_jarray<jdouble>, &jarray_jdouble.type_object);
+
+    memset(&(obj->array), 0, sizeof(JArray<jdouble>));
+    obj->array = *this;
+
+    return (PyObject *) obj;
+}
+
+PyObject *JArray<jfloat>::wrap()
+{
+    _t_jarray<jfloat> *obj =
+        PyObject_New(_t_jarray<jfloat>, &jarray_jfloat.type_object);
+
+    memset(&(obj->array), 0, sizeof(JArray<jfloat>));
+    obj->array = *this;
+
+    return (PyObject *) obj;
+}
+
+PyObject *JArray<jint>::wrap()
+{
+    _t_jarray<jint> *obj =
+        PyObject_New(_t_jarray<jint>, &jarray_jint.type_object);
+
+    memset(&(obj->array), 0, sizeof(JArray<jint>));
+    obj->array = *this;
+
+    return (PyObject *) obj;
+}
+
+PyObject *JArray<jlong>::wrap()
+{
+    _t_jarray<jlong> *obj =
+        PyObject_New(_t_jarray<jlong>, &jarray_jlong.type_object);
+
+    memset(&(obj->array), 0, sizeof(JArray<jlong>));
+    obj->array = *this;
+
+    return (PyObject *) obj;
+}
+
+PyObject *JArray<jshort>::wrap()
+{
+    _t_jarray<jshort> *obj =
+        PyObject_New(_t_jarray<jshort>, &jarray_jshort.type_object);
+
+    memset(&(obj->array), 0, sizeof(JArray<jshort>));
+    obj->array = *this;
+
+    return (PyObject *) obj;
+}
+
+PyObject *JArray_Type(PyObject *self, PyObject *arg)
+{
+    PyObject *type_name = NULL, *type;
+    char *name = NULL;
+
+    if (PyType_Check(arg))
+    {
+        type_name = PyObject_GetAttrString(arg, "__name__");
+        if (!type_name)
+            return NULL;
+    }
+    else if (PyString_Check(arg))
+    {
+        type_name = arg;
+        Py_INCREF(type_name);
+    }
+    else if (PyFloat_Check(arg))
+    {
+        type_name = NULL;
+        name = "double";
+    }
+    else
+    {
+        PyObject *arg_type = (PyObject *) arg->ob_type;
+
+        type_name = PyObject_GetAttrString(arg_type, "__name__");
+        if (!type_name)
+            return NULL;
+    }
+
+    if (type_name != NULL)
+    {
+        name = PyString_AsString(type_name);
+        Py_DECREF(type_name);
+
+        if (!name)
+            return NULL;
+    }
+
+    if (!strcmp(name, "object"))
+        type = (PyObject *) &jarray_jobject.type_object;
+    else if (!strcmp(name, "string"))
+        type = (PyObject *) &jarray_jstring.type_object;
+    else if (!strcmp(name, "bool"))
+        type = (PyObject *) &jarray_jboolean.type_object;
+    else if (!strcmp(name, "byte"))
+        type = (PyObject *) &jarray_jbyte.type_object;
+    else if (!strcmp(name, "char"))
+        type = (PyObject *) &jarray_jchar.type_object;
+    else if (!strcmp(name, "double"))
+        type = (PyObject *) &jarray_jdouble.type_object;
+    else if (!strcmp(name, "float"))
+        type = (PyObject *) &jarray_jfloat.type_object;
+    else if (!strcmp(name, "int"))
+        type = (PyObject *) &jarray_jint.type_object;
+    else if (!strcmp(name, "long"))
+        type = (PyObject *) &jarray_jlong.type_object;
+    else if (!strcmp(name, "short"))
+        type = (PyObject *) &jarray_jshort.type_object;
+    else
+    {
+        PyErr_SetObject(PyExc_ValueError, arg);
+        return NULL;
+    }
+
+    Py_INCREF(type);
+    return type;
+}
+
+PyTypeObject *JArrayObjectType;
+PyTypeObject *JArrayStringType;
+PyTypeObject *JArrayBoolType;
+PyTypeObject *JArrayByteType;
+PyTypeObject *JArrayCharType;
+PyTypeObject *JArrayDoubleType;
+PyTypeObject *JArrayFloatType;
+PyTypeObject *JArrayIntType;
+PyTypeObject *JArrayLongType;
+PyTypeObject *JArrayShortType;
+
+void _install_jarray(PyObject *module)
+{
+    jarray_jobject.install("JArray_object", "object",
+                            "__JArray_object_iterator", module);
+    JArrayObjectType = &jarray_jobject.type_object;
+
+    jarray_jstring.install("JArray_string", "string",
+                            "__JArray_string_iterator", module);
+    JArrayStringType = &jarray_jstring.type_object;
+
+    jarray_jboolean.install("JArray_bool", "bool",
+                            "__JArray_bool_iterator", module);
+    JArrayBoolType = &jarray_jboolean.type_object;
+
+    jarray_jbyte.install("JArray_byte", "byte",
+                         "__JArray_byte_iterator", module);
+    JArrayByteType = &jarray_jbyte.type_object;
+
+    jarray_jchar.install("JArray_char", "char",
+                         "__JArray_char_iterator", module);
+    JArrayCharType = &jarray_jchar.type_object;
+
+    jarray_jdouble.install("JArray_double", "double",
+                           "__JArray_double_iterator", module);
+    JArrayDoubleType = &jarray_jdouble.type_object;
+
+    jarray_jfloat.install("JArray_float", "float",
+                          "__JArray_float_iterator", module);
+    JArrayFloatType = &jarray_jfloat.type_object;
+
+    jarray_jint.install("JArray_int", "int",
+                        "__JArray_int_iterator", module);
+    JArrayIntType = &jarray_jint.type_object;
+
+    jarray_jlong.install("JArray_long", "long",
+                         "__JArray_long_iterator", module);
+    JArrayLongType = &jarray_jlong.type_object;
+
+    jarray_jshort.install("JArray_short", "short",
+                          "__JArray_short_iterator", module);
+    JArrayShortType = &jarray_jshort.type_object;
+}
+
+#endif /* PYTHON */

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

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



Mime
View raw message