[Python-apps-team] SPE & pychecker2

SPE Stani's Python Editor spe.stani.be at gmail.com
Tue Nov 27 21:36:34 UTC 2007


> Emilio Pozuelo Monfort schreef:

>  > Ok, I've downloaded the pychecker source package and it includes
> pychecker2.
>  > I've filled bug requesting its packaging.
>  >
>  > http://bugs.debian.org/453092
>  >
>  > I'm curious about your modifications... what kind of changes were
> them? If they
>  > are suitable for upstream we could forward them.

I'll show the changes I did.  SPE uses pychecker2 which is dependent
on pychecker. The file _spe/plugins/Pycheck.py executes in a wxpython
process:
python pychecker2/main.py --incremental foo.py

This has the advantage that:
- it is executed in a seperate process without blocking spe or using
threads (which are not possible in a blender environment)
- the resulting report can be shown incrementally in the sidebar

As the patches are only in pychecker2 we could consider of stripping
out pychecker and only leaving in pychecker2 in the spe plugins
directory as Debian doesn't seem to ship pychecker2. Even if it would
it has to be patched anyway for spe.

Globally speaking I did two changes for pychecker2/main.py, which are
clearly marked with comments:

*1* In the original pychecker2 the CACHE_FILE is defined as '/tmp/t'.
I prefer to have a solution to use ~/.pychecker_cache in a way which
works cross-platform also on Windows and Mac.

@@ -1,5 +1,29 @@
 import sys
 from os.path import dirname, realpath
+
+#---Patched by Stani http://pythonide.stani.be (begin)
+import os
+def userPath(dirname=''):
+    """'safer' function to find user path."""
+    # 'safer' function to find user path: look for one of these directories
+    try:
+        path = os.path.expanduser("~")
+        if os.path.isdir(path):
+            return os.path.join(path, dirname)
+    except:
+        pass
+    for evar in ('HOME', 'USERPROFILE', 'TMP'):
+        try:
+            path = os.environ[evar]
+            if os.path.isdir(path):
+                return os.path.join(path, dirname)
+        except:
+            pass
+    #if no match found, use module directory
+    return os.path.join(os.path.dirname(os.path.abspath(__file__)), dirname)
+
+CACHE_FILE = userPath(".pychecker_cache")
+#Patched by Stani http://pythonide.stani.be (end)
 sys.path.append(dirname(dirname(realpath(sys.argv[0]))))

 from pychecker2.Check import CheckList
@@ -16,7 +40,6 @@
 from pychecker2 import ConditionalChecks
 from pychecker2 import FormatStringChecks

-CACHE_FILE = '/tmp/t'

 def print_warnings(f, out):
     if not f.warnings:



*2* pychecker2 fails to load local modules relative to the file which
is being checked. This solves it.
@@ -69,7 +92,8 @@
     options = Options.Options()
     try:
         checker = cPickle.load(open(CACHE_FILE, 'rb'))
-    except (EOFError, IOError):
+    #---Patched by Stani http://pythonide.stani.be (added ImportError)
+    except (EOFError, IOError, ImportError):
         checker = create_checklist(options)

     try:
@@ -79,10 +103,18 @@
         options.usage(sys.argv[0], sys.stderr)
         return 1

+    #---Patched by Stani http://pythonide.stani.be (begin)
+    sys_path    = sys.path[:]
     for f in files:
+        f_dir   = dirname(f.name)
+        sys.path= sys_path[:]
+        if f_dir not in sys.path:
+            sys.path.insert(0,f_dir)
         checker.check_file(f)
         if options.incremental and not options.profile:
             print_warnings(f, sys.stdout)
+    sys.path    = sys_path
+    #Patched by Stani http://pythonide.stani.be (end)

     result = 0
     if not options.incremental and not options.profile:


-- 
http://pythonide.stani.be



More information about the Python-apps-team mailing list