annotate etc/emacs2.py @ 86563:d404da216302

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