raw string from mmap.read() possible?

Fredrik Lundh fredrik at pythonware.com
Sun Nov 9 17:25:48 EST 2003


Dan Jones wrote:

> yuvframe = mmap.read(WIDTH*HEIGHT*3)
> rgbframe = yuv.yuv2rgb(WIDTH, HEIGHT, yuvframe)
>
> At this point it gives a typeerror: argument 3 must be string without
> null bytes, not str.

your C extension uses the "s" marker for the third argument.  don't
do that if you plan to pass in null bytes; use "s#" instead.

see the documentation for details:

    http://www.python.org/doc/current/api/arg-parsing.html

    "s" (string or Unicode object) [char *]

    Convert a Python string or Unicode object to a C pointer to a character
    string. /.../ The C string is NUL-terminated. The Python string must not
    contain embedded NUL bytes; if it does, a TypeError exception is raised.
    /.../

    "s#" (string, Unicode or any read buffer compatible object) [char *, int]

    This variant on "s" stores into two C variables, the first one a pointer to
    a character string, the second one its length. In this case the Python
    string may contain embedded null bytes. /.../

> but if I send it a raw string it doesn't complain:
>
> rgbframe = yuv.yuv2rgb(WIDTH, HEIGHT, r"\0\0\0\0")
>
> I'm assuming this is happening because C interprets NULL as the end of
> the string. Is there a way to get a raw string directly from the
> mmap.read() call or do I have to do a conversion after the read?

when you've fixed the C extension, I suggest reading the section
on "raw strings" in the language reference:

    http://www.python.org/doc/current/ref/strings.html

</F>








More information about the Python-list mailing list