emacs3.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. # Copyright (C) 2004-2012 Free Software Foundation, Inc.
  2. # Author: Dave Love <fx@gnu.org>
  3. # This file is part of GNU Emacs.
  4. # GNU Emacs is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. # GNU Emacs is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. # You should have received a copy of the GNU General Public License
  13. # along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  14. import os, sys, traceback, inspect, imp, __main__
  15. try:
  16. set
  17. except:
  18. from sets import Set as set
  19. __all__ = ["eexecfile", "eargs", "complete", "ehelp", "eimport", "modpath"]
  20. def format_exception (filename, should_remove_self):
  21. type, value, tb = sys.exc_info ()
  22. sys.last_type = type
  23. sys.last_value = value
  24. sys.last_traceback = tb
  25. if type is SyntaxError:
  26. try: # parse the error message
  27. msg, (dummy_filename, lineno, offset, line) = value
  28. except:
  29. pass # Not the format we expect; leave it alone
  30. else:
  31. # Stuff in the right filename
  32. value = SyntaxError(msg, (filename, lineno, offset, line))
  33. sys.last_value = value
  34. res = traceback.format_exception_only (type, value)
  35. # There are some compilation errors which do not provide traceback so we
  36. # should not massage it.
  37. if should_remove_self:
  38. tblist = traceback.extract_tb (tb)
  39. del tblist[:1]
  40. res = traceback.format_list (tblist)
  41. if res:
  42. res.insert(0, "Traceback (most recent call last):\n")
  43. res[len(res):] = traceback.format_exception_only (type, value)
  44. # traceback.print_exception(type, value, tb)
  45. for line in res: print(line, end=' ')
  46. def eexecfile (file):
  47. """Execute FILE and then remove it.
  48. Execute the file within the __main__ namespace.
  49. If we get an exception, print a traceback with the top frame
  50. (ourselves) excluded."""
  51. # We cannot use real execfile since it has a bug where the file stays
  52. # locked forever (under w32) if SyntaxError occurs.
  53. # --- code based on code.py and PyShell.py.
  54. try:
  55. try:
  56. source = open (file, "r").read()
  57. code = compile (source, file, "exec")
  58. # Other exceptions (shouldn't be any...) will (correctly) fall
  59. # through to "final".
  60. except (OverflowError, SyntaxError, ValueError):
  61. # FIXME: When can compile() raise anything else than
  62. # SyntaxError ????
  63. format_exception (file, False)
  64. return
  65. try:
  66. exec(code, __main__.__dict__)
  67. except:
  68. format_exception (file, True)
  69. finally:
  70. os.remove (file)
  71. def eargs (name, imports):
  72. "Get arglist of NAME for Eldoc &c."
  73. try:
  74. if imports: exec(imports)
  75. parts = name.split ('.')
  76. if len (parts) > 1:
  77. exec('import ' + parts[0]) # might fail
  78. func = eval (name)
  79. if inspect.isbuiltin (func) or type(func) is type:
  80. doc = func.__doc__
  81. if doc.find (' ->') != -1:
  82. print('_emacs_out', doc.split (' ->')[0])
  83. else:
  84. print('_emacs_out', doc.split ('\n')[0])
  85. return
  86. if inspect.ismethod (func):
  87. func = func.im_func
  88. if not inspect.isfunction (func):
  89. print('_emacs_out ')
  90. return
  91. (args, varargs, varkw, defaults) = inspect.getargspec (func)
  92. # No space between name and arglist for consistency with builtins.
  93. print('_emacs_out', \
  94. func.__name__ + inspect.formatargspec (args, varargs, varkw,
  95. defaults))
  96. except:
  97. print("_emacs_out ")
  98. def all_names (object):
  99. """Return (an approximation to) a list of all possible attribute
  100. names reachable via the attributes of OBJECT, i.e. roughly the
  101. leaves of the dictionary tree under it."""
  102. def do_object (object, names):
  103. if inspect.ismodule (object):
  104. do_module (object, names)
  105. elif inspect.isclass (object):
  106. do_class (object, names)
  107. # Might have an object without its class in scope.
  108. elif hasattr (object, '__class__'):
  109. names.add ('__class__')
  110. do_class (object.__class__, names)
  111. # Probably not a good idea to try to enumerate arbitrary
  112. # dictionaries...
  113. return names
  114. def do_module (module, names):
  115. if hasattr (module, '__all__'): # limited export list
  116. names.update(module.__all__)
  117. for i in module.__all__:
  118. do_object (getattr (module, i), names)
  119. else: # use all names
  120. names.update(dir (module))
  121. for i in dir (module):
  122. do_object (getattr (module, i), names)
  123. return names
  124. def do_class (object, names):
  125. ns = dir (object)
  126. names.update(ns)
  127. if hasattr (object, '__bases__'): # superclasses
  128. for i in object.__bases__: do_object (i, names)
  129. return names
  130. return do_object (object, set([]))
  131. def complete (name, imports):
  132. """Complete TEXT in NAMESPACE and print a Lisp list of completions.
  133. Exec IMPORTS first."""
  134. import __main__, keyword
  135. def class_members(object):
  136. names = dir (object)
  137. if hasattr (object, '__bases__'):
  138. for super in object.__bases__:
  139. names = class_members (super)
  140. return names
  141. names = set([])
  142. base = None
  143. try:
  144. dict = __main__.__dict__.copy()
  145. if imports: exec(imports, dict)
  146. l = len (name)
  147. if not "." in name:
  148. for src in [dir (__builtins__), keyword.kwlist, list(dict.keys())]:
  149. for elt in src:
  150. if elt[:l] == name: names.add(elt)
  151. else:
  152. base = name[:name.rfind ('.')]
  153. name = name[name.rfind('.')+1:]
  154. try:
  155. object = eval (base, dict)
  156. names = set(dir (object))
  157. if hasattr (object, '__class__'):
  158. names.add('__class__')
  159. names.update(class_members (object))
  160. except: names = all_names (dict)
  161. except:
  162. print(sys.exc_info())
  163. names = []
  164. l = len(name)
  165. print('_emacs_out (', end=' ')
  166. for n in names:
  167. if name == n[:l]:
  168. if base: print('"%s.%s"' % (base, n), end=' ')
  169. else: print('"%s"' % n, end=' ')
  170. print(')')
  171. def ehelp (name, imports):
  172. """Get help on string NAME.
  173. First try to eval name for, e.g. user definitions where we need
  174. the object. Otherwise try the string form."""
  175. locls = {}
  176. if imports:
  177. try: exec(imports, locls)
  178. except: pass
  179. try: help (eval (name, globals(), locls))
  180. except: help (name)
  181. def eimport (mod, dir):
  182. """Import module MOD with directory DIR at the head of the search path.
  183. NB doesn't load from DIR if MOD shadows a system module."""
  184. from __main__ import __dict__
  185. path0 = sys.path[0]
  186. sys.path[0] = dir
  187. try:
  188. try:
  189. if mod in __dict__ and inspect.ismodule (__dict__[mod]):
  190. imp.reload (__dict__[mod])
  191. else:
  192. __dict__[mod] = __import__ (mod)
  193. except:
  194. (type, value, tb) = sys.exc_info ()
  195. print("Traceback (most recent call last):")
  196. traceback.print_exception (type, value, tb.tb_next)
  197. finally:
  198. sys.path[0] = path0
  199. def modpath (module):
  200. """Return the source file for the given MODULE (or None).
  201. Assumes that MODULE.py and MODULE.pyc are in the same directory."""
  202. try:
  203. path = __import__ (module).__file__
  204. if path[-4:] == '.pyc' and os.path.exists (path[0:-1]):
  205. path = path[:-1]
  206. print("_emacs_out", path)
  207. except:
  208. print("_emacs_out ()")
  209. # print '_emacs_ok' # ready for input and can call continuation