annotate etc/emacs2.py @ 98973:4604df28ddb5

*** empty log message ***
author Eli Zaretskii <eliz@gnu.org>
date Tue, 21 Oct 2008 12:15:25 +0000
parents cbf5528cf447
children c90853557b90
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
87649
107ccd98fa12 Merge from emacs--rel--22
Miles Bader <miles@gnu.org>
parents: 84821
diff changeset
3 # Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
84821
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
95004
cbf5528cf447 Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents: 87649
diff changeset
8 # GNU Emacs is free software: you can redistribute it and/or modify
84821
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
95004
cbf5528cf447 Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents: 87649
diff changeset
10 # the Free Software Foundation, either version 3 of the License, or
cbf5528cf447 Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents: 87649
diff changeset
11 # (at your option) any later version.
84821
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
95004
cbf5528cf447 Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents: 87649
diff changeset
19 # along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
84821
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
20
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
21 import os, sys, traceback, inspect, __main__
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 try:
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
24 set
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
25 except:
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
26 from sets import Set as set
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
27
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
28 __all__ = ["eexecfile", "eargs", "complete", "ehelp", "eimport", "modpath"]
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 def format_exception (filename, should_remove_self):
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
31 type, value, tb = sys.exc_info ()
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
32 sys.last_type = type
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
33 sys.last_value = value
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
34 sys.last_traceback = tb
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
35 if type is SyntaxError:
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
36 try: # parse the error message
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
37 msg, (dummy_filename, lineno, offset, line) = value
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
38 except:
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
39 pass # Not the format we expect; leave it alone
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
40 else:
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
41 # Stuff in the right filename
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
42 value = SyntaxError(msg, (filename, lineno, offset, line))
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
43 sys.last_value = value
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
44 res = traceback.format_exception_only (type, value)
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
45 # 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
46 # should not massage it.
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
47 if should_remove_self:
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
48 tblist = traceback.extract_tb (tb)
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
49 del tblist[:1]
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
50 res = traceback.format_list (tblist)
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
51 if res:
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
52 res.insert(0, "Traceback (most recent call last):\n")
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
53 res[len(res):] = traceback.format_exception_only (type, value)
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
54 # traceback.print_exception(type, value, tb)
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
55 for line in res: print line,
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
56
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
57 def eexecfile (file):
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
58 """Execute FILE and then remove it.
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
59 Execute the file within the __main__ namespace.
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
60 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
61 (ourselves) excluded."""
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
62 # 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
63 # locked forever (under w32) if SyntaxError occurs.
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
64 # --- code based on code.py and PyShell.py.
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
65 try:
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
66 try:
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
67 source = open (file, "r").read()
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
68 code = compile (source, file, "exec")
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
69 # Other exceptions (shouldn't be any...) will (correctly) fall
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
70 # through to "final".
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
71 except (OverflowError, SyntaxError, ValueError):
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
72 # FIXME: When can compile() raise anything else than
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
73 # SyntaxError ????
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
74 format_exception (file, False)
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
75 return
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
76 try:
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
77 exec code in __main__.__dict__
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
78 except:
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
79 format_exception (file, True)
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
80 finally:
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
81 os.remove (file)
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
82
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
83 def eargs (name, imports):
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
84 "Get arglist of NAME for Eldoc &c."
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
85 try:
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
86 if imports: exec imports
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
87 parts = name.split ('.')
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
88 if len (parts) > 1:
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
89 exec 'import ' + parts[0] # might fail
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
90 func = eval (name)
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
91 if inspect.isbuiltin (func) or type(func) is type:
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
92 doc = func.__doc__
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
93 if doc.find (' ->') != -1:
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
94 print '_emacs_out', doc.split (' ->')[0]
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
95 else:
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
96 print '_emacs_out', doc.split ('\n')[0]
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
97 return
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
98 if inspect.ismethod (func):
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
99 func = func.im_func
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
100 if not inspect.isfunction (func):
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
101 print '_emacs_out '
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
102 return
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
103 (args, varargs, varkw, defaults) = inspect.getargspec (func)
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
104 # 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
105 print '_emacs_out', \
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
106 func.__name__ + inspect.formatargspec (args, varargs, varkw,
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
107 defaults)
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
108 except:
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
109 print "_emacs_out "
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
110
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
111 def all_names (object):
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
112 """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
113 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
114 leaves of the dictionary tree under it."""
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
115
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
116 def do_object (object, names):
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
117 if inspect.ismodule (object):
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
118 do_module (object, names)
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
119 elif inspect.isclass (object):
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
120 do_class (object, names)
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
121 # Might have an object without its class in scope.
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
122 elif hasattr (object, '__class__'):
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
123 names.add ('__class__')
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
124 do_class (object.__class__, names)
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
125 # 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
126 # dictionaries...
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
127 return names
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
128
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
129 def do_module (module, names):
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
130 if hasattr (module, '__all__'): # limited export list
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
131 names.update(module.__all__)
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
132 for i in module.__all__:
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
133 do_object (getattr (module, i), names)
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
134 else: # use all names
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
135 names.update(dir (module))
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
136 for i in dir (module):
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
137 do_object (getattr (module, i), names)
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
138 return names
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
139
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
140 def do_class (object, names):
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
141 ns = dir (object)
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
142 names.update(ns)
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
143 if hasattr (object, '__bases__'): # superclasses
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
144 for i in object.__bases__: do_object (i, names)
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
145 return names
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
146
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
147 return do_object (object, set([]))
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 def complete (name, imports):
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
150 """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
151 Exec IMPORTS first."""
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
152 import __main__, keyword
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
153
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
154 def class_members(object):
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
155 names = dir (object)
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
156 if hasattr (object, '__bases__'):
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
157 for super in object.__bases__:
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
158 names = class_members (super)
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
159 return names
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
160
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
161 names = set([])
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
162 base = None
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
163 try:
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
164 dict = __main__.__dict__.copy()
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
165 if imports: exec imports in dict
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
166 l = len (name)
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
167 if not "." in name:
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
168 for src in [dir (__builtins__), keyword.kwlist, dict.keys()]:
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
169 for elt in src:
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
170 if elt[:l] == name: names.add(elt)
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
171 else:
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
172 base = name[:name.rfind ('.')]
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
173 name = name[name.rfind('.')+1:]
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
174 try:
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
175 object = eval (base, dict)
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
176 names = set(dir (object))
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
177 if hasattr (object, '__class__'):
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
178 names.add('__class__')
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
179 names.update(class_members (object))
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
180 except: names = all_names (dict)
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
181 except:
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
182 print sys.exc_info()
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
183 names = []
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
184
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
185 l = len(name)
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
186 print '_emacs_out (',
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
187 for n in names:
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
188 if name == n[:l]:
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
189 if base: print '"%s.%s"' % (base, n),
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
190 else: print '"%s"' % n,
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
191 print ')'
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
192
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
193 def ehelp (name, imports):
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
194 """Get help on string NAME.
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
195 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
196 the object. Otherwise try the string form."""
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
197 locls = {}
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
198 if imports:
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
199 try: exec imports in locls
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
200 except: pass
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
201 try: help (eval (name, globals(), locls))
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
202 except: help (name)
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
203
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
204 def eimport (mod, dir):
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
205 """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
206 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
207 from __main__ import __dict__
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
208
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
209 path0 = sys.path[0]
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
210 sys.path[0] = dir
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
211 try:
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
212 try:
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
213 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
214 reload (__dict__[mod])
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
215 else:
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
216 __dict__[mod] = __import__ (mod)
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
217 except:
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
218 (type, value, tb) = sys.exc_info ()
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
219 print "Traceback (most recent call last):"
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
220 traceback.print_exception (type, value, tb.tb_next)
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
221 finally:
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
222 sys.path[0] = path0
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
223
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
224 def modpath (module):
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
225 """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
226 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
227 try:
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
228 path = __import__ (module).__file__
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
229 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
230 path = path[:-1]
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
231 print "_emacs_out", path
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
232 except:
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
233 print "_emacs_out ()"
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
234
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
235 # 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
236
fa7f4408009f Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
237 # arch-tag: d90408f3-90e2-4de4-99c2-6eb9c7b9ca46