Converting fixed-size C pointer to memoryview in the pure Python mode

9 views
Skip to first unread message

Takeshi Ikuma

unread,
Aug 31, 2025, 3:02:41 AM (14 days ago) Aug 31
to cython-users
My question is similar to this one on StackExchange but in the pure Python mode. First, a couple disclaimers, this is my first time using Cython, and I'm working off of others' work (PyAV). which constrains me to use the pure Python mode.

My understanding is in Cython, I can do something like

cdef char[::1] view = <char[:size]>incoming_p_void

to convert a void C pointer, which points to an array of a known size, to a memoryview of the designated data type (char in this case). 

How do you do the same in the pure Python mode?

Here is my (failing) code in progress (the src_view line is the contentious line):

@cython.cclass
class Packet(Buffer):

@cython.cfunc
@cython.locals(size=cython.size_t, pal=cython.p_void, dst_view = cython.char[::1])
@cython.returns(cython.char[:])
def _get_palette(self):

pal = lib.av_packet_get_side_data(self.ptr, lib.AV_PKT_DATA_PALETTE, cython.address(size))

if pal:
result = np.empty(size, dtype=np.uint8)
dst_view = result
src_view = cython.declare(cython.char[:size], cython.cast(cython.p_char,pal))
dst_view[:] = src_view
return result
else:
return np.empty(0, dtype=np.uint8)


When compiled, I get the error that I pasted down below. If anybody here could help me sort this out, I'd be greatly appreciated.

Thanks,
Kesh

[1/1] Cythonizing av/packet.py

Error compiling Cython file:
------------------------------------------------------------
...
        pal = lib.av_packet_get_side_data(self.ptr, lib.AV_PKT_DATA_PALETTE, cython.address(size))

        if pal:
            result = np.empty(size, dtype=np.uint8)
            dst_view = result
            src_view = cython.declare(cython.char[:size], cython.cast(cython.p_char,pal))
                                                 ^
------------------------------------------------------------

av/packet.py:252:49: Unknown type

Error compiling Cython file:
------------------------------------------------------------
...
        pal = lib.av_packet_get_side_data(self.ptr, lib.AV_PKT_DATA_PALETTE, cython.address(size))

        if pal:
            result = np.empty(size, dtype=np.uint8)
            dst_view = result
            src_view = cython.declare(cython.char[:size], cython.cast(cython.p_char,pal))
                             ^
------------------------------------------------------------

av/packet.py:252:29: 'declare' not a valid cython language construct

Error compiling Cython file:
------------------------------------------------------------
...
        pal = lib.av_packet_get_side_data(self.ptr, lib.AV_PKT_DATA_PALETTE, cython.address(size))

        if pal:
            result = np.empty(size, dtype=np.uint8)
            dst_view = result
            src_view = cython.declare(cython.char[:size], cython.cast(cython.p_char,pal))
                             ^
------------------------------------------------------------

av/packet.py:252:29: 'declare' not a valid cython attribute or is being used incorrectly

Error compiling Cython file:
------------------------------------------------------------
...
        pal = lib.av_packet_get_side_data(self.ptr, lib.AV_PKT_DATA_PALETTE, cython.address(size))

        if pal:
            result = np.empty(size, dtype=np.uint8)
            dst_view = result
            src_view = cython.declare(cython.char[:size], cython.cast(cython.p_char,pal))
                                     ^
------------------------------------------------------------

av/packet.py:252:37: Compiler crash in AnalyseExpressionsTransform

da-woods

unread,
Sep 3, 2025, 3:08:38 AM (11 days ago) Sep 3
to cython...@googlegroups.com

I think  this is a known issue unfortunately and I'm not sure there's a good workaround: https://github.com/cython/cython/issues/4270

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/cython-users/a6e2b7cf-6adf-4698-b6c1-0ce65e353bf8n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages