Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(36628)

Side by Side Diff: Tools/scripts/findnocoding.py

Issue 18873: "Encoding" detected in non-comment lines
Patch Set: Created 6 years ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/tokenize.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/env python3 1 #!/usr/bin/env python3
2 2
3 """List all those Python files that require a coding directive 3 """List all those Python files that require a coding directive
4 4
5 Usage: findnocoding.py dir1 [dir2...] 5 Usage: findnocoding.py dir1 [dir2...]
6 """ 6 """
7 7
8 __author__ = "Oleg Broytmann, Georg Brandl" 8 __author__ = "Oleg Broytmann, Georg Brandl"
9 9
10 import sys, os, re, getopt 10 import sys, os, re, getopt
(...skipping 14 matching lines...) Expand all
25 for filename in files: 25 for filename in files:
26 if filename.endswith(".py"): 26 if filename.endswith(".py"):
27 yield os.path.join(root, filename) 27 yield os.path.join(root, filename)
28 pysource = pysource() 28 pysource = pysource()
29 29
30 30
31 print("The pysource module is not available; " 31 print("The pysource module is not available; "
32 "no sophisticated Python source file search will be don e.", file=sys.stderr) 32 "no sophisticated Python source file search will be don e.", file=sys.stderr)
33 33
34 34
35 decl_re = re.compile(rb"coding[=:]\s*([-\w.]+)") 35 decl_re = re.compile(rb'^[ \t\f]*#.*coding[:=][ \t]*([-\w.]+)')
36 36
37 def get_declaration(line): 37 def get_declaration(line):
38 match = decl_re.search(line) 38 match = decl_re.match(line)
39 if match: 39 if match:
40 return match.group(1) 40 return match.group(1)
41 return '' 41 return b''
42 42
43 def has_correct_encoding(text, codec): 43 def has_correct_encoding(text, codec):
44 try: 44 try:
45 str(text, codec) 45 str(text, codec)
46 except UnicodeDecodeError: 46 except UnicodeDecodeError:
47 return False 47 return False
48 else: 48 else:
49 return True 49 return True
50 50
51 def needs_declaration(fullpath): 51 def needs_declaration(fullpath):
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 if not args: 96 if not args:
97 print(usage, file=sys.stderr) 97 print(usage, file=sys.stderr)
98 sys.exit(1) 98 sys.exit(1)
99 99
100 for fullpath in pysource.walk_python_files(args, is_python): 100 for fullpath in pysource.walk_python_files(args, is_python):
101 if debug: 101 if debug:
102 print("Testing for coding: %s" % fullpath) 102 print("Testing for coding: %s" % fullpath)
103 result = needs_declaration(fullpath) 103 result = needs_declaration(fullpath)
104 if result: 104 if result:
105 print(fullpath) 105 print(fullpath)
OLDNEW
« no previous file with comments | « Lib/tokenize.py ('k') | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+