Welcome to mirror list, hosted at ThFree Co, Russian Federation.

pycompat.py « util « sphinx - github.com/sphinx-doc/sphinx.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 664387cacfbeaa11a61ccf94833e1e9d2fd89397 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
"""
    sphinx.util.pycompat
    ~~~~~~~~~~~~~~~~~~~~

    Stuff for Python version compatibility.

    :copyright: Copyright 2007-2020 by the Sphinx team, see AUTHORS.
    :license: BSD, see LICENSE for details.
"""

import html
import io
import sys
import textwrap
import warnings
from typing import Any, Callable

from sphinx.deprecation import RemovedInSphinx40Warning, deprecated_alias
from sphinx.locale import __
from sphinx.util import logging
from sphinx.util.console import terminal_safe
from sphinx.util.typing import NoneType


logger = logging.getLogger(__name__)


# ------------------------------------------------------------------------------
# Python 2/3 compatibility

# convert_with_2to3():
# support for running 2to3 over config files
def convert_with_2to3(filepath: str) -> str:
    try:
        from lib2to3.refactor import RefactoringTool, get_fixers_from_package
        from lib2to3.pgen2.parse import ParseError
    except ImportError:
        # python 3.9.0a6+ emits PendingDeprecationWarning for lib2to3.
        # Additionally, removal of the module is still discussed at PEP-594.
        # To support future python, this catches ImportError for lib2to3.
        raise SyntaxError

    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)


class UnicodeMixin:
    """Mixin class to handle defining the proper __str__/__unicode__
    methods in Python 2 or 3.

    .. deprecated:: 2.0
    """
    def __str__(self) -> str:
        warnings.warn('UnicodeMixin is deprecated',
                      RemovedInSphinx40Warning, stacklevel=2)
        return self.__unicode__()  # type: ignore


def execfile_(filepath: str, _globals: Any, open: Callable = open) -> None:
    from sphinx.util.osutil import fs_encoding
    with open(filepath, 'rb') as f:
        source = f.read()

    # compile to a code object, handle syntax errors
    filepath_enc = filepath.encode(fs_encoding)
    try:
        code = compile(source, filepath_enc, 'exec')
    except SyntaxError:
        # 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')
        # TODO: When support for evaluating Python 2 syntax is removed,
        # deprecate convert_with_2to3().
        logger.warning(__('Support for evaluating Python 2 syntax is deprecated '
                          'and will be removed in Sphinx 4.0. '
                          'Convert %s to Python 3 syntax.'),
                       filepath)
    exec(code, _globals)


deprecated_alias('sphinx.util.pycompat',
                 {
                     'NoneType': NoneType,
                     'TextIOWrapper': io.TextIOWrapper,
                     'htmlescape': html.escape,
                     'indent': textwrap.indent,
                     'terminal_safe': terminal_safe,
                     'sys_encoding': sys.getdefaultencoding(),
                     'u': '',
                 },
                 RemovedInSphinx40Warning)