classification
Title: rlcompleter doesn't work if __main__ can't be imported
Type: behavior Stage: resolved
Components: Library (Lib) Versions:
process
Status: closed Resolution: third party
Dependencies: Superseder:
Assigned To: Nosy List: Aaron.Meurer, asmeurer, iritkatriel
Priority: normal Keywords:

Created on 2013-01-14 03:59 by Aaron.Meurer, last changed 2021-06-19 16:53 by iritkatriel. This issue is now closed.

Messages (5)
msg179917 - (view) Author: Aaron Meurer (Aaron.Meurer) Date: 2013-01-14 03:59
The rlcompleter module does not work if __main__ cannot be imported, even though it can be used without it.  For example, on the App Engine, __main__ is not usable.  

If one creates the example app described at https://developers.google.com/appengine/docs/python/gettingstartedpython27/helloworld, and adds "import rlcompleter" to the top of helloworld.py, you get the following error in the AppEngine

Traceback (most recent call last):
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 196, in Handle
    handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 255, in _LoadHandler
    handler = __import__(path[0])
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 692, in Decorate
    return func(self, *args, **kwargs)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 1766, in load_module
    return self.FindAndLoadModule(submodule, fullname, search_path)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 692, in Decorate
    return func(self, *args, **kwargs)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 1630, in FindAndLoadModule
    description)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 692, in Decorate
    return func(self, *args, **kwargs)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 1577, in LoadModuleRestricted
    description)
  File "/Users/aaronmeurer/Desktop/helloworld/helloworld.py", line 2, in <module>
    import rlcompleter
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 692, in Decorate
    return func(self, *args, **kwargs)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 1766, in load_module
    return self.FindAndLoadModule(submodule, fullname, search_path)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 692, in Decorate
    return func(self, *args, **kwargs)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 1630, in FindAndLoadModule
    description)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 692, in Decorate
    return func(self, *args, **kwargs)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 1577, in LoadModuleRestricted
    description)
  File "/sw/lib/python2.7/rlcompleter.py", line 43, in <module>
    import __main__
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 692, in Decorate
    return func(self, *args, **kwargs)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 1766, in load_module
    return self.FindAndLoadModule(submodule, fullname, search_path)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 692, in Decorate
    return func(self, *args, **kwargs)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 1630, in FindAndLoadModule
    description)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 692, in Decorate
    return func(self, *args, **kwargs)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 1577, in LoadModuleRestricted
    description)
ImportError: Cannot re-init internal module __main__

Now, I don't really understand what the issue is here.  I don't really get how __main__ is supposed to work, whether this is something that might be expected to happen in an environment that isn't highly modded like the App Engine's.  Probably this should at least be considered an App Engine bug. 

My point is that it's possible to use the rlcompleter module completely without __main__, if one passes the optional argument to Completer.  

My suggestion is to catch ImportError when importing __main__, and if it isn't importable, make the argument to Completer required. It should be a minimal code change.
msg179918 - (view) Author: Aaron Meurer (Aaron.Meurer) Date: 2013-01-14 04:07
For completion, here's the corresponding App Engine issue I opened: http://code.google.com/p/googleappengine/issues/detail?id=8665.
msg396108 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-06-18 23:21
Your traceback indicates you had this problem on Python 2.7. Are you still seeing it on a current version?
msg396112 - (view) Author: Aaron Meurer (asmeurer) Date: 2021-06-19 00:41
A quick glance at the source shows that it still imports __main__ at the top-level. I have no idea how legitimate it is that the App Engine (used to?) makes it so that __main__ can't be imported.
msg396140 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-06-19 16:53
Since this is not currently a problem for you, and nobody else reported it since 2013, I’ll close the issue and if anyone runs into it again we can investigate on current python/app engine versions.
History
Date User Action Args
2021-06-19 16:53:30iritkatrielsetstatus: open -> closed
resolution: third party
messages: + msg396140

stage: resolved
2021-06-19 00:41:55asmeurersetnosy: + asmeurer
messages: + msg396112
2021-06-18 23:21:23iritkatrielsetnosy: + iritkatriel
messages: + msg396108
2013-01-14 04:07:14Aaron.Meurersetmessages: + msg179918
2013-01-14 03:59:03Aaron.Meurercreate