Ejemplo n.º 1
0
def _readSourceCodeFromFilename3(source_filename):
    import tokenize

    try:
        with open(source_filename, "rb") as source_file:
            encoding = tokenize.detect_encoding(source_file.readline)[0]  # @UndefinedVariable

            # Rewind to get the whole file.
            source_file.seek(0)

            source_code = source_file.read()

        return source_code.decode(encoding)
    except SyntaxError as e:
        if Options.isFullCompat():
            if PythonVersions.doShowUnknownEncodingName():
                match = re.match("unknown encoding for '.*?': (.*)", e.args[0])
                complaint = match.group(1)
            else:
                complaint = "with BOM"

            e.args = (
                "encoding problem: %s" % complaint,
                (source_filename, 1, None, None)
            )

            if hasattr(e, "msg"):
                e.msg = e.args[0]

        raise
Ejemplo n.º 2
0
def _makeSyntaxErrorCompatible(e):
    # Encoding problems for Python happen here, for Python3, this was
    # already done when we read the source code.
    if Options.isFullCompat() and (
        e.args[0].startswith("unknown encoding:") or e.args[0].startswith("encoding problem:")
    ):
        if PythonVersions.doShowUnknownEncodingName():
            complaint = e.args[0].split(":", 2)[1]
        else:
            complaint = " with BOM"

        e.args = ("encoding problem:%s" % complaint, (e.args[1][0], 1, None, None))

        if hasattr(e, "msg"):
            e.msg = e.args[0]
Ejemplo n.º 3
0
def _makeSyntaxErrorCompatible(e):
    # Encoding problems for Python happen here, for Python3, this was
    # already done when we read the source code.
    if Options.isFullCompat() and \
       (e.args[0].startswith("unknown encoding:") or \
        e.args[0].startswith("encoding problem:")):
        if PythonVersions.doShowUnknownEncodingName():
            complaint = e.args[0].split(':', 2)[1]
        else:
            complaint = " with BOM"

        e.args = ("encoding problem:%s" % complaint, (e.args[1][0], 1, None,
                                                      None))

        if hasattr(e, "msg"):
            e.msg = e.args[0]
Ejemplo n.º 4
0
def _readSourceCodeFromFilename3(source_filename):
    import tokenize

    try:
        return tokenize.open(source_filename).read()  # @UndefinedVariable
    except SyntaxError as e:
        if Options.isFullCompat():
            if PythonVersions.doShowUnknownEncodingName():
                match = re.match("unknown encoding for '.*?': (.*)", e.args[0])
                complaint = match.group(1)
            else:
                complaint = "with BOM"

            e.args = ("encoding problem: %s" % complaint, (source_filename, 1, None, None))

            if hasattr(e, "msg"):
                e.msg = e.args[0]

        raise
Ejemplo n.º 5
0
def _readSourceCodeFromFilename3(source_filename):
    import tokenize

    try:
        with tokenize.open(
                source_filename) as source_file:  # @UndefinedVariable
            return source_file.read()
    except SyntaxError as e:
        if Options.isFullCompat():
            if PythonVersions.doShowUnknownEncodingName():
                match = re.match("unknown encoding for '.*?': (.*)", e.args[0])
                complaint = match.group(1)
            else:
                complaint = "with BOM"

            e.args = ("encoding problem: %s" % complaint, (source_filename, 1,
                                                           None, None))

            if hasattr(e, "msg"):
                e.msg = e.args[0]

        raise