(calendar-dst-check-each-year-flag): Avoid
[emacs.git] / etc / emacs.py
blobcc72233602fc1d5f902a9e15c16907f26d584e3c
1 """Definitions used by commands sent to inferior Python in python.el."""
3 # Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
4 # Author: Dave Love <fx@gnu.org>
6 # This file is part of GNU Emacs.
8 # GNU Emacs is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2, or (at your option)
11 # any later version.
13 # GNU Emacs is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with GNU Emacs; see the file COPYING. If not, write to the
20 # Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 # Boston, MA 02110-1301, USA.
23 import os, sys, traceback, inspect, __main__
24 from sets import Set
26 __all__ = ["eexecfile", "eargs", "complete", "ehelp", "eimport", "modpath"]
28 def eexecfile (file):
29 """Execute FILE and then remove it.
30 Execute the file within the __main__ namespace.
31 If we get an exception, print a traceback with the top frame
32 (ourselves) excluded."""
33 try:
34 try: execfile (file, __main__.__dict__)
35 except:
36 (type, value, tb) = sys.exc_info ()
37 # Lose the stack frame for this location.
38 tb = tb.tb_next
39 if tb is None: # print_exception won't do it
40 print "Traceback (most recent call last):"
41 traceback.print_exception (type, value, tb)
42 finally:
43 os.remove (file)
45 def eargs (name, imports):
46 "Get arglist of NAME for Eldoc &c."
47 try:
48 if imports: exec imports
49 parts = name.split ('.')
50 if len (parts) > 1:
51 exec 'import ' + parts[0] # might fail
52 func = eval (name)
53 if inspect.isbuiltin (func) or type(func) is type:
54 doc = func.__doc__
55 if doc.find (' ->') != -1:
56 print '_emacs_out', doc.split (' ->')[0]
57 else:
58 print '_emacs_out', doc.split ('\n')[0]
59 return
60 if inspect.ismethod (func):
61 func = func.im_func
62 if not inspect.isfunction (func):
63 print '_emacs_out '
64 return
65 (args, varargs, varkw, defaults) = inspect.getargspec (func)
66 # No space between name and arglist for consistency with builtins.
67 print '_emacs_out', \
68 func.__name__ + inspect.formatargspec (args, varargs, varkw,
69 defaults)
70 except:
71 print "_emacs_out "
73 def all_names (object):
74 """Return (an approximation to) a list of all possible attribute
75 names reachable via the attributes of OBJECT, i.e. roughly the
76 leaves of the dictionary tree under it."""
78 def do_object (object, names):
79 if inspect.ismodule (object):
80 do_module (object, names)
81 elif inspect.isclass (object):
82 do_class (object, names)
83 # Might have an object without its class in scope.
84 elif hasattr (object, '__class__'):
85 names.add ('__class__')
86 do_class (object.__class__, names)
87 # Probably not a good idea to try to enumerate arbitrary
88 # dictionaries...
89 return names
91 def do_module (module, names):
92 if hasattr (module, '__all__'): # limited export list
93 names.union_update (module.__all__)
94 for i in module.__all__:
95 do_object (getattr (module, i), names)
96 else: # use all names
97 names.union_update (dir (module))
98 for i in dir (module):
99 do_object (getattr (module, i), names)
100 return names
102 def do_class (object, names):
103 ns = dir (object)
104 names.union_update (ns)
105 if hasattr (object, '__bases__'): # superclasses
106 for i in object.__bases__: do_object (i, names)
107 return names
109 return do_object (object, Set ([]))
111 def complete (name, imports):
112 """Complete TEXT in NAMESPACE and print a Lisp list of completions.
113 Exec IMPORTS first."""
114 import __main__, keyword
116 def class_members(object):
117 names = dir (object)
118 if hasattr (object, '__bases__'):
119 for super in object.__bases__:
120 names = class_members (super)
121 return names
123 names = Set ([])
124 base = None
125 try:
126 dict = __main__.__dict__.copy()
127 if imports: exec imports in dict
128 l = len (name)
129 if not "." in name:
130 for list in [dir (__builtins__), keyword.kwlist, dict.keys()]:
131 for elt in list:
132 if elt[:l] == name: names.add(elt)
133 else:
134 base = name[:name.rfind ('.')]
135 name = name[name.rfind('.')+1:]
136 try:
137 object = eval (base, dict)
138 names = Set (dir (object))
139 if hasattr (object, '__class__'):
140 names.add('__class__')
141 names.union_update (class_members (object))
142 except: names = all_names (dict)
143 except: return []
144 l = len(name)
145 print '_emacs_out (',
146 for n in names:
147 if name == n[:l]:
148 if base: print '"%s.%s"' % (base, n),
149 else: print '"%s"' % n,
150 print ')'
152 def ehelp (name, imports):
153 """Get help on string NAME.
154 First try to eval name for, e.g. user definitions where we need
155 the object. Otherwise try the string form."""
156 locls = {}
157 if imports:
158 try: exec imports in locls
159 except: pass
160 try: help (eval (name, globals(), locls))
161 except: help (name)
163 def eimport (mod, dir):
164 """Import module MOD with directory DIR at the head of the search path.
165 NB doesn't load from DIR if MOD shadows a system module."""
166 from __main__ import __dict__
168 path0 = sys.path[0]
169 sys.path[0] = dir
170 try:
171 try:
172 if __dict__.has_key(mod) and inspect.ismodule (__dict__[mod]):
173 reload (__dict__[mod])
174 else:
175 __dict__[mod] = __import__ (mod)
176 except:
177 (type, value, tb) = sys.exc_info ()
178 print "Traceback (most recent call last):"
179 traceback.print_exception (type, value, tb.tb_next)
180 finally:
181 sys.path[0] = path0
183 def modpath (module):
184 """Return the source file for the given MODULE (or None).
185 Assumes that MODULE.py and MODULE.pyc are in the same directory."""
186 try:
187 path = __import__ (module).__file__
188 if path[-4:] == '.pyc' and os.path.exists (path[0:-1]):
189 path = path[:-1]
190 print "_emacs_out", path
191 except:
192 print "_emacs_out ()"
194 # print '_emacs_ok' # ready for input and can call continuation
196 # arch-tag: d90408f3-90e2-4de4-99c2-6eb9c7b9ca46