Make pythonpath behaviour closer to python's (closes #88218)

When pylint is called with a single filename as argument, walk its path backwards to find the first directory that doesn't have a __init__.py, and add that to sys.path instead of the cwd.

authorJT Olds <jtolds@xnet5.com>
changeset5432e78e7aec
branchdefault
phasepublic
hiddenno
parent revision#ffc9061c07af Emit W0102 (dangerous default value) when sets are used as default arguments for functions. Closes #92911
child revision#b68f274aaab7 fix gui reporter broken by addition of coloffset information. Closes #92584
files modified by this revision
lint.py
# HG changeset patch
# User JT Olds <jtolds@xnet5.com>
# Date 1337086364 -7200
# Tue May 15 14:52:44 2012 +0200
# Node ID 5432e78e7aec126cea273398ea0bb051439fd177
# Parent ffc9061c07aff28e8069da3de6b2153c82fb640d
Make pythonpath behaviour closer to python's (closes #88218)

When pylint is called with a single filename as argument, walk its path
backwards to find the first directory that doesn't have a __init__.py,
and add that to sys.path instead of the cwd.

diff --git a/lint.py b/lint.py
@@ -66,10 +66,22 @@
1                      'msvs': VSTextReporter,
2                      'colorized': ColorizedTextReporter,
3                      'html': HTMLReporter,}
4 
5 
6 +def _get_python_path(filepath):
7 +    dirname = os.path.dirname(os.path.realpath(
8 +            os.path.expanduser(filepath)))
9 +    while True:
10 +        if not os.path.exists(os.path.join(dirname, "__init__.py")):
11 +            return dirname
12 +        old_dirname = dirname
13 +        dirname = os.path.dirname(dirname)
14 +        if old_dirname == dirname:
15 +            return os.getcwd()
16 +
17 +
18  # Python Linter class #########################################################
19 
20  MSGS = {
21      'F0001': ('%s',
22                'Used when an error occurred preventing the analysis of a \
@@ -872,11 +884,14 @@
23          if not args:
24              print linter.help()
25              sys.exit(32)
26          # insert current working directory to the python path to have a correct
27          # behaviour
28 -        sys.path.insert(0, os.getcwd())
29 +        if len(args) == 1:
30 +            sys.path.insert(0, _get_python_path(args[0]))
31 +        else:
32 +            sys.path.insert(0, os.getcwd())
33          if self.linter.config.profile:
34              print >> sys.stderr, '** profiled run'
35              import cProfile, pstats
36              cProfile.runctx('linter.check(%r)' % args, globals(), locals(), 'stones.prof' )
37              data = pstats.Stats('stones.prof')