-
Maybe someone can point me to the doc for these methods - I'm using them but wanted to check:
Same question for mp_obj_str_get_data : is it a copy or a reference and what happens after? Right now I copy the buffer if I want to use the data afterwards, but just want to know if thats a waste of time/space.... thanks for any pointers. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
The internal / C functions generally don't have separate docs, so it'd generally go straight to the source. Line 590 in 5473200 The These functions don't copy data, they just give a C struct references to the data in the buffer. If you keep a copy of the obj reference to the buffer that gets passed into these functions that should keep your original buffer alive, this is essentially holds the reference. |
Beta Was this translation helpful? Give feedback.
-
As Andrew said, the
It's owned by the object that gave you the buffer. In general it's not safe to hold onto this if you're going to let other code run (i.e. return to Python). If you're sure that the object won't change its buffer, then you can hold onto the pointer as long as you also ensure that the object won't be garbage collected. If the object can change its buffer then you shouldn't keep the pointer, instead keep the mp_obj_t and use mp_get_buffer each time you need the buffer. (i.e. in both cases, keeping a reference to the mp_obj_t is the safe thing to do).
I think this should be covered above -- you need to hold a reference to the mp_obj_t. This either needs to be via MP_REGISTER_ROOT_POINTER or from an object that has at least the lifetime of what you need. See e.g. extmod/modframebuf.c where the mp_obj_framebuf_t holds both the bytearray object and the underlying buffer. So as long as something holds a reference to the framebuf, the backing bytearray is also kept live, even if the user code doesn't keep a reference to the bytearray directly.
Same as above. See #12404 (comment) where I forgot to take this into account recently.
With strings, they can be interned in which case the buffer is totally fine to hold on to. But of they're not interned then the string object might be gc'ed. But in either case strings are immutable, so as long as you hold onto the mp_obj_t you're fine.
|
Beta Was this translation helpful? Give feedback.
-
thanks a lot guys, that gives me a much better picture of what's going on and what I can (and can't) do.... |
Beta Was this translation helpful? Give feedback.
As Andrew said, the
_raise
version will raise an exception for you if the buffer doesn't support the requested read/write mode.It's owned by the object that gave you the buffer.
In general it's not safe to hold onto this if you're going to let other code run (i.e. return to Python).
If you're sure that the object won't change its buffer, then you can hold onto the pointer as long as you also ensure that the object won't be garbage collected.
If the object can change its buffer then you shouldn't keep the pointer, instead keep the mp_obj_t and use mp_…