(eexecfile): Use the __main__ rather than `emacs' namespace.
[bpt/emacs.git] / etc / emacs.py
1 """Definitions used by commands sent to inferior Python in python.el."""
2
3 # Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
4 # Author: Dave Love <d.love@dl.ac.uk>
5
6 # This file is part of GNU Emacs.
7
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.
12
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.
17
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.
22
23 import os, sys, traceback, inspect, rlcompleter, __main__
24
25 __all__ = ["eexecfile", "args", "complete", "ehelp", "eimport"]
26
27 def eexecfile (file):
28 """Execute FILE and then remove it.
29 Execute the file within the __main__ namespace.
30 If we get an exception, print a traceback with the top frame
31 (ourselves) excluded."""
32 try:
33 try: execfile (file, __main__.__dict__)
34 except:
35 (type, value, tb) = sys.exc_info ()
36 # Lose the stack frame for this location.
37 tb = tb.tb_next
38 if tb is None: # print_exception won't do it
39 print "Traceback (most recent call last):"
40 traceback.print_exception (type, value, tb)
41 finally:
42 os.remove (file)
43
44 def eargs (name):
45 "Get arglist of NAME for Eldoc &c."
46 try:
47 parts = name.split ('.')
48 if len (parts) > 1:
49 exec 'import ' + parts[0] # might fail
50 func = eval (name)
51 if inspect.isbuiltin (func):
52 doc = func.__doc__
53 if doc.find (' ->') != -1:
54 print '_emacs_out', doc.split (' ->')[0]
55 elif doc.find ('\n') != -1:
56 print '_emacs_out', doc.split ('\n')[0]
57 return
58 if inspect.ismethod (func):
59 func = func.im_func
60 if not inspect.isfunction (func):
61 return
62 (args, varargs, varkw, defaults) = inspect.getargspec (func)
63 # No space between name and arglist for consistency with builtins.
64 print '_emacs_out', \
65 func.__name__ + inspect.formatargspec (args, varargs, varkw,
66 defaults)
67 except: pass
68
69 def complete (text, namespace = None):
70 """Complete TEXT in NAMESPACE and print a Lisp list of completions.
71 NAMESPACE is currently not used."""
72 if namespace is None: namespace = __main__.__dict__
73 c = rlcompleter.Completer (namespace)
74 try:
75 if '.' in text:
76 matches = c.attr_matches (text)
77 else:
78 matches = c.global_matches (text)
79 print '_emacs_out (',
80 for elt in matches:
81 print '"%s"' % elt,
82 print ')'
83 except:
84 print '_emacs_out ()'
85
86 def ehelp (name, g, l):
87 """Get help on string NAME using globals G and locals L.
88 First try to eval name for, e.g. user definitions where we need
89 the object. Otherwise try the string form."""
90 try: help (eval (name, g, l))
91 except: help (name)
92
93 def eimport (mod, dir):
94 """Import module MOD with directory DIR at the head of the search path.
95 NB doesn't load from DIR if MOD shadows a system module."""
96 path0 = sys.path[0]
97 sys.path[0] = dir
98 try:
99 try:
100 if globals().has_key(mod) and inspect.ismodule (eval (mod)):
101 reload(eval (mod))
102 else:
103 globals ()[mod] = __import__ (mod)
104 except:
105 (type, value, tb) = sys.exc_info ()
106 print "Traceback (most recent call last):"
107 traceback.print_exception (type, value, tb.tb_next)
108 finally:
109 sys.path[0] = path0
110
111 print '_emacs_ok' # ready for input and can call continuation
112
113 # arch-tag: d90408f3-90e2-4de4-99c2-6eb9c7b9ca46