本文整理汇总了Python中tokenize.detect_encoding方法的典型用法代码示例。如果您正苦于以下问题:Python tokenize.detect_encoding方法的具体用法?Python tokenize.detect_encoding怎么用?Python tokenize.detect_encoding使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tokenize
的用法示例。
在下文中一共展示了tokenize.detect_encoding方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_source
# 需要导入模块: import tokenize [as 别名]
# 或者: from tokenize import detect_encoding [as 别名]
def get_source(self, fullname):
"""Concrete implementation of InspectLoader.get_source."""
import tokenize
path = self.get_filename(fullname)
try:
source_bytes = self.get_data(path)
except IOError as exc:
raise ImportError("source not available through get_data()",
name=fullname) from exc
readsource = _io.BytesIO(source_bytes).readline
try:
encoding = tokenize.detect_encoding(readsource)
except SyntaxError as exc:
raise ImportError("Failed to detect encoding",
name=fullname) from exc
newline_decoder = _io.IncrementalNewlineDecoder(None, True)
try:
return newline_decoder.decode(source_bytes.decode(encoding[0]))
except UnicodeDecodeError as exc:
raise ImportError("Failed to decode source file",
name=fullname) from exc
示例2: get_data
# 需要导入模块: import tokenize [as 别名]
# 或者: from tokenize import detect_encoding [as 别名]
def get_data(self, path):
"""Gross hack to contort loader to deal w/ load_*()'s bad API."""
if self.file and path == self.path:
if not self.file.closed:
file = self.file
else:
self.file = file = open(self.path, 'r')
with file:
# Technically should be returning bytes, but
# SourceLoader.get_code() just passed what is returned to
# compile() which can handle str. And converting to bytes would
# require figuring out the encoding to decode to and
# tokenize.detect_encoding() only accepts bytes.
return file.read()
else:
return super().get_data(path)
示例3: source_to_unicode
# 需要导入模块: import tokenize [as 别名]
# 或者: from tokenize import detect_encoding [as 别名]
def source_to_unicode(txt, errors='replace', skip_encoding_cookie=True):
"""Converts a bytes string with python source code to unicode.
Unicode strings are passed through unchanged. Byte strings are checked
for the python source file encoding cookie to determine encoding.
txt can be either a bytes buffer or a string containing the source
code.
"""
if isinstance(txt, unicode):
return txt
if isinstance(txt, bytes):
buffer = BytesIO(txt)
else:
buffer = txt
try:
encoding, _ = detect_encoding(buffer.readline)
except SyntaxError:
encoding = "ascii"
buffer.seek(0)
text = TextIOWrapper(buffer, encoding, errors=errors, line_buffering=True)
text.mode = 'r'
if skip_encoding_cookie:
return u"".join(strip_encoding_cookie(text))
else:
return text.read()
示例4: execfile
# 需要导入模块: import tokenize [as 别名]
# 或者: from tokenize import detect_encoding [as 别名]
def execfile(file, glob=None, loc=None):
if glob is None:
import sys
glob = sys._getframe().f_back.f_globals
if loc is None:
loc = glob
# It seems that the best way is using tokenize.open(): http://code.activestate.com/lists/python-dev/131251/
# (but tokenize.open() is only available for python 3.2)
import tokenize
if hasattr(tokenize, 'open'):
# version 3.2
stream = tokenize.open(file) # @UndefinedVariable
else:
# version 3.0 or 3.1
detect_encoding = tokenize.detect_encoding(open(file, mode="rb" ).readline)
stream = open(file, encoding=detect_encoding[0])
try:
contents = stream.read()
finally:
stream.close()
#execute the script (note: it's important to compile first to have the filename set in debug mode)
exec(compile(contents+"\n", file, 'exec'), glob, loc)
示例5: open_source_file
# 需要导入模块: import tokenize [as 别名]
# 或者: from tokenize import detect_encoding [as 别名]
def open_source_file(filename):
with open(filename, 'rb') as byte_stream:
encoding = detect_encoding(byte_stream.readline)[0]
stream = open(filename, 'r', newline=None, encoding=encoding)
data = stream.read()
return stream, encoding, data
示例6: file_build
# 需要导入模块: import tokenize [as 别名]
# 或者: from tokenize import detect_encoding [as 别名]
def file_build(self, path, modname=None):
"""Build astroid from a source code file (i.e. from an ast)
*path* is expected to be a python source file
"""
try:
stream, encoding, data = open_source_file(path)
except IOError as exc:
util.reraise(exceptions.AstroidBuildingError(
'Unable to load file {path}:\n{error}',
modname=modname, path=path, error=exc))
except (SyntaxError, LookupError) as exc:
util.reraise(exceptions.AstroidSyntaxError(
'Python 3 encoding specification error or unknown encoding:\n'
'{error}', modname=modname, path=path, error=exc))
except UnicodeError: # wrong encoding
# detect_encoding returns utf-8 if no encoding specified
util.reraise(exceptions.AstroidBuildingError(
'Wrong or no encoding specified for {filename}.',
filename=path))
with stream:
# get module name if necessary
if modname is None:
try:
modname = '.'.join(modutils.modpath_from_file(path))
except ImportError:
modname = os.path.splitext(os.path.basename(path))[0]
# build astroid representation
module = self._data_build(data, modname, path)
return self._post_build(module, encoding)
示例7: _readlines_py3
# 需要导入模块: import tokenize [as 别名]
# 或者: from tokenize import detect_encoding [as 别名]
def _readlines_py3(self):
# type: () -> List[str]
try:
with open(self.filename, 'rb') as fd:
(coding, lines) = tokenize.detect_encoding(fd.readline)
textfd = io.TextIOWrapper(fd, coding, line_buffering=True)
return ([l.decode(coding) for l in lines] +
textfd.readlines())
except (LookupError, SyntaxError, UnicodeError):
# If we can't detect the codec with tokenize.detect_encoding, or
# the detected encoding is incorrect, just fallback to latin-1.
with open(self.filename, encoding='latin-1') as fd:
return fd.readlines()
示例8: _stdin_get_value_py3
# 需要导入模块: import tokenize [as 别名]
# 或者: from tokenize import detect_encoding [as 别名]
def _stdin_get_value_py3():
stdin_value = sys.stdin.buffer.read()
fd = io.BytesIO(stdin_value)
try:
(coding, lines) = tokenize.detect_encoding(fd.readline)
return io.StringIO(stdin_value.decode(coding))
except (LookupError, SyntaxError, UnicodeError):
return io.StringIO(stdin_value.decode('utf-8'))
示例9: _read
# 需要导入模块: import tokenize [as 别名]
# 或者: from tokenize import detect_encoding [as 别名]
def _read(filename):
if (2, 5) < sys.version_info < (3, 0):
with open(filename, 'rU') as f:
return f.read()
elif (3, 0) <= sys.version_info < (4, 0):
"""Read the source code."""
try:
with open(filename, 'rb') as f:
(encoding, _) = tokenize.detect_encoding(f.readline)
except (LookupError, SyntaxError, UnicodeError):
# Fall back if file encoding is improperly declared
with open(filename, encoding='latin-1') as f:
return f.read()
with open(filename, 'r', encoding=encoding) as f:
return f.read()
示例10: open_source_file
# 需要导入模块: import tokenize [as 别名]
# 或者: from tokenize import detect_encoding [as 别名]
def open_source_file(filename):
with open(filename, "rb") as byte_stream:
encoding = detect_encoding(byte_stream.readline)[0]
stream = open(filename, "r", newline=None, encoding=encoding)
data = stream.read()
return stream, encoding, data
示例11: file_build
# 需要导入模块: import tokenize [as 别名]
# 或者: from tokenize import detect_encoding [as 别名]
def file_build(self, path, modname=None):
"""Build astroid from a source code file (i.e. from an ast)
*path* is expected to be a python source file
"""
try:
stream, encoding, data = open_source_file(path)
except IOError as exc:
raise exceptions.AstroidBuildingError(
"Unable to load file {path}:\n{error}",
modname=modname,
path=path,
error=exc,
) from exc
except (SyntaxError, LookupError) as exc:
raise exceptions.AstroidSyntaxError(
"Python 3 encoding specification error or unknown encoding:\n"
"{error}",
modname=modname,
path=path,
error=exc,
) from exc
except UnicodeError as exc: # wrong encoding
# detect_encoding returns utf-8 if no encoding specified
raise exceptions.AstroidBuildingError(
"Wrong or no encoding specified for {filename}.", filename=path
) from exc
with stream:
# get module name if necessary
if modname is None:
try:
modname = ".".join(modutils.modpath_from_file(path))
except ImportError:
modname = os.path.splitext(os.path.basename(path))[0]
# build astroid representation
module = self._data_build(data, modname, path)
return self._post_build(module, encoding)
示例12: readlines
# 需要导入模块: import tokenize [as 别名]
# 或者: from tokenize import detect_encoding [as 别名]
def readlines(filename):
"""Read the source code."""
try:
with open(filename, 'rb') as f:
(coding, lines) = tokenize.detect_encoding(f.readline)
f = TextIOWrapper(f, coding, line_buffering=True)
return [line.decode(coding) for line in lines] + f.readlines()
except (LookupError, SyntaxError, UnicodeError):
# Fall back if file encoding is improperly declared
with open(filename, encoding='latin-1') as f:
return f.readlines()
示例13: open
# 需要导入模块: import tokenize [as 别名]
# 或者: from tokenize import detect_encoding [as 别名]
def open(filename):
"""Open a file in read only mode using the encoding detected by
detect_encoding().
"""
buffer = io.open(filename, 'rb') # Tweaked to use io.open for Python 2
encoding, lines = detect_encoding(buffer.readline)
buffer.seek(0)
text = TextIOWrapper(buffer, encoding, line_buffering=True)
text.mode = 'r'
return text
示例14: _source_encoding_py3
# 需要导入模块: import tokenize [as 别名]
# 或者: from tokenize import detect_encoding [as 别名]
def _source_encoding_py3(source):
"""Determine the encoding for `source`, according to PEP 263.
`source` is a byte string: the text of the program.
Returns a string, the name of the encoding.
"""
readline = iternext(source.splitlines(True))
return tokenize.detect_encoding(readline)[0]
示例15: read_pyfile
# 需要导入模块: import tokenize [as 别名]
# 或者: from tokenize import detect_encoding [as 别名]
def read_pyfile(filename):
"""Read and return the contents of a Python source file (as a
string), taking into account the file encoding."""
with open(filename, "rb") as pyfile:
encoding = tokenize.detect_encoding(pyfile.readline)[0]
with open(filename, "r", encoding=encoding) as pyfile:
source = pyfile.read()
return source