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
23import os, sys, traceback, inspect, rlcompleter, __main__
24
25__all__ = ["eexecfile", "args", "complete", "ehelp", "eimport"]
26
27def eexecfile (file):
28    """Execute FILE and then remove it.
29    If we get an exception, print a traceback with the top frame
30    (oursleves) excluded."""
31    try:
32	try: execfile (file, globals (), globals ())
33        except:
34	    (type, value, tb) = sys.exc_info ()
35	    # Lose the stack frame for this location.
36	    tb = tb.tb_next
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)
40    finally:
41	os.remove (file)
42
43def eargs (name):
44    "Get arglist of NAME for Eldoc &c."
45    try:
46	parts = name.split ('.')
47	if len (parts) > 1:
48	    exec 'import ' + parts[0] # might fail
49	func = eval (name)
50	if inspect.isbuiltin (func):
51	    doc = func.__doc__
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]
56	    return
57	if inspect.ismethod (func):
58	    func = func.im_func
59	if not inspect.isfunction (func):
60            return
61	(args, varargs, varkw, defaults) = inspect.getargspec (func)
62	# No space between name and arglist for consistency with builtins.
63	print '_emacs_out', \
64	    func.__name__ + inspect.formatargspec (args, varargs, varkw,
65						   defaults)
66    except: pass
67
68def 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)
73    try:
74        if '.' in text:
75            matches = c.attr_matches (text)
76	else:
77            matches = c.global_matches (text)
78        print '_emacs_out (',
79        for elt in matches:
80            print '"%s"' % elt,
81        print ')'
82    except:
83        print '_emacs_out ()'
84
85def 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))
90    except: help (name)
91
92def 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."""
95    path0 = sys.path[0]
96    sys.path[0] = dir
97    try:
98	try:
99	    if globals().has_key(mod) and inspect.ismodule (eval (mod)):
100		reload(eval (mod))
101	    else:
102		globals ()[mod] = __import__ (mod)
103	except:
104	    (type, value, tb) = sys.exc_info ()
105	    print "Traceback (most recent call last):"
106	    traceback.print_exception (type, value, tb.tb_next)
107    finally:
108	sys.path[0] = path0
109
110print '_emacs_ok'		# ready for input and can call continuation
111
112# arch-tag: d90408f3-90e2-4de4-99c2-6eb9c7b9ca46
113