1 """Definitions used by commands sent to inferior Python in python.el."""
3 # Copyright (C) 2004, 2005 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 If we get an exception, print a traceback with the top frame
30 (oursleves) excluded."""
32 try: execfile (file, globals (), globals ())
34 (type, value
, tb
) = sys
.exc_info ()
35 # Lose the stack frame for this location.
37 if tb
is None: # print_exception won't do it
38 print "Traceback (most recent call last):"
39 traceback
.print_exception (type, value
, tb
)
44 "Get arglist of NAME for Eldoc &c."
46 parts
= name
.split ('.')
48 exec 'import ' + parts
[0] # might fail
50 if inspect
.isbuiltin (func
):
52 if doc
.find (' ->') != -1:
53 print '_emacs_out', doc
.split (' ->')[0]
54 elif doc
.find ('\n') != -1:
55 print '_emacs_out', doc
.split ('\n')[0]
57 if inspect
.ismethod (func
):
59 if not inspect
.isfunction (func
):
61 (args
, varargs
, varkw
, defaults
) = inspect
.getargspec (func
)
62 # No space between name and arglist for consistency with builtins.
64 func
.__name
__ + inspect
.formatargspec (args
, varargs
, varkw
,
68 def complete (text
, namespace
= None):
69 """Complete TEXT in NAMESPACE and print a Lisp list of completions.
70 NAMESPACE is currently not used."""
71 if namespace
is None: namespace
= __main__
.__dict
__
72 c
= rlcompleter
.Completer (namespace
)
75 matches
= c
.attr_matches (text
)
77 matches
= c
.global_matches (text
)
85 def ehelp (name
, g
, l
):
86 """Get help on string NAME using globals G and locals L.
87 First try to eval name for, e.g. user definitions where we need
88 the object. Otherwise try the string form."""
89 try: help (eval (name
, g
, l
))
92 def eimport (mod
, dir):
93 """Import module MOD with directory DIR at the head of the search path.
94 NB doesn't load from DIR if MOD shadows a system module."""
99 if globals().has_key(mod
) and inspect
.ismodule (eval (mod
)):
102 globals ()[mod
] = __import__ (mod
)
104 (type, value
, tb
) = sys
.exc_info ()
105 print "Traceback (most recent call last):"
106 traceback
.print_exception (type, value
, tb
.tb_next
)
110 print '_emacs_ok' # ready for input and can call continuation
112 # arch-tag: d90408f3-90e2-4de4-99c2-6eb9c7b9ca46