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

Use sys.getdefaultencoding() rather than assume 'utf-8'. #1020

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ New features
variables were not set by default by rpy2 and this could lead to issues (see issue #1033).
The variables are now added to `os.environ`.

- On systems where the default encoding is not utf-8, converting
R strings to Python can result in decoding errors (issue #1018).

Bugs fixed
----------

Expand Down
3 changes: 2 additions & 1 deletion rpy2/rinterface.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import platform
import signal
import subprocess
import sys
import textwrap
import threading
import typing
Expand Down Expand Up @@ -293,7 +294,7 @@ def __str__(self) -> str:
return conversion._cchar_to_str(
openrlib._STRING_VALUE(
self._sexpobject._cdata
), 'utf-8'
), sys.getdefaultencoding()
)


Expand Down
3 changes: 2 additions & 1 deletion rpy2/rinterface_lib/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from contextlib import contextmanager
import logging
import sys
import typing
import os
from rpy2.rinterface_lib import openrlib
Expand All @@ -14,7 +15,7 @@

logger = logging.getLogger(__name__)

_CCHAR_ENCODING = 'utf-8'
_CCHAR_ENCODING = sys.getdefaultencoding()


# TODO: rename to "replace_in_module"
Expand Down
4 changes: 3 additions & 1 deletion rpy2/rinterface_lib/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# TODO: rename the module with a prefix _ to indicate that this should
# not be used outside of rpy2's own code

import sys
from typing import Callable
from typing import Dict
from typing import Optional
Expand Down Expand Up @@ -118,7 +119,8 @@ def _complex_to_sexp(val: complex):
# CE_ANY = 99

# Default encoding for converting R strings to Python
_R_ENC_PY = {None: 'ascii'}
_R_ENC_PY = {None: 'ascii',
'native.enc': sys.getdefaultencoding()}


def _str_to_cchar(s: str, encoding: str = 'utf-8'):
Expand Down
Loading