Skip to content

pathlib.Path.__rtruediv__ micropython incompatability. #621

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

Closed
BrianPugh opened this issue Feb 24, 2023 · 9 comments
Closed

pathlib.Path.__rtruediv__ micropython incompatability. #621

BrianPugh opened this issue Feb 24, 2023 · 9 comments

Comments

@BrianPugh
Copy link
Contributor

I wanted to add the following magic method to the pathlib.Path object:

    def __rtruediv__(self, other):
        return Path(other, self._path)

so that the following unittest would be satisfied:

    def test_rtruediv(self):
        res = "foo" / Path("bar")
        self.assertTrue(res == Path("foo/bar"))

However, I get the following stacktrace:

Traceback (most recent call last):
  File "/Users/brianpugh/.micropython/lib/unittest/__init__.py", line 392, in run_one
  File "./tests/test_pathlib.py", line 327, in test_rtruediv
TypeError: can't convert 'Path' object to str implicitly

The __rtruediv__ method never gets called, suggesting that the str.__truediv__ gets called in a way that is different than in cpython. I'm not familiar enough with cpython or micropython internals to know str's __truediv__ implementation, but something is up.

@dlech
Copy link

dlech commented Feb 24, 2023

Does your target have MICROPY_PY_ALL_SPECIAL_METHODS enabled?

@BrianPugh
Copy link
Contributor Author

I no longer see the options in ports/unix/mpconfigport.h, but I added it as true. Did a clean rebuild; same issue.

@dlech
Copy link

dlech commented Feb 24, 2023

And also MICROPY_PY_REVERSE_SPECIAL_METHODS?

@BrianPugh
Copy link
Contributor Author

no luck setting both to true; i'm digging a lil deeper.

@BrianPugh
Copy link
Contributor Author

BrianPugh commented Feb 24, 2023

I think this comment here is incorrect (as in, we shouldn't allow truediv with a string, not sure if comment is up to date with implementation):

https://github.com/micropython/micropython/blob/2e4dda3c2072c4de15ac8d51ddb471b9c3833db7/py/objstr.c#L376

I'll see if I can come up with a fix.

@BrianPugh
Copy link
Contributor Author

Will be resolved in:

micropython/micropython#10844

Closing this issue for now.

@dpgeorge
Copy link
Member

This (reverse inplace ops with str) is now supported in MicroPython.

@BrianPugh
Copy link
Contributor Author

excellent! iirc i have a test somewhere just to make sure this works as expected for pathlib, I'll open a PR. Thank you very much!

@BrianPugh
Copy link
Contributor Author

Added support in #663

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

No branches or pull requests

3 participants