Skip to content

Commit

Permalink
Merge pull request #6928 from tk0miya/deprecate_detect_encoding
Browse files Browse the repository at this point in the history
Deprecate sphinx.util:detect_encoding()
  • Loading branch information
tk0miya committed Dec 27, 2019
2 parents 402d011 + c8074b4 commit 858d5ff
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 12 deletions.
3 changes: 3 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ Incompatible changes
Deprecated
----------

* The ``decode`` argument of ``sphinx.pycode.ModuleAnalyzer()``
* ``sphinx.environment.BuildEnvironment.indexentries``
* ``sphinx.environment.collectors.indexentries.IndexEntriesCollector``
* ``sphinx.io.FiletypeNotFoundError``
* ``sphinx.io.get_filetype()``
* ``sphinx.pycode.ModuleAnalyzer.encoding``
* ``sphinx.util.detect_encoding()``
* ``sphinx.util.get_module_source()``

Features added
Expand Down
15 changes: 15 additions & 0 deletions doc/extdev/deprecated.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ The following is a list of deprecated interfaces.
- (will be) Removed
- Alternatives

* - ``decode`` argument of ``sphinx.pycode.ModuleAnalyzer()``
- 2.4
- 4.0
- N/A

* - ``sphinx.environment.BuildEnvironment.indexentries``
- 2.4
- 4.0
Expand All @@ -51,6 +56,16 @@ The following is a list of deprecated interfaces.
- 4.0
- N/A

* - ``sphinx.pycode.ModuleAnalyzer.encoding``
- 2.4
- 4.0
- N/A

* - ``sphinx.util.detect_encoding()``
- 2.4
- 4.0
- ``tokenize.detect_encoding()``

* - ``sphinx.builders.gettext.POHEADER``
- 2.3
- 4.0
Expand Down
24 changes: 17 additions & 7 deletions sphinx/pycode/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@
"""

import re
import tokenize
import warnings
from importlib import import_module
from io import StringIO
from os import path
from typing import Any, Dict, IO, List, Tuple, Optional
from zipfile import ZipFile

from sphinx.deprecation import RemovedInSphinx40Warning
from sphinx.errors import PycodeError
from sphinx.pycode.parser import Parser
from sphinx.util import detect_encoding


class ModuleAnalyzer:
Expand Down Expand Up @@ -82,8 +84,8 @@ def for_file(cls, filename: str, modname: str) -> "ModuleAnalyzer":
if ('file', filename) in cls.cache:
return cls.cache['file', filename]
try:
with open(filename, 'rb') as f:
obj = cls(f, modname, filename)
with tokenize.open(filename) as f:
obj = cls(f, modname, filename, decoded=True)
cls.cache['file', filename] = obj
except Exception as err:
if '.egg' + path.sep in filename:
Expand Down Expand Up @@ -130,11 +132,13 @@ def __init__(self, source: IO, modname: str, srcname: str, decoded: bool = False
# cache the source code as well
pos = source.tell()
if not decoded:
self.encoding = detect_encoding(source.readline)
warnings.warn('decode option for ModuleAnalyzer is deprecated.',
RemovedInSphinx40Warning)
self._encoding, _ = tokenize.detect_encoding(source.readline)
source.seek(pos)
self.code = source.read().decode(self.encoding)
self.code = source.read().decode(self._encoding)
else:
self.encoding = None
self._encoding = None
self.code = source.read()

# will be filled by parse()
Expand All @@ -145,7 +149,7 @@ def __init__(self, source: IO, modname: str, srcname: str, decoded: bool = False
def parse(self) -> None:
"""Parse the source code."""
try:
parser = Parser(self.code, self.encoding)
parser = Parser(self.code, self._encoding)
parser.parse()

self.attr_docs = {}
Expand Down Expand Up @@ -173,3 +177,9 @@ def find_tags(self) -> Dict[str, Tuple[str, int, int]]:
self.parse()

return self.tags

@property
def encoding(self) -> str:
warnings.warn('ModuleAnalyzer.encoding is deprecated.',
RemovedInSphinx40Warning)
return self._encoding
2 changes: 2 additions & 0 deletions sphinx/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,8 @@ def get_full_modname(modname: str, attribute: str) -> str:

def detect_encoding(readline: Callable[[], bytes]) -> str:
"""Like tokenize.detect_encoding() from Py3k, but a bit simplified."""
warnings.warn('sphinx.util.detect_encoding() is deprecated',
RemovedInSphinx40Warning)

def read_or_stop() -> bytes:
try:
Expand Down
6 changes: 1 addition & 5 deletions tests/test_pycode.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,20 @@ def test_ModuleAnalyzer_for_string():
analyzer = ModuleAnalyzer.for_string('print("Hello world")', 'module_name')
assert analyzer.modname == 'module_name'
assert analyzer.srcname == '<string>'
assert analyzer.encoding is None


def test_ModuleAnalyzer_for_file():
analyzer = ModuleAnalyzer.for_string(SPHINX_MODULE_PATH, 'sphinx')
assert analyzer.modname == 'sphinx'
assert analyzer.srcname == '<string>'
assert analyzer.encoding is None


def test_ModuleAnalyzer_for_module(rootdir):
analyzer = ModuleAnalyzer.for_module('sphinx')
assert analyzer.modname == 'sphinx'
assert analyzer.srcname in (SPHINX_MODULE_PATH,
os.path.abspath(SPHINX_MODULE_PATH))
# source should be loaded via native loader, so don`t know file enconding
assert analyzer.encoding == None

path = rootdir / 'test-pycode'
sys.path.insert(0, path)
try:
Expand All @@ -57,7 +54,6 @@ def test_ModuleAnalyzer_for_module(rootdir):
assert docs == {('', 'X'): ['It MUST look like X="\u0425"', '']}
finally:
sys.path.pop(0)



def test_ModuleAnalyzer_for_file_in_egg(rootdir):
Expand Down

0 comments on commit 858d5ff

Please sign in to comment.