summaryrefslogtreecommitdiff
path: root/sphinx/pycode
diff options
context:
space:
mode:
Diffstat (limited to 'sphinx/pycode')
-rw-r--r--sphinx/pycode/__init__.py24
1 files changed, 17 insertions, 7 deletions
diff --git a/sphinx/pycode/__init__.py b/sphinx/pycode/__init__.py
index c908b076e..1a2581dce 100644
--- a/sphinx/pycode/__init__.py
+++ b/sphinx/pycode/__init__.py
@@ -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:
@@ -82,8 +84,8 @@ class 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:
@@ -130,11 +132,13 @@ class ModuleAnalyzer:
# 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()
@@ -145,7 +149,7 @@ class ModuleAnalyzer:
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 = {}
@@ -173,3 +177,9 @@ class ModuleAnalyzer:
self.parse()
return self.tags
+
+ @property
+ def encoding(self) -> str:
+ warnings.warn('ModuleAnalyzer.encoding is deprecated.',
+ RemovedInSphinx40Warning)
+ return self._encoding