annotate etc/emacs.py @ 83949:839526eca1bf

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