コード例 #1
0
def check(args, source_code, filename=None, options=None):
    """Check source code with checker defined with *args* (list)
    Returns an empty list if checker is not installed"""
    if args is None:
        return []
    if options is not None:
        args += options
    if any(['pyflakes' in arg for arg in args]):
        #  Pyflakes requires an ending new line (pycodestyle don't! -- see Issue 1123)
        #  Note: this code is not used right now as it is faster to invoke
        #  pyflakes in current Python interpreter (see `check_with_pyflakes`
        #  function above) than calling it through a subprocess
        source_code += '\n'
    if filename is None:
        # Creating a temporary file because file does not exist yet
        # or is not up-to-date
        tempfd = tempfile.NamedTemporaryFile(suffix=".py", delete=False)
        tempfd.write(source_code)
        tempfd.close()
        args.append(tempfd.name)
    else:
        args.append(filename)
    cmd = args[0]
    cmdargs = args[1:]
    proc = programs.run_program(cmd, cmdargs)
    output = proc.communicate()[0].strip().decode().splitlines()
    if filename is None:
        os.unlink(tempfd.name)
    results = []
    coding = encoding.get_coding(source_code)
    lines = source_code.splitlines()
    for line in output:
        lineno = int(re.search(r'(\:[\d]+\:)', line).group()[1:-1])
        try:
            text = to_text_string(lines[lineno - 1], coding)
        except TypeError:
            text = to_text_string(lines[lineno - 1])
        except UnicodeDecodeError:
            # Needed to handle UnicodeDecodeError and force the use
            # of chardet to detect enconding. See issue 6970
            coding = encoding.get_coding(source_code, force_chardet=True)
            text = to_text_string(lines[lineno - 1], coding)
        if 'analysis:ignore' not in text:
            message = line[line.find(': ') + 2:]
            results.append((message, lineno))
    return results
コード例 #2
0
ファイル: codeanalysis.py プロジェクト: burrbull/spyder
def check(args, source_code, filename=None, options=None):
    """Check source code with checker defined with *args* (list)
    Returns an empty list if checker is not installed"""
    if args is None:
        return []
    if options is not None:
        args += options
    if any(['pyflakes' in arg for arg in args]):
        #  Pyflakes requires an ending new line (pycodestyle don't! -- see Issue 1123)
        #  Note: this code is not used right now as it is faster to invoke 
        #  pyflakes in current Python interpreter (see `check_with_pyflakes` 
        #  function above) than calling it through a subprocess
        source_code += '\n'
    if filename is None:
        # Creating a temporary file because file does not exist yet 
        # or is not up-to-date
        tempfd = tempfile.NamedTemporaryFile(suffix=".py", delete=False)
        tempfd.write(source_code)
        tempfd.close()
        args.append(tempfd.name)
    else:
        args.append(filename)
    cmd = args[0]
    cmdargs = args[1:]
    proc = programs.run_program(cmd, cmdargs)
    output = proc.communicate()[0].strip().decode().splitlines()
    if filename is None:
        os.unlink(tempfd.name)
    results = []
    coding = encoding.get_coding(source_code)
    lines = source_code.splitlines()
    for line in output:
        lineno = int(re.search(r'(\:[\d]+\:)', line).group()[1:-1])
        try:
            text = to_text_string(lines[lineno-1], coding)
        except TypeError:
            text = to_text_string(lines[lineno-1])
        except UnicodeDecodeError:
            # Needed to handle UnicodeDecodeError and force the use
            # of chardet to detect enconding. See issue 6970
            coding = encoding.get_coding(source_code, force_chardet=True)
            text = to_text_string(lines[lineno-1], coding)
        if 'analysis:ignore' not in text:
            message = line[line.find(': ')+2:]
            results.append((message, lineno))
    return results
コード例 #3
0
ファイル: client.py プロジェクト: donydong/spyder
 def _read_stderr(self):
     """Read the stderr file of the kernel."""
     f = open(self.stderr_file, 'rb')
     try:
         stderr_text = f.read()
         encoding = get_coding(stderr_text)
         stderr = to_text_string(stderr_text, encoding)
         return stderr
     finally:
         f.close()
コード例 #4
0
 def _read_stderr(self):
     """Read the stderr file of the kernel."""
     f = open(self.stderr_file, 'rb')
     try:
         stderr_text = f.read()
         # This is needed since the stderr file could be encoded
         # in something different to utf-8.
         # See issue 4191
         encoding = get_coding(stderr_text)
         stderr_text = to_text_string(stderr_text, encoding)
         return stderr_text
     finally:
         f.close()
コード例 #5
0
ファイル: client.py プロジェクト: burrbull/spyder
 def _read_stderr(self):
     """Read the stderr file of the kernel."""
     f = open(self.stderr_file, 'rb')
     try:
         stderr_text = f.read()
         # This is needed since the stderr file could be encoded
         # in something different to utf-8.
         # See issue 4191
         encoding = get_coding(stderr_text)
         stderr_text = to_text_string(stderr_text, encoding)
         return stderr_text
     finally:
         f.close()
コード例 #6
0
ファイル: dataframeeditor.py プロジェクト: yinxx/spyder
    def headerData(self, section, orientation, role=Qt.DisplayRole):
        """Set header data"""
        if role != Qt.DisplayRole:
            return to_qvariant()

        if orientation == Qt.Horizontal:
            if section == 0:
                return 'Index'
            elif section == 1 and PY2:
                # Get rid of possible BOM utf-8 data present at the
                # beginning of a file, which gets attached to the first
                # column header when headers are present in the first
                # row.
                # Fixes Issue 2514
                try:
                    header = to_text_string(self.df_header[0],
                                            encoding='utf-8-sig')
                except:
                    header = to_text_string(self.df_header[0])
                return to_qvariant(header)
            elif isinstance(self.df_header[section - 1], TEXT_TYPES):
                # Get the proper encoding of the text in the header.
                # Fixes Issue 3896
                if not PY2:
                    try:
                        header = self.df_header[section - 1].encode('utf-8')
                        coding = 'utf-8-sig'
                    except:
                        header = self.df_header[section - 1].encode('utf-8')
                        coding = encoding.get_coding(header)
                else:
                    header = self.df_header[section - 1]
                    coding = encoding.get_coding(header)
                return to_qvariant(to_text_string(header, encoding=coding))
            else:
                return to_qvariant(to_text_string(self.df_header[section - 1]))
        else:
            return to_qvariant()
コード例 #7
0
ファイル: dataframeeditor.py プロジェクト: rlaverde/spyder
    def headerData(self, section, orientation, role=Qt.DisplayRole):
        """Set header data"""
        if role != Qt.DisplayRole:
            return to_qvariant()

        if orientation == Qt.Horizontal:
            if section == 0:
                return 'Index'
            elif section == 1 and PY2:
                # Get rid of possible BOM utf-8 data present at the
                # beginning of a file, which gets attached to the first
                # column header when headers are present in the first
                # row.
                # Fixes Issue 2514
                try:
                    header = to_text_string(self.df_header[0],
                                            encoding='utf-8-sig')
                except:
                    header = to_text_string(self.df_header[0])
                return to_qvariant(header)
            elif isinstance(self.df_header[section-1], TEXT_TYPES):
                # Get the proper encoding of the text in the header.
                # Fixes Issue 3896
                if not PY2:
                    try:
                        header = self.df_header[section-1].encode('utf-8')
                        coding = 'utf-8-sig'
                    except:
                        header = self.df_header[section-1].encode('utf-8')
                        coding = encoding.get_coding(header)
                else:
                    header = self.df_header[section-1]
                    coding = encoding.get_coding(header)
                return to_qvariant(to_text_string(header, encoding=coding))
            else:
                return to_qvariant(to_text_string(self.df_header[section-1]))
        else:
            return to_qvariant()
コード例 #8
0
ファイル: codeanalysis.py プロジェクト: M155K4R4/Spidr
def check_with_pyflakes(source_code, filename=None):
    """Check source code with pyflakes
    Returns an empty list if pyflakes is not installed"""
    try:
        if filename is None:
            filename = '<string>'
        try:
            source_code += '\n'
        except TypeError:
            # Python 3
            source_code += to_binary_string('\n')
            
        import _ast
        from pyflakes.checker import Checker
        # First, compile into an AST and handle syntax errors.
        try:
            tree = compile(source_code, filename, "exec", _ast.PyCF_ONLY_AST)
        except SyntaxError as value:
            # If there's an encoding problem with the file, the text is None.
            if value.text is None:
                results = []
            else:
                results = [(value.args[0], value.lineno)]
        except (ValueError, TypeError):
            # Example of ValueError: file contains invalid \x escape character
            # (see http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=674797)
            # Example of TypeError: file contains null character
            # (see http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=674796)
            results = []
        else:
            # Okay, it's syntactically valid.  Now check it.
            w = Checker(tree, filename)
            w.messages.sort(key=lambda x: x.lineno)
            results = []
            coding = encoding.get_coding(source_code)
            lines = source_code.splitlines()
            for warning in w.messages:
                if 'analysis:ignore' not in \
                   to_text_string(lines[warning.lineno-1], coding):
                    results.append((warning.message % warning.message_args,
                                    warning.lineno))
    except Exception:
        # Never return None to avoid lock in spyder/widgets/editor.py
        # See Issue 1547
        results = []
        if DEBUG_EDITOR:
            traceback.print_exc()  # Print exception in internal console
    return results
コード例 #9
0
ファイル: stdfile.py プロジェクト: hlouzada/spyder
    def get_contents(self):
        """Get the contents of the std kernel file."""
        try:
            with open(self.filename, 'rb') as f:
                # We need to read the file as bytes to be able to
                # detect its encoding with chardet
                text = f.read()

                # This is needed to avoid showing an empty error message
                # when the kernel takes too much time to start.
                # See spyder-ide/spyder#8581.
                if not text:
                    return ''

                # This is needed since the file could be encoded
                # in something different to utf-8.
                # See spyder-ide/spyder#4191.
                encoding = get_coding(text)
                text = to_text_string(text, encoding)
                return text
        except Exception:
            return None
コード例 #10
0
ファイル: client.py プロジェクト: akdor1154/spyder
    def _read_stderr(self):
        """Read the stderr file of the kernel."""
        # We need to read stderr_file as bytes to be able to
        # detect its encoding with chardet
        f = open(self.stderr_file, 'rb')

        try:
            stderr_text = f.read()

            # This is needed to avoid showing an empty error message
            # when the kernel takes too much time to start.
            # See spyder-ide/spyder#8581.
            if not stderr_text:
                return ''

            # This is needed since the stderr file could be encoded
            # in something different to utf-8.
            # See spyder-ide/spyder#4191.
            encoding = get_coding(stderr_text)
            stderr_text = to_text_string(stderr_text, encoding)
            return stderr_text
        finally:
            f.close()
コード例 #11
0
ファイル: client.py プロジェクト: impact27/spyder
    def _read_stderr(self):
        """Read the stderr file of the kernel."""
        # We need to read stderr_file as bytes to be able to
        # detect its encoding with chardet
        f = open(self.stderr_file, 'rb')

        try:
            stderr_text = f.read()

            # This is needed to avoid showing an empty error message
            # when the kernel takes too much time to start.
            # See issue 8581
            if not stderr_text:
                return ''

            # This is needed since the stderr file could be encoded
            # in something different to utf-8.
            # See issue 4191
            encoding = get_coding(stderr_text)
            stderr_text = to_text_string(stderr_text, encoding)
            return stderr_text
        finally:
            f.close()
コード例 #12
0
 def _read_stderr(self):
     """Read the stderr file of the kernel."""
     stderr_text = open(self.stderr_file, 'rb').read()
     encoding = get_coding(stderr_text)
     stderr = to_text_string(stderr_text, encoding)
     return stderr
コード例 #13
0
def test_files_encodings(expected_encoding, text_file):
    with open(os.path.join(__location__, text_file), 'rb') as f:
        text = f.read()
        assert get_coding(text).lower() == expected_encoding.lower()
コード例 #14
0
ファイル: client.py プロジェクト: 0xBADCA7/spyder
 def _read_stderr(self):
     """Read the stderr file of the kernel."""
     stderr_text = open(self.stderr_file, 'rb').read()
     encoding = get_coding(stderr_text)
     stderr = to_text_string(stderr_text, encoding)
     return stderr