Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
    Noah
    @coolreader18
    yeah, wooo 🙃
    I think CPython makes the object repr dependent on the __slots__, I assume we would just have a SlottedObjectpayload type with a vec field instead?
    Jeong YunWon
    @youknowone
    that make sense. but i don’t know how cpython handle it
    that sounds like to bea perfect solution if multiple inheritance is not possible with slot parent and other payload-attached types
    Zomatree
    @Zomatree
    do you have an example of using the _json module, i cant seem to get it to work
    Zomatree
    @Zomatree
    nvm got it
    Jonathan
    @jboi:jboi.nl
    [m]
    "lobby" implies there are more rooms, are there more rooms?
    Jeong YunWon
    @youknowone
    no, it is lobby and plaza at the same time. we don’t have that much traffic to separate channels yet
    Jonathan
    @jboi:jboi.nl
    [m]
    ah ok
    Prefix Required Jones
    @mojosd_twitter
    A discord server would be nice.
    Prefix Required Jones
    @mojosd_twitter
    Having threads would make reading these exchanges easier I think. Also, the Dev Guide mentions that there are contributors here willing to mentor those of us who are new to the project, and in my case new to OSS. If you one of those people willing to help and mentor someone new to RustPython, please let me know. I'm hoping to get some help with how to work with this project. Thank you.
    10 replies
    Pluriscient
    @Pluriscient

    A discord server would be nice.

    Zulip is also an alternative: https://zulip.com/

    0xc
    @tcarrio:matrix.org
    [m]
    What does the performance of RustPython look like so far?
    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