Mercurial > hg
annotate tests/filterpyflakes.py @ 19933:621a26eb3a99
demandimport: allow extensions to import own modules by absolute name
Before this patch, python modules of each extensions can't import
another one in own extension by absolute name, because root modules of
each extensions are loaded with "hgext_" prefix.
For example, "import extroot.bar" in "extroot/foo.py" of "extroot"
extension fails, even though "import bar" in it succeeds.
Installing extensions into site-packages of python library path can
avoid this problem, but this solution is not reasonable in some cases:
using binary package of Mercurial on Windows, for example.
This patch retries to import with "hgext_" prefix after ImportError,
if the module in the extension may try to import another one in own
extension.
This patch doesn't change some "_import()"/"_origimport()" invocations
below, because ordinary extensions shouldn't cause such invocations.
- invocation of "_import()" when root module imports sub-module by
absolute path without "fromlist"
for example, "import a.b" in "a.__init__.py".
extensions are loaded with "hgext_" prefix, and this causes
execution of another (= fixed by this patch) code path.
- invocation of "_origimport()" when "level != -1" with "fromlist"
for example, importing after "from __future__ import
absolute_import" (level == 0), or "from . import b" or "from .a
import b" (0 < level),
for portability between python versions and environments,
extensions shouldn't cause "level != -1".
author | FUJIWARA Katsunori <foozy@lares.dti.ne.jp> |
---|---|
date | Sat, 05 Oct 2013 01:02:22 +0900 |
parents | 681f7b9213a4 |
children | 0768cda8b579 |
rev | line source |
---|---|
14140
82f0412ef7de
tests: add pyflakes checking for unused imports
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
82f0412ef7de
tests: add pyflakes checking for unused imports
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
2 |
82f0412ef7de
tests: add pyflakes checking for unused imports
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
3 # Filter output by pyflakes to control which warnings we check |
82f0412ef7de
tests: add pyflakes checking for unused imports
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
4 |
14209
08d84bdce1a5
pyflakes: ignore files marked no-check-code
Augie Fackler <durin42@gmail.com>
parents:
14176
diff
changeset
|
5 import sys, re, os |
14140
82f0412ef7de
tests: add pyflakes checking for unused imports
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
6 |
19335
77440de177f7
tests: simplify and document the sorting of pyflake messages
Simon Heimberg <simohe@besonet.ch>
parents:
14209
diff
changeset
|
7 def makekey(typeandline): |
77440de177f7
tests: simplify and document the sorting of pyflake messages
Simon Heimberg <simohe@besonet.ch>
parents:
14209
diff
changeset
|
8 """ |
77440de177f7
tests: simplify and document the sorting of pyflake messages
Simon Heimberg <simohe@besonet.ch>
parents:
14209
diff
changeset
|
9 for sorting lines by: msgtype, path/to/file, lineno, message |
77440de177f7
tests: simplify and document the sorting of pyflake messages
Simon Heimberg <simohe@besonet.ch>
parents:
14209
diff
changeset
|
10 |
77440de177f7
tests: simplify and document the sorting of pyflake messages
Simon Heimberg <simohe@besonet.ch>
parents:
14209
diff
changeset
|
11 typeandline is a sequence of a message type and the entire message line |
77440de177f7
tests: simplify and document the sorting of pyflake messages
Simon Heimberg <simohe@besonet.ch>
parents:
14209
diff
changeset
|
12 the message line format is path/to/file:line: message |
77440de177f7
tests: simplify and document the sorting of pyflake messages
Simon Heimberg <simohe@besonet.ch>
parents:
14209
diff
changeset
|
13 |
77440de177f7
tests: simplify and document the sorting of pyflake messages
Simon Heimberg <simohe@besonet.ch>
parents:
14209
diff
changeset
|
14 >>> makekey((3, 'example.py:36: any message')) |
77440de177f7
tests: simplify and document the sorting of pyflake messages
Simon Heimberg <simohe@besonet.ch>
parents:
14209
diff
changeset
|
15 (3, 'example.py', 36, ' any message') |
77440de177f7
tests: simplify and document the sorting of pyflake messages
Simon Heimberg <simohe@besonet.ch>
parents:
14209
diff
changeset
|
16 >>> makekey((7, 'path/to/file.py:68: dummy message')) |
77440de177f7
tests: simplify and document the sorting of pyflake messages
Simon Heimberg <simohe@besonet.ch>
parents:
14209
diff
changeset
|
17 (7, 'path/to/file.py', 68, ' dummy message') |
77440de177f7
tests: simplify and document the sorting of pyflake messages
Simon Heimberg <simohe@besonet.ch>
parents:
14209
diff
changeset
|
18 >>> makekey((2, 'fn:88: m')) > makekey((2, 'fn:9: m')) |
77440de177f7
tests: simplify and document the sorting of pyflake messages
Simon Heimberg <simohe@besonet.ch>
parents:
14209
diff
changeset
|
19 True |
77440de177f7
tests: simplify and document the sorting of pyflake messages
Simon Heimberg <simohe@besonet.ch>
parents:
14209
diff
changeset
|
20 """ |
77440de177f7
tests: simplify and document the sorting of pyflake messages
Simon Heimberg <simohe@besonet.ch>
parents:
14209
diff
changeset
|
21 |
77440de177f7
tests: simplify and document the sorting of pyflake messages
Simon Heimberg <simohe@besonet.ch>
parents:
14209
diff
changeset
|
22 msgtype, line = typeandline |
77440de177f7
tests: simplify and document the sorting of pyflake messages
Simon Heimberg <simohe@besonet.ch>
parents:
14209
diff
changeset
|
23 fname, line, message = line.split(":", 2) |
77440de177f7
tests: simplify and document the sorting of pyflake messages
Simon Heimberg <simohe@besonet.ch>
parents:
14209
diff
changeset
|
24 # line as int for ordering 9 before 88 |
77440de177f7
tests: simplify and document the sorting of pyflake messages
Simon Heimberg <simohe@besonet.ch>
parents:
14209
diff
changeset
|
25 return msgtype, fname, int(line), message |
77440de177f7
tests: simplify and document the sorting of pyflake messages
Simon Heimberg <simohe@besonet.ch>
parents:
14209
diff
changeset
|
26 |
14173
419539ea79cb
test-pyflake: improve sorting algorithm
timeless <timeless@mozdev.org>
parents:
14140
diff
changeset
|
27 |
419539ea79cb
test-pyflake: improve sorting algorithm
timeless <timeless@mozdev.org>
parents:
14140
diff
changeset
|
28 lines = [] |
14140
82f0412ef7de
tests: add pyflakes checking for unused imports
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
29 for line in sys.stdin: |
19335
77440de177f7
tests: simplify and document the sorting of pyflake messages
Simon Heimberg <simohe@besonet.ch>
parents:
14209
diff
changeset
|
30 # We whitelist tests (see more messages in pyflakes.messages) |
14175
b452abffcb15
tests: add pyflakes checking for assigned to but never used
timeless <timeless@mozdev.org>
parents:
14173
diff
changeset
|
31 pats = [ |
b452abffcb15
tests: add pyflakes checking for assigned to but never used
timeless <timeless@mozdev.org>
parents:
14173
diff
changeset
|
32 r"imported but unused", |
b452abffcb15
tests: add pyflakes checking for assigned to but never used
timeless <timeless@mozdev.org>
parents:
14173
diff
changeset
|
33 r"local variable '.*' is assigned to but never used", |
14176
dea68bddfb87
test: add pyflakes checking for unable to detect undefined names
timeless <timeless@mozdev.org>
parents:
14175
diff
changeset
|
34 r"unable to detect undefined names", |
14175
b452abffcb15
tests: add pyflakes checking for assigned to but never used
timeless <timeless@mozdev.org>
parents:
14173
diff
changeset
|
35 ] |
19335
77440de177f7
tests: simplify and document the sorting of pyflake messages
Simon Heimberg <simohe@besonet.ch>
parents:
14209
diff
changeset
|
36 for msgtype, pat in enumerate(pats): |
77440de177f7
tests: simplify and document the sorting of pyflake messages
Simon Heimberg <simohe@besonet.ch>
parents:
14209
diff
changeset
|
37 if re.search(pat, line): |
77440de177f7
tests: simplify and document the sorting of pyflake messages
Simon Heimberg <simohe@besonet.ch>
parents:
14209
diff
changeset
|
38 break # pattern matches |
77440de177f7
tests: simplify and document the sorting of pyflake messages
Simon Heimberg <simohe@besonet.ch>
parents:
14209
diff
changeset
|
39 else: |
77440de177f7
tests: simplify and document the sorting of pyflake messages
Simon Heimberg <simohe@besonet.ch>
parents:
14209
diff
changeset
|
40 continue # no pattern matched, next line |
14209
08d84bdce1a5
pyflakes: ignore files marked no-check-code
Augie Fackler <durin42@gmail.com>
parents:
14176
diff
changeset
|
41 fn = line.split(':', 1)[0] |
08d84bdce1a5
pyflakes: ignore files marked no-check-code
Augie Fackler <durin42@gmail.com>
parents:
14176
diff
changeset
|
42 f = open(os.path.join(os.path.dirname(os.path.dirname(__file__)), fn)) |
08d84bdce1a5
pyflakes: ignore files marked no-check-code
Augie Fackler <durin42@gmail.com>
parents:
14176
diff
changeset
|
43 data = f.read() |
08d84bdce1a5
pyflakes: ignore files marked no-check-code
Augie Fackler <durin42@gmail.com>
parents:
14176
diff
changeset
|
44 f.close() |
19381
e033a7d444ac
tests: do not skip code-checking on some whole files
Simon Heimberg <simohe@besonet.ch>
parents:
19335
diff
changeset
|
45 if 'no-' 'check-code' in data: |
14209
08d84bdce1a5
pyflakes: ignore files marked no-check-code
Augie Fackler <durin42@gmail.com>
parents:
14176
diff
changeset
|
46 continue |
19335
77440de177f7
tests: simplify and document the sorting of pyflake messages
Simon Heimberg <simohe@besonet.ch>
parents:
14209
diff
changeset
|
47 lines.append((msgtype, line)) |
14173
419539ea79cb
test-pyflake: improve sorting algorithm
timeless <timeless@mozdev.org>
parents:
14140
diff
changeset
|
48 |
19872
681f7b9213a4
check-code: check for spaces around = for named parameters
Mads Kiilerich <madski@unity3d.com>
parents:
19381
diff
changeset
|
49 for msgtype, line in sorted(lines, key=makekey): |
14140
82f0412ef7de
tests: add pyflakes checking for unused imports
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
50 sys.stdout.write(line) |
82f0412ef7de
tests: add pyflakes checking for unused imports
timeless <timeless@mozdev.org>
parents:
diff
changeset
|
51 print |