Ejemplo n.º 1
0
def _scrub_locals_from_traceback(traceback, names, depth=1):
    # type: (Optional[TracebackType], Tuple[str, ...], int) -> None
    for frame in _iter_stacks(traceback):
        if frame.f_globals.get("__name__") == __name__:
            continue

        if depth <= 0:
            break

        depth -= 1

        locals = frame.f_locals

        if locals.get("_sensitive_variables_scrubbed"):
            continue

        locals_modified = False

        if names:
            for name in names:
                if name in locals:
                    locals[name] = PLACEHOLDER
                    locals_modified = True
        else:
            locals.clear()
            locals_modified = True

        if locals_modified:
            locals_to_fast(frame)

        locals["_sensitive_variables_scrubbed"] = True
Ejemplo n.º 2
0
def _scrub_locals_from_traceback(traceback,
                                 names,
                                 depth=1,
                                 custom_scrub_fn=None):
    # type: (Optional[TracebackType], Tuple[str, ...], int, Optional[Callable[[Any, str], Tuple[bool, Any]]]) -> None
    for frame in _iter_stacks(traceback):
        if frame.f_globals.get("__name__") == __name__:
            continue

        if depth <= 0:
            break

        depth -= 1

        locals = frame.f_locals

        if locals.get("_sensitive_variables_scrubbed"):
            continue

        locals_modified = False

        if custom_scrub_fn:
            for local_key in locals:
                obj = locals[local_key]
                local_locals_modified, locals[local_key] = custom_scrub_fn(
                    obj, local_key)
                locals_modified = locals_modified or local_locals_modified

        if names:
            for name in names:
                if name in locals:
                    locals[name] = PLACEHOLDER
                    locals_modified = True

        if not names and not custom_scrub_fn:
            for k, v in locals.items():
                locals[k] = PLACEHOLDER
            locals_modified = True

        if locals_modified:
            locals_to_fast(frame)

        locals["_sensitive_variables_scrubbed"] = True
Ejemplo n.º 3
0
def _clear_locals_in_traceback(traceback, target_frames):
    try:
        frame = traceback.tb_frame
        if frame is None or frame in target_frames:
            return

        new_hash = {}
        for key in frame.f_locals:
            new_hash[key] = None
        if hasattr(ctypes, "pythonapi") and hasattr(ctypes.pythonapi, "PyFrame_LocalsToFast"):
            frame.f_locals.update(new_hash)
            ctypes.pythonapi.PyFrame_LocalsToFast(ctypes.py_object(frame), ctypes.c_int(0))
        elif '__pypy__' in sys.builtin_module_names:
            import __pypy__
            if hasattr(__pypy__, "locals_to_fast"):
                frame.f_locals.update(new_hash)
                __pypy__.locals_to_fast(frame)
    finally:
        del frame
Ejemplo n.º 4
0
    def storeFrameLocals(self, frmnr=0):
        """Stores the locals into the frame.

        Thus an access to frame.f_locals returns the last data
        """
        cf = self.currentFrame
        while cf is not None and frmnr > 0:
            cf = cf.f_back
            frmnr -= 1

        try:
            if '__pypy__' in sys.builtin_module_names:
                import __pypy__
                __pypy__.locals_to_fast(cf)
                return
        except Exception:
            pass

        ctypes.pythonapi.PyFrame_LocalsToFast(ctypes.py_object(cf),
                                              ctypes.c_int(0))
Ejemplo n.º 5
0
def _clear_locals_in_traceback(traceback, target_frames):
    try:
        frame = traceback.tb_frame
        if frame is None or frame in target_frames:
            return

        new_hash = {}
        for key in frame.f_locals:
            new_hash[key] = None
        if hasattr(ctypes, "pythonapi") and hasattr(ctypes.pythonapi,
                                                    "PyFrame_LocalsToFast"):
            frame.f_locals.update(new_hash)
            ctypes.pythonapi.PyFrame_LocalsToFast(ctypes.py_object(frame),
                                                  ctypes.c_int(0))
        elif '__pypy__' in sys.builtin_module_names:
            import __pypy__
            if hasattr(__pypy__, "locals_to_fast"):
                frame.f_locals.update(new_hash)
                __pypy__.locals_to_fast(frame)
    finally:
        del frame
Ejemplo n.º 6
0
 def locals_to_fast(frame, *_, **__):  # pylint: disable=missing-function-docstring
     __pypy__.locals_to_fast(frame)