lucene-pylucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From va...@apache.org
Subject svn commit: r1363436 - in /lucene/pylucene/trunk/jcc: CHANGES jcc/cpp.py jcc/python.py jcc/sources/jcc.cpp
Date Thu, 19 Jul 2012 17:47:18 GMT
Author: vajda
Date: Thu Jul 19 17:47:18 2012
New Revision: 1363436

URL: http://svn.apache.org/viewvc?rev=1363436&view=rev
Log:
 - added support for using full Java class names in Python via --use_full_names

Modified:
    lucene/pylucene/trunk/jcc/CHANGES
    lucene/pylucene/trunk/jcc/jcc/cpp.py
    lucene/pylucene/trunk/jcc/jcc/python.py
    lucene/pylucene/trunk/jcc/jcc/sources/jcc.cpp

Modified: lucene/pylucene/trunk/jcc/CHANGES
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/jcc/CHANGES?rev=1363436&r1=1363435&r2=1363436&view=diff
==============================================================================
--- lucene/pylucene/trunk/jcc/CHANGES (original)
+++ lucene/pylucene/trunk/jcc/CHANGES Thu Jul 19 17:47:18 2012
@@ -2,6 +2,7 @@ Version 2.13 ->
 --------------------
  - fixed class initialization race bug PYLUCENE-17 (with Patrick J. McNerthney)
  - added support for initVM's vmargs to be a [list, of, args], PYLUCENE-19
+ - added support for using full Java class names in Python via --use_full_names
  - 
 
 Version 2.12 -> 2.13

Modified: lucene/pylucene/trunk/jcc/jcc/cpp.py
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/jcc/jcc/cpp.py?rev=1363436&r1=1363435&r2=1363436&view=diff
==============================================================================
--- lucene/pylucene/trunk/jcc/jcc/cpp.py (original)
+++ lucene/pylucene/trunk/jcc/jcc/cpp.py Thu Jul 19 17:47:18 2012
@@ -360,6 +360,7 @@ def jcc(args):
     mappings = {}
     sequences = {}
     renames = {}
+    use_full_names = False
     env = None
     wrapperFiles = 1
     prefix = None
@@ -450,6 +451,8 @@ def jcc(args):
                 i += 1
                 renames.update(dict([arg.split('=')
                                      for arg in args[i].split(',')]))
+            elif arg == '--use_full_names':
+                use_full_names = True
             elif arg == '--files':
                 i += 1
                 wrapperFiles = args[i]
@@ -603,10 +606,11 @@ def jcc(args):
             if moduleName:
                 for cls in importset:
                     name = cls.getName().split_pkg('.')[-1]
-                    if name in pythonNames:
-                        raise ValueError, (cls, 'python class name already in use, use --rename',
name, pythonNames[name])
-                    else:
-                        pythonNames[name] = cls
+                    if not use_full_names:
+                        if name in pythonNames:
+                            raise ValueError, (cls, 'python class name already in use, use
--rename', name, pythonNames[name])
+                        else:
+                            pythonNames[name] = cls
 
         todo = typeset - done
         if allInOne and wrapperFiles > 1:
@@ -640,10 +644,11 @@ def jcc(args):
                                          declares, typeset)
                 if moduleName:
                     name = renames.get(className) or names[-1]
-                    if name in pythonNames:
-                        raise ValueError, (cls, 'python class name already in use, use --rename',
name, pythonNames[name])
-                    else:
-                        pythonNames[name] = cls
+                    if not use_full_names:
+                        if name in pythonNames:
+                            raise ValueError, (cls, 'python class name already in use, use
--rename', name, pythonNames[name])
+                        else:
+                            pythonNames[name] = cls
                     python(env, out_h, out_cpp,
                            cls, superCls, names, superNames,
                            constructors, methods, protectedMethods,
@@ -676,7 +681,7 @@ def jcc(args):
         if moduleName:
             out = file(os.path.join(cppdir, moduleName) + '.cpp', 'w')
             module(out, allInOne, done, imports, cppdir, moduleName,
-                   shared, generics)
+                   shared, generics, use_full_names)
             out.close()
             if build or install or dist or egg_info:
                 compile(env, os.path.dirname(args[0]), output, moduleName,

Modified: lucene/pylucene/trunk/jcc/jcc/python.py
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/jcc/jcc/python.py?rev=1363436&r1=1363435&r2=1363436&view=diff
==============================================================================
--- lucene/pylucene/trunk/jcc/jcc/python.py (original)
+++ lucene/pylucene/trunk/jcc/jcc/python.py Thu Jul 19 17:47:18 2012
@@ -1411,7 +1411,7 @@ def python(env, out_h, out, cls, superCl
         line(out, indent, '}')
 
 
-def package(out, allInOne, cppdir, namespace, names):
+def package(out, allInOne, cppdir, namespace, names, use_full_names):
 
     if not allInOne:
         out = file(os.path.join(os.path.join(cppdir, *names),
@@ -1426,6 +1426,8 @@ def package(out, allInOne, cppdir, names
     if not names:
         line(out)
         line(out, 0, 'PyObject *initVM(PyObject *module, PyObject *args, PyObject *kwds);')
+        if use_full_names:
+            line(out, 0, 'PyObject *getJavaModule(PyObject *module, const char *parent, const
char *name);')
 
     packages = []
     types = []
@@ -1459,6 +1461,13 @@ def package(out, allInOne, cppdir, names
     line(out)
     line(out, indent, 'void __install__(PyObject *module)')
     line(out, indent, '{')
+
+    if use_full_names and names:
+        parent_name = '.'.join(names[:-1])
+        line(out, indent + 1, 'module = getJavaModule(module, "%s", "%s");',
+             parent_name, names[-1])
+        line(out)
+
     for name in types:
         line(out, indent + 1, 't_%s::install(module);', name)
     for name, entries in packages:
@@ -1479,6 +1488,12 @@ def package(out, allInOne, cppdir, names
     else:
         line(out, indent, 'void __initialize__(PyObject *module)')
         line(out, indent, '{')
+
+    if use_full_names and names:
+        line(out, indent + 1, 'module = getJavaModule(module, "%s", "%s");',
+             parent_name, names[-1])
+        line(out)
+
     for name in types:
         line(out, indent + 1, 't_%s::initialize(module);', name)
     for name, entries in packages:
@@ -1507,11 +1522,11 @@ def package(out, allInOne, cppdir, names
         line(out)
 
     for name, entries in packages:
-        package(out, allInOne, cppdir, entries, names + (name,))
+        package(out, allInOne, cppdir, entries, names + (name,), use_full_names)
 
 
 def module(out, allInOne, classes, imports, cppdir, moduleName,
-           shared, generics):
+           shared, generics, use_full_names):
 
     extname = '_%s' %(moduleName)
     line(out, 0, '#include <Python.h>')
@@ -1532,10 +1547,10 @@ def module(out, allInOne, classes, impor
                 namespace = namespace.setdefault(className, {})
             namespace[classNames[-1]] = True
     if allInOne:
-        package(out_init, True, cppdir, namespaces, ())
+        package(out_init, True, cppdir, namespaces, (), use_full_names)
         out_init.close()
     else:
-        package(None, False, cppdir, namespaces, ())
+        package(None, False, cppdir, namespaces, (), use_full_names)
 
     line(out)
     line(out, 0, 'PyObject *initJCC(PyObject *module);')

Modified: lucene/pylucene/trunk/jcc/jcc/sources/jcc.cpp
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/jcc/jcc/sources/jcc.cpp?rev=1363436&r1=1363435&r2=1363436&view=diff
==============================================================================
--- lucene/pylucene/trunk/jcc/jcc/sources/jcc.cpp (original)
+++ lucene/pylucene/trunk/jcc/jcc/sources/jcc.cpp Thu Jul 19 17:47:18 2012
@@ -537,6 +537,57 @@ _DLL_EXPORT PyObject *initVM(PyObject *s
     }
 }
 
+/* returns borrowed reference */
+_DLL_EXPORT PyObject *getJavaModule(PyObject *module,
+                                    const char *parent, const char *name) {
+    PyObject *modules = PyImport_GetModuleDict();
+    PyObject *parent_module, *full_name;
+
+    if (parent[0] == '\0')
+    {
+        parent_module = NULL;
+        full_name = PyString_FromString(name);
+    }
+    else if ((parent_module = PyDict_GetItemString(modules, parent)) == NULL)
+    {
+        PyErr_Format(PyExc_ValueError, "Parent module '%s' not found", parent);
+        return NULL;
+    }
+    else
+        full_name = PyString_FromFormat("%s.%s", parent, name);
+
+    PyObject *child_module = PyDict_GetItem(modules, full_name);
+
+    if (child_module == NULL)
+    {
+        child_module = PyModule_New(PyString_AS_STRING(full_name));
+        if (child_module != NULL)
+        {
+            if (parent_module != NULL)
+                PyDict_SetItemString(PyModule_GetDict(parent_module),
+                                     name, child_module);
+            PyDict_SetItem(modules, full_name, child_module);
+            Py_DECREF(child_module);  /* borrow reference */
+        }
+    }
+    Py_DECREF(full_name);
+
+    /* During __install__ pass, __file__ is not yet set on module.
+     * During __initialize__ pass, __file__ is passed down to child_module.
+     */
+    if (child_module != NULL)
+    {
+        PyObject *__file__ = PyString_FromString("__file__");
+        PyObject *file = PyDict_GetItem(PyModule_GetDict(module), __file__);
+
+        if (file != NULL)
+            PyDict_SetItem(PyModule_GetDict(child_module), __file__, file);
+        Py_DECREF(__file__);
+    }
+
+    return child_module;
+}
+
 #ifdef _jcc_lib
 
 static void raise_error(JNIEnv *vm_env, const char *message)



Mime
View raw message