Skip to content

Commit

Permalink
Merge pull request #21 from asieira/i20_get_var
Browse files Browse the repository at this point in the history
Better handling of encoding (resolves #20)
  • Loading branch information
asieira committed May 29, 2016
2 parents afcc907 + d8e7a5e commit 8df0671
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 11 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: SnakeCharmR
Version: 1.0.2
Date: 2016-05-20
Version: 1.0.3
Date: 2016-05-29
Title: R and Python Integration
Author: Alexandre Sieira, forked off of rPython by Carlos J. Gil Bellosta
Maintainer: Alexandre Sieira <[email protected]>
Expand Down
8 changes: 8 additions & 0 deletions NEWS.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ knitr::opts_chunk$set(
fig.retina = 2
)
```
# SnakeCharmR 1.0.3

Taking measures to ensure SnakeCharmR will work properly on systems where the native encoding is not UTF-8:

* Using Python API to check if variable is str/bytes or unicode and handling things correctly. In particular, when we read Python unicode values we flag the resulting R string as UTF-8 since that’s what we Python C API we are using will return;

* Ensuring that Python code being executed is converted to UTF-8 and that the Python interpreter is made aware of that according to PEP 263.

# SnakeCharmR 1.0.2

Fixed linking and building errors on some platforms (such as Amazon Linux) by updating the
Expand Down
9 changes: 9 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@

<!-- NEWS.md is generated from NEWS.Rmd. Please edit that file -->
SnakeCharmR 1.0.3
=================

Taking measures to ensure SnakeCharmR will work properly on systems where the native encoding is not UTF-8:

- Using Python API to check if variable is str/bytes or unicode and handling things correctly. In particular, when we read Python unicode values we flag the resulting R string as UTF-8 since that’s what we Python C API we are using will return;

- Ensuring that Python code being executed is converted to UTF-8 and that the Python interpreter is made aware of that according to PEP 263.

SnakeCharmR 1.0.2
=================

Expand Down
2 changes: 1 addition & 1 deletion R/misc.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.onAttach <- function(libname, pkgname) {
packageStartupMessage(
c("SnakeCharmR v1.0.2 - R and Python Integration\n",
c("SnakeCharmR v1.0.3 - R and Python Integration\n",
"Contribute and submit issues at https://github.com/asieira/SnakeCharmR")
)
}
Expand Down
22 changes: 14 additions & 8 deletions src/python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,25 @@ void rcpp_Py_Finalize() {

// [[Rcpp::export]]
int rcpp_Py_run_code(String code) {
if (code.get_encoding() != "UTF-8")
code.set_encoding("UTF-8");
code.push_front("# -*- coding: utf-8 -*-\n");
return PyRun_SimpleString(code.get_cstring());
}

// [[Rcpp::export]]
String rcpp_Py_get_var(String varname) {
PyObject *result = PyDict_GetItemString(PyModule_GetDict(PyImport_AddModule("__main__")),
varname.get_cstring());
if (result == NULL)
PyObject *value = PyDict_GetItemString(PyModule_GetDict(PyImport_AddModule("__main__")),
varname.get_cstring());
if (value == NULL)
return NA_STRING;

#if PY_MAJOR_VERSION >= 3
return String(PyBytes_AS_STRING(PyUnicode_AsUTF8String(result)));
#else
return String(PyString_AS_STRING(result));
#endif
if (PyUnicode_Check(value)) {
String retval(PyString_AS_STRING(PyUnicode_AsUTF8String(value)));
retval.set_encoding("UTF-8");
return retval;
} else if (PyString_Check(value))
return String(PyString_AS_STRING(value));
else
throw std::invalid_argument("variable is not a string");
}
15 changes: 15 additions & 0 deletions tests/testthat/test-rcpp-get.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# context("Rcpp get of string/bytes and unicode types")
#
# test_that("we can read string//bytes and unicode", {
# py.exec("_test_var = 'blah'")
# expect_equal(rcpp_Py_get_var("_test_var"), "blah")
#
# py.exec("_test_var = u'blah'")
# expect_equal(rcpp_Py_get_var("_test_var"), "blah")
#
# py.exec("_test_var = u'áéíóú'")
# expect_equal(rcpp_Py_get_var("_test_var"), "áéíóú")
#
# py.exec("_test_var = 1")
# expect_error(rcpp_Py_get_var("_test_var"), "variable is not a string")
# })

0 comments on commit 8df0671

Please sign in to comment.