Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
    Klas Segeljakt
    @segeljakt

    Noob question, with python3-REPL I can import numpy as np, but with rustpython-REPL I get:

    >>>>> import numpy as np
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ModuleNotFoundError: No module named 'numpy'

    is there a way to make it work?

    Noah
    @coolreader18
    No, we don't support numpy yet
    Klas Segeljakt
    @segeljakt
    hmm, how does it work, do all libraries need to be implemented in Rust for it to work?
    fbpyr
    @fbpyr:tchncs.de
    [m]
    @segeljakt: I would assume quite a few of the pure python libs should work directly, but probably not the ones having compiled C/asm/.. parts in it - like numpy.
    Noah
    @coolreader18
    I think the current plan is to implement the hpy abi, and then eventually numpy might support that as well: https://github.com/hpyproject/HPy https://github.com/hpyproject/numpy-hpy
    Klas Segeljakt
    @segeljakt
    πŸ‘
    jrmarcum
    @jrmarcum
    image.png
    Just checking to see if anyone saw this. The dirs crate is evidently not managed anymore and is listed as not secure for use. As it stands no one can install rust python with this crate like that.
    Jesse Hoobergs
    @jhoobergs
    Hi Guys! I am trying to embed RustPython in one of my programs. I based myself on https://github.com/RustPython/RustPython/blob/master/examples/hello_embed.rs en that works fine, but now I need to be able to 'interact' with the python code. With interact I mean that I would like to get the value of a variable after the execution, is this possible?
    I actually want to run the python code and set a certain string as stdin and read another one at the stdout, but I could not find an example for that? So now I add some code to the executed python snipped that sets stdin and stdout to a StringIO object and it is the value of this stdout StringIO variable that I would like to read.
    But if it is also possible to set the stdin from rust before executing the python code and reading the stdout afterwards, that would be even better.
    Noah
    @coolreader18
    I would recommend having a Python function that runs stuff, rather than appending to the user's Python code. So like:
    const CODE: &str = "
    import io, sys
    def run(code):
        stdout = io.StringIO()
        sys.stdout = stdout
        try:
            exec(code, {}, {}) # maybe you'll want to do something with the locals+globals
        finally:
            sys.stdout = sys.__stdout__
        return stdout.getvalue()
    ";
    Noah
    @coolreader18
    And then run it and get the run function from the globals scope. Then, when you have code to run, call vm.compile() with the code string and pass the code object to the run() function you got.
    Jesse Hoobergs
    @jhoobergs
    Thanks Noah. Could you elaborate a bit more on how I should compile the run function and how I can get access to it in the global scope? Should I compile in Eval or Exec mode etc?
    Jesse Hoobergs
    @jhoobergs
    I got some inspiration from the mini_repl example and came up with the following solution:
    use rustpython_vm as vm;
    
    // See: https://github.com/RustPython/RustPython/blob/d6c8247886ff504f77e8995b08d9a15c22dfa8a0/examples/mini_repl.rs
    macro_rules! add_python_function {
        ( $scope:ident, $vm:ident, $src:literal $(,)? ) => {{
            // compile the code to bytecode
            let code = vm::py_compile!(source = $src);
            // convert the rustpython_bytecode::CodeObject to a PyCodeRef
            let code = $vm.new_code_object(code);
    
            // run the python code in the scope to store the function
            $vm.run_code_obj(code, $scope.clone())
        }};
    }
    
    fn main() {
        let res = execute(r#"print(sum(map(int,input().split(" "))))"#, "1 2 3 4 5");
        println!("{}", res.expect("Failed executing"));
    }
    
    fn execute(code: &str, input: &str) -> Result<String, ()> {
        vm::Interpreter::default().enter(|vm| {
            let scope = vm.new_scope_with_builtins();
    
            add_python_function!(
                scope,
                vm,
                r#"
    import _io, sys
    def run(code, input):
        stdout = _io.StringIO()
        sys.stdin = _io.StringIO(input)
        sys.stdout = stdout
        try:
            exec(code, {}, {}) # maybe you'll want to do something with the locals+globals
        finally:
            sys.stdout = sys.__stdout__
            sys.stdin = sys.__stdin__
        return stdout.getvalue()            
    "#
            );
    
            let run_input = format!("run('{}', '{}')", code, input); // TODO: escape quotes?
    
            match vm
                .compile(&run_input, vm::compile::Mode::Eval, "<embedded>".to_owned())
                .map_err(|err| vm.new_syntax_error(&err))
                .and_then(|code_obj| vm.run_code_obj(code_obj, scope.clone()))
            {
                Ok(output) => {
                    if !vm.is_none(&output) {
                        match vm.to_str(&output) {
                            Ok(s) => Ok(s.as_ref().to_owned()),
                            Err(e) => Err(()), // TODO
                        }
                    } else {
                        Err(()) // TODO
                    }
                }
                Err(e) => {
                    vm::exceptions::print_exception(vm, e);
                    Err(()) // TODO
                }
            }
        })
    }
    The only thing that I should do differently, is compiling the given code to bytecode instead of pasting the given code in the run_input variable. I don't know how I can make it execute the run function with the bytecode as it's argument.
    Technisha
    @devstar:matrix.org
    [m]
    Heya!
    I'm compiling RustPython from source to try it out so -w-
    But uh, is this okay?
    warning: unused import: `crate::builtins::dict::PyMapping`
        --> vm/src/stdlib/os.rs:1406:9
         |
    1406 |     use crate::builtins::dict::PyMapping;
         |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         |
         = note: `#[warn(unused_imports)]` on by default
    
    warning: unused import: `std::convert::TryFrom`
        --> vm/src/stdlib/os.rs:1412:9
         |
    1412 |     use std::convert::TryFrom;
         |         ^^^^^^^^^^^^^^^^^^^^^
    
    error[E0308]: mismatched types
       --> vm/src/stdlib/os.rs:162:33
        |
    162 | fn path_from_fd(raw_fd: i64) -> Result<PathBuf, String> {
        |    ------------                 ^^^^^^^^^^^^^^^^^^^^^^^ expected enum `std::result::Result`, found `()`
        |    |
        |    implicitly returns `()` as its body has no tail or `return` expression
    ...
    175 |             Err("fd not supported on wasi yet".to_owned());
        |                                                           - help: consider removing this semicolon
        |
        = note:   expected enum `std::result::Result<std::path::PathBuf, std::string::String>`
                found unit type `()`
    
    error: aborting due to previous error; 2 warnings emitted
    
    For more information about this error, try `rustc --explain E0308`.
    error: could not compile `rustpython-vm`
    
    To learn more, run the command again with --verbose.
    warning: build failed, waiting for other jobs to finish...
    Technisha
    @devstar:matrix.org
    [m]
    And it won't build the parser now
    After 10 minutes it's still frozen
    Ah it finally failed :/
    Noah
    @coolreader18
    Ach, sorry about that, I can push a fix for that in a sec
    Noah
    @coolreader18
    @devstar:matrix.org try pulling again?
    1 reply
    Noah
    @coolreader18
    Jesse Hoobergs
    @jhoobergs
    Great, that works! I had to change a small thing (dict to globals), see my comment on the gist
    Technisha
    @devstar:matrix.org
    [m]
    Attempting to build rn!
    Technisha
    @devstar:matrix.org
    [m]
    Worked!
    How do i install pip with RustPython?
    Technisha
    @devstar:matrix.org
    [m]
    :p
    Noah
    @coolreader18
    Just run http://bootstrap.pypa.io/get-pip.py in rustpython, you'll need to build with the ssl feature (cargo build --release --features ssl) and also maybe use the coolreader18/misc-fixes branch which fixes a regression that affected pip
    Technisha
    @devstar:matrix.org
    [m]
    Ah alright!
    ceo-of-programming
    @ceo-of-programming
    The logo is cute!
    sychomaiko
    @sychomaiko:matrix.org
    [m]
    hi guys
    is rustpython usable? i want to use it in a project
    Noah
    @coolreader18
    Very interesting performance-oriented fork of CPython! https://github.com/facebookincubator/cinder
    Ryuta Suzuki
    @oroppas

    Don't know if anyone tried IPython before but it seems RustPython can handle the basic IPython already

    Screenshot from 2021-05-17 11-21-47.png

    It's pretty cool!

    One caveat is ctypes
    rustpython -m pip install ipython
        Finished release [optimized] target(s) in 0.16s
         Running `rustpython/target/release/rustpython -m pip install ipython`
    Defaulting to user installation because normal site-packages is not writeable
    Collecting ipython
      Using cached ipython-7.23.1-py3-none-any.whl (785 kB)
    Requirement already satisfied: setuptools>=18.5 in /home/ryuta/.local/lib/rustpython3.9/site-packages (from ipython) (56.2.0)
    Collecting jedi>=0.16
      Downloading jedi-0.18.0-py2.py3-none-any.whl (1.4 MB)
         |β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 1.4 MB 29 kB/s 
    Collecting decorator
      Downloading decorator-5.0.8-py3-none-any.whl (8.9 kB)
    Collecting pickleshare
      Downloading pickleshare-0.7.5-py2.py3-none-any.whl (6.9 kB)
    Collecting traitlets>=4.2
      Downloading traitlets-5.0.5-py3-none-any.whl (100 kB)
         |β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 100 kB 674 kB/s 
    Collecting prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0
      Downloading prompt_toolkit-3.0.18-py3-none-any.whl (367 kB)
         |β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 367 kB 9.4 MB/s 
    Requirement already satisfied: pygments in /home/ryuta/.local/lib/rustpython3.9/site-packages (from ipython) (2.8.1)
    Collecting backcall
      Downloading backcall-0.2.0-py2.py3-none-any.whl (11 kB)
    Collecting matplotlib-inline
      Downloading matplotlib-inline-0.1.2.tar.gz (7.5 kB)
        ERROR: Command errored out with exit status 1:
         command: /home/ryuta/packages/rustpython/rustpython/target/release/rustpython -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-hbfr1vlb/matplotlib-inline_ed63da4beb514e6bb9e29ef16cfb17b8/setup.py'"'"'; __file__='"'"'/tmp/pip-install-hbfr1vlb/matplotlib-inline_ed63da4beb514e6bb9e29ef16cfb17b8/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-pip-egg-info-vrmiqp4g
             cwd: /tmp/pip-install-hbfr1vlb/matplotlib-inline_ed63da4beb514e6bb9e29ef16cfb17b8/
        Complete output (9 lines):
        Traceback (most recent call last):
          File "<stdin>", line 1, in <module>
          File "/home/ryuta/.local/lib/rustpython3.9/site-packages/setuptools/__init__.py", line 18, in <module>
            from setuptools.dist import Distribution
          File "/home/ryuta/.local/lib/rustpython3.9/site-packages/setuptools/dist.py", line 36, in <module>
            from setuptools import windows_support
          File "/home/ryuta/.local/lib/rustpython3.9/site-packages/setuptools/windows_support.py", line 2, in <module>
            import ctypes
        ModuleNotFoundError: No module named 'ctypes'
        ----------------------------------------
    WARNING: Discarding https://files.pythonhosted.org/packages/c1/fb/3361c4bf5ae8ffb9249132e70f4853ef511c0894a938fdff29320df55534/matplotlib-inline-0.1.2.tar.gz#sha256=f41d5ff73c9f5385775d5c0bc13b424535c8402fe70ea8210f93e11f3683993e (from https://pypi.org/simple/matplotlib-inline/) (requires-python:>=3.5). Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
      Downloading matplotlib_inline-0.1.2-py3-none-any.whl (8.2 kB)
    
    ...
    Ryuta Suzuki
    @oroppas
    (continued)
    Collecting pexpect>4.3
      Downloading pexpect-4.8.0-py2.py3-none-any.whl (59 kB)
         |β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 59 kB 355 kB/s 
    Collecting parso<0.9.0,>=0.8.0
      Downloading parso-0.8.2-py2.py3-none-any.whl (94 kB)
         |β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 94 kB 180 kB/s 
    Collecting ptyprocess>=0.5
      Downloading ptyprocess-0.7.0-py2.py3-none-any.whl (13 kB)
    Collecting wcwidth
      Downloading wcwidth-0.2.5-py2.py3-none-any.whl (30 kB)
    Collecting ipython-genutils
      Downloading ipython_genutils-0.2.0-py2.py3-none-any.whl (26 kB)
    Installing collected packages: ipython-genutils, wcwidth, traitlets, ptyprocess, parso, prompt-toolkit, pickleshare, pexpect, matplotlib-inline, jedi, decorator, backcall, ipython
      WARNING: The scripts iptest, iptest3, ipython and ipython3 are installed in '/home/ryuta/.local/bin' which is not on PATH.
      Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
    Successfully installed backcall-0.2.0 decorator-5.0.8 ipython-7.23.1 ipython-genutils-0.2.0 jedi-0.18.0 matplotlib-inline-0.1.2 parso-0.8.2 pexpect-4.8.0 pickleshare-0.7.5 prompt-toolkit-3.0.18 ptyprocess-0.7.0 traitlets-5.0.5 wcwidth-0.2.5
    Noah
    @coolreader18
    Yeah, it looks like that's hopefully fine since it only needed that to build a wheel but it can fallback to just running setup.py
    But there is a ctypes implementation half-finished in a PR somewhere, so hopefully we'll eventually have that
    Ryuta Suzuki
    @oroppas
    That is great news. Right now, simply building hpy requires ctypes.
    Darley Barreto
    @darleybarreto
    Me and @rodrigocam started the ctypes impl. I would say I might have time to go back to it at some point in the next three months. If any of you guys is interested to help, I could help on mentoring(ish).
    Jim Fasarakis-Hilliard
    @DimitrisJim
    Is there anything similar to the Sequence Protocol as CPython defines it? I've found an ItemProtocol that appears to be similar to the Mapping protocol but can't seem to find one for Sequences. Is vm.get_special_method a viable alternative for now?
    Jeong YunWon
    @youknowone
    maybe trait SimpleSeq covers a part of them
    Jeong YunWon
    @youknowone
    i found it doesn’t match to sequence protocol that well
    so we don’t have sequence protocol yet