diff options
author | Georg Brandl <georg@python.org> | 2010-10-29 05:30:17 (GMT) |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2010-10-29 05:30:17 (GMT) |
commit | a17fd1f3b5e731f0d942e75bfc7d82bee0c65b68 (patch) | |
tree | 9fbff5939f3e1a22c642f15da7ea54e5e325d94b /Doc/tools | |
parent | b98273fff90cf80c14cd13e03dc26483b4681af4 (diff) | |
download | cpython-a17fd1f3b5e731f0d942e75bfc7d82bee0c65b68.zip cpython-a17fd1f3b5e731f0d942e75bfc7d82bee0c65b68.tar.gz cpython-a17fd1f3b5e731f0d942e75bfc7d82bee0c65b68.tar.bz2 |
Port suspicious markup builder and patchlevel.py so that they can be used with Python 2 and 3 without conversion.
Diffstat (limited to 'Doc/tools')
-rw-r--r-- | Doc/tools/sphinxext/patchlevel.py | 2 | ||||
-rw-r--r-- | Doc/tools/sphinxext/suspicious.py | 52 |
2 files changed, 37 insertions, 17 deletions
diff --git a/Doc/tools/sphinxext/patchlevel.py b/Doc/tools/sphinxext/patchlevel.py index 082858e..b070d60 100644 --- a/Doc/tools/sphinxext/patchlevel.py +++ b/Doc/tools/sphinxext/patchlevel.py @@ -68,4 +68,4 @@ def get_version_info(): return version, release if __name__ == '__main__': - print get_header_version_info('.')[1] + print(get_header_version_info('.')[1]) diff --git a/Doc/tools/sphinxext/suspicious.py b/Doc/tools/sphinxext/suspicious.py index f15e931b..888b231 100644 --- a/Doc/tools/sphinxext/suspicious.py +++ b/Doc/tools/sphinxext/suspicious.py @@ -49,13 +49,15 @@ import sys from docutils import nodes from sphinx.builders import Builder -detect_all = re.compile(ur''' +detect_all = re.compile(r''' ::(?=[^=])| # two :: (but NOT ::=) :[a-zA-Z][a-zA-Z0-9]+| # :foo `| # ` (seldom used by itself) (?<!\.)\.\.[ \t]*\w+: # .. foo: (but NOT ... else:) ''', re.UNICODE | re.VERBOSE).finditer +py3 = sys.version_info >= (3, 0) + class Rule: def __init__(self, docname, lineno, issue, line): @@ -136,7 +138,11 @@ class CheckSuspiciousMarkupBuilder(Builder): if not self.any_issue: self.info() self.any_issue = True self.write_log_entry(lineno, issue, text) - self.warn('[%s:%d] "%s" found in "%-.120s"' % ( + if py3: + self.warn('[%s:%d] "%s" found in "%-.120s"' % + (self.docname, lineno, issue, text)) + else: + self.warn('[%s:%d] "%s" found in "%-.120s"' % ( self.docname.encode(sys.getdefaultencoding(),'replace'), lineno, issue.encode(sys.getdefaultencoding(),'replace'), @@ -144,13 +150,19 @@ class CheckSuspiciousMarkupBuilder(Builder): self.app.statuscode = 1 def write_log_entry(self, lineno, issue, text): - f = open(self.log_file_name, 'ab') - writer = csv.writer(f, dialect) - writer.writerow([self.docname.encode('utf-8'), - lineno, - issue.encode('utf-8'), - text.strip().encode('utf-8')]) - f.close() + if py3: + f = open(self.log_file_name, 'a') + writer = csv.writer(f, dialect) + writer.writerow([self.docname, lineno, issue, text.strip()]) + f.close() + else: + f = open(self.log_file_name, 'ab') + writer = csv.writer(f, dialect) + writer.writerow([self.docname.encode('utf-8'), + lineno, + issue.encode('utf-8'), + text.strip().encode('utf-8')]) + f.close() def load_rules(self, filename): """Load database of previously ignored issues. @@ -160,18 +172,26 @@ class CheckSuspiciousMarkupBuilder(Builder): """ self.info("loading ignore rules... ", nonl=1) self.rules = rules = [] - try: f = open(filename, 'rb') - except IOError: return + try: + if py3: + f = open(filename, 'r') + else: + f = open(filename, 'rb') + except IOError: + return for i, row in enumerate(csv.reader(f)): if len(row) != 4: raise ValueError( "wrong format in %s, line %d: %s" % (filename, i+1, row)) docname, lineno, issue, text = row - docname = docname.decode('utf-8') - if lineno: lineno = int(lineno) - else: lineno = None - issue = issue.decode('utf-8') - text = text.decode('utf-8') + if lineno: + lineno = int(lineno) + else: + lineno = None + if not py3: + docname = docname.decode('utf-8') + issue = issue.decode('utf-8') + text = text.decode('utf-8') rule = Rule(docname, lineno, issue, text) rules.append(rule) f.close() |