Hi,

Here is a little patch, which fixes the following problems in pycomplete:

py-find-global-imports(): Support dot in "from ... import" statements,
e.g. "from PyQt4.QtGui import QMainWindow".

get_all_completions(): Return an empty list when no symbol is found
(e.g. when trying to complete "[].ap").

Then I have integrated a few changes ported from the current version
in http://www.rwdev.eu/articles/emacspyeng:

py-complete(), get_all_completions(): Additional except statements
when calling eval, exec and __import__, checks if symbol is empty.

pycomplete(): Change to directory where file is, so that relative
imports also work when file is not in the current directory of the
Python interpreter.

Is it OK to send patches like this, or would you prefer an attachment
or a bzr merge request?

Regards,
Urs

=== modified file 'completion/pycomplete.el'
--- completion/pycomplete.el    2012-05-10 11:19:31 +0000
+++ completion/pycomplete.el    2012-07-01 11:42:53 +0000
@@ -49,7 +49,7 @@
       (goto-char (point-min))
       (setq imports nil)
       (while (re-search-forward
-             "^\\(import \\|from \\([A-Za-z_][A-Za-z_0-9]*\\) import \\).*"
+             "^\\(import \\|from \\([A-Za-z_\\.][A-Za-z_0-9\\.]*\\) import 
\\).*"
              nil t)
        (setq imports (append imports
                              (list (buffer-substring
@@ -62,7 +62,7 @@
   (let* ((pymacs-forget-mutability t)
          (symbol (py-symbol-near-point))
          (completions
-          (pycomplete-pycomplete symbol
+          (pycomplete-pycomplete symbol (buffer-file-name)
                                  (py-find-global-imports))))
     (cond ((null completions) ; no matching symbol
            (message "Can't find completion for \"%s\"" symbol)

=== modified file 'completion/pycomplete.py'
--- completion/pycomplete.py    2012-05-10 11:19:31 +0000
+++ completion/pycomplete.py    2012-07-01 12:12:23 +0000
@@ -59,6 +59,8 @@
                 exec stmt in globals(), locald
             except TypeError:
                 raise TypeError, "invalid type: %s" % stmt
+            except Exception:
+                continue

     dots = s.split(".")
     if not s or len(dots) == 1:
@@ -77,6 +79,8 @@
     sym = None
     for i in range(1, len(dots)):
         s = ".".join(dots[:i])
+        if not s:
+            continue
         try:
             sym = eval(s, globals(), locald)
         except NameError:
@@ -84,11 +88,23 @@
                 sym = __import__(s, globals(), locald, [])
             except ImportError:
                 return []
+            except AttributeError:
+                try:
+                    sym = __import__(s, globals(), locald, [])
+                except ImportError:
+                    pass
     if sym is not None:
         s = dots[-1]
         return [k for k in dir(sym) if k.startswith(s)]
-
-def pycomplete(s, imports=None):
+    return []
+
+def pycomplete(s, fname=None, imports=None):
+    if not s:
+        return ''
+    # changes to where the file is
+    if fname:
+        os.chdir(os.path.dirname(fname))
+
     completions = get_all_completions(s, imports)
     if len(completions) == 0:
         return None
_______________________________________________
Python-mode mailing list
Python-mode@python.org
http://mail.python.org/mailman/listinfo/python-mode

Reply via email to