Giter VIP home page Giter VIP logo

Comments (2)

moodymudskipper avatar moodymudskipper commented on August 22, 2024

This seems to work :

debug_warning <- function(pattern = NULL, class = NULL) {
  code <- '`*tmp_warning*` <- function (..., call. = TRUE, immediate. = FALSE, noBreaks. = FALSE, domain = NULL) {
    if (...length() == 1L && inherits(..1, "condition")) {
      if ((is.null(CLASS) || inherits(..1, CLASS)) && (is.null(PATTERN) || grepl(PATTERN , ..1))) {
        called_by_rlang_warn <- !is.null(call <- sys.call(-1)) && is.call(call) && (
          identical(call[[1]], quote(warn)) || identical(call[[1]], quote(rlang::warn))
        )
        env <- if (called_by_rlang_warn) parent.frame(2) else parent.frame()
        on.exit(eval(quote(browser()), env), add = TRUE)
      }
      cond <- ..1
      if (nargs() > 1L)
        cat(gettext("additional arguments ignored in warning()"),
            "\n", sep = "", file = stderr())
      message <- conditionMessage(cond)
      call <- conditionCall(cond)
      withRestarts({
        .Internal(.signalCondition(cond, message, call))
        .Internal(.dfltWarn(message, call))
      }, muffleWarning = function() NULL)
      invisible(message)
    }
    else {
      if (is.null(PATTERN) || grepl(PATTERN , paste(c(...), collapse = ""))) {
        called_by_rlang_warn <- !is.null(call <- sys.call(-1)) && is.call(call) && (
          identical(call[[1]], quote(warn)) || identical(call[[1]], quote(rlang::warn))
        )
        env <- if (called_by_rlang_warn) parent.frame(2) else parent.frame()
        on.exit(eval(quote(browser()), env), add = TRUE)
      }
      .Internal(warning(call., immediate., noBreaks., .makeMessage(..., domain = domain)))
    }
  }
  unlockBinding("warning", baseenv())
  assign("warning", value = `*tmp_warning*`, envir = baseenv())
  lockBinding("warning", baseenv())
  #rm(`*tmp_warning*`)
  '
  code <- gsub("PATTERN", deparse(pattern), code)
  code <- gsub("CLASS", deparse(class), code)
  con <- textConnection(code)
  source(con, .GlobalEnv)
}

test <- function() {
  foo <- 1
  print(foo)
  warning("some warning")
  foo <- foo + 1
  print(foo)
  rlang::warn("other warning", "foo")
  foo
}

test()

It debugs all warnings by default but we can restrict on class and regex

We need the undebug function that reset the warnings to original .

This belongs in the same package as keep_browsing()

keep_browsing <- function(env = parent.frame()) rlang::eval_bare(quote(on.exit(eval.parent(quote(browser())), add = TRUE)), env)

We might have debug_stop() too, which avoids the failure and set browser(), and its little sister ignore_stop(), that does the same without browser()

from debugverse.

moodymudskipper avatar moodymudskipper commented on August 22, 2024

Let's have ignore_warning() and ignore_message() too

We might have a helper package_stack(ignore.base = TRUE) that displays which packages call which at any point by looking at sys.calls().

A wrapper around package_stack(), browse_package_stack(), might prompt the user to choose a package at any point and add on.exit(browser()) calls in the right places.

debug_warning(), debug_message() and debug_stop() might use browse_package_stack() so we can easily debug at the right place, which might often be the currently developed package. We could make this additional prompt optional but I'm not sure if it costs much.

{browse}, {browser}, {inspect} are available, {debug} and {debugr} are not

I think I wouldn't mind debug_warning() -> browse::browse_warning(), debugging is many things, browsing is really what we're doing.

A general browse(fun, at = NULL, if = NULL, after = NULL, called_by = NULL) function might provide additional flexibility to the original debug function.

We might add some _once variants too.

from debugverse.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.