Esempio n. 1
0
    def __init__(self,
                 sexp: rinterface.SexpClosure,
                 init_prm_translate=None,
                 on_conflict='warn',
                 symbol_r2python=default_symbol_r2python,
                 symbol_resolve=default_symbol_resolve):
        super(SignatureTranslatedFunction, self).__init__(sexp)
        if init_prm_translate is None:
            self._prm_translate = OrderedDict()
        else:
            assert isinstance(init_prm_translate, dict)
            self._prm_translate = init_prm_translate

        formals = self.formals()
        if formals.__sexp__._cdata != rinterface.NULL.__sexp__._cdata:
            (symbol_mapping, conflicts,
             resolutions) = _map_symbols(formals.names,
                                         translation=self._prm_translate,
                                         symbol_r2python=symbol_r2python,
                                         symbol_resolve=symbol_resolve)

            msg_prefix = ('Conflict when converting R symbols'
                          ' in the function\'s signature:\n- ')
            exception = ValueError
            _fix_map_symbols(symbol_mapping, conflicts, on_conflict,
                             msg_prefix, exception)
            symbol_mapping.update(resolutions)
            reserved_pynames = set(dir(self))

            self._prm_translate.update(
                (k, v[0]) for k, v in symbol_mapping.items())
        if hasattr(sexp, '__rname__'):
            self.__rname__ = sexp.__rname__
def test__fix_map_symbols_conflictwarn():
    msg_prefix = ''
    exception = ValueError
    symbol_mappings = {'foo': 'foo'}
    conflicts = {'foo_bar': ['foo.bar', 'foo_bar']}
    on_conflict = 'warn'
    with pytest.warns(UserWarning):
        p_u._fix_map_symbols(symbol_mappings, conflicts, on_conflict,
                             msg_prefix, exception)
def test__fix_map_symbols_invalidonconflict():
    msg_prefix = ''
    exception = ValueError
    symbol_mappings = {'foo': 'foo'}
    conflicts = {'foo_bar': ['foo.bar', 'foo_bar']}
    on_conflict = 'foo'
    with pytest.raises(ValueError):
        p_u._fix_map_symbols(symbol_mappings, conflicts, on_conflict,
                             msg_prefix, exception)
Esempio n. 4
0
    def __fill_rpy2r__(self, on_conflict='fail'):
        """ Fill the attribute _rpy2r.

        - on_conflict: 'fail' or 'warn' (default: 'fail')
        """
        assert(on_conflict in ('fail', 'warn'))

        name = self.__rname__

        (symbol_mapping,
         conflicts,
         resolutions) = _map_symbols(
             self._env,
             translation=self._translation,
             symbol_r2python=self._symbol_r2python,
             symbol_resolve=self._symbol_resolve
         )
        msg_prefix = ('Conflict when converting R symbols'
                      ' in the package "%s"'
                      ' to Python symbols: \n-' % self.__rname__)
        exception = LibraryError
        _fix_map_symbols(symbol_mapping,
                         conflicts,
                         on_conflict,
                         msg_prefix,
                         exception)
        symbol_mapping.update(resolutions)
        reserved_pynames = set(dir(self))
        for rpyname, rnames in symbol_mapping.items():
            # last paranoid check
            if len(rnames) > 1:
                raise ValueError(
                    'Only one R name should be associated with %s '
                    '(and we have %s)' % (rpyname, str(rnames))
                )
            rname = rnames[0]
            if rpyname in reserved_pynames:
                raise LibraryError('The symbol ' + rname +
                                   ' in the package "' + name + '"' +
                                   ' is conflicting with' +
                                   ' a Python object attribute')
            self._rpy2r[rpyname] = rname
            if (rpyname != rname) and (rname in self._exported_names):
                self._exported_names.remove(rname)
                self._exported_names.add(rpyname)
            try:
                riobj = self._env[rname]
            except rinterface.RRuntimeError as rre:
                warn(str(rre))
            rpyobj = conversion.rpy2py(riobj)
            if hasattr(rpyobj, '__rname__'):
                rpyobj.__rname__ = rname
            # TODO: shouldn't the original R name be also in the __dict__ ?
            self.__dict__[rpyname] = rpyobj
Esempio n. 5
0
    def __fill_rpy2r__(self, on_conflict = 'fail'):
        """ Fill the attribute _rpy2r.

        - on_conflict: 'fail' or 'warn' (default: 'fail')
        """

        assert(on_conflict in ('fail', 'warn'))

        name = self.__rname__

        (symbol_mapping, 
         conflicts, 
         resolutions) = _map_symbols(self._env,
                                     translation = self._translation,
                                     symbol_r2python = self._symbol_r2python,
                                     symbol_check_after = self._symbol_check_after)
        msg_prefix = 'Conflict when converting R symbols'+\
                     ' in the package "%s"' % self.__rname__ +\
                     ' to Python symbols: \n-'
        exception = LibraryError
        _fix_map_symbols(symbol_mapping,
                         conflicts,
                         on_conflict,
                         msg_prefix,
                         exception)
        symbol_mapping.update(resolutions)
        reserved_pynames = set(dir(self))
        for rpyname, rnames in symbol_mapping.items():
            # last paranoid check
            if len(rnames) > 1:
                raise ValueError('Only one R name should be associated with %s (and we have %s)' % (rpyname, str(rnames)))
            rname = rnames[0]
            if rpyname in reserved_pynames:
                raise LibraryError('The symbol ' + rname +\
                                   ' in the package "' + name + '"' +\
                                   ' is conflicting with' +\
                                   ' a Python object attribute')
            self._rpy2r[rpyname] = rname
            if (rpyname != rname) and (rname in self._exported_names):
                self._exported_names.remove(rname)
                self._exported_names.add(rpyname)
            try:
                riobj = self._env[rname]
            except rinterface.RRuntimeError as rre:
                warn(str(rre))
            rpyobj = conversion.ri2ro(riobj)
            if hasattr(rpyobj, '__rname__'):
                rpyobj.__rname__ = rname
            #FIXME: shouldn't the original R name be also in the __dict__ ?
            self.__dict__[rpyname] = rpyobj
Esempio n. 6
0
    def __init__(self, sexp,
                 init_prm_translate = None,
                 on_conflict = 'warn',
                 symbol_r2python = default_symbol_r2python,
                 symbol_check_after = default_symbol_check_after):
        super(SignatureTranslatedFunction, self).__init__(sexp)
        if init_prm_translate is None:
            prm_translate = OrderedDict()
        else:
            assert isinstance(init_prm_translate, dict)
            prm_translate = init_prm_translate

        formals = self.formals()
        if formals is not rinterface.NULL:
            (symbol_mapping, 
             conflicts, 
             resolutions) = _map_symbols(formals.names,
                                         translation = prm_translate,
                                         symbol_r2python = symbol_r2python,
                                         symbol_check_after = symbol_check_after)

            msg_prefix = 'Conflict when converting R symbols'+\
                ' in the function\'s signature:\n- '
            exception = ValueError
            _fix_map_symbols(symbol_mapping,
                             conflicts,
                             on_conflict,
                             msg_prefix,
                             exception)
            symbol_mapping.update(resolutions)
            reserved_pynames = set(dir(self))
            
            prm_translate.update((k, v[0]) for k, v in symbol_mapping.items())
        self._prm_translate = prm_translate
        if hasattr(sexp, '__rname__'):
            self.__rname__ = sexp.__rname__