Skip to content

Commit

Permalink
preserve hidden functions hidden
Browse files Browse the repository at this point in the history
  • Loading branch information
bbgw committed Jul 16, 2020
1 parent dd37e65 commit ccf22cc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
3 changes: 2 additions & 1 deletion rpy2/robjects/packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,8 @@ def __fill_rpy2r__(self, on_conflict='fail'):
self._env,
translation=self._translation,
symbol_r2python=self._symbol_r2python,
symbol_resolve=self._symbol_resolve
symbol_resolve=self._symbol_resolve,
exported_names=self._exported_names
)
msg_prefix = ('Conflict when converting R symbols'
' in the package "%s"'
Expand Down
14 changes: 10 additions & 4 deletions rpy2/robjects/packages_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@ def get_packagepath(package):
# The functions are in this module in order to facilitate
# their access from other modules (without circular dependencies).
# It not necessarily the absolute best place to have the functions though.
def default_symbol_r2python(rname):
def default_symbol_r2python(rname, exported_names=None):
"""Replace each dot (.) with an underscore (_)."""
return rname.replace('.', '_')
res = rname.replace('.', '_')
return res \
if exported_names is None \
or rname in exported_names \
or res.startswith('_') \
else "__" + res


def default_symbol_resolve(symbol_mapping):
Expand Down Expand Up @@ -75,7 +80,8 @@ def default_symbol_resolve(symbol_mapping):
def _map_symbols(rnames,
translation=dict(),
symbol_r2python=default_symbol_r2python,
symbol_resolve=default_symbol_resolve):
symbol_resolve=default_symbol_resolve,
exported_names=None):
"""
:param names: an iterable of rnames
:param translation: a mapping for R name->python name
Expand All @@ -89,7 +95,7 @@ def _map_symbols(rnames,
if rname in translation:
rpyname = translation[rname]
else:
rpyname = symbol_r2python(rname)
rpyname = symbol_r2python(rname, exported_names=exported_names)
symbol_mapping[rpyname].append(rname)
conflicts, resolutions = symbol_resolve(symbol_mapping)

Expand Down
12 changes: 12 additions & 0 deletions rpy2/tests/robjects/test_packages_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ def test___map_symbols():
(symbol_mapping,
conflicts,
resolutions) = p_u._map_symbols(rnames, translations)

def test__map_symbols_hidden_function():
rnames = ('exported_func', 'hidden_func', '.onAttach', '__NAMESPACE__')
(symbol_mapping,
conflicts,
resolutions) = p_u._map_symbols(rnames,
exported_names={'exported_func'})
assert len(symbol_mapping.keys()) == len(rnames) \
and 'exported_func' in symbol_mapping \
and '__hidden_func' in symbol_mapping \
and '_onAttach' in symbol_mapping \
and '__NAMESPACE__' in symbol_mapping


def test__fix_map_symbols_invalidonconflict():
Expand Down

0 comments on commit ccf22cc

Please sign in to comment.