Well, I think I sorted it out (kind of).
The problem is caused by the twisted.internet.reactor module, while chooses
the most appropriate reactor for the system - see
http://twistedmatrix.com/trac/browser/tags/releases/twisted-12.0.0/twisted/internet/reactor.py
I guess maybe it's when the twisted code does stuff like: del sys.modules[
'twisted.internet.reactor'] that results in the globals object being having
a lot of Nones in it, which breaks web2py's assumptions.
Anyway I hacked around it for now by modifying lines 269-271 in
custom_import.py to:
if not name.startswith(".") and level <= 0 \
and not name.startswith("applications.") \
and isinstance(globals, dict) and
globals.get("__file__"):
The twisted import then works and everything is good..
On Wednesday, May 23, 2012 1:07:16 PM UTC+10, Hugh Barker wrote:
>
> Hi,
> I need to run some code that uses the Twisted library from within web2py
> (listening to multicast events on the network, parsing the data and then
> inserting it into the web2pt DB for integration into a webapp).
>
> I get the following error upon importing Twisted. In the interests
> of reproducibility, I added
>
> from twisted.internet import reactor
>
> to the first line of the default.py controller in the welcome app. This is
> enough to trigger the bug:
>
> Traceback (most recent call last):
> File "/var/www/web2py/gluon/restricted.py", line 204, in restricted
> exec ccode in environment
> File "/var/www/web2py/applications/welcome/controllers/default.py"
> <https://ss-geospatial-hf/admin/default/edit/welcome/controllers/default.py>,
> line 1, in <module>
> from twisted.internet import reactor
> File "/var/www/web2py/gluon/custom_import.py", line 293, in __call__
> fromlist, level)
> File "/var/www/web2py/gluon/custom_import.py", line 78, in __call__
> level)
> File "/usr/lib/python2.7/dist-packages/twisted/internet/reactor.py", line
> 37, in <module>
> from twisted.internet import default
> File "/var/www/web2py/gluon/custom_import.py", line 271, in __call__
> globals.get("__file__", ""))
> File "/usr/lib/python2.7/posixpath.py", line 66, in join
> if b.startswith('/'):
> AttributeError: 'NoneType' object has no attribute 'startswith'
>
> Ubuntu 12.04, Python 2.7.3, Twisted 12.0, web2py 1.99.4
>
>
> There's nothing wrong with my twisted install - running the code I want to
> run within the web2py context as a standalone script works fine.
>
>
> Any help much appreciated.
>
>
> Cheers,
>
> Hugh.
>
>