Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
  • 13:27

    konstin on master

    Add support for macOS universal… Merge pull request #403 from me… (compare)

  • 13:27
    konstin closed #403
  • 13:27
    konstin closed #402
  • 13:26
    konstin commented #403
  • 13:25

    konstin on master

    Clippy lints (compare)

  • 06:02
    messense ready_for_review #403
  • 06:00
    messense labeled #403
  • 05:53
    messense commented #403
  • 05:46
    messense synchronize #403
  • 05:41
    messense review_requested #403
  • 05:41
    messense opened #403
  • 00:31
    messense opened #402
  • Jan 15 20:20
    houqp closed #401
  • Jan 15 20:20
    houqp commented #401
  • Jan 15 20:17
    houqp commented #401
  • Jan 15 20:15
    houqp commented #401
  • Jan 15 19:19
    konstin commented #375
  • Jan 15 19:05

    konstin on master

    Use PYO3_CROSS_LIB_DIR as libs … (compare)

  • Jan 15 19:00
    konstin commented #401
  • Jan 15 14:52

    github-actions[bot] on gh-pages

    Upload documentation for master (compare)

Harrison Burt
@ChillFish8
well i fixed the deadlocking issue, spawned the runtime in a seperate thread and then just made python wait, not sure why this causes an issue though as im just doing what tokio is already doing just with an extra thread.
but meh
Paul Fariello
@paulfariello
@Lehona yes that's simpler, thanks
Jonathan Reem
@reem
Hello, I'm back again trying to do something very unusual, I want to call python from typescript by going through rust in the middle, however I am having some issues with being unable to load the pyo3 compiled extension inside of nodejs - basically all of the python symbols are missing - is this even possible to resolve? If so what libraries do I have to load explicitly?
(using neon for the rust<->js bridge)
Jonathan Reem
@reem
Basically trying to do something like this https://github.com/ISNIT0/Node-Python-FFI but when I load my module in node I am missing all the upstream python symbols
Leon von Mulert
@Lehona
Does anybody have an idea how I can emulate Python's Enums? So far I simply used a ZST with class attributes, but the consumers of my library apparently insisted on constructing the value via a constructor rather than just accessing the class's members. I only need string and usize enums.
Leon von Mulert
@Lehona
@davidhewitt I saw in the issue tracker that Rust-style Enums (ADT) are supported, but not c-style enums?
Leon von Mulert
@Lehona
alternatively - and I'm not sure whether this would be sufficient - does PyO3 use/support the Into-Traits? In particular I get the error TypeError: Can't convert <FileType object at 0x7fe7fa782870> to PyString, because I'm trying to assign a class to a field of type string. Can I implement e.g. Into<String> or Into<PyString> to make it work?
Leon von Mulert
@Lehona
I could also go the other way around and make FileType assignable from String or PyString, that should be supported right?
Benjamin Scholtz
@BenSchZA
Hi everyone! I'm writing a library where I'd like to avoid mutation of a Python object passed from a Rust function to a Python function. Basically, implement the equivalent of some_python_method(arg.copy()). I've tried, from Rust, calling the PyObject .call_method0('copy') function as well as Rust's .copy() method before passing the PyObject argument to the Python function, but it still ends up mutating the original PyObject. Thought there's maybe something simple I'm missing.
Leon von Mulert
@Lehona
A PyObject is a Py<PyAny>, which is basically a Rc<T>, i.e. cloneing it will just bump the reference count and return a reference to the same object.
(I don't think PyObject has copy so I assume you meant clone?)
can you share some more of your code?
@davidhewitt I've made do with class attributes to emulate Python's enums, I think that's enough for my use case.
Benjamin Scholtz
@BenSchZA
You're correct - it's not a PyObject, it's a PyDict. I'll try condense the code into a gist.
Leon von Mulert
@Lehona
I think PyDict::Copyonly does a shallow copy - do you want to avoid mutation of the dictionary or mutation of the contained values/keys?
other than that I'm pretty sure that .copy() does exactly the same thing as it does in python (because it calls into python).
Benjamin Scholtz
@BenSchZA
That's a good point - I'd like to avoid mutation of the values. I'll need to use the copy package and use deepcopy! I'm sure that is the issue :) Will give that a try.
Thanks @Lehona ! deepcopy was the answer.
David Hewitt
@davidhewitt
@reem sounds like a complicated setup - might be easier for me to think about your case if you post it as an issue with a link to any example repo and other notes you can share.
David Hewitt
@davidhewitt
@Lehona for enums at the moment we only have #[derive(FromPyObject)] - if you're interested in helping finish off PyO3/pyo3#1045 I think we're not far from supporting C-style enums more fully. For the FileType object assignment I'm a little confused exactly what you're asking. I think you might be looking for .to_string() from std::string::ToString trait? Just impl Display for FileType? Or maybe if you're wanting PyString to automatically convert to FileType - that's an interesting one, not supported right now. Maybe could open an issue about it? Not sure if we'd want that, implicit conversions are not very Rusty...
Leon von Mulert
@Lehona
I just want an implicit conversion on python's side, i.e. if I execute foo.filetype_member = "foobar" I want it to convert naturally
but I think class attributes that produce a FileType are even better anyway!
Carl Kadie
@CarlKCarlK
Is there any way to define a function that works on any 2D array? Right now, I'm converting PyArray2's to ArrayViewMut2's (via "as_array_mut()"), which is OK. But for calls from Rust ndarray, converting to a view [via ".view_mut()"] is annoying. Ideally, the function would also work with Rust [[]]'s, but I can't figure that out at all.
-- Aside: Although I'm motivated by the Python to Rust use case, this is really a ndarray question. Is there a ndarray user forum where I could ask?
David Hewitt
@davidhewitt
"any 2D array" probably means you will want a trait which abstracts over all 2D arrays you care about. But this means generic function, which doesn't sit well with Python.
The best you could do is make a Rust function which is generic on said trait, and then a pyO3 #[pyfunction] which takes PyArray2 argument and then calls the Rust function.
You could try users.rust-lang.org if there's no dedicated ndarray forum
Jonathan Reem
@reem
Anyone written numpy bindings or examples of using numpy to/from rust
Harrison Burt
@ChillFish8
@reem ^
Sergi Delgado Segura
@sr-gi
Is there any way to automatically give getters (and alternatively setters) to a rust struct wrapped in a pyclass? I've been decorating functions with #[getter] for each element so far, but I'm wondering if there's a way with less boilerplate.
eg:
pub struct InnerStruct {
    pub a: u32,
    pub b: u16,

    ...

    pub z: u64,
}

#[pyclass]
pub struct MyWrapperClass {
    pub inner: InnerStruct,
}
Daniil Konovalenko
@daniil-konovalenko
I have seen this pattern (wrapping Rust struct in a #[pyclass]) several times in different projects, but I haven't yet got to understand why it might be necessary. Could someone please explain it to me?
Leon von Mulert
@Lehona
I think the example was just simplified, it's unrealistic to assume that a #[pyclass] contains only "primitive" (int, String, Vec, ..) types.
Daniil Konovalenko
@daniil-konovalenko
What types can cause trouble for #[pyclass]?
Georg Brandl
@birkenfeld
@daniil-konovalenko @Lehona I don't think the types of inner members are problematic, as long as they aren't exposed to Python. A wrapper struct should only be necessary if the "inner" type is from another library.
(also @sr-gi)
Sergi Delgado Segura
@sr-gi
@daniil-konovalenko I use this pattern since I'm binding a rust library I do not maintain, so I cannot modify the source of the original library. @Lehona Yeah, this is indeed a simplification, in the real case most of the types are non-primitive, what actually made me realise this may not be as straightforward as I though on first sight, given passing those types to Python require adding the con
version between the native rust type and the wrapped python type.
Also, to the best of my knowledge using PyO3 (which is not that extensive tbh), this pattern can be used to allow trait bounds from classes defined in Python. You can define the wrapper class and make it implement a certain trait bound, and then pass a wrapped Python class and use it where a trait bound is required
Sergi Delgado Segura
@sr-gi
I've seen that are some wrappers around time in PyO3, is there any conversion between them and core::time::Duration (or any recommended way to work around it)?
David Hewitt
@davidhewitt
If you are willing to add conversions to pyo3 for `datetime
datetime.timedelta, I think it'd be appropriate?
2 replies
Ritchie Vink
@ritchie46
I use pyo3 extensively in py-polars. After compilation of the python library, would it be possible for users (with their own rustc) to compile user defined functions that interchange with my Po3 objects?
David Hewitt
@davidhewitt
Hmm, that's an interesting question. I don't think there's an easy answer, but it should be possible. You'll probably need to make users link against the polars extension library? Not entirely sure. Want to open an issue and we can figure out a POC?
David
@drmason13
Hello, I have a question as a newbie. I've written a function in rust that aims to pretty print some data. That function returns a PyResult<String>
While creating the String, I use writeln!() to add newline characters. My problem is these are printed as escaped '\n' on the python side. I'd like them to be "decoded" I guess and actually print on the next line.
Ah! Sorry, no issue here! This is only occuring when quickly testing in a python interpreter (and not running print, D'oh!)- running as a python file with a print statement and all is well :D
Árni Dagur
@ArniDagur
Is there a way to call __repr__() for Rust types like bool?
to get a string like True rather than true
Something in the spirit of macro_rules! python_repr { ($x:expr) => { ToPyObject::to_object($x).__repr__() } }