Fix listing modules with `help`
@@ -70,9 +70,14 @@ import time
import tokenize
import urllib.parse
import warnings
+import sharing
from collections import deque
from reprlib import Repr
from traceback import format_exception_only
+from rubicon.objc import ObjCClass
+
+
+NSBundle = ObjCClass("NSBundle")
# --------------------------------------------------------- common routines
@@ -2099,8 +2104,25 @@ Please wait a moment while I gather a list of all available modules...
''')
modules = {}
+ mods = []
+ for item in sys.builtin_module_names:
+ if "__" in item:
+ continue
+ mods.append(item)
+
for item in pkgutil.iter_modules():
+ mods.append(item)
+
+ for item in mods:
+
+ if not isinstance(item, str) and item.module_finder.path == NSBundle.mainBundle.bundlePath:
+ continue
+
+ try:
modname = item.name
+ except AttributeError:
+ modname = item
+
if modname and modname[-9:] == '.__init__':
modname = modname[:-9] + ' (package)'
if modname.find('.') < 0:
@@ -2177,15 +2199,69 @@ class ModuleScanner:
def apropos(key):
"""Print all the one-line module summaries that contain a substring."""
- def callback(path, modname, desc):
- if modname[-9:] == '.__init__':
- modname = modname[:-9] + ' (package)'
- print(modname, desc and '- ' + desc)
- def onerror(modname):
+
+ _stdout = sys.stdout
+ _stderr = sys.stderr
+
+ def ignore_url(url):
pass
+
+ open_url = sharing.open_url
+
with warnings.catch_warnings():
warnings.filterwarnings('ignore') # ignore problems during import
- ModuleScanner().run(callback, key, onerror=onerror)
+
+ mods = []
+
+ for item in sys.builtin_module_names:
+ if "__" in item:
+ continue
+ mods.append(item)
+
+ for item in pkgutil.iter_modules():
+ mods.append(item)
+
+ for item in mods:
+
+ if not isinstance(item, str) and item.module_finder.path == NSBundle.mainBundle.bundlePath:
+ continue
+
+ if not isinstance(item, str):
+ ispkg = item.ispkg
+
+ modname = item.name
+ if modname[-9:] == '.__init__':
+ modname = modname[:-9] + ' (package)'
+
+ sys.stdout = sys.__stdout__
+ sys.stderr = sys.__stderr__
+ sharing.open_url = ignore_url
+ try:
+ if not isinstance(item, str):
+ name = __import__(modname).__doc__ or ''
+ else:
+ name = __import__(item).__doc__ or ''
+ except:
+ sys.stdout = _stdout
+ sys.stderr = _stderr
+ sharing.open_url = open_url
+ continue
+
+ sys.stdout = _stdout
+ sys.stderr = _stderr
+ sharing.open_url = open_url
+
+ if name.startswith("\n"):
+ desc = name.split('\n')[1]
+ else:
+ desc = name.split('\n')[0]
+
+ if (name+"\n"+desc).lower().find(key) >= 0:
+ if isinstance(item, str):
+ print(item, desc and '- ' + desc)
+ else:
+ print(modname, desc and '- ' + desc)
+
# --------------------------------------- enhanced Web browser interface
