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
class __setattr__ #2375
Comments
The |
There are plans to implement this? |
No, there are no plans, we've not had a request for it before. Implementing it would really give a performance hit because it requires doing a load (to check for What's the use-case? |
I'm working on porting a project from cPy (asn1 generator/parser) in uPy. This work in declarative mode for generator and i need to implement
|
any hint to implement it? |
The following will implement set and del. But your test case won't fully work because the
|
Thanks, the following (with your patch) will implement get, set and del support these special methods into object type for fully work in my test case.
now my test case:
produce the same result on cPy and uPy:
Thanks for your support. |
Attached .path file to enable getattr, delattr and setattr test:
|
Thanks, tried this with https://github.com/mavier/jsobject/blob/master/jsobject/jsobject.py and that works ok |
Let's first see if other people find they need/use it. As I said above it gives a big performance hit so should only be used if you really need such a feature. |
... And so should be submitted as a proper pull request, likely will fail CI checks, and then can go (slowly) from there. |
Shouldn't all changes be guarded by #if MICROPY_CPYTHON_COMPAT, instead of just the table? |
I suspect it's seldom used. I'd be loath to take a performance hit to stores which are heavily used. |
I can add a new macro into py/mpconfig.h like:
enable it into specific ports if needed and guard like
|
done |
The use case where this really helps is in an embedded system where we use uctypes to access hardware. With settings registers you can't just say |
Fair enough, but just using self.register.SetBitField(number) doesn't seem like that much of a deal-breaker? |
Agreed. So a wrapper class that implements getattr would intercept the field name, perform a lookup in the uctypes dictionary to see if the field name is legit, return another class that implements the SetBitfield method. This class would intercept the value write it to the uctypes field with the normal '=' operation (I need to test if this works) and then perform the serial bus write of the byte array that uctypes manages. Having setattr would eliminate the need for the second class. Only one class that decorates the field access with a bus write needs to be used. The simple assignment operator is all that is needed. This is all doable in C++ with a proxy class returned in the assignment operator that determines if the access is on the right or left hand side of the equal sign. |
I just came up with the same idea recently! |
That should be kept open, since there's code available provided here untill someone proposes a properly formatted PR. Though they should be kept optionnal for size/perf unless MICROPY_PY_DELATTR_SETATTR (1) is solving that issue. |
Can you explain why? |
MP3File: Bug fixes
I spent several hours discovering my code didn't work because circuitpython doesn't implement |
@rtwfroody See #2375 (comment) above -- MicroPython has supported You're right though, CircuitPython does not appear to enable this option by default on any boards. But you'll need to raise an issue for CircuitPython at https://github.com/adafruit/circuitpython/issues/new/choose |
Hi all,
i have this:
on cPy the execution result is:
on uPy:
How i can call setattr?
Thanks.
The text was updated successfully, but these errors were encountered: