Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f412899
commit cbe7495
Showing
6 changed files
with
67 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,63 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Python compatibility functions. | ||
""" | ||
|
||
import sys | ||
import os | ||
from six import PY3, exec_, b | ||
|
||
FS_ENCODING = sys.getfilesystemencoding() or sys.getdefaultencoding() | ||
|
||
|
||
if sys.version_info >= (2, 6): | ||
# Python >= 2.6 | ||
relpath = os.path.relpath | ||
else: | ||
from os.path import curdir | ||
|
||
def relpath(path, start=curdir): | ||
"""Return a relative version of a path""" | ||
from os.path import sep, abspath, commonprefix, join, pardir | ||
|
||
if not path: | ||
raise ValueError("no path specified") | ||
import warnings | ||
|
||
start_list = abspath(start).split(sep) | ||
path_list = abspath(path).split(sep) | ||
if False: | ||
from typing import Any, Callable # NOQA | ||
|
||
# Work out how much of the filepath is shared by start and path. | ||
i = len(commonprefix([start_list, path_list])) | ||
|
||
rel_list = [pardir] * (len(start_list)-i) + path_list[i:] | ||
if not rel_list: | ||
return start | ||
return join(*rel_list) | ||
del curdir | ||
# ------------------------------------------------------------------------------ | ||
# Python 2/3 compatibility | ||
fs_encoding = sys.getfilesystemencoding() or sys.getdefaultencoding() | ||
|
||
if PY3: | ||
def convert_with_2to3(filepath): | ||
from lib2to3.refactor import RefactoringTool, get_fixers_from_package | ||
from lib2to3.pgen2.parse import ParseError | ||
fixers = get_fixers_from_package('lib2to3.fixes') | ||
refactoring_tool = RefactoringTool(fixers) | ||
source = refactoring_tool._read_python_source(filepath)[0] | ||
try: | ||
tree = refactoring_tool.refactor_string(source, 'conf.py') | ||
except ParseError: | ||
typ, err, tb = sys.exc_info() | ||
# do not propagate lib2to3 exceptions | ||
lineno, offset = err.context[1] | ||
# try to match ParseError details with SyntaxError details | ||
raise SyntaxError(err.msg, (filepath, lineno, offset, err.value)) | ||
return str(tree) | ||
else: | ||
convert_with_2to3 = None | ||
|
||
|
||
def execfile_(filepath, _globals): | ||
# get config source -- 'b' is a no-op under 2.x, while 'U' is | ||
# ignored under 3.x (but 3.x compile() accepts \r\n newlines) | ||
f = open(filepath, 'rbU') | ||
def relpath(path, start=os.curdir): | ||
# type: (str, str) -> str | ||
"""Return a relative version of a path""" | ||
try: | ||
return os.path.relpath(path, start) | ||
except ValueError: | ||
return path | ||
|
||
|
||
# convert_with_2to3(): | ||
# support for running 2to3 over config files | ||
def convert_with_2to3(filepath): | ||
# type: (str) -> str | ||
from lib2to3.refactor import RefactoringTool, get_fixers_from_package | ||
from lib2to3.pgen2.parse import ParseError | ||
fixers = get_fixers_from_package('lib2to3.fixes') | ||
refactoring_tool = RefactoringTool(fixers) | ||
source = refactoring_tool._read_python_source(filepath)[0] | ||
try: | ||
tree = refactoring_tool.refactor_string(source, 'conf.py') | ||
except ParseError as err: | ||
# do not propagate lib2to3 exceptions | ||
lineno, offset = err.context[1] | ||
# try to match ParseError details with SyntaxError details | ||
raise SyntaxError(err.msg, (filepath, lineno, offset, err.value)) | ||
return str(tree) | ||
|
||
|
||
def execfile_(filepath, _globals, open=open): | ||
# type: (str, Any, Callable) -> None | ||
with open(filepath, 'rb') as f: | ||
source = f.read() | ||
finally: | ||
f.close() | ||
|
||
# py25,py26,py31 accept only LF eol instead of CRLF | ||
if sys.version_info[:2] in ((2, 5), (2, 6), (3, 1)): | ||
source = source.replace(b('\r\n'), b('\n')) | ||
|
||
# compile to a code object, handle syntax errors | ||
filepath_enc = filepath.encode(FS_ENCODING) | ||
filepath_enc = filepath.encode(fs_encoding) | ||
try: | ||
code = compile(source, filepath_enc, 'exec') | ||
except SyntaxError: | ||
if convert_with_2to3: | ||
# maybe the file uses 2.x syntax; try to refactor to | ||
# 3.x syntax using 2to3 | ||
source = convert_with_2to3(filepath) | ||
code = compile(source, filepath_enc, 'exec') | ||
else: | ||
raise | ||
|
||
exec_(code, _globals) | ||
# maybe the file uses 2.x syntax; try to refactor to | ||
# 3.x syntax using 2to3 | ||
source = convert_with_2to3(filepath) | ||
code = compile(source, filepath_enc, 'exec') | ||
warnings.warn('Support for evaluating Python 2 syntax is deprecated ' | ||
'and will be removed in sphinx-intl 4.0. ' | ||
'Convert %s to Python 3 syntax.', | ||
filepath) | ||
exec(code, _globals) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
if False: | ||
from typing import Iterator, List | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters