Re: resize content, not type
eryk sun <[email protected]> Thu, 28 Jan 2016 08:03:44 -0600
| Newsgroups | gmane.comp.python.ctypes |
|---|---|
| Message-ID | <CACL+1auEBqW1t+yKZ95UQ4GY0PT=iWi1-HqXRZ+qH4GwXNL=JA@mail.gmail.com> |
On Mon, Jan 25, 2016 at 8:37 PM, Carlos Pita <[email protected]> wrote: > > Also, regarding this last part: from_address created instances have > nothing in _objects, while from_buffer ones have a reference there. > Does this mean that an object created from the buffer of another one > will keep the buffer alive despite the first one may have been garbage > collected at some point? 2.x from_buffer stores a reference to the source object in _objects. In 3.x it's a memoryview that in turn references the source object. In either case the object that owns the memory is kept alive. There's no transfer of ownership. It's just CPython reference counting. You can also use a pointer: short_array = (c_short * 4)() resize(short_array, 32) short_array_ex = POINTER(c_short * 16)(short_array)[0] >>> short_array_ex._b_base_ <__main__.LP_c_short_Array_16 object at 0x7fb07a409710> >>> short_array_ex._b_base_._objects {'1': <__main__.c_short_Array_4 object at 0x7fb07a4097a0>, '0': {}} Or a cast for a similar result: short_array_ex = cast(short_array, POINTER(c_short * 16))[0] >>> short_array_ex._b_base_ <__main__.LP_c_short_Array_16 object at 0x7fb07a4098c0> >>> short_array_ex._b_base_._objects {140395942025120: <__main__.c_short_Array_4 object at 0x7fb07a4097a0>} Only from_buffer verifies the buffer size. Be careful with pointer casts. ------------------------------------------------------------------------------ Site24x7 APM Insight: Get Deep Visibility into Application Performance APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month Monitor end-to-end web transactions and take corrective actions now Troubleshoot faster and improve end-user experience. Signup Now! http://pubads.g.doubleclick.net/gampad/clk?id=267308311&iu=/4140