Random "TypeError: object not callable" for a bound method when called from C #14377
-
Hello! We have a set of custom C modules and some Python code on top of them. We use a lot of callbacks defined in the Python code and called from the C code. That all works well. However, just recently we made some modifications to the Python side and started using object methods as the callback functions. That seemed to work well too, but now I noticed that sometimes a callback that is set only once and has already been successfully called many times during the run, suddenly causes a "TypeError: object not callable" when it gets called from the C code. The Python object in question is created at the start at global level and never destroyed on purpose, so I don't see why it's method is suddenly not callable anymore. What could cause this and is there anything that I can try to fix it? We're using the 1.20.0 release of the MicroPython and the I see that there are some changes related to bound methods in release 1.22.0, but I'm not sure do they have something to do with this. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Haven't been able to solve this yet. Some updates:
If the program crashes, the stacktrace ends either at line
|
Beta Was this translation helpful? Give feedback.
-
This seemingly random behavior often is a result of a piece of memory allocated for the callback getting garbage collected. The first error is then quite typically because the memory which was originally allocated for the callback (i.e. some function object) itself was freed, and then allocated again for another type of object which happens to not be callable. To fix this you 'd have to make sure that the pointer to your callback memory is reachable by the grbage collector such that it sees it's in use, e.g. by storing it in a list which itself is registered as root pointer. Alternatively it's a bug in MicroPython and it's doing things which it shouldn't, but then you should post minimal code which reproduces the issue. |
Beta Was this translation helpful? Give feedback.
This seemingly random behavior often is a result of a piece of memory allocated for the callback getting garbage collected. The first error is then quite typically because the memory which was originally allocated for the callback (i.e. some function object) itself was freed, and then allocated again for another type of object which happens to not be callable. To fix this you 'd have to make sure that the pointer to your callback memory is reachable by the grbage collector such that it sees it's in use, e.g. by storing it in a list which itself is registered as root pointer. Alternati…