Ejemplo n.º 1
0
def get_ipython_hidden_vars_dict():
    useful_ipython_vars = ["_", "__"]

    try:
        if IPYTHON and hasattr(__builtin__, "interpreter"):
            pydev_interpreter = get_interpreter().interpreter
            if hasattr(pydev_interpreter, "ipython") and hasattr(pydev_interpreter.ipython, "user_ns_hidden"):
                user_ns_hidden = pydev_interpreter.ipython.user_ns_hidden
                if isinstance(user_ns_hidden, dict):
                    # Since IPython 2 dict `user_ns_hidden` contains hidden variables and values
                    user_hidden_dict = user_ns_hidden
                else:
                    # In IPython 1.x `user_ns_hidden` used to be a set with names of hidden variables
                    user_hidden_dict = dict(
                        [
                            (key, val)
                            for key, val in dict_iter_items(pydev_interpreter.ipython.user_ns)
                            if key in user_ns_hidden
                        ]
                    )
                return dict(
                    [(key, val) for key, val in dict_iter_items(user_hidden_dict) if key not in useful_ipython_vars]
                )
        return None
    except Exception:
        traceback.print_exc()
        return None
    def connectToDebugger(self, debuggerPort, debugger_options=None, extra_envs=None):
        '''
        Used to show console with variables connection.
        Mainly, monkey-patches things in the debugger structure so that the debugger protocol works.
        '''

        if debugger_options is None:
            debugger_options = {}

        for (env_name, value) in dict_iter_items(extra_envs):
            existing_value = os.environ.get(env_name, None)
            if existing_value:
                os.environ[env_name] = "%s%c%s" % (existing_value, os.path.pathsep, value)
            else:
                os.environ[env_name] = value
            if env_name == "PYTHONPATH":
                sys.path.append(value)

        def do_connect_to_debugger():
            try:
                # Try to import the packages needed to attach the debugger
                import pydevd
                from _pydev_imps._pydev_saved_modules import threading

            except:
                # This happens on Jython embedded in host eclipse
                traceback.print_exc()
                sys.stderr.write('pydevd is not available, cannot connect\n', )

            from _pydev_bundle import pydev_localhost
            threading.currentThread().__pydevd_id__ = "console_main"

            self.orig_find_frame = pydevd_vars.find_frame
            pydevd_vars.find_frame = self._findFrame

            self.debugger = pydevd.PyDB()
            try:
                pydevd.apply_debugger_options(debugger_options)
                self.debugger.connect(pydev_localhost.get_localhost(), debuggerPort)
                self.debugger.prepare_to_run()
                from _pydevd_bundle import pydevd_tracing
                pydevd_tracing.SetTrace(None)
            except:
                traceback.print_exc()
                sys.stderr.write('Failed to connect to target debugger.\n')

            # Register to process commands when idle
            self.debugrunning = False
            try:
                import pydevconsole
                pydevconsole.set_debug_hook(self.debugger.process_internal_commands)
            except:
                traceback.print_exc()
                sys.stderr.write('Version of Python does not support debuggable Interactive Console.\n')

        # Important: it has to be really enabled in the main thread, so, schedule
        # it to run in the main thread.
        self.exec_queue.put(do_connect_to_debugger)

        return ('connect complete',)
Ejemplo n.º 3
0
def get_type_of_value(value, ignore_module_name=('__main__', '__builtin__', 'builtins'), recursive=False):
    tp = type(value)
    class_name = tp.__name__
    if class_name == 'instance':  # old-style classes
        tp = value.__class__
        class_name = tp.__name__

    if hasattr(tp, '__module__') and tp.__module__ and tp.__module__ not in ignore_module_name:
        class_name = "%s.%s"%(tp.__module__, class_name)

    if class_name == 'list':
        class_name = 'List'
        if len(value) > 0 and recursive:
            class_name += '[%s]' % get_type_of_value(value[0], recursive=recursive)
        return class_name

    if class_name == 'dict':
        class_name = 'Dict'
        if len(value) > 0 and recursive:
            for (k, v) in dict_iter_items(value):
                class_name += '[%s, %s]' % (get_type_of_value(k, recursive=recursive), 
                                            get_type_of_value(v, recursive=recursive))
                break
        return class_name

    if class_name == 'tuple':
        class_name = 'Tuple'
        if len(value) > 0 and recursive:
            class_name += '['
            class_name += ', '.join(get_type_of_value(v, recursive=recursive) for v in value)
            class_name += ']'

    return class_name
    def get_dictionary(self, dict):
        dict, offset = get_var_and_offset(dict)

        ret = self.init_dict()

        for i, (key, val) in enumerate(dict_iter_items(dict)):
            if i >= offset:
                if i >= offset + MAX_ITEMS_TO_HANDLE:
                    if not IS_PYCHARM:
                        ret[TOO_LARGE_ATTR] = TOO_LARGE_MSG
                    break
                # we need to add the id because otherwise we cannot find the real object to get its contents later on.
                key = '%s (%s)' % (self.key_to_str(key), id(key))
                ret[key] = val

        ret['__len__'] = len(dict)
        # in case if the class extends built-in type and has some additional fields
        additional_fields = defaultResolver.get_dictionary(dict)
        if IS_PYCHARM:
            if offset == 0:
                additional_fields.update(ret)
                ret = additional_fields
        else:
            ret.update(additional_fields)
        return ret
Ejemplo n.º 5
0
    def get_contents_debug_adapter_protocol(self, dct, fmt=None):
        '''
        This method is to be used in the case where the variables are all saved by its id (and as
        such don't need to have the `resolve` method called later on, so, keys don't need to
        embed the reference in the key).

        Note that the return should be ordered.

        :return list(tuple(name:str, value:object, evaluateName:str))
        '''
        ret = []

        i = 0
        for key, val in dict_iter_items(dct):
            i += 1
            key_as_str = self.key_to_str(key, fmt)
            eval_key_str = self.key_to_str(key)  # do not format the key
            ret.append((key_as_str, val, '[%s]' % (eval_key_str,)))
            if i > MAX_ITEMS_TO_HANDLE:
                ret.append((TOO_LARGE_ATTR, TOO_LARGE_MSG, None))
                break

        ret.append(('__len__', len(dct), partial(_apply_evaluate_name, evaluate_name='len(%s)')))
        # in case the class extends built-in type and has some additional fields
        from_default_resolver = defaultResolver.get_contents_debug_adapter_protocol(dct, fmt)

        if from_default_resolver:
            ret = from_default_resolver + ret

        return sorted(ret, key=lambda tup: sorted_attributes_key(tup[0]))
Ejemplo n.º 6
0
def frame_vars_to_xml(frame_f_locals, hidden_ns=None):
    """ dumps frame variables to XML
    <var name="var_name" scope="local" type="type" value="value"/>
    """
    xml = ""

    keys = dict_keys(frame_f_locals)
    if hasattr(keys, 'sort'):
        keys.sort()  # Python 3.0 does not have it
    else:
        keys = sorted(keys)  # Jython 2.1 does not have it

    return_values_xml = ''

    for k in keys:
        try:
            v = frame_f_locals[k]
            eval_full_val = should_evaluate_full_value(v)

            if k == RETURN_VALUES_DICT:
                for name, val in dict_iter_items(v):
                    return_values_xml += var_to_xml(val, name, additional_in_xml=' isRetVal="True"')

            else:
                if hidden_ns is not None and k in hidden_ns:
                    xml += var_to_xml(v, str(k), additional_in_xml=' isIPythonHidden="True"',
                                      evaluate_full_value=eval_full_val)
                else:
                    xml += var_to_xml(v, str(k), evaluate_full_value=eval_full_val)
        except Exception:
            traceback.print_exc()
            pydev_log.error("Unexpected error, recovered safely.\n")

    # Show return values as the first entry.
    return return_values_xml + xml
Ejemplo n.º 7
0
    def get_contents_debug_adapter_protocol(self, obj, fmt=None):
        if MethodWrapperType:
            dct, used___dict__ = self._get_py_dictionary(obj)
        else:
            dct = self._get_jy_dictionary(obj)[0]

        lst = sorted(dict_iter_items(dct), key=lambda tup: sorted_attributes_key(tup[0]))
        if used___dict__:
            return [(attr_name, attr_value, '.__dict__[%s]' % attr_name) for (attr_name, attr_value) in lst]
        else:
            return [(attr_name, attr_value, '.%s' % attr_name) for (attr_name, attr_value) in lst]
 def get_ipython_hidden_vars_dict(self):
     try:
         useful_ipython_vars = ["_", "__"]
         if hasattr(self.interpreter, "ipython") and hasattr(self.interpreter.ipython, "user_ns_hidden"):
             user_ns_hidden = self.interpreter.ipython.user_ns_hidden
             if isinstance(user_ns_hidden, dict):
                 # Since IPython 2 dict `user_ns_hidden` contains hidden variables and values
                 user_hidden_dict = user_ns_hidden
             else:
                 # In IPython 1.x `user_ns_hidden` used to be a set with names of hidden variables
                 user_hidden_dict = dict(
                     [
                         (key, val)
                         for key, val in dict_iter_items(self.interpreter.ipython.user_ns)
                         if key in user_ns_hidden
                     ]
                 )
             return dict(
                 [(key, val) for key, val in dict_iter_items(user_hidden_dict) if key not in useful_ipython_vars]
             )
     except:
         # Getting IPython variables shouldn't break loading frame variables
         traceback.print_exc()
Ejemplo n.º 9
0
    def _get_tracker_for_variable_reference(self, variable_reference):
        tracker = self._variable_reference_to_frames_tracker.get(variable_reference)
        if tracker is not None:
            return tracker

        for _thread_id, tracker in dict_iter_items(self._thread_id_to_tracker):
            try:
                tracker.get_variable(variable_reference)
            except KeyError:
                pass
            else:
                return tracker

        return None
def update_frame_local_variables_and_save(frame, values):
    """Update the frame local variables with the values from `values`, remove those that no longer exist, and save."""
    # It is important to access to `frame.f_locals` through a local reference.
    # Any changes done to it will be lost as soon as we access the attribute next time.
    f_locals, f_globals = frame.f_locals, frame.f_globals

    for key, value in dict_iter_items(values):
        if key not in f_globals or value is not f_globals[key]:
            f_locals[key] = value

    for key in dict_keys(f_locals):
        if key not in values:
            f_locals.pop(key)

    pydevd_save_locals.save_locals(frame)
Ejemplo n.º 11
0
    def getVariable(self, attributes):
        xml = StringIO.StringIO()
        xml.write("<xml>")
        val_dict = pydevd_vars.resolve_compound_var_object_fields(self.get_namespace(), attributes)
        if val_dict is None:
            val_dict = {}

        for k, val in dict_iter_items(val_dict):
            val = val_dict[k]
            evaluate_full_value = pydevd_xml.should_evaluate_full_value(val)
            xml.write(pydevd_vars.var_to_xml(val, k, evaluate_full_value=evaluate_full_value))

        xml.write("</xml>")

        return xml.getvalue()
Ejemplo n.º 12
0
    def _get_tracker_for_variable_reference(self, variable_reference):
        tracker = self._variable_reference_to_frames_tracker.get(
            variable_reference)
        if tracker is not None:
            return tracker

        for _thread_id, tracker in dict_iter_items(self._thread_id_to_tracker):
            try:
                tracker.get_variable(variable_reference)
            except KeyError:
                pass
            else:
                return tracker

        return None
Ejemplo n.º 13
0
 def merge_code(self, code):
     if DEBUG:
         print('merge code ----')
     # for d in dir(code):
     #     if not d.startswith('_'):
     #         print(d, getattr(code, d))
     line_to_contents = _PyCodeToSource(code,
                                        self.memo).build_line_to_contents()
     lines = []
     for line, contents in sorted(dict_iter_items(line_to_contents)):
         lines.append(line)
         self.writer.get_line(line).extend(contents)
     if DEBUG:
         print('end merge code ----')
     return lines
Ejemplo n.º 14
0
def frame_vars_to_struct(frame_f_locals,
                         hidden_ns=None,
                         user_type_renderers={}):
    """Returns frame variables as the list of `DebugValue` structures
    """
    values = []

    keys = dict_keys(frame_f_locals)
    if hasattr(keys, 'sort'):
        keys.sort()  # Python 3.0 does not have it
    else:
        keys = sorted(keys)  # Jython 2.1 does not have it

    return_values = []

    for k in keys:
        try:
            v = frame_f_locals[k]
            eval_full_val = should_evaluate_full_value(v)

            if k == RETURN_VALUES_DICT:
                for name, val in dict_iter_items(v):
                    value = var_to_struct(val, name)
                    value.isRetVal = True
                    return_values.append(value)
            else:
                if hidden_ns is not None and k in hidden_ns:
                    value = var_to_struct(
                        v,
                        str(k),
                        evaluate_full_value=eval_full_val,
                        user_type_renderers=user_type_renderers)
                    value.isIPythonHidden = True
                    values.append(value)
                else:
                    value = var_to_struct(
                        v,
                        str(k),
                        evaluate_full_value=eval_full_val,
                        user_type_renderers=user_type_renderers)
                    values.append(value)
        except Exception:
            traceback.print_exc()
            pydev_log.error("Unexpected error, recovered safely.\n")

    # Show return values as the first entry.
    return return_values + values
Ejemplo n.º 15
0
    def get_children_variables(self, fmt=None):
        children_variables = []
        for key, val in dict_items(self.frame.f_locals):
            is_return_value = key == RETURN_VALUES_DICT
            if is_return_value:
                for return_key, return_value in dict_iter_items(val):
                    variable = _ObjectVariable(
                        return_key, return_value, self._register_variable, is_return_value, '%s[%r]' % (key, return_key))
                    children_variables.append(variable)
            else:
                variable = _ObjectVariable(key, val, self._register_variable, is_return_value, key)
                children_variables.append(variable)

        # Frame variables always sorted.
        children_variables.sort(key=sorted_variables_key)

        return children_variables
Ejemplo n.º 16
0
    def get_children_variables(self, fmt=None):
        children_variables = []
        for key, val in dict_items(self.frame.f_locals):
            is_return_value = key == RETURN_VALUES_DICT
            if is_return_value:
                for return_key, return_value in dict_iter_items(val):
                    variable = _ObjectVariable(
                        return_key, return_value, self._register_variable, is_return_value, '%s[%r]' % (key, return_key), frame=self.frame)
                    children_variables.append(variable)
            else:
                variable = _ObjectVariable(key, val, self._register_variable, is_return_value, key, frame=self.frame)
                children_variables.append(variable)

        # Frame variables always sorted.
        children_variables.sort(key=sorted_variables_key)

        return children_variables
Ejemplo n.º 17
0
    def get_contents_debug_adapter_protocol(self, obj, fmt=None):
        if MethodWrapperType:
            dct, used___dict__ = self._get_py_dictionary(obj)
        else:
            dct = self._get_jy_dictionary(obj)[0]

        lst = sorted(dict_iter_items(dct), key=lambda tup: sorted_attributes_key(tup[0]))
        if used___dict__:
            eval_name = '.__dict__[%s]'
        else:
            eval_name = '.%s'

        ret = []
        for attr_name, attr_value in lst:
            entry = (attr_name, attr_value, eval_name % attr_name)
            ret.append(entry)

        return ret
    def get_dictionary(self, dict):
        ret = {}

        i = 0
        for key, val in dict_iter_items(dict):
            i += 1
            #we need to add the id because otherwise we cannot find the real object to get its contents later on.
            key = '%s (%s)' % (self.key_to_str(key), id(key))
            ret[key] = val
            if i > MAX_ITEMS_TO_HANDLE:
                ret[TOO_LARGE_ATTR] = TOO_LARGE_MSG
                break

        ret['__len__'] = len(dict)
        # in case if the class extends built-in type and has some additional fields
        additional_fields = defaultResolver.get_dictionary(dict)
        ret.update(additional_fields)
        return ret
Ejemplo n.º 19
0
    def get_dictionary(self, dict):
        ret = self.init_dict()

        i = 0
        for key, val in dict_iter_items(dict):
            i += 1
            # we need to add the id because otherwise we cannot find the real object to get its contents later on.
            key = '%s (%s)' % (self.key_to_str(key), id(key))
            ret[key] = val
            if i > MAX_ITEMS_TO_HANDLE:
                ret[TOO_LARGE_ATTR] = TOO_LARGE_MSG
                break

        ret['__len__'] = len(dict)
        # in case if the class extends built-in type and has some additional fields
        additional_fields = defaultResolver.get_dictionary(dict)
        ret.update(additional_fields)
        return ret
Ejemplo n.º 20
0
    def disassemble(self):
        show_lines = False
        line_to_contents = self.build_line_to_contents()
        from io import StringIO

        stream = StringIO()
        last_line = 0
        indent = ''
        previous_line_tokens = set()
        for i_line, contents in sorted(dict_iter_items(line_to_contents)):
            while last_line < i_line - 1:
                if show_lines:
                    stream.write(u"%s.\n" % (last_line + 1, ))
                else:
                    stream.write(u"\n")
                last_line += 1

            line_contents = []
            dedents_found = 0
            for part in contents:
                if part is INDENT_MARKER:
                    if DEBUG:
                        print('found indent', i_line)
                    indent += '    '
                    continue
                if part is DEDENT_MARKER:
                    if DEBUG:
                        print('found dedent', i_line)
                    dedents_found += 1
                    continue
                line_contents.append(part)

            s = indent + _compose_line_contents(line_contents,
                                                previous_line_tokens)
            if show_lines:
                stream.write(u"%s. %s\n" % (i_line, s))
            else:
                stream.write(u"%s\n" % s)

            if dedents_found:
                indent = indent[:-(4 * dedents_found)]
            last_line = i_line

        return stream.getvalue()
Ejemplo n.º 21
0
def take_first_n_coll_elements(coll, n):
    if coll.__class__ in (list, tuple):
        return coll[:n]
    elif coll.__class__ in (set, frozenset):
        buf = []
        for i, x in enumerate(coll):
            if i >= n:
                break
            buf.append(x)
        return type(coll)(buf)
    elif coll.__class__ in (dict, OrderedDict):
        ret = type(coll)()
        for i, (k, v) in enumerate(dict_iter_items(coll)):
            if i >= n:
                break
            ret[k] = v
        return ret
    else:
        raise TypeError("Unsupported collection type: '%s'" % str(coll.__class__))
Ejemplo n.º 22
0
def take_first_n_coll_elements(coll, n):
    if coll.__class__ in (list, tuple):
        return coll[:n]
    elif coll.__class__ in (set, frozenset):
        buf = []
        for i, x in enumerate(coll):
            if i >= n:
                break
            buf.append(x)
        return type(coll)(buf)
    elif coll.__class__ in (dict, OrderedDict):
        ret = type(coll)()
        for i, (k, v) in enumerate(dict_iter_items(coll)):
            if i >= n:
                break
            ret[k] = v
        return ret
    else:
        raise TypeError("Unsupported collection type: '%s'" % str(coll.__class__))
Ejemplo n.º 23
0
def frame_vars_to_xml(frame_f_locals, hidden_ns=None):
    """ dumps frame variables to XML
    <var name="var_name" scope="local" type="type" value="value"/>
    """
    xml = ""

    keys = dict_keys(frame_f_locals)
    if hasattr(keys, 'sort'):
        keys.sort()  # Python 3.0 does not have it
    else:
        keys = sorted(keys)  # Jython 2.1 does not have it

    return_values_xml = ''

    for k in keys:
        try:
            v = frame_f_locals[k]
            eval_full_val = should_evaluate_full_value(v)

            if k == '_pydev_stop_at_break':
                continue

            if k == RETURN_VALUES_DICT:
                for name, val in dict_iter_items(v):
                    return_values_xml += var_to_xml(
                        val, name, additional_in_xml=' isRetVal="True"')

            else:
                if hidden_ns is not None and k in hidden_ns:
                    xml += var_to_xml(
                        v,
                        str(k),
                        additional_in_xml=' isIPythonHidden="True"',
                        evaluate_full_value=eval_full_val)
                else:
                    xml += var_to_xml(v,
                                      str(k),
                                      evaluate_full_value=eval_full_val)
        except Exception:
            pydev_log.exception("Unexpected error, recovered safely.")

    # Show return values as the first entry.
    return return_values_xml + xml
    def resolve(self, dict, key):
        if key in ('__len__', TOO_LARGE_ATTR):
            return None

        if '(' not in key:
            #we have to treat that because the dict resolver is also used to directly resolve the global and local
            #scopes (which already have the items directly)
            try:
                return dict[key]
            except:
                return getattr(dict, key)

        #ok, we have to iterate over the items to find the one that matches the id, because that's the only way
        #to actually find the reference from the string we have before.
        expected_id = int(key.split('(')[-1][:-1])
        for key, val in dict_iter_items(dict):
            if id(key) == expected_id:
                return val

        raise UnableToResolveVariableException()
 def regular_handle(self,
                    key,
                    value,
                    hidden_ns,
                    evaluate_full_value,
                    is_dict_iter_items=False,
                    user_type_renderers=None):
     if self.is_belong_to_group(key, value, hidden_ns):
         if is_dict_iter_items:
             for name, val in dict_iter_items(value):
                 self.lst.append(
                     self.fun(name, val, hidden_ns, evaluate_full_value,
                              user_type_renderers))
         else:
             self.lst.append(
                 self.fun(key, value, hidden_ns, evaluate_full_value,
                          user_type_renderers))
     else:
         self.give_to_next(key, value, hidden_ns, evaluate_full_value,
                           user_type_renderers)
Ejemplo n.º 26
0
    def resolve(self, dict, key):
        if key in ('__len__', TOO_LARGE_ATTR):
            return None

        if '(' not in key:
            # we have to treat that because the dict resolver is also used to directly resolve the global and local
            # scopes (which already have the items directly)
            try:
                return dict[key]
            except:
                return getattr(dict, key)

        # ok, we have to iterate over the items to find the one that matches the id, because that's the only way
        # to actually find the reference from the string we have before.
        expected_id = int(key.split('(')[-1][:-1])
        for key, val in dict_iter_items(dict):
            if id(key) == expected_id:
                return val

        raise UnableToResolveVariableException()
Ejemplo n.º 27
0
    def get_ipython_hidden_vars_dict(self):
        try:
            if hasattr(self.interpreter, 'ipython') and hasattr(self.interpreter.ipython, 'user_ns_hidden'):
                user_ns_hidden = self.interpreter.ipython.user_ns_hidden
                if isinstance(user_ns_hidden, dict):
                    # Since IPython 2 dict `user_ns_hidden` contains hidden variables and values
                    user_hidden_dict = user_ns_hidden.copy()
                else:
                    # In IPython 1.x `user_ns_hidden` used to be a set with names of hidden variables
                    user_hidden_dict = dict([(key, val) for key, val in dict_iter_items(self.interpreter.ipython.user_ns)
                                             if key in user_ns_hidden])

                # while `_`, `__` and `___` were not initialized, they are not presented in `user_ns_hidden`
                user_hidden_dict.setdefault('_', '')
                user_hidden_dict.setdefault('__', '')
                user_hidden_dict.setdefault('___', '')

                return user_hidden_dict
        except:
            # Getting IPython variables shouldn't break loading frame variables
            traceback.print_exc()
def get_ipython_hidden_vars(ipython_shell):
    try:
        if hasattr(ipython_shell, 'user_ns_hidden'):
            user_ns_hidden = ipython_shell.user_ns_hidden
            if isinstance(user_ns_hidden, dict):
                # Since IPython 2 dict `user_ns_hidden` contains hidden variables and values
                user_hidden_dict = user_ns_hidden.copy()
            else:
                # In IPython 1.x `user_ns_hidden` used to be a set with names of hidden variables
                user_hidden_dict = dict([(key, val) for key, val in dict_iter_items(ipython_shell.user_ns)
                                         if key in user_ns_hidden])

            # while `_`, `__` and `___` were not initialized, they are not presented in `user_ns_hidden`
            user_hidden_dict.setdefault('_', '')
            user_hidden_dict.setdefault('__', '')
            user_hidden_dict.setdefault('___', '')

            return user_hidden_dict
    except:
        # Getting IPython variables shouldn't break loading frame variables
        traceback.print_exc()
Ejemplo n.º 29
0
def get_type_of_value(value,
                      ignore_module_name=('__main__', '__builtin__',
                                          'builtins'),
                      recursive=False):
    tp = type(value)
    class_name = tp.__name__
    if class_name == 'instance':  # old-style classes
        tp = value.__class__
        class_name = tp.__name__

    if hasattr(tp, '__module__'
               ) and tp.__module__ and tp.__module__ not in ignore_module_name:
        class_name = "%s.%s" % (tp.__module__, class_name)

    if class_name == 'list':
        class_name = 'List'
        if len(value) > 0 and recursive:
            class_name += '[%s]' % get_type_of_value(value[0],
                                                     recursive=recursive)
        return class_name

    if class_name == 'dict':
        class_name = 'Dict'
        if len(value) > 0 and recursive:
            for (k, v) in dict_iter_items(value):
                class_name += '[%s, %s]' % (
                    get_type_of_value(k, recursive=recursive),
                    get_type_of_value(v, recursive=recursive))
                break
        return class_name

    if class_name == 'tuple':
        class_name = 'Tuple'
        if len(value) > 0 and recursive:
            class_name += '['
            class_name += ', '.join(
                get_type_of_value(v, recursive=recursive) for v in value)
            class_name += ']'

    return class_name
    def get_children_variables(self, fmt=None, scope=None):
        children_variables = []
        if scope is not None:
            assert isinstance(scope, ScopeRequest)
            scope = scope.scope

        if scope in ('locals', None):
            dct = self.frame.f_locals
        elif scope == 'globals':
            dct = self.frame.f_globals
        else:
            raise AssertionError('Unexpected scope: %s' % (scope,))

        lst, group_entries = self._group_entries([(x[0], x[1], None) for x in dict_items(dct) if x[0] != '_pydev_stop_at_break'], handle_return_values=True)
        group_variables = []

        for key, val, _ in group_entries:
            # Make sure that the contents in the group are also sorted.
            val.contents_debug_adapter_protocol.sort(key=lambda v:sorted_attributes_key(v[0]))
            variable = _ObjectVariable(self.py_db, key, val, self._register_variable, False, key, frame=self.frame)
            group_variables.append(variable)

        for key, val, _ in lst:
            is_return_value = key == RETURN_VALUES_DICT
            if is_return_value:
                for return_key, return_value in dict_iter_items(val):
                    variable = _ObjectVariable(
                        self.py_db, return_key, return_value, self._register_variable, is_return_value, '%s[%r]' % (key, return_key), frame=self.frame)
                    children_variables.append(variable)
            else:
                variable = _ObjectVariable(self.py_db, key, val, self._register_variable, is_return_value, key, frame=self.frame)
                children_variables.append(variable)

        # Frame variables always sorted.
        children_variables.sort(key=sorted_variables_key)
        if group_variables:
            # Groups have priority over other variables.
            children_variables = group_variables + children_variables

        return children_variables
Ejemplo n.º 31
0
def frame_vars_to_struct(frame_f_locals, hidden_ns=None):
    """Returns frame variables as the list of `DebugValue` structures
    """
    values = []

    keys = dict_keys(frame_f_locals)
    if hasattr(keys, 'sort'):
        keys.sort()  # Python 3.0 does not have it
    else:
        keys = sorted(keys)  # Jython 2.1 does not have it

    return_values = []

    for k in keys:
        try:
            v = frame_f_locals[k]
            eval_full_val = should_evaluate_full_value(v)

            if k == RETURN_VALUES_DICT:
                for name, val in dict_iter_items(v):
                    value = var_to_struct(val, name)
                    value.isRetVal = True
                    return_values.append(value)
            else:
                if hidden_ns is not None and k in hidden_ns:
                    value = var_to_struct(v, str(k), evaluate_full_value=eval_full_val)
                    value.isIPythonHidden = True
                    values.append(value)
                else:
                    value = var_to_struct(v, str(k), evaluate_full_value=eval_full_val)
                    values.append(value)
        except Exception:
            traceback.print_exc()
            pydev_log.error("Unexpected error, recovered safely.\n")

    # Show return values as the first entry.
    return return_values + values
    def connectToDebugger(self, debuggerPort, debugger_options=None):
        '''
        Used to show console with variables connection.
        Mainly, monkey-patches things in the debugger structure so that the debugger protocol works.
        '''

        if debugger_options is None:
            debugger_options = {}
        env_key = "PYDEVD_EXTRA_ENVS"
        if env_key in debugger_options:
            for (env_name,
                 value) in dict_iter_items(debugger_options[env_key]):
                os.environ[env_name] = value
            del debugger_options[env_key]

        def do_connect_to_debugger():
            try:
                # Try to import the packages needed to attach the debugger
                import pydevd
                from _pydev_imps._pydev_saved_modules import threading

            except:
                # This happens on Jython embedded in host eclipse
                traceback.print_exc()
                sys.stderr.write('pydevd is not available, cannot connect\n', )

            from _pydev_bundle import pydev_localhost
            threading.currentThread().__pydevd_id__ = "console_main"

            self.orig_find_frame = pydevd_vars.find_frame
            pydevd_vars.find_frame = self._findFrame

            self.debugger = pydevd.PyDB()
            try:
                pydevd.apply_debugger_options(debugger_options)
                self.debugger.connect(pydev_localhost.get_localhost(),
                                      debuggerPort)
                self.debugger.prepare_to_run()
                from _pydevd_bundle import pydevd_tracing
                pydevd_tracing.SetTrace(None)
            except:
                traceback.print_exc()
                sys.stderr.write('Failed to connect to target debugger.\n')

            # Register to process commands when idle
            self.debugrunning = False
            try:
                import pydevconsole
                pydevconsole.set_debug_hook(
                    self.debugger.process_internal_commands)
            except:
                traceback.print_exc()
                sys.stderr.write(
                    'Version of Python does not support debuggable Interactive Console.\n'
                )

        # Important: it has to be really enabled in the main thread, so, schedule
        # it to run in the main thread.
        self.exec_queue.put(do_connect_to_debugger)

        return ('connect complete', )
Ejemplo n.º 33
0
def return_values_from_dict_to_xml(return_dict):
    res = ""
    for name, val in dict_iter_items(return_dict):
        res += var_to_xml(val, name, additional_in_xml=' isRetVal="True"')
    return res
Ejemplo n.º 34
0
    def connectToDebugger(self,
                          debuggerPort,
                          debugger_host=None,
                          debugger_options=None,
                          extra_envs=None):
        '''
        Used to show console with variables connection.
        Mainly, monkey-patches things in the debugger structure so that the debugger protocol works.
        '''

        if debugger_options is None:
            debugger_options = {}

        for (env_name, value) in dict_iter_items(extra_envs):
            existing_value = os.environ.get(env_name, None)
            if existing_value:
                os.environ[env_name] = "%s%c%s" % (existing_value,
                                                   os.path.pathsep, value)
            else:
                os.environ[env_name] = value
            if env_name == "PYTHONPATH":
                sys.path.append(value)

        def do_connect_to_debugger():
            try:
                # Try to import the packages needed to attach the debugger
                import pydevd
                from _pydev_imps._pydev_saved_modules import threading

            except:
                # This happens on Jython embedded in host eclipse
                traceback.print_exc()
                sys.stderr.write('pydevd is not available, cannot connect\n', )

            from _pydevd_bundle.pydevd_constants import set_thread_id
            from _pydev_bundle import pydev_localhost
            set_thread_id(threading.currentThread(), "console_main")

            self.orig_find_frame = pydevd_vars.find_frame
            pydevd_vars.find_frame = self._findFrame

            self.debugger = pydevd.PyDB()
            try:
                pydevd.apply_debugger_options(debugger_options)
                if debugger_host is None or pydev_localhost.is_localhost(
                        debugger_host):
                    host = pydev_localhost.get_localhost()
                else:
                    host = debugger_host
                self.debugger.connect(host, debuggerPort)
                self.debugger.prepare_to_run()
                self.debugger.disable_tracing()
            except:
                traceback.print_exc()
                sys.stderr.write('Failed to connect to target debugger.\n')

            # Register to process commands when idle
            self.debugrunning = False
            try:
                import pydevconsole
                pydevconsole.set_debug_hook(
                    self.debugger.process_internal_commands)
            except:
                traceback.print_exc()
                sys.stderr.write(
                    'Version of Python does not support debuggable Interactive Console.\n'
                )

        # Important: it has to be really enabled in the main thread, so, schedule
        # it to run in the main thread.
        self.exec_queue.put(do_connect_to_debugger)

        return ('connect complete', )
Ejemplo n.º 35
0
    def _next_instruction_to_str(self, line_to_contents):
        # indent = ''
        # if self.level > 0:
        #     indent += '    ' * self.level
        # print(indent, 'handle', self.instructions[0])

        if self.instructions:
            ret = self._lookahead()
            if ret:
                return ret

        msg = self._create_msg_part

        instruction = self.instructions.pop(0)

        if instruction.opname in ('LOAD_GLOBAL', 'LOAD_FAST', 'LOAD_CONST',
                                  'LOAD_NAME'):
            next_instruction = self.instructions[0]
            if next_instruction.opname in ('STORE_FAST', 'STORE_NAME'):
                self.instructions.pop(0)
                return (msg(next_instruction), msg(next_instruction,
                                                   ' = '), msg(instruction))

            if next_instruction.opname == 'RETURN_VALUE':
                self.instructions.pop(0)
                return (msg(instruction,
                            'return ',
                            line=self.min_line(instruction)), msg(instruction))

            if next_instruction.opname == 'RAISE_VARARGS' and next_instruction.argval == 1:
                self.instructions.pop(0)
                return (msg(instruction,
                            'raise ',
                            line=self.min_line(instruction)), msg(instruction))

        if instruction.opname == 'LOAD_CONST':
            if inspect.iscode(instruction.argval):

                code_line_to_contents = _Disassembler(
                    instruction.argval, self.firstlineno,
                    self.level + 1).build_line_to_contents()

                for contents in dict_iter_values(code_line_to_contents):
                    contents.insert(0, '    ')
                for line, contents in dict_iter_items(code_line_to_contents):
                    line_to_contents.setdefault(line, []).extend(contents)
                return msg(instruction, 'LOAD_CONST(code)')

        if instruction.opname == 'RAISE_VARARGS':
            if instruction.argval == 0:
                return msg(instruction, 'raise')

        if instruction.opname == 'SETUP_FINALLY':
            return msg(instruction, ('try(', instruction.argrepr, '):'))

        if instruction.argrepr:
            return msg(instruction,
                       (instruction.opname, '(', instruction.argrepr, ')'))

        if instruction.argval:
            return msg(instruction, '%s{%s}' % (
                instruction.opname,
                instruction.argval,
            ))

        return msg(instruction, instruction.opname)
def has_line_breaks(plugin):
    for file, breakpoints in dict_iter_items(
            plugin.main_debugger.django_breakpoints):
        if len(breakpoints) > 0:
            return True
    return False
Ejemplo n.º 37
0
    def connectToDebugger(self, debuggerPort, debugger_options=None):
        '''
        Used to show console with variables connection.
        Mainly, monkey-patches things in the debugger structure so that the debugger protocol works.
        '''

        if debugger_options is None:
            debugger_options = {}
        env_key = "PYDEVD_EXTRA_ENVS"
        if env_key in debugger_options:
            for (env_name, value) in dict_iter_items(debugger_options[env_key]):
                existing_value = os.environ.get(env_name, None)
                if existing_value:
                    os.environ[env_name] = "%s%c%s" % (existing_value, os.path.pathsep, value)
                else:
                    os.environ[env_name] = value
                if env_name == "PYTHONPATH":
                    sys.path.append(value)

            del debugger_options[env_key]

        def do_connect_to_debugger():
            try:
                # Try to import the packages needed to attach the debugger
                import pydevd
                from _pydev_imps._pydev_saved_modules import threading

            except:
                # This happens on Jython embedded in host eclipse
                traceback.print_exc()
                sys.stderr.write('pydevd is not available, cannot connect\n', )

            from _pydevd_bundle.pydevd_constants import set_thread_id
            from _pydev_bundle import pydev_localhost
            set_thread_id(threading.currentThread(), "console_main")

            VIRTUAL_FRAME_ID = "1"  # matches PyStackFrameConsole.java
            VIRTUAL_CONSOLE_ID = "console_main"  # matches PyThreadConsole.java
            f = FakeFrame()
            f.f_back = None
            f.f_globals = {}  # As globals=locals here, let's simply let it empty (and save a bit of network traffic).
            f.f_locals = self.get_namespace()

            self.debugger = pydevd.PyDB()
            self.debugger.add_fake_frame(thread_id=VIRTUAL_CONSOLE_ID, frame_id=VIRTUAL_FRAME_ID, frame=f)
            try:
                pydevd.apply_debugger_options(debugger_options)
                self.debugger.connect(pydev_localhost.get_localhost(), debuggerPort)
                self.debugger.prepare_to_run()
                self.debugger.disable_tracing()
            except:
                traceback.print_exc()
                sys.stderr.write('Failed to connect to target debugger.\n')

            # Register to process commands when idle
            self.debugrunning = False
            try:
                import pydevconsole
                pydevconsole.set_debug_hook(self.debugger.process_internal_commands)
            except:
                traceback.print_exc()
                sys.stderr.write('Version of Python does not support debuggable Interactive Console.\n')

        # Important: it has to be really enabled in the main thread, so, schedule
        # it to run in the main thread.
        self.exec_queue.put(do_connect_to_debugger)

        return ('connect complete',)
    def connectToDebugger(self, debuggerPort, debugger_options=None):
        '''
        Used to show console with variables connection.
        Mainly, monkey-patches things in the debugger structure so that the debugger protocol works.
        '''

        if debugger_options is None:
            debugger_options = {}
        env_key = "PYDEVD_EXTRA_ENVS"
        if env_key in debugger_options:
            for (env_name,
                 value) in dict_iter_items(debugger_options[env_key]):
                existing_value = os.environ.get(env_name, None)
                if existing_value:
                    os.environ[env_name] = "%s%c%s" % (existing_value,
                                                       os.path.pathsep, value)
                else:
                    os.environ[env_name] = value
                if env_name == "PYTHONPATH":
                    sys.path.append(value)

            del debugger_options[env_key]

        def do_connect_to_debugger():
            try:
                # Try to import the packages needed to attach the debugger
                import pydevd
                from _pydev_imps._pydev_saved_modules import threading
            except:
                # This happens on Jython embedded in host eclipse
                traceback.print_exc()
                sys.stderr.write('pydevd is not available, cannot connect\n')

            from _pydevd_bundle.pydevd_constants import set_thread_id
            from _pydev_bundle import pydev_localhost
            set_thread_id(threading.current_thread(), "console_main")

            VIRTUAL_FRAME_ID = "1"  # matches PyStackFrameConsole.java
            VIRTUAL_CONSOLE_ID = "console_main"  # matches PyThreadConsole.java
            f = FakeFrame()
            f.f_back = None
            f.f_globals = {
            }  # As globals=locals here, let's simply let it empty (and save a bit of network traffic).
            f.f_locals = self.get_namespace()

            self.debugger = pydevd.PyDB()
            self.debugger.add_fake_frame(thread_id=VIRTUAL_CONSOLE_ID,
                                         frame_id=VIRTUAL_FRAME_ID,
                                         frame=f)
            try:
                pydevd.apply_debugger_options(debugger_options)
                self.debugger.connect(pydev_localhost.get_localhost(),
                                      debuggerPort)
                self.debugger.prepare_to_run()
                self.debugger.disable_tracing()
            except:
                traceback.print_exc()
                sys.stderr.write('Failed to connect to target debugger.\n')

            # Register to process commands when idle
            self.debugrunning = False
            try:
                import pydevconsole
                pydevconsole.set_debug_hook(
                    self.debugger.process_internal_commands)
            except:
                traceback.print_exc()
                sys.stderr.write(
                    'Version of Python does not support debuggable Interactive Console.\n'
                )

        # Important: it has to be really enabled in the main thread, so, schedule
        # it to run in the main thread.
        self.exec_queue.put(do_connect_to_debugger)

        return ('connect complete', )
Ejemplo n.º 39
0
def return_values_from_dict_to_xml(return_dict):
    res = ""
    for name, val in dict_iter_items(return_dict):
        res += var_to_xml(val, name, additional_in_xml=' isRetVal="True"')
    return res
Ejemplo n.º 40
0
def has_line_breaks(plugin):
    for file, breakpoints in dict_iter_items(plugin.main_debugger.jinja2_breakpoints):
        if len(breakpoints) > 0:
            return True
    return False
Ejemplo n.º 41
0
def has_line_breaks(plugin):
    for _canonical_normalized_filename, breakpoints in dict_iter_items(plugin.main_debugger.jinja2_breakpoints):
        if len(breakpoints) > 0:
            return True
    return False