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 <d.love@dl.ac.uk>
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)
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
, rlcompleter
, __main__
25 __all__
= ["eexecfile", "args", "complete", "ehelp", "eimport"]
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."""
33 try: execfile (file, __main__
.__dict
__)
35 (type, value
, tb
) = sys
.exc_info ()
36 # Lose the stack frame for this location.
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
)
45 "Get arglist of NAME for Eldoc &c."
47 parts
= name
.split ('.')
49 exec 'import ' + parts
[0] # might fail
51 if inspect
.isbuiltin (func
):
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]
58 if inspect
.ismethod (func
):
60 if not inspect
.isfunction (func
):
62 (args
, varargs
, varkw
, defaults
) = inspect
.getargspec (func
)
63 # No space between name and arglist for consistency with builtins.
65 func
.__name
__ + inspect
.formatargspec (args
, varargs
, varkw
,
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
)
76 matches
= c
.attr_matches (text
)
78 matches
= c
.global_matches (text
)
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
))
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."""
100 if globals().has_key(mod
) and inspect
.ismodule (eval (mod
)):
103 globals ()[mod
] = __import__ (mod
)
105 (type, value
, tb
) = sys
.exc_info ()
106 print "Traceback (most recent call last):"
107 traceback
.print_exception (type, value
, tb
.tb_next
)
111 print '_emacs_ok' # ready for input and can call continuation
113 # arch-tag: d90408f3-90e2-4de4-99c2-6eb9c7b9ca46