-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
py/objcomplex: Allow strings of the form -A+Bj. #7623
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
Conversation
This works by splitting the string at the non-leading sign. The following strings will work. print(complex("1+2j")) print(complex("+1+2j")) print(complex("-1+2j")) Cases without a non-leading sign will be treated as they already were. Checks are added to force A to be real and B to be imaginary. This raises exceptions for the same cases as CPython, like Aj+B, even though MicroPython would otherwise return the correct result. Signed-off-by: Laurens Valk <laurens@pybricks.com>
64ab364
to
84bb633
Compare
Codecov Report
@@ Coverage Diff @@
## master #7623 +/- ##
==========================================
- Coverage 98.27% 98.27% -0.01%
==========================================
Files 154 154
Lines 19994 20001 +7
==========================================
+ Hits 19650 19656 +6
- Misses 344 345 +1
Continue to review full report at Codecov.
|
Thanks! This is +96 bytes on PYBV11. I notice that objcomplex.c is the only place that sets the Here's an attempt at doing this: ae8a38e Comes in at +64 bytes. This also deals with an extra case where there's leading spaces (i.e. the +/- for the real component may not be the first character, e.g. |
@jimmo 's version does look slightly more efficient, and it only creates one complex object whereas the version here creates 2 and then adds them (creating a third on the heap). What do you think @laurensvalk ? |
Thanks for your quick responses. I'm all for @jimmo's more efficient implementation, so feel free to close this one. For context, I came across this while documenting the MicroPython modules in more detail (still a work in progress) for use with Pybricks. Our plan is to provide typing and autocomplete in our online editor. |
Correctly raise OS error in socketpool_socket_recv_into()
This reverts commit 7e6e824. Fixes micropython#7770 The change in micropython#7623 needs to be revered; the raise-site added in micropython#7632 is the correct one and the one in socketpool needs to be reverted. This is not affecting 8.0.x because micropython#7623 was not back-ported to there before we realized it was not a full fix. Both micropython#7770 and micropython#7606 should be re-tested. I didn't test.
Allows parsing of a complex string with a similar result as CPython.
Until now, only strings like
3j
were supported, with strings like1+2j
giving a syntax error.The following strings will now also work.
Cases with only a sign at [0] or no sign at all are treated as they already were.
Checks are added to force A to be real and B to be imaginary. This raises exceptions for the same cases as CPython, like Aj+B, even though MicroPython would otherwise return the correct result.
Fixes this MicroPython forum post.
Edit: Fix commit message < 75 characters per line.
Signed-off-by: Laurens Valk laurens@pybricks.com