Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check the pointer is valid #874

Merged
merged 5 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Package: polars
Title: Lightning-Fast 'DataFrame' Library
Version: 0.14.1.9000
Depends: R (>= 4.2)
Imports: utils, codetools
Imports: utils, codetools, methods
Authors@R:
c(person("Ritchie", "Vink", , "ritchie46@gmail.com", role = c("aut")),
person("Soren", "Welling", , "sorhawell@gmail.com", role = c("aut", "cre")),
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ export(polars_envvars)
export(polars_info)
export(polars_options)
export(polars_options_reset)
importFrom(methods,new)
importFrom(stats,median)
importFrom(stats,na.omit)
importFrom(utils,.DollarNames)
Expand Down
7 changes: 3 additions & 4 deletions R/after-wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,11 @@ extendr_method_to_pure_functions = function(env, class_name = NULL) {
macro_add_syntax_check_to_class = function(Class_name) {
tokens = paste0(
"`$.", Class_name, "` <- function (self, name) {\n",
" verify_method_call(", Class_name, ",name)\n",
" verify_not_null_pointer(self, 'in `$.", Class_name, "`')\n",
" verify_method_call(", Class_name, ", name)\n",
" func <- ", Class_name, "[[name]]\n",
" environment(func) <- environment()\n",
" if(inherits(func,'property')) {\n",
" if(inherits(func, 'property')) {\n",
" func()\n",
" } else {\n",
" func\n",
Expand Down Expand Up @@ -329,8 +330,6 @@ pl_mem_address = function(robj) {
#' - Code completion is facilitated by `.DollarNames.ClassName`-s3method see e.g. 'R/dataframe__frame.R'
#' - Implementation of property-methods as DataFrame_columns() and syntax checking is an extension to `$.ClassName`
#' See function macro_add_syntax_check_to_class().
#'
#' @importFrom utils .DollarNames
#' @return not applicable
#' @examples
#' # all a polars object is only made of:
Expand Down
1 change: 0 additions & 1 deletion R/pkg-knitr.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ knit_print.RPolarsDataFrame = function(x, ...) {
#' @examples
#' to_html_table(mtcars, 3, 3)
#' @noRd
#' @importFrom utils getFromNamespace
to_html_table = function(x, max_cols = 75, max_rows = 40) {
if (!requireNamespace("knitr", quietly = TRUE)) {
stop("Please install the `knitr` package to use `to_html_table`.")
Expand Down
4 changes: 3 additions & 1 deletion R/polars-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
"_PACKAGE"

## usethis namespace: start
#' @importFrom utils globalVariables head tail download.file capture.output str
#' @importFrom stats na.omit median
eitsupi marked this conversation as resolved.
Show resolved Hide resolved
#' @importFrom utils .DollarNames globalVariables getFromNamespace head tail download.file capture.output str
#' @importFrom methods new
## usethis namespace: end
NULL

Expand Down
5 changes: 0 additions & 5 deletions R/s3_methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -341,16 +341,13 @@ mean.RPolarsSeries = function(x, ...) x$mean()
#'
#' @export
#' @rdname S3_median
#' @importFrom stats median
median.RPolarsDataFrame = function(x, ...) x$median()

#' @export
#' @importFrom stats median
#' @rdname S3_median
median.RPolarsLazyFrame = function(x, ...) x$median()

#' @export
#' @importFrom stats median
#' @rdname S3_median
median.RPolarsSeries = function(x, ...) x$median()

Expand Down Expand Up @@ -494,8 +491,6 @@ c.RPolarsSeries = \(x, ...) {
#' @param object A [DataFrame][DataFrame_class] or [LazyFrame][LazyFrame_class]
#' @param subset Character vector of column names to drop missing values from.
#' @param ... Not used.
#'
#' @importFrom stats na.omit
#' @export
#' @rdname S3_na.omit
#' @examples
Expand Down
27 changes: 27 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,34 @@ check_no_missing_args = function(
}


# https://stackoverflow.com/a/27350487/3297472
is_null_external_pointer = function(pointer) {
a = attributes(pointer)
attributes(pointer) = NULL
out = identical(pointer, new("externalptr"))
attributes(pointer) = a
out
}


verify_not_null_pointer = function(pointer, context = NULL) {
valid = FALSE
tryCatch(
{
valid = !is_null_external_pointer(pointer)
},
error = function(c) {}
)

if (!valid) {
Err_plain(
"This Polars object is not valid. Execute `rm(<object>)` to remove the object or restart the R session."
) |>
unwrap(context = context)
}

invisible(NULL)
}


#' Verify user selected method/attribute exists
Expand Down
7 changes: 7 additions & 0 deletions tests/testthat/test-after-wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,10 @@ patrick::with_parameters_test_that("public and private methods of each class",
},
.cases = make_class_cases()
)


test_that("check the polars object is valid", {
raw = as_polars_df(mtcars) |>
serialize(connection = NULL)
expect_error(print(unserialize(raw)), "restart the R session")
})
Loading