Beispiel #1
0
 def compile(self, src):
     """Compiles source code and returns the (possibly modified) source and
     a valid code object.
     """
     _cache = should_use_cache(self.execer, 'single')
     if _cache:
         codefname = code_cache_name(src)
         cachefname = get_cache_filename(codefname, code=True)
         usecache, code = code_cache_check(cachefname)
         if usecache:
             self.reset_buffer()
             return src, code
     try:
         code = self.execer.compile(src,
                                    mode='single',
                                    glbs=self.ctx,
                                    locs=None)
         if _cache:
             update_cache(code, cachefname)
         self.reset_buffer()
     except SyntaxError:
         partial_string_info = check_for_partial_string(src)
         in_partial_string = (partial_string_info[0] is not None
                              and partial_string_info[1] is None)
         if (src == '\n' or src.endswith('\n\n')) and not in_partial_string:
             self.reset_buffer()
             print_exception()
             return src, None
         self.need_more_lines = True
     except Exception:  # pylint: disable=broad-except
         self.reset_buffer()
         print_exception()
         return src, None
     return src, code
Beispiel #2
0
 def push(self, line):
     """Pushes a line onto the buffer and compiles the code in a way that
     enables multiline input.
     """
     code = None
     self.buffer.append(line)
     if self.need_more_lines:
         return None, code
     src = "".join(self.buffer)
     _cache = should_use_cache(self.execer, "single")
     if _cache:
         codefname = code_cache_name(src)
         cachefname = get_cache_filename(codefname, code=True)
         usecache, code = code_cache_check(cachefname)
         if usecache:
             self.reset_buffer()
             return src, code
     try:
         code = self.execer.compile(src, mode="single", glbs=self.ctx, locs=None)
         if _cache:
             update_cache(code, cachefname)
         self.reset_buffer()
     except SyntaxError:
         if line == "\n":
             self.reset_buffer()
             print_exception()
             return src, None
         self.need_more_lines = True
     except Exception:  # pylint: disable=broad-except
         self.reset_buffer()
         print_exception()
         return src, None
     return src, code
Beispiel #3
0
 def compile(self, src):
     """Compiles source code and returns the (possibly modified) source and
     a valid code object.
     """
     _cache = should_use_cache(self.execer, 'single')
     if _cache:
         codefname = code_cache_name(src)
         cachefname = get_cache_filename(codefname, code=True)
         usecache, code = code_cache_check(cachefname)
         if usecache:
             self.reset_buffer()
             return src, code
     try:
         code = self.execer.compile(src,
                                    mode='single',
                                    glbs=self.ctx,
                                    locs=None)
         if _cache:
             update_cache(code, cachefname)
         self.reset_buffer()
     except SyntaxError:
         partial_string_info = check_for_partial_string(src)
         in_partial_string = (partial_string_info[0] is not None and
                              partial_string_info[1] is None)
         if (src == '\n' or src.endswith('\n\n')) and not in_partial_string:
             self.reset_buffer()
             print_exception()
             return src, None
         self.need_more_lines = True
         code = None
     except Exception:  # pylint: disable=broad-except
         self.reset_buffer()
         print_exception()
         code = None
     return src, code
Beispiel #4
0
 def compile(self, src):
     """Compiles source code and returns the (possibly modified) source and
     a valid code object.
     """
     _cache = should_use_cache(self.execer, "single")
     if _cache:
         codefname = code_cache_name(src)
         cachefname = get_cache_filename(codefname, code=True)
         usecache, code = code_cache_check(cachefname)
         if usecache:
             self.reset_buffer()
             return src, code
     lincont = get_line_continuation()
     if src.endswith(lincont + "\n"):
         self.need_more_lines = True
         return src, None
     try:
         code = self.execer.compile(
             src,
             mode="single",
             glbs=self.ctx,
             locs=None,
             filename="<stdin>",
             compile_empty_tree=False,
         )
         if _cache:
             update_cache(code, cachefname)
         self.reset_buffer()
     except SyntaxError:
         partial_string_info = check_for_partial_string(src)
         in_partial_string = (partial_string_info[0] is not None
                              and partial_string_info[1] is None)
         if (src == "\n" or src.endswith("\n\n")) and not in_partial_string:
             self.reset_buffer()
             print_exception()
             return src, None
         self.need_more_lines = True
         code = None
     except Exception:  # pylint: disable=broad-except
         self.reset_buffer()
         print_exception()
         code = None
     return src, code
Beispiel #5
0
 def push(self, line):
     """Pushes a line onto the buffer and compiles the code in a way that
     enables multiline input.
     """
     code = None
     self.buffer.append(line)
     if self.need_more_lines:
         return None, code
     src = ''.join(self.buffer)
     _cache = should_use_cache(self.execer, 'single')
     if _cache:
         codefname = code_cache_name(src)
         cachefname = get_cache_filename(codefname, code=True)
         usecache, code = code_cache_check(cachefname)
         if usecache:
             self.reset_buffer()
             return src, code
     try:
         code = self.execer.compile(src,
                                    mode='single',
                                    glbs=self.ctx,
                                    locs=None)
         if _cache:
             update_cache(code, cachefname)
         self.reset_buffer()
     except SyntaxError:
         partial_string_info = check_for_partial_string(src)
         in_partial_string = (partial_string_info[0] is not None
                              and partial_string_info[1] is None)
         if ((line == '\n' and not in_partial_string)):
             self.reset_buffer()
             print_exception()
             return src, None
         self.need_more_lines = True
     except Exception:  # pylint: disable=broad-except
         self.reset_buffer()
         print_exception()
         return src, None
     return src, code
Beispiel #6
0
 def push(self, line):
     """Pushes a line onto the buffer and compiles the code in a way that
     enables multiline input.
     """
     code = None
     self.buffer.append(line)
     if self.need_more_lines:
         return None, code
     src = ''.join(self.buffer)
     _cache = should_use_cache(self.execer, 'single')
     if _cache:
         codefname = code_cache_name(src)
         cachefname = get_cache_filename(codefname, code=True)
         usecache, code = code_cache_check(cachefname)
         if usecache:
             self.reset_buffer()
             return src, code
     try:
         code = self.execer.compile(src,
                                    mode='single',
                                    glbs=self.ctx,
                                    locs=None)
         if _cache:
             update_cache(code, cachefname)
         self.reset_buffer()
     except SyntaxError:
         partial_string_info = check_for_partial_string(src)
         in_partial_string = (partial_string_info[0] is not None and
                              partial_string_info[1] is None)
         if ((line == '\n' and not in_partial_string)):
             self.reset_buffer()
             print_exception()
             return src, None
         self.need_more_lines = True
     except Exception:  # pylint: disable=broad-except
         self.reset_buffer()
         print_exception()
         return src, None
     return src, code