diff --git a/NAMESPACE b/NAMESPACE index 4a29070d777..2758e7bf6b5 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -68,6 +68,7 @@ S3method(print,igraphHRG) S3method(print,igraphHRGConsensus) S3method(print,igraph_layout_modifier) S3method(print,igraph_layout_spec) +S3method(print,igraph_version) S3method(print,membership) S3method(quantile,sir) S3method(rep,igraph) diff --git a/R/aaa-auto.R b/R/aaa-auto.R index 23282a77b5b..56040f992d6 100644 --- a/R/aaa-auto.R +++ b/R/aaa-auto.R @@ -3979,3 +3979,14 @@ vertex_path_from_edge_path_impl <- function(graph, start, edge.path, mode=c("out res } +version_impl <- function() { + # Argument checks + + + on.exit( .Call(R_igraph_finalizer) ) + # Function call + res <- .Call(R_igraph_version) + + res +} + diff --git a/R/test.R b/R/test.R deleted file mode 100644 index ca61f7a1b3d..00000000000 --- a/R/test.R +++ /dev/null @@ -1,59 +0,0 @@ -#' Query igraph's version string -#' -#' @description -#' `r lifecycle::badge("deprecated")` -#' -#' `igraph.version()` was renamed to `igraph_version()` to create a more -#' consistent API. -#' -#' @keywords internal -#' @export -igraph.version <- function() { # nocov start - lifecycle::deprecate_soft("2.0.0", "igraph.version()", "igraph_version()") - igraph_version() -} # nocov end -# IGraph R package -# Copyright (C) 2005-2013 Gabor Csardi -# 334 Harvard street, Cambridge, MA 02139 USA -# -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA -# 02110-1301 USA -# -################################################################### - -# R_igraph_vers ----------------------------------------------------------------------- - -#' Query igraph's version string -#' -#' Returns the package version. -#' -#' The igraph version string is always the same as the version of the R package. -#' -#' @return A character scalar, the igraph version string. -#' @author Gabor Csardi \email{csardi.gabor@@gmail.com} -#' @keywords graphs -#' @keywords internal -#' @export -#' @examples -#' -#' ## Compare to the package version -#' packageDescription("igraph")$Version -#' igraph_version() -#' -igraph_version <- function() { - # Better than packageVersion("igraph") because it uses the loaded package - # and is independent of .libPaths() - getNamespaceInfo("igraph", "spec")[["version"]] -} diff --git a/R/versions.R b/R/versions.R index 2375b3ee642..4258d2c4cdc 100644 --- a/R/versions.R +++ b/R/versions.R @@ -21,7 +21,6 @@ ## ## ---------------------------------------------------------------------- - # format versions ver_0_1_1 <- 0L # 0.1.1 ver_0_4 <- 1L # 0.4 @@ -93,7 +92,12 @@ upgrade_graph <- function(graph) { } if (g_ver > p_ver) { - stop("Don't know how to downgrade graph from version ", g_ver, " to ", p_ver) + stop( + "Don't know how to downgrade graph from version ", + g_ver, + " to ", + p_ver + ) } # g_ver < p_ver @@ -153,7 +157,9 @@ warn_version <- function(graph) { return(TRUE) } - stop("This graph was created by a new(er) igraph version. Please install the latest version of igraph and try again.") + stop( + "This graph was created by a new(er) igraph version. Please install the latest version of igraph and try again." + ) } oldpredecessors <- function() { @@ -183,3 +189,58 @@ clear_native_ptr <- function(g) { gx[[igraph_t_idx_env]]$igraph <- NULL g } + +#' Query igraph's version string +#' +#' @description +#' `r lifecycle::badge("deprecated")` +#' +#' `igraph.version()` was renamed to `igraph_version()` to create a more +#' consistent API. +#' +#' @keywords internal +#' @export +igraph.version <- function() { + # nocov start + lifecycle::deprecate_soft("2.0.0", "igraph.version()", "igraph_version()") + igraph_version() +} # nocov end + + +# R_igraph_vers ----------------------------------------------------------------------- + +#' Query igraph's version string +#' +#' Returns the R package version, +#' prints the R package version and C library version. +#' +#'#' @return A character scalar, the igraph version string. +#' @author Gabor Csardi \email{csardi.gabor@@gmail.com} +#' @keywords graphs +#' @keywords internal +#' @export +#' @examples +#' igraph_version() +#' +igraph_version <- function(software = c("R", "C")) { + r_version <- getNamespaceInfo("igraph", "spec")[["version"]] + + version <- structure( + r_version, + class = c("igraph_version", class(r_version)), + c_version = c_version() + ) + + invisible(version) +} + +#' @export +print.igraph_version <- function(x, ...) { + writeLines(paste0("R version: ", x)) + writeLines(paste0("C version: ", attr(x, "c_version"))) + invisible(x) +} + +c_version <- function() { + version_impl()[["version_string"]] +} diff --git a/man/igraph.version.Rd b/man/igraph.version.Rd index 672971579b7..b782b69ed00 100644 --- a/man/igraph.version.Rd +++ b/man/igraph.version.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/test.R +% Please edit documentation in R/versions.R \name{igraph.version} \alias{igraph.version} \title{Query igraph's version string} diff --git a/man/igraph_version.Rd b/man/igraph_version.Rd index 4a9849c4c80..426390911cb 100644 --- a/man/igraph_version.Rd +++ b/man/igraph_version.Rd @@ -1,24 +1,19 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/test.R +% Please edit documentation in R/versions.R \name{igraph_version} \alias{igraph_version} \title{Query igraph's version string} \usage{ -igraph_version() -} -\value{ -A character scalar, the igraph version string. +igraph_version(software = c("R", "C")) } \description{ -Returns the package version. +Returns the R package version, +prints the R package version and C library version. } \details{ -The igraph version string is always the same as the version of the R package. +#' @return A character scalar, the igraph version string. } \examples{ - -## Compare to the package version -packageDescription("igraph")$Version igraph_version() } diff --git a/src/cpp11.cpp b/src/cpp11.cpp index afe03ffc244..5e7d835f982 100644 --- a/src/cpp11.cpp +++ b/src/cpp11.cpp @@ -449,6 +449,7 @@ extern SEXP R_igraph_turan(SEXP, SEXP); extern SEXP R_igraph_unfold_tree(SEXP, SEXP, SEXP); extern SEXP R_igraph_union(SEXP, SEXP); extern SEXP R_igraph_vcount(SEXP); +extern SEXP R_igraph_version(void); extern SEXP R_igraph_vertex_coloring_greedy(SEXP, SEXP); extern SEXP R_igraph_vertex_connectivity(SEXP, SEXP); extern SEXP R_igraph_vertex_disjoint_paths(SEXP, SEXP, SEXP); @@ -906,6 +907,7 @@ static const R_CallMethodDef CallEntries[] = { {"R_igraph_unfold_tree", (DL_FUNC) &R_igraph_unfold_tree, 3}, {"R_igraph_union", (DL_FUNC) &R_igraph_union, 2}, {"R_igraph_vcount", (DL_FUNC) &R_igraph_vcount, 1}, + {"R_igraph_version", (DL_FUNC) &R_igraph_version, 0}, {"R_igraph_vertex_coloring_greedy", (DL_FUNC) &R_igraph_vertex_coloring_greedy, 2}, {"R_igraph_vertex_connectivity", (DL_FUNC) &R_igraph_vertex_connectivity, 2}, {"R_igraph_vertex_disjoint_paths", (DL_FUNC) &R_igraph_vertex_disjoint_paths, 3}, diff --git a/src/rinterface.c b/src/rinterface.c index 9160506caca..211f2aa20d8 100644 --- a/src/rinterface.c +++ b/src/rinterface.c @@ -11778,3 +11778,48 @@ SEXP R_igraph_vertex_path_from_edge_path(SEXP graph, SEXP start, SEXP edge_path, UNPROTECT(1); return(r_result); } + +/*-------------------------------------------/ +/ igraph_version / +/-------------------------------------------*/ +SEXP R_igraph_version(void) { + /* Declarations */ + const char* c_version_string; + int c_major; + int c_minor; + int c_subminor; + SEXP version_string; + SEXP major; + SEXP minor; + SEXP subminor; + + SEXP r_result, r_names; + /* Convert input */ + + /* Call igraph */ + igraph_version(&c_version_string, &c_major, &c_minor, &c_subminor); + + /* Convert output */ + PROTECT(r_result=NEW_LIST(4)); + PROTECT(r_names=NEW_CHARACTER(4)); + PROTECT(version_string = Rf_ScalarString(Rf_mkCharLenCE(c_version_string, strlen(c_version_string), CE_UTF8))); + PROTECT(major=NEW_INTEGER(1)); + INTEGER(major)[0]=(int) c_major; + PROTECT(minor=NEW_INTEGER(1)); + INTEGER(minor)[0]=(int) c_minor; + PROTECT(subminor=NEW_INTEGER(1)); + INTEGER(subminor)[0]=(int) c_subminor; + SET_VECTOR_ELT(r_result, 0, version_string); + SET_VECTOR_ELT(r_result, 1, major); + SET_VECTOR_ELT(r_result, 2, minor); + SET_VECTOR_ELT(r_result, 3, subminor); + SET_STRING_ELT(r_names, 0, Rf_mkChar("version_string")); + SET_STRING_ELT(r_names, 1, Rf_mkChar("major")); + SET_STRING_ELT(r_names, 2, Rf_mkChar("minor")); + SET_STRING_ELT(r_names, 3, Rf_mkChar("subminor")); + SET_NAMES(r_result, r_names); + UNPROTECT(5); + + UNPROTECT(1); + return(r_result); +} diff --git a/tests/testthat/test-versions.R b/tests/testthat/test-versions.R index ea1263b82d1..b7824c95c7f 100644 --- a/tests/testthat/test-versions.R +++ b/tests/testthat/test-versions.R @@ -128,5 +128,16 @@ test_that("igraph_version returns a version string", { "\\b" ) - expect_true(grepl(regex, igraph_version())) + expect_match(igraph_version("R"), regex) + expect_match(igraph_version(), regex) + + c_regex <- paste0( + "\\b", # word boundary + "(?:0|[1-9][0-9]*)\\.", # major + "(?:0|[1-9][0-9]*)\\.", # minor + "(?:0|[1-9][0-9]*)", # subminor + "\\b" + ) + + expect_match(igraph_version("C"), c_regex) }) diff --git a/tools/stimulus/functions-R.yaml b/tools/stimulus/functions-R.yaml index bb3ec95f65a..3bac0129fbd 100644 --- a/tools/stimulus/functions-R.yaml +++ b/tools/stimulus/functions-R.yaml @@ -1594,4 +1594,3 @@ igraph_vertex_path_from_edge_path: ####################################### igraph_version: - IGNORE: RR, RC diff --git a/tools/stimulus/types-RC.yaml b/tools/stimulus/types-RC.yaml index 95244bd425d..2682aa49d87 100644 --- a/tools/stimulus/types-RC.yaml +++ b/tools/stimulus/types-RC.yaml @@ -72,7 +72,7 @@ CSTRING: %C% = Rf_translateCharUTF8(%I%); OUTCONV: OUT: |- - PROTECT(%I% = Rf_mkCharLenCE(%C%, strlen(%C%), CE_UTF8)); + PROTECT(%I% = Rf_ScalarString(Rf_mkCharLenCE(%C%, strlen(%C%), CE_UTF8))); REAL: CTYPE: igraph_real_t