當前位置: 首頁>>代碼示例>>Python>>正文


Python tokenize.detect_encoding方法代碼示例

本文整理匯總了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 
開發者ID:war-and-code,項目名稱:jawfish,代碼行數:23,代碼來源:_bootstrap.py

示例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) 
開發者ID:war-and-code,項目名稱:jawfish,代碼行數:19,代碼來源:imp.py

示例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() 
開發者ID:ktraunmueller,項目名稱:Computable,代碼行數:27,代碼來源:openpy.py

示例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) 
開發者ID:fabioz,項目名稱:PyDev.Debugger,代碼行數:26,代碼來源:_pydev_execfile.py

示例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 
開發者ID:AtomLinter,項目名稱:linter-pylama,代碼行數:8,代碼來源:builder.py

示例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) 
開發者ID:AtomLinter,項目名稱:linter-pylama,代碼行數:32,代碼來源:builder.py

示例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() 
開發者ID:AtomLinter,項目名稱:linter-pylama,代碼行數:15,代碼來源:processor.py

示例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')) 
開發者ID:AtomLinter,項目名稱:linter-pylama,代碼行數:10,代碼來源:utils.py

示例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() 
開發者ID:AtomLinter,項目名稱:linter-pylama,代碼行數:17,代碼來源:mccabe.py

示例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 
開發者ID:sofia-netsurv,項目名稱:python-netsurv,代碼行數:8,代碼來源:builder.py

示例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) 
開發者ID:sofia-netsurv,項目名稱:python-netsurv,代碼行數:39,代碼來源:builder.py

示例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() 
開發者ID:sofia-netsurv,項目名稱:python-netsurv,代碼行數:13,代碼來源:pycodestyle.py

示例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 
開發者ID:ktraunmueller,項目名稱:Computable,代碼行數:12,代碼來源:openpy.py

示例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] 
開發者ID:nedbat,項目名稱:coveragepy-bbmirror,代碼行數:12,代碼來源:phystokens.py

示例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 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:10,代碼來源:test_unparse.py


注:本文中的tokenize.detect_encoding方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。