From c39435aea0586112bbbc0e7fc01e6c88ecce24f1 Mon Sep 17 00:00:00 2001
From: t-kalinowski #1628)
conda_run2()
is now exported (#1637, contributed by @dramanica)
Fixes for CRAN check failures (#1645)
as.character()
method for python.builtin.bytes
gains a nul
argument, allowing for convenient handling of embedded NULs in the string. (#1652)
R/python.R
as.character.python.builtin.bytes.Rd
Convert Python bytes to an R character vector
+Convert Python bytes to an R character or raw vector
# S3 method for class 'python.builtin.bytes'
-as.character(x, encoding = "utf-8", errors = "strict", ...)
Action to take if the bytes contain an embedded NUL (\x00
).
+Python allows embedded NUL
s in strings, while R does not. There are four
+options for handling embedded NUL
s:
Error: This is the default
Replace: Supply a replacement string: nul = "<NUL>"
Remove: Supply an empty string: nul = ""
Split: Supply an R NULL
to indicate that string should be split at embedded NUL
bytes: nul = NULL
further arguments passed to or from other methods.
if (FALSE) { # reticulate::py_available()
+# A bytes object with embedded NULLs
+b <- import_builtins(convert = FALSE)$bytes(
+ as.raw(c(0x61, 0x20, 0x62, 0x00, 0x63, 0x20, 0x64)) # "a b<NUL>c d"
+)
+
+try(as.character(b)) # Error : Embedded NUL in string.
+as.character(b, nul = "<NUL>") # Replace: "a b<NUL>c d"
+as.character(b, nul = "") # Remove: "a bc d"
+as.character(b, nul = NULL) # Split: "a b" "c d"
+}
+