コード例 #1
0
def test_functions():
    def f(x):
        return x + 3

    fun = robject(f)
    assert "PyCallable" in rclass(fun)
    assert rcopy(rcall(fun, 4)) == f(4)
    assert rcopy(rcall(fun, x=4)) == f(4)

    fun = robject(lambda x: x + 3, convert=False)
    assert "PyCallable" in rclass(fun)
    ret = rcall(fun, 4)
    assert "PyObject" in rclass(ret)
    assert rcopy(ret) == f(4)

    makef = robject(lambda: f, convert=False)
    ret = rcall(makef)
    assert "PyCallable" in rclass(ret)
    assert rcopy(ret) == f
コード例 #2
0
def test_py_tools():
    env = rcall(("base", "new.env"))
    reval("getOption('rchitect.py_tools')$attach()", envir=env)
    assert "import" in rcall("names", env, _convert=True)

    reval("os <- import('os')", envir=env)

    path = reval("""
        os$path$join("foo", "bar")
    """, envir=env)
    assert "character" in rclass(path)
    assert rcopy(path) == os.path.join("foo", "bar")

    path = reval("""
        py_call(os$path$join, "foo", "bar")
    """,
                 envir=env)
    assert "PyObject" in rclass(path)
    assert rcopy(path) == os.path.join("foo", "bar")

    ret = reval("""
        bulitins <- import_builtins()
        len <- bulitins$len
        len(py_eval("[1, 2, 3]"))
    """,
                envir=env)
    assert rcopy(ret) == 3

    ret = reval("""
        pyo <- py_object("hello")
        py_copy(pyo)
    """,
                envir=env)
    assert rcopy(ret) == "hello"

    ret = reval("""
        x <- py_eval("[1, 2, 3]")
        x[2L]
    """,
                envir=env)
    assert rcopy(ret) == 3

    ret = reval("""
        x <- py_eval("[1, 2, 3]")
        x[2L] <- 4L
        x
    """,
                envir=env)
    assert rcopy(ret) == [1, 2, 4]

    ret = reval("""
        d <- dict(a = 1L, b = 2L)
        d['b']
    """,
                envir=env)
    assert rcopy(ret) == 2

    ret = reval("""
        Foo <- py_eval("type(str('Foo'), (object,), {})")
        foo <- Foo()
        foo$x <- 1L
        foo
    """,
                envir=env)
    assert rcopy(ret).x == 1

    assert rcopy(reval("py_unicode('hello')", envir=env)) == "hello"

    assert rcopy(reval("tuple('a', 3)", envir=env)) == ('a', 3)
コード例 #3
0
def test_reval(gctorture):
    exp = rparse("x = 1L")
    assert "expression" in rclass(exp)
    assert "integer" in rclass(reval(exp))
    assert str(exp) == 'RObject{EXPRSXP}\nexpression(x = 1L)'