Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Constructors for PyLong and PyInt #2221

Open
a1phyr opened this issue Mar 11, 2022 · 10 comments
Open

Constructors for PyLong and PyInt #2221

a1phyr opened this issue Mar 11, 2022 · 10 comments

Comments

@a1phyr
Copy link
Contributor

a1phyr commented Mar 11, 2022

Most types in pyo3::types have helper methods to create them easily (examples: PyString::new, PyBool::new). Is there a reason why there is no such method for PyLong and PyInt or is it an overlook ? Could we imagine adding them ?

Being able to build python objects in an easy and type-safe manner is a real strength of pyo3, and having a hole here seems a bit weird.

@mejrs
Copy link
Member

mejrs commented Mar 13, 2022

Thanks for the suggestion!

One thing that is tricky is deciding what this function should look like. Should it accept just a single integer type, or should it accept multiple types, via a trait?

Fwiw, I have used 0_u8.into_py(py) the one time I needed this. What is your use case for this?

@bjohnnyd
Copy link

bjohnnyd commented Jun 4, 2022

@mejrs if this is not being worked on happy to take it on?

@mejrs
Copy link
Member

mejrs commented Jun 4, 2022

@mejrs if this is not being worked on happy to take it on?

Please do!

@iyakushev
Copy link

@bjohnnyd are you still workin on it?
@mejrs I've also noticed that PyFloat::new accepts only c_double aka f64. Perhaps, if PyLong needs a more generic approach, it might be implemented for PyFloat as well

@bjohnnyd
Copy link

bjohnnyd commented Jul 11, 2022

@iyakushev I have some local changes for a future PR, so yes working on it. Though some discussion on whether it should be:

  1. A generic implementation?
  2. If generic, should it be via a trait?

would be beneficial.

I can make a PR and we can move the discussion there, unless @mejrs thinks otherwise?

@Trolldemorted
Copy link

as of now, is it possible to build rust functions that return Py<PyLong> without casting magic?

@nicoCalvo
Copy link

Is there a workaround for this? I'm having a hard time to build a PyInt/PyLong object from rust numeric types.

@birkenfeld
Copy link
Member

birkenfeld commented Jan 28, 2024

@nicoCalvo as @mejrs said, you can go the into_py route and then downcast. For interest, what to you need the PyLong object for?

@nicoCalvo
Copy link

@birkenfeld Thanks. I solved with rust native usize and converting to float. I was working on a rust implementation of python's dictor lib:
https://github.com/nicoCalvo/dicto_r/blob/v1.0/src/lib.rs#L265
Any comments/suggestions are very welcomed as these are my first steps with Pyo3 and have not so much experience with rust either.

@birkenfeld
Copy link
Member

@nicoCalvo I have to admit I don't understand that code. What is the original type of inner_object?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants