Show
Ignore:
Timestamp:
09/09/06 16:30:10 (2 years ago)
Author:
miyoshi
Message:

Sync up with Emacs CVS HEAD.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/etc/emacs.py

    r4037 r4161  
    11"""Definitions used by commands sent to inferior Python in python.el.""" 
    22 
    3 # Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc. 
    4 # Author: Dave Love <d.love@dl.ac.uk
     3# Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc. 
     4# Author: Dave Love <fx@gnu.org
    55 
    66# This file is part of GNU Emacs. 
     
    2121# Boston, MA 02110-1301, USA. 
    2222 
    23 import os, sys, traceback, inspect, rlcompleter, __main__ 
     23import os, sys, traceback, inspect, __main__ 
     24from sets import Set 
    2425 
    25 __all__ = ["eexecfile", "args", "complete", "ehelp", "eimport"] 
     26__all__ = ["eexecfile", "eargs", "complete", "ehelp", "eimport", "modpath"] 
    2627 
    2728def eexecfile (file): 
    2829    """Execute FILE and then remove it. 
     30    Execute the file within the __main__ namespace. 
    2931    If we get an exception, print a traceback with the top frame 
    30     (oursleves) excluded.""" 
     32    (ourselves) excluded.""" 
    3133    try: 
    32        try: execfile (file, globals (), globals ()
    33        except: 
     34       try: execfile (file, __main__.__dict__
     35       except: 
    3436            (type, value, tb) = sys.exc_info () 
    3537            # Lose the stack frame for this location. 
     
    4143        os.remove (file) 
    4244 
    43 def eargs (name): 
     45def eargs (name, imports): 
    4446    "Get arglist of NAME for Eldoc &c." 
    4547    try: 
     48        if imports: exec imports 
    4649        parts = name.split ('.') 
    4750        if len (parts) > 1: 
     
    5760        if inspect.ismethod (func): 
    5861            func = func.im_func 
    59         if not inspect.isfunction (func): 
    60             return 
     62        if not inspect.isfunction (func): return 
    6163        (args, varargs, varkw, defaults) = inspect.getargspec (func) 
    6264        # No space between name and arglist for consistency with builtins. 
     
    6668    except: pass 
    6769 
    68 def complete (text, namespace = None): 
     70def all_names (object): 
     71    """Return (an approximation to) a list of all possible attribute 
     72    names reachable via the attributes of OBJECT, i.e. roughly the 
     73    leaves of the dictionary tree under it.""" 
     74 
     75    def do_object (object, names): 
     76        if inspect.ismodule (object): 
     77            do_module (object, names) 
     78        elif inspect.isclass (object): 
     79            do_class (object, names) 
     80        # Might have an object without its class in scope. 
     81        elif hasattr (object, '__class__'): 
     82            names.add ('__class__') 
     83            do_class (object.__class__, names) 
     84        # Probably not a good idea to try to enumerate arbitrary 
     85        # dictionaries... 
     86        return names 
     87 
     88    def do_module (module, names): 
     89        if hasattr (module, '__all__'): # limited export list 
     90            names.union_update (module.__all__) 
     91            for i in module.__all__: 
     92                do_object (getattr (module, i), names) 
     93        else:                   # use all names 
     94            names.union_update (dir (module)) 
     95            for i in dir (module): 
     96                do_object (getattr (module, i), names) 
     97        return names 
     98 
     99    def do_class (object, names): 
     100        ns = dir (object) 
     101        names.union_update (ns) 
     102        if hasattr (object, '__bases__'): # superclasses 
     103            for i in object.__bases__: do_object (i, names) 
     104        return names 
     105 
     106    return do_object (object, Set ([])) 
     107 
     108def complete (name, imports): 
    69109    """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) 
     110    Exec IMPORTS first.""" 
     111    import __main__, keyword 
     112 
     113    def class_members(object): 
     114        names = dir (object) 
     115        if hasattr (object, '__bases__'): 
     116            for super in object.__bases__: 
     117                names = class_members (super) 
     118        return names     
     119 
     120    names = Set ([]) 
     121    base = None 
    73122    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 ()' 
     123        dict = __main__.__dict__.copy() 
     124        if imports: exec imports in dict 
     125        l = len (name) 
     126        if not "." in name: 
     127            for list in [dir (__builtins__), keyword.kwlist, dict.keys()]: 
     128                for elt in list: 
     129                    if elt[:l] == name: names.add(elt) 
     130        else: 
     131            base = name[:name.rfind ('.')] 
     132            name = name[name.rfind('.')+1:] 
     133            try: 
     134                object = eval (base, dict) 
     135                names = Set (dir (object)) 
     136                if hasattr (object, '__class__'): 
     137                    names.add('__class__') 
     138                    names.union_update (class_members (object)) 
     139            except: names = all_names (dict) 
     140    except: return [] 
     141    l = len(name) 
     142    print '_emacs_out (', 
     143    for n in names: 
     144        if name == n[:l]: 
     145            if base: print '"%s.%s"' % (base, n), 
     146            else: print '"%s"' % n, 
     147    print ')' 
    84148 
    85 def ehelp (name, g, l): 
    86     """Get help on string NAME using globals G and locals L
     149def ehelp (name, imports): 
     150    """Get help on string NAME
    87151    First try to eval name for, e.g. user definitions where we need 
    88152    the object.  Otherwise try the string form.""" 
    89     try: help (eval (name, g, l)) 
     153    locls = {} 
     154    if imports: 
     155        try: exec imports in locls 
     156        except: pass 
     157    try: help (eval (name, globals(), locls)) 
    90158    except: help (name) 
    91159 
     
    93161    """Import module MOD with directory DIR at the head of the search path. 
    94162    NB doesn't load from DIR if MOD shadows a system module.""" 
     163    from __main__ import __dict__ 
     164 
    95165    path0 = sys.path[0] 
    96166    sys.path[0] = dir 
    97167    try: 
    98168        try: 
    99             if globals().has_key(mod) and inspect.ismodule (eval (mod)): 
    100                 reload(eval (mod)
     169            if __dict__.has_key(mod) and inspect.ismodule (__dict__[mod]): 
     170                reload (__dict__[mod]
    101171            else: 
    102                 globals ()[mod] = __import__ (mod) 
     172                __dict__[mod] = __import__ (mod) 
    103173        except: 
    104174            (type, value, tb) = sys.exc_info () 
     
    108178        sys.path[0] = path0 
    109179 
    110 print '_emacs_ok'               # ready for input and can call continuation 
     180def modpath (module): 
     181    """Return the source file for the given MODULE (or None). 
     182Assumes that MODULE.py and MODULE.pyc are in the same directory.""" 
     183    try: 
     184        path = __import__ (module).__file__ 
     185        if path[-4:] == '.pyc' and os.path.exists (path[0:-1]): 
     186            path = path[:-1] 
     187        print "_emacs_out", path 
     188    except: 
     189        print "_emacs_out ()" 
     190 
     191# print '_emacs_ok'             # ready for input and can call continuation 
    111192 
    112193# arch-tag: d90408f3-90e2-4de4-99c2-6eb9c7b9ca46