Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
    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
    André Carvalhais
    @carvalhais
    Hi, I'm willing to possibly contribute to the project, is this the place to get start participating?
    Jeong YunWon
    @youknowone
    @carvalhais did you check this section in readme? https://github.com/RustPython/RustPython#contributing
    André Carvalhais
    @carvalhais
    @youknowone Yes sir, I have looked at it, I hope I may help fixing some of the failing tests.
    André Carvalhais
    @carvalhais

    So I successfully downloaded and compiled rustpython. Suppose I now want to contribute by fixing compatibility issues with the unit tests, as instructed in https://rustpython.github.io/blog/2020/04/04/how-to-contribute-by-cpython-unittest.html

    There it's said I should try to fix one or two interesting bugs, but where? In the unit test themselves? It doesn't make much sense to me.

    André Carvalhais
    @carvalhais
    For example, in Lib/test/test_complex.py, line 219-286, I believe this test should be "doable", since it involves simple concepts.
    Jim Fasarakis-Hilliard
    @DimitrisJim
    @carvalhais most tests pass there. The issue there (the same issue also exists for ints and floats) is that subclasses of complex, float and int (not sure if others also exist) must return a complex, float, int in their __complex__, __float__, __int__ and __trunc__ respectively.
    For complex objects, the place to raise this deprecation error would be in try_complex. I'm guessing similar functions would exist for floats and ints.
    André Carvalhais
    @carvalhais
    @DimitrisJim Thanks for your reply. What is not clear to me is "where" to fix this, as I mentioned, fixing the unit tests themselves doesn't defeat their purpose?
    Jim Fasarakis-Hilliard
    @DimitrisJim
    In short, you'd go into complex.rs and check what __complex__ returns. If it isn't a complex object (and instead is something else, including a complex subclass), you'd raise a Deprecation warning. Then you remove the skip decorator from the test and run it again to see that everything works.
    James Webber
    @jamestwebber
    I must be missing some setup step. When I run --install-pip I get NameError: name 'ParserCreate' is not defined which I see has been stubbed out in plistlib.py
    but it seems like it works for others?
    Noah
    @coolreader18
    @jamestwebber Ah, you're on macOS? Yeah, we haven't really done much for that yet, since plists are XML and we don't have expat XML bindings yet. Also, I don't have a Mac, so I haven't been able to do much with that, I think @youknowone does have one though.
    James Webber
    @jamestwebber
    ah i see.
    Jeong YunWon
    @youknowone
    I did scproxy (related to plistlib) but didnt do expat
    2 replies