Apologies for reanimating this old thread but I too just tried Jython
with the current web2py version too :-)

I made a few hacks and I can get much further, the admin interface
starts BUT the ticketing system doesn't work under Jython. This means
exceptions are not handled so I ended up putting some horrible debug
code in to help with that. The example app doesn't work either, it
seems to fall over somewhere in the custom import code (complains that
"globals" a parameter to _Web2pyImporter.__call__ isn't present.
Compiling works ... but when the compiled application is opened/viewed
it fails with "invalid function (default/index)" to the web browser, I
suspect this is due to Jython compiled files ending in *$.py.class

Here is the Windows batch file I'm using (NOTE this us using 2.5.1
rather than 2.5.2 as the CTRL-C behavior is better under the older
release).

##############
C:\svn\web2py\hg_20110714\web2py> type jython_sqlite.bat
setlocal

set CLASSPATH=C:\svn\web2py\hg_20110714\web2py\sqlitejdbc-v056.jar

call c:\jython2.5.1\jython.bat web2py.py

endlocal
##############

Here are the horrible hacks I implemented, for some the version of the
sqlite driver I'm using doesn't like the create function call being
made (see batch file for version).

diff -r 1656ee788d03 gluon/compileapp.py
--- a/gluon/compileapp.py       Mon Jul 04 09:10:10 2011 -0500
+++ b/gluon/compileapp.py       Tue Jul 19 16:06:21 2011 -0700
@@ -12,6 +12,7 @@
 FOR INTERNAL USE ONLY
 """

+import sys
 import re
 import fnmatch
 import os
@@ -234,7 +235,28 @@
     current.T = environment['T'] = translator(request)
     current.cache = environment['cache'] = Cache(request)

-    __builtins__['__import__'] = __builtin__.__import__
+    JYTHON_RUNTIME_DETECTED = 'java' in sys.platform.lower() or
hasattr(sys, 'JYTHON_JAR') or str(copyright).find('Jython') > 0
+    if JYTHON_RUNTIME_DETECTED:
+        # for now if jython avoid import hacks....
+        import __builtin__
+
+        class mybuiltin(object):
+            """NOTE could simple use a dict and populate it, NOTE not
sure if this changes things though if monkey patching import.....
+            """
+            #__builtins__
+            def __getitem__(self, key):
+                try:
+                    return getattr(__builtin__, key)
+                except AttributeError:
+                    raise KeyError, key
+
+            def __setitem__(self, key, value):
+                setattr(self, key, value)
+
+        __builtins__ = mybuiltin()
+    else:
+        __builtins__['__import__'] = __builtin__.__import__
+
     environment['__builtins__'] = __builtins__
     environment['HTTP'] = HTTP
     environment['redirect'] = redirect
diff -r 1656ee788d03 gluon/dal.py
--- a/gluon/dal.py      Mon Jul 04 09:10:10 2011 -0500
+++ b/gluon/dal.py      Tue Jul 19 16:06:21 2011 -0700
@@ -282,6 +282,7 @@
     try:
         from com.ziclix.python.sql import zxJDBC
         import java.sql
+        # Try sqlite jdbc driver from http://www.zentus.com/sqlitejdbc/
         from org.sqlite import JDBC # required by java.sql; ensure we
have it
         drivers.append('zxJDBC')
         logger.warning('zxJDBC support is experimental')
@@ -1548,7 +1549,8 @@
             return
zxJDBC.connect(java.sql.DriverManager.getConnection('jdbc:sqlite:'+dbpath),**driver_args)
         self.pool_connection(connect)
         self.cursor = self.connection.cursor()
-        self.connection.create_function('web2py_extract', 2,
SQLiteAdapter.web2py_extract)
+        # FIXME See http://www.zentus.com/sqlitejdbc/custom_functions.html
for UDFs
+        #self.connection.create_function('web2py_extract', 2,
SQLiteAdapter.web2py_extract)

     def execute(self,a):
         return self.log_execute(a[:-1])
@@ -3939,6 +3941,7 @@
                     except SyntaxError:
                         raise
                     except Exception, error:
+                        print 'DEBUG_c: Exception %r' % ((Exception,
error,),)
                         pass
                 if connected:
                     break
diff -r 1656ee788d03 gluon/restricted.py
--- a/gluon/restricted.py       Mon Jul 04 09:10:10 2011 -0500
+++ b/gluon/restricted.py       Tue Jul 19 16:06:21 2011 -0700
@@ -192,7 +192,12 @@
         exec ccode in environment
     except HTTP:
         raise
-    except Exception:
+    except Exception, error:
+        print 'DEBUG_c: Exception %r' % ((Exception, error,),)
+        print 'DEBUG_c: =============== '
+        import traceback
+        traceback.print_exc()
+        ############################
         # XXX Show exception in Wing IDE if running in debugger
         if __debug__ and 'WINGDB_ACTIVE' in os.environ:
             etype, evalue, tb = sys.exc_info()


Finally here is a trace back (sadly some of the output is merged) when
opening the example application:



################################
C:\svn\web2py\hg_20110714\web2py>call c:\jython2.5.1\jython.bat
web2py.py
No handlers could be found for logger "web2py.dal"
New installation: unable to create welcome.w2p fileweb2py Web
Framework
Created by Massimo Di Pierro, Copyright 2007-2011
Version 1.97.1 (2011-07-04 00:39:51)
Database drivers available: pymysql, zxJDBC
Starting hardcron...
WARNING:web2py.cron:WEB2PY CRON: Disabled because no file locking
WARNING:web2py:GUI not available because Tk library is not installed
choose a password:WARNING:web2py.cron:WEB2PY CRON: Disabled because no
file lock
ing
password_super_secure:-)
please visit:
        http://127.0.0.1:8000
use "kill -SIGTERM 4144" to shutdown the web2py server
WARNING:web2py.cron:WEB2PY CRON: Disabled because no file locking
DEBUG_c: Exception (<type 'exceptions.Exception'>,
UnboundLocalError("local vari
able 'globals' referenced before assignment",))
DEBUG_c: ===============
Traceback (most recent call last):
  File "C:\svn\web2py\hg_20110714\web2py\gluon\restricted.py", line
192, in rest
ricted
    exec ccode in environment
  File "C:\svn\web2py\hg_20110714\web2py\applications\examples\views
\default/ind
ex.html", line 85, in <module>
  File "C:\svn\web2py\hg_20110714\web2py\applications\examples\models
\feeds_read
er.py", line 32, in code_feed_reader
    g = feedparser.parse(url)
  File "C:\svn\web2py\hg_20110714\web2py\gluon\contrib\feedparser.py",
line 3773
, in parse
    feedparser.feed(data)
  File "C:\svn\web2py\hg_20110714\web2py\gluon\contrib\feedparser.py",
line 2037
, in feed
    sgmllib.SGMLParser.feed(self, data)
  File "C:\jython2.5.1\Lib\sgmllib.py", line 99, in feed
    self.goahead(0)
  File "C:\jython2.5.1\Lib\sgmllib.py", line 138, in goahead
    k = self.parse_endtag(i)
  File "C:\jython2.5.1\Lib\sgmllib.py", line 315, in parse_endtag
    self.finish_endtag(tag)
  File "C:\jython2.5.1\Lib\sgmllib.py", line 355, in finish_endtag
    self.unknown_endtag(tag)
  File "C:\svn\web2py\hg_20110714\web2py\gluon\contrib\feedparser.py",
line 863,
 in unknown_endtag
    method()
  File "C:\svn\web2py\hg_20110714\web2py\gluon\contrib\feedparser.py",
line 863,
 in unknown_endtag
    method()
  File "C:\svn\web2py\hg_20110714\web2py\gluon\contrib\feedparser.py",
line 1714
, in _end_title
    value = self.popContent('title')
  File "C:\svn\web2py\hg_20110714\web2py\gluon\contrib\feedparser.py",
line 1159
, in popContent
    value = self.pop(tag)
  File "C:\svn\web2py\hg_20110714\web2py\gluon\contrib\feedparser.py",
line 1137
, in pop
    contentparams = copy.deepcopy(self.contentparams)
  File "C:\jython2.5.1\Lib\copy.py", line 181, in deepcopy
    rv = reductor(2)
  File "C:\svn\web2py\hg_20110714\web2py\gluon\custom_import.py", line
253, in _
_call__
    if not name.startswith(".") and level <= 0 \
UnboundLocalError: local variable 'globals' referenced before
assignment
DEBUG_c: Exception (<type 'exceptions.Exception'>, RestrictedError())
DEBUG_c: ===============
Traceback (most recent call last):
  File "C:\svn\web2py\hg_20110714\web2py\gluon\restricted.py", line
192, in rest
ricted
    exec ccode in environment
  File "C:\svn\web2py\hg_20110714\web2py\applications\examples
\controllers/defau
lt.py", line 71, in <module>
  File "C:\svn\web2py\hg_20110714\web2py\gluon\globals.py", line 137,
in <lambda
>
    self._caller = lambda f: f()
  File "C:\svn\web2py\hg_20110714\web2py\gluon\cache.py", line 424, in
action
    return cache_model(key, func, time_expire)
  File "C:\svn\web2py\hg_20110714\web2py\gluon\cache.py", line 184, in
__call__
    value = f()
  File "C:\svn\web2py\hg_20110714\web2py\applications\examples
\controllers/defau
lt.py", line 13, in index
    return response.render()
  File "C:\svn\web2py\hg_20110714\web2py\gluon\globals.py", line 173,
in render
    run_view_in(self._view_environment)
  File "C:\svn\web2py\hg_20110714\web2py\gluon\compileapp.py", line
514, in run_
view_in
    restricted(ccode, environment, layer)
  File "C:\svn\web2py\hg_20110714\web2py\gluon\restricted.py", line
205, in rest
ricted
    raise RestrictedError(layer, code, '', environment)
RestrictedError
ERROR:web2py:Traceback (most recent call last):
  File "C:\svn\web2py\hg_20110714\web2py\gluon\restricted.py", line
192, in rest
ricted
    exec ccode in environment
  File "C:\svn\web2py\hg_20110714\web2py\applications\examples
\controllers/defau
lt.py", line 71, in <module>
  File "C:\svn\web2py\hg_20110714\web2py\gluon\globals.py", line 137,
in <lambda
>
    self._caller = lambda f: f()
  File "C:\svn\web2py\hg_20110714\web2py\gluon\cache.py", line 424, in
action
    return cache_model(key, func, time_expire)
  File "C:\svn\web2py\hg_20110714\web2py\gluon\cache.py", line 184, in
__call__
    value = f()
  File "C:\svn\web2py\hg_20110714\web2py\applications\examples
\controllers/defau
lt.py", line 13, in index
    return response.render()
  File "C:\svn\web2py\hg_20110714\web2py\gluon\globals.py", line 173,
in render
    run_view_in(self._view_environment)
  File "C:\svn\web2py\hg_20110714\web2py\gluon\compileapp.py", line
514, in run_
view_in
    restricted(ccode, environment, layer)
  File "C:\svn\web2py\hg_20110714\web2py\gluon\restricted.py", line
205, in rest
ricted
    raise RestrictedError(layer, code, '', environment)
RestrictedError

WARNING:web2py.cron:WEB2PY CRON: Disabled because no file locking

################################

Is there an easy way to disable the auto importer/compiler? This may
be the easiest way to make progress with Jython support. The importer
code is complex enough that I'm not sure I can contribute more help :-
(


Chris

Reply via email to