From ba4dcaa8dd5eeec72940a3aebce80d756bcb921d Mon Sep 17 00:00:00 2001 From: Takayuki Shimizukawa Date: Thu, 1 May 2014 22:54:09 +0900 Subject: [PATCH] use b prefix for bytes instead of sphinx.pycompat.b function. --- sphinx/builders/html.py | 3 +-- sphinx/config.py | 4 +-- sphinx/ext/intersphinx.py | 9 +++---- sphinx/ext/pngmath.py | 4 +-- sphinx/util/png.py | 5 ++-- sphinx/util/pycompat.py | 6 +---- tests/test_config.py | 5 ++-- tests/test_markup.py | 3 +-- tests/test_search.py | 3 +-- utils/check_sources.py | 56 +++++++++++++++++---------------------- 10 files changed, 41 insertions(+), 57 deletions(-) diff --git a/sphinx/builders/html.py b/sphinx/builders/html.py index 15e848029..eeda60317 100644 --- a/sphinx/builders/html.py +++ b/sphinx/builders/html.py @@ -32,7 +32,6 @@ from sphinx.util.osutil import SEP, os_path, relative_uri, ensuredir, \ movefile, ustrftime, copyfile from sphinx.util.nodes import inline_all_toctrees from sphinx.util.matching import patmatch, compile_matchers -from sphinx.util.pycompat import b from sphinx.locale import _ from sphinx.search import js_index from sphinx.theming import Theme @@ -221,7 +220,7 @@ class StandaloneHTMLBuilder(Builder): """Utility: Render a lone doctree node.""" if node is None: return {'fragment': ''} - doc = new_document(b('')) + doc = new_document(b'') doc.append(node) if self._publisher is None: diff --git a/sphinx/config.py b/sphinx/config.py index 7b35f8ab5..1b67f89a2 100644 --- a/sphinx/config.py +++ b/sphinx/config.py @@ -18,9 +18,9 @@ from six import PY3, iteritems, string_types, binary_type, integer_types from sphinx.errors import ConfigError from sphinx.locale import l_ from sphinx.util.osutil import make_filename -from sphinx.util.pycompat import b, execfile_ +from sphinx.util.pycompat import execfile_ -nonascii_re = re.compile(b(r'[\x80-\xff]')) +nonascii_re = re.compile(br'[\x80-\xff]') CONFIG_SYNTAX_ERROR = "There is a syntax error in your configuration file: %s" if PY3: diff --git a/sphinx/ext/intersphinx.py b/sphinx/ext/intersphinx.py index 772b1e7f3..6dcd3ea13 100644 --- a/sphinx/ext/intersphinx.py +++ b/sphinx/ext/intersphinx.py @@ -38,7 +38,6 @@ from docutils.utils import relative_path from sphinx.locale import _ from sphinx.builders.html import INVENTORY_FILENAME -from sphinx.util.pycompat import b handlers = [request.ProxyHandler(), request.HTTPRedirectHandler(), @@ -86,19 +85,19 @@ def read_inventory_v2(f, uri, join, bufsize=16*1024): def read_chunks(): decompressor = zlib.decompressobj() - for chunk in iter(lambda: f.read(bufsize), b('')): + for chunk in iter(lambda: f.read(bufsize), b''): yield decompressor.decompress(chunk) yield decompressor.flush() def split_lines(iter): - buf = b('') + buf = b'' for chunk in iter: buf += chunk - lineend = buf.find(b('\n')) + lineend = buf.find(b'\n') while lineend != -1: yield buf[:lineend].decode('utf-8') buf = buf[lineend+1:] - lineend = buf.find(b('\n')) + lineend = buf.find(b'\n') assert not buf for line in split_lines(read_chunks()): diff --git a/sphinx/ext/pngmath.py b/sphinx/ext/pngmath.py index 9bc1e546a..e0fed6e0d 100644 --- a/sphinx/ext/pngmath.py +++ b/sphinx/ext/pngmath.py @@ -27,7 +27,7 @@ from docutils import nodes from sphinx.errors import SphinxError from sphinx.util.png import read_png_depth, write_png_depth from sphinx.util.osutil import ensuredir, ENOENT -from sphinx.util.pycompat import b, sys_encoding +from sphinx.util.pycompat import sys_encoding from sphinx.ext.mathbase import setup_math as mathbase_setup, wrap_displaymath class MathExtError(SphinxError): @@ -67,7 +67,7 @@ DOC_BODY_PREVIEW = r''' \end{document} ''' -depth_re = re.compile(b(r'\[\d+ depth=(-?\d+)\]')) +depth_re = re.compile(br'\[\d+ depth=(-?\d+)\]') def render_math(self, math): """Render the LaTeX math expression *math* using latex and dvipng. diff --git a/sphinx/util/png.py b/sphinx/util/png.py index 65fc4d8d2..397adb245 100644 --- a/sphinx/util/png.py +++ b/sphinx/util/png.py @@ -12,14 +12,13 @@ import struct import binascii -from sphinx.util.pycompat import b LEN_IEND = 12 LEN_DEPTH = 22 DEPTH_CHUNK_LEN = struct.pack('!i', 10) -DEPTH_CHUNK_START = b('tEXtDepth\x00') -IEND_CHUNK = b('\x00\x00\x00\x00IEND\xAE\x42\x60\x82') +DEPTH_CHUNK_START = b'tEXtDepth\x00' +IEND_CHUNK = b'\x00\x00\x00\x00IEND\xAE\x42\x60\x82' def read_png_depth(filename): diff --git a/sphinx/util/pycompat.py b/sphinx/util/pycompat.py index af2037d08..5031dd9b5 100644 --- a/sphinx/util/pycompat.py +++ b/sphinx/util/pycompat.py @@ -19,9 +19,6 @@ from six import PY3, text_type, exec_ if PY3: # Python 3 - # the ubiquitous "bytes" helper functions - def b(s): - return s.encode('utf-8') # prefix for Unicode strings u = '' from io import TextIOWrapper @@ -57,7 +54,6 @@ if PY3: else: # Python 2 - b = str u = 'u' # no need to refactor on 2.x versions convert_with_2to3 = None @@ -92,7 +88,7 @@ def execfile_(filepath, _globals): # py26 accept only LF eol instead of CRLF if sys.version_info[:2] == (2, 6): - source = source.replace(b('\r\n'), b('\n')) + source = source.replace(b'\r\n', b'\n') # compile to a code object, handle syntax errors filepath_enc = filepath.encode(fs_encoding) diff --git a/tests/test_config.py b/tests/test_config.py index eaecb066e..d4bf24cf5 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -15,7 +15,6 @@ from util import TestApp, with_app, with_tempdir, raises, raises_msg from sphinx.config import Config from sphinx.errors import ExtensionError, ConfigError, VersionRequirementError -from sphinx.util.pycompat import b @with_app(confoverrides={'master_doc': 'master', 'nonexisting_value': 'True', @@ -122,8 +121,8 @@ def test_needs_sphinx(): def test_config_eol(tmpdir): # test config file's eol patterns: LF, CRLF configfile = tmpdir / 'conf.py' - for eol in ('\n', '\r\n'): - configfile.write_bytes(b('project = "spam"' + eol)) + for eol in (b'\n', b'\r\n'): + configfile.write_bytes(b'project = "spam"' + eol) cfg = Config(tmpdir, 'conf.py', {}, None) cfg.init_values(lambda warning: 1/0) assert cfg.project == u'spam' diff --git a/tests/test_markup.py b/tests/test_markup.py index f84aa3714..e58cfe688 100644 --- a/tests/test_markup.py +++ b/tests/test_markup.py @@ -15,7 +15,6 @@ from docutils import frontend, utils, nodes from docutils.parsers import rst from sphinx.util import texescape -from sphinx.util.pycompat import b from sphinx.writers.html import HTMLWriter, SmartyPantsHTMLTranslator from sphinx.writers.latex import LaTeXWriter, LaTeXTranslator @@ -54,7 +53,7 @@ class ForgivingLaTeXTranslator(LaTeXTranslator, ForgivingTranslator): def verify_re(rst, html_expected, latex_expected): - document = utils.new_document(b('test data'), settings) + document = utils.new_document(b'test data', settings) document['file'] = 'dummy' parser.parse(rst, document) for msg in document.traverse(nodes.system_message): diff --git a/tests/test_search.py b/tests/test_search.py index 2efd753cc..a7e99e042 100644 --- a/tests/test_search.py +++ b/tests/test_search.py @@ -13,7 +13,6 @@ from docutils import frontend, utils from docutils.parsers import rst from sphinx.search import IndexBuilder -from sphinx.util.pycompat import b settings = parser = None @@ -32,7 +31,7 @@ test that non-comments are indexed: fermion ''' def test_wordcollector(): - doc = utils.new_document(b('test data'), settings) + doc = utils.new_document(b'test data', settings) doc['file'] = 'dummy' parser.parse(FILE_CONTENTS, doc) diff --git a/utils/check_sources.py b/utils/check_sources.py index 6b984ad6d..1b78ab62c 100755 --- a/utils/check_sources.py +++ b/utils/check_sources.py @@ -17,12 +17,6 @@ import cStringIO from optparse import OptionParser from os.path import join, splitext, abspath -if sys.version_info >= (3, 0): - def b(s): - return s.encode('utf-8') -else: - b = str - checkers = {} @@ -37,24 +31,24 @@ def checker(*suffixes, **kwds): name_mail_re = r'[\w ]+(<.*?>)?' -copyright_re = re.compile(b(r'^ :copyright: Copyright 200\d(-20\d\d)? ' - r'by %s(, %s)*[,.]$' % - (name_mail_re, name_mail_re))) -license_re = re.compile(b(r" :license: (.*?).\n")) -copyright_2_re = re.compile(b(r'^ %s(, %s)*[,.]$' % - (name_mail_re, name_mail_re))) -coding_re = re.compile(b(r'coding[:=]\s*([-\w.]+)')) -not_ix_re = re.compile(b(r'\bnot\s+\S+?\s+i[sn]\s\S+')) -is_const_re = re.compile(b(r'if.*?==\s+(None|False|True)\b')) +copyright_re = re.compile(br'^ :copyright: Copyright 200\d(-20\d\d)? ' + br'by %s(, %s)*[,.]$' % + (name_mail_re, name_mail_re)) +license_re = re.compile(br" :license: (.*?).\n") +copyright_2_re = re.compile(br'^ %s(, %s)*[,.]$' % + (name_mail_re, name_mail_re)) +coding_re = re.compile(br'coding[:=]\s*([-\w.]+)') +not_ix_re = re.compile(br'\bnot\s+\S+?\s+i[sn]\s\S+') +is_const_re = re.compile(br'if.*?==\s+(None|False|True)\b') -misspellings = [b("developement"), b("adress"), # ALLOW-MISSPELLING - b("verificate"), b("informations")] # ALLOW-MISSPELLING +misspellings = [b"developement", b"adress", # ALLOW-MISSPELLING + b"verificate", b"informations"] # ALLOW-MISSPELLING if sys.version_info < (3, 0): @checker('.py') def check_syntax(fn, lines): try: - compile(b('').join(lines), fn, "exec") + compile(b''.join(lines), fn, "exec") except SyntaxError as err: yield 0, "not compilable: %s" % err @@ -69,7 +63,7 @@ def check_style_and_encoding(fn, lines): co = coding_re.search(line) if co: encoding = co.group(1).decode('ascii') - if line.strip().startswith(b('#')): + if line.strip().startswith(b'#'): continue #m = not_ix_re.search(line) #if m: @@ -89,7 +83,7 @@ def check_style_and_encoding(fn, lines): def check_fileheader(fn, lines): # line number correction c = 1 - if lines[0:1] == [b('#!/usr/bin/env python\n')]: + if lines[0:1] == [b'#!/usr/bin/env python\n']: lines = lines[1:] c = 2 @@ -98,38 +92,38 @@ def check_fileheader(fn, lines): for lno, l in enumerate(lines): llist.append(l) if lno == 0: - if l == b('# -*- coding: rot13 -*-\n'): + if l == b'# -*- coding: rot13 -*-\n': # special-case pony package return - elif l != b('# -*- coding: utf-8 -*-\n'): + elif l != b'# -*- coding: utf-8 -*-\n': yield 1, "missing coding declaration" elif lno == 1: - if l != b('"""\n') and l != b('r"""\n'): + if l != b'"""\n' and l != b'r"""\n': yield 2, 'missing docstring begin (""")' else: docopen = True elif docopen: - if l == b('"""\n'): + if l == b'"""\n': # end of docstring if lno <= 4: yield lno+c, "missing module name in docstring" break - if l != b("\n") and l[:4] != b(' ') and docopen: + if l != b"\n" and l[:4] != b' ' and docopen: yield lno+c, "missing correct docstring indentation" if lno == 2: # if not in package, don't check the module name modname = fn[:-3].replace('/', '.').replace('.__init__', '') while modname: - if l.lower()[4:-1] == b(modname): + if l.lower()[4:-1] == bytes(modname): break modname = '.'.join(modname.split('.')[1:]) else: yield 3, "wrong module name in docstring heading" modnamelen = len(l.strip()) elif lno == 3: - if l.strip() != modnamelen * b("~"): + if l.strip() != modnamelen * b"~": yield 4, "wrong module name underline, should be ~~~...~" else: @@ -152,16 +146,16 @@ def check_fileheader(fn, lines): @checker('.py', '.html', '.rst') def check_whitespace_and_spelling(fn, lines): for lno, line in enumerate(lines): - if b("\t") in line: + if b"\t" in line: yield lno+1, "OMG TABS!!!1 " - if line[:-1].rstrip(b(' \t')) != line[:-1]: + if line[:-1].rstrip(b' \t') != line[:-1]: yield lno+1, "trailing whitespace" for word in misspellings: - if word in line and b('ALLOW-MISSPELLING') not in line: + if word in line and b'ALLOW-MISSPELLING' not in line: yield lno+1, '"%s" used' % word -bad_tags = map(b, ['', '', '', '
', '', b'', b'', b'
', b'