Пример #1
0
    def load_module(self, fullname):

        if fullname in sys.modules:
            return sys.modules[fullname]

        cache_path = os.path.join(os.path.dirname(self.path), '__pycache__')
        cache_name = os.path.basename(self.path).rstrip('bpy') + 'pyc'
        map_name = cache_name + '.map'
        cache_file = os.path.join(cache_path, cache_name)
        map_file = os.path.join(cache_path, map_name)

        if os.path.exists(cache_file):
            cache_time = os.path.getmtime(cache_file)
            cache_load = cache_time >= os.path.getmtime(self.path)

        if not os.path.exists(cache_file) or not cache_load:
            with open(self.path, 'r', encoding='utf8') as f:
                source = f.read()
            source, debug, original = translate(source)

            try:
                code = compile(source, self.path, 'exec')
            except Exception as e:
                exception_handler(e, debug, original)

            st = os.stat(self.path)
            write_code_as_pyc(st, cache_file, code)
            write_map(debug, original, map_file)

        if os.path.exists(map_file):
            map_time = os.path.getmtime(map_file)
            map_load = map_time >= os.path.getmtime(self.path)

        if not os.path.exists(map_file) or not map_load:
            if os.path.exists(cache_file) and cache_load:
                with open(self.path, 'r', encoding='utf8') as f:
                    source = f.read()
                source, debug, original = translate(source)

            write_map(debug, original, map_file)

        else:
            with open(map_file, 'rb') as f:
                map_obj = marshal.load(f)
                debug = map_obj['debug']
                original = map_obj['original']

        try:
            spec = importlib.util.spec_from_file_location(fullname, cache_file)
            module = importlib.util.module_from_spec(spec)
            spec.loader.exec_module(module)
            sys.modules[fullname] = module
        except Exception as e:
            exception_handler(e, debug, original)

        return module
Пример #2
0
def main():
    import sys
    import os.path
    file = sys.argv[1]
    with open(file, 'r', encoding='utf8') as f:
        compiled, debug, original = brackets.translate(f.read(), True)
    print(compiled)
Пример #3
0
    def do_is_complete(self, code):
        status, indent_spaces = self.shell.input_transformer_manager.check_complete(code)
        r = {'status': status}

        if status == 'incomplete':
            r['indent'] = ' ' * indent_spaces

        try:
            code = brackets.translate(code)
        except:
            r = {'status': 'incomplete', 'indent': '    '}

        return r
Пример #4
0
    def runsource(self, source, filename="<input>", symbol="single"):
        try:
            code, debug, original = brackets.translate(source)
        except:
            if source.endswith('\n'):
                # Case 1
                self.showsyntaxerror(filename)
                return False
            else:
                return True

        if code is None:
            # Case 2
            return True

        # Case 3
        if self.spy:
            print(('-' * 80) + '\n{0}'.format(code) + ('-' * 80))
        self.runcode(code)
        return False
Пример #5
0
    def do_execute(self, code, silent, store_history=True,
                   user_expressions=None, allow_stdin=False):
        shell = self.shell # we'll need this a lot here

        self._forward_input(allow_stdin)

        reply_content = {}

        try:
            code = brackets.translate(code)
            res = shell.run_cell(code, store_history=store_history, silent=silent)
        finally:
            self._restore_input()

        if res.error_before_exec is not None:
            err = res.error_before_exec
        else:
            err = res.error_in_exec

        if res.success:
            reply_content[u'status'] = u'ok'
        else:
            reply_content[u'status'] = u'error'

            reply_content.update({
                u'traceback': shell._last_traceback or [],
                u'ename': unicode_type(type(err).__name__),
                u'evalue': safe_unicode(err),
            })

            # FIXME: deprecated piece for ipyparallel (remove in 5.0):
            e_info = dict(engine_uuid=self.ident, engine_id=self.int_id,
                          method='execute')
            reply_content['engine_info'] = e_info


        # Return the execution counter so clients can display prompts
        reply_content['execution_count'] = shell.execution_count - 1

        if 'traceback' in reply_content:
            self.log.info("Exception in execute request:\n%s", '\n'.join(reply_content['traceback']))


        # At this point, we can tell whether the main code execution succeeded
        # or not.  If it did, we proceed to evaluate user_expressions
        if reply_content['status'] == 'ok':
            reply_content[u'user_expressions'] = \
                         shell.user_expressions(user_expressions or {})
        else:
            # If there was an error, don't even try to compute expressions
            reply_content[u'user_expressions'] = {}

        # Payloads should be retrieved regardless of outcome, so we can both
        # recover partial output (that could have been generated early in a
        # block, before an error) and always clear the payload system.
        reply_content[u'payload'] = shell.payload_manager.read_payload()
        # Be aggressive about clearing the payload because we don't want
        # it to sit in memory until the next execute_request comes in.
        shell.payload_manager.clear_payload()

        return reply_content
Пример #6
0
def run_command(command):
    code, debug, original = brackets.translate(command)
    try:
        sys.exit(Console().runcode(code))
    except Exception as e:
        exception_handler(e, debug, original)