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

feature suggestion: extending PyTuple #1647

Open
alonblade opened this issue Jun 1, 2021 · 3 comments
Open

feature suggestion: extending PyTuple #1647

alonblade opened this issue Jun 1, 2021 · 3 comments

Comments

@alonblade
Copy link
Contributor

Right now it is impossible to

#[pyclass(extends=PyTuple)]
struct Bla {
// ...
}

I'd like to do that to get a rust namedtuple like class.

What would that entail?

@davidhewitt
Copy link
Member

At the moment this is disabled because we weren't confident that subclassing PyTuple would work correctly.

The complexity is during __new__ implementation. A subclass of PyTuple needs to pass the number of items in the tuple up to the PyTuple base class, which we don't do correctly right now. In addition, the items are laid out in memory after the main class data. This shouldn't be a problem, but we weren't sure.

I actually have a patch which might tentatively fix the above (among with some other problems). If you give me a few days, I'll try to push that patch and cc you. Once it works, we can try subclassing PyTuple and see whether we can make it work.

@alonblade
Copy link
Contributor Author

alonblade commented Jun 1, 2021 via email

@staticintlucas
Copy link

As an alternative, for the specific case of creating namedtuple-like classes, you could look at using PyStructSequence under the hood.

This is what CPython uses internally to create sys.version_info, for example.

From the docs:

Struct sequence objects are the C equivalent of namedtuple() objects, i.e. a sequence whose items can also be accessed through attributes.

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

4 participants