Exemple #1
0
    def get_reticulate_completions(document, complete_event):
        word = document.get_word_before_cursor()
        prefix_length = settings.completion_prefix_length
        if len(word
               ) < prefix_length and not complete_event.completion_requested:
            return []

        glo = rcopy(rcall(("reticulate", "py_run_string"), "globals()"))
        loc = rcopy(rcall(("reticulate", "py_run_string"), "locals()"))
        try:
            script = jedi.Interpreter(document.text,
                                      column=document.cursor_position_col,
                                      line=document.cursor_position_row + 1,
                                      path="input-text",
                                      namespaces=[glo, loc])
            return [
                Completion(
                    text_type(c.name_with_symbols),
                    len(text_type(c.complete)) -
                    len(text_type(c.name_with_symbols)))
                for c in script.completions()
            ]

        except Exception:
            return []
Exemple #2
0
def test_strings():
    assert rcall("identical", robject("abc"), rstring("abc"), _convert=True)
    assert rcall("identical", robject("β"), rstring("β"), _convert=True)
    assert rcall("identical", robject("你"), rstring("你"), _convert=True)
    assert rcall("identical",
                 robject(['a', 'b']),
                 reval("c('a', 'b')"),
                 _convert=True)
Exemple #3
0
def installed_packages():
    try:
        return rcall(("base", ".packages"), **{
            "all.available": True,
            "_convert": True
        })
    except Exception:
        return []
Exemple #4
0
def apply_settings(session, settings):
    setoption("prompt", settings.prompt)

    if settings.auto_width:
        output_width = session.app.output.get_size().columns
        if output_width:
            setoption("width", output_width)

    # necessary on windows
    setoption("menu.graphics", False)

    if is_windows() and roption("crayon.enabled", None) is None:
        setoption("crayon.enabled", True)

    # enables completion of installed package names
    if rcopy(rcall(("utils", "rc.settings"), "ipck")) is None:
        rcall(("utils", "rc.settings"), ipck=True)
Exemple #5
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
Exemple #6
0
def apply_settings(session, settings):
    setoption("prompt", settings.prompt)

    if settings.auto_width:
        output_width = session.app.output.get_size().columns
        if output_width:
            setoption("width", output_width)

    # necessary on windows
    setoption("menu.graphics", False)

    def askpass(message):
        from prompt_toolkit import prompt
        return prompt(message, is_password=True)

    setoption("askpass", askpass)

    # enables completion of installed package names
    if rcopy(rcall(("utils", "rc.settings"), "ipck")) is None:
        rcall(("utils", "rc.settings"), ipck=True)
Exemple #7
0
def get_reticulate_completions(document, complete_event):
    word = document.get_word_before_cursor()
    if len(word) < 3 and not complete_event.completion_requested:
        return []

    glo = rcopy(rcall(("reticulate", "py_run_string"), "globals()"))
    loc = rcopy(rcall(("reticulate", "py_run_string"), "locals()"))
    try:
        script = jedi.Interpreter(document.text,
                                  column=document.cursor_position_col,
                                  line=document.cursor_position_row + 1,
                                  path="input-text",
                                  namespaces=[glo, loc])
    except Exception:
        script = None

    if not script:
        return []

    return list(script.completions())
Exemple #8
0
def test_numbers():
    assert rcall("identical", robject(1), rint(1), _convert=True)
    assert rcall("identical", robject(1.0), rdouble(1), _convert=True)
    assert not rcall("identical", robject(1), rdouble(1), _convert=True)
    assert not rcall("identical", robject(1.0), rint(1), _convert=True)

    assert rcall("identical",
                 robject(complex(1, 2)),
                 reval("1 + 2i"),
                 _convert=True)

    assert rcall("identical",
                 robject([1, 2]),
                 reval("c(1L, 2L)"),
                 _convert=True)
    assert rcall("identical",
                 robject([1.0, 2.0]),
                 reval("c(1, 2)"),
                 _convert=True)
    assert rcall("identical",
                 robject([complex(1, 2), complex(2, 1)]),
                 reval("c(1 + 2i, 2 + 1i)"),
                 _convert=True)
Exemple #9
0
def package_is_loaded(pkg):
    return pkg in rcopy(rcall(("base", "loadedNamespaces")))
Exemple #10
0
def test_rprint():
    la = rlang(robject(rprint, asis=True), robject(1))
    assert rcall("capture.output", la, _convert=True) == ['[1] 1', 'NULL']
Exemple #11
0
def user_path(*args):
    return make_path(rcopy(rcall(("base", "path.expand"), "~")), *args)
Exemple #12
0
def source_file(path):
    rcall(("base", "source"), path, rcall(("base", "new.env")))
Exemple #13
0
def test_rprint(gctorture):
    la = rlang(robject(rprint, asis=True, invisible=True), robject(1))
    assert rcall("capture.output", la, _convert=True) == "[1] 1"
Exemple #14
0
def test_raw():
    assert rcall("rawToChar", robject("raw", b"hello"),
                 _convert=True) == "hello"
Exemple #15
0
 def reticulate_prompt(*args):
     rcall(("base", "source"),
           os.path.join(os.path.dirname(__file__), "data",
                        "register_reticulate.R"), new_env())
Exemple #16
0
 def reticulate_hook(*args):
     rcall(("base", "source"),
           os.path.join(os.path.dirname(__file__), "data",
                        "patching_reticulate.R"), new_env())
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)
Exemple #18
0
def test_booleans():
    assert rcall("identical",
                 robject([True, False]),
                 reval("c(TRUE, FALSE)"),
                 _convert=True)
Exemple #19
0
def test_rcall_error(gctorture):
    with pytest.raises(Exception) as excinfo:
        rcall("sum", ["a", "b"])
        assert "invalid 'type' (character) of argument" in str(excinfo.value)
Exemple #20
0
def test_ordered_list():
    d = OrderedDict([("a", 2), ("b", "hello")])
    assert rcall("identical",
                 robject(d),
                 reval("list(a = 2L, b = 'hello')"),
                 _convert=True)
Exemple #21
0
def test_none():
    assert rcall("identical", robject(None), reval("NULL"), _convert=True)
Exemple #22
0
def execute_key_bindings_script(*args):
    rcall(
        ("base", "source"),
        os.path.join(os.path.dirname(__file__), "R", "key_bindings.R"),
        rcall("new.env"))
Exemple #23
0
def finalizer(cleanup):
    rcall(("base", "reg.finalizer"),
          rcall(("base", "getOption"), "rchitect.py_tools"),
          cleanup,
          onexit=True)
Exemple #24
0
def gctorture():
    rcall("gctorture", True)
    yield
    rcall("gctorture", False)
Exemple #25
0
def reticulate_prompt_hook(*args):
    rcall(
        ("base", "source"),
        os.path.join(os.path.dirname(__file__), "R", "reticulate.R"),
        rcall("new.env"))
Exemple #26
0
def test_lambda():
    rcopy(rcall(robject(lambda x: x + 3), 4)) == 7
Exemple #27
0
 def reticulate_message_hook(*args):
     if not roption("radian.suppress_reticulate_message", False):
         rcall("packageStartupMessage", RETICULATE_MESSAGE)
Exemple #28
0
def user_path(*args):
    return os.path.join(rcopy(rcall(("base", "path.expand"), "~")), *args)
Exemple #29
0
def test_r_to_py_rchitect_object():
    reval("library(reticulate)")
    foo = Foo()
    x = rcall("r_to_py", robject(foo))
    assert "python.builtin.object" in rcopy(rcall("class", x))
Exemple #30
0
def greeting():
    info = rcopy(rcall(rsym("R.Version")))
    return "{} -- \"{}\"\nPlatform: {} ({}-bit)\n".format(
        info["version.string"], info["nickname"], info["platform"], 8 * struct.calcsize("P"))