Exemple #1
0
    def getlines(filename, module_globals=None):
        """Get the lines (as unicode) for a file from the cache.
        Update the cache if it doesn't contain an entry for this file already."""
        filename = py3compat.cast_bytes(filename, sys.getfilesystemencoding())
        lines = linecache.getlines(filename, module_globals=module_globals)

        if (not lines) or isinstance(lines[0], str):
            return lines

        readline = openpy._list_readline(lines)
        try:
            encoding, _ = openpy.detect_encoding(readline)
        except SyntaxError:
            encoding = 'ascii'
        return [l.decode(encoding, 'replace') for l in lines]
Exemple #2
0
 def getlines(filename, module_globals=None):
     """Get the lines (as unicode) for a file from the cache.
     Update the cache if it doesn't contain an entry for this file already."""
     filename = py3compat.cast_bytes(filename, sys.getfilesystemencoding())
     lines = linecache.getlines(filename, module_globals=module_globals)
     
     # The bits we cache ourselves can be unicode.
     if (not lines) or isinstance(lines[0], unicode):
         return lines
     
     readline = openpy._list_readline(lines)
     try:
         encoding, _ = openpy.detect_encoding(readline)
     except SyntaxError:
         encoding = 'ascii'
     return [l.decode(encoding, 'replace') for l in lines]
Exemple #3
0
def get_encoding(obj):
    """Get encoding for python source file defining obj

    Returns None if obj is not defined in a sourcefile.
    """
    ofile = find_file(obj)
    # run contents of file through pager starting at line where the object
    # is defined, as long as the file isn't binary and is actually on the
    # filesystem.
    if ofile is None:
        return None
    elif ofile.endswith(('.so', '.dll', '.pyd')):
        return None
    elif not os.path.isfile(ofile):
        return None
    else:
        # Print only text files, not extension binaries.  Note that
        # getsourcelines returns lineno with 1-offset and page() uses
        # 0-offset, so we must adjust.
        with stdlib_io.open(ofile, 'rb') as buffer:   # Tweaked to use io.open for Python 2
            encoding, lines = openpy.detect_encoding(buffer.readline)
        return encoding
Exemple #4
0
def get_encoding(obj):
    """Get encoding for python source file defining obj

    Returns None if obj is not defined in a sourcefile.
    """
    ofile = find_file(obj)
    # run contents of file through pager starting at line where the object
    # is defined, as long as the file isn't binary and is actually on the
    # filesystem.
    if ofile is None:
        return None
    elif ofile.endswith(('.so', '.dll', '.pyd')):
        return None
    elif not os.path.isfile(ofile):
        return None
    else:
        # Print only text files, not extension binaries.  Note that
        # getsourcelines returns lineno with 1-offset and page() uses
        # 0-offset, so we must adjust.
        with stdlib_io.open(ofile, 'rb') as buffer:  # Tweaked to use io.open for Python 2
            encoding, lines = openpy.detect_encoding(buffer.readline)
        return encoding
Exemple #5
0
def test_detect_encoding():
    with open(nonascii_path, "rb") as f:
        enc, lines = openpy.detect_encoding(f.readline)
    assert enc == "iso-8859-5"
Exemple #6
0
def test_detect_encoding():
    with open(nonascii_path, 'rb') as f:
        enc, lines = openpy.detect_encoding(f.readline)
    nt.assert_equal(enc, 'iso-8859-5')
Exemple #7
0
def test_detect_encoding():
    f = open(nonascii_path, 'rb')
    enc, lines = openpy.detect_encoding(f.readline)
    nt.assert_equal(enc, 'iso-8859-5')