Re: ctypes & strings

Discussion in 'Python' started by Gabriel Genellina, Dec 27, 2008.

  1. En Sat, 27 Dec 2008 10:54:32 -0200, Hendrik van Rooyen
    <> escribió:

    > The c routine will actually break Python's normal string
    > immmutability and give you back a changed ins.


    ....so don't do that!
    If you require a mutable string to pass to C functions, use
    ctypes.create_string_buffer()

    > It is in general not a good idea to change the passed string
    > like I am doing - but you wanted to know how
    > to pass a python string, and the outs example should get
    > you going - a string is an array of characters in c...


    Hmmm, I don't think posting a potentially harmful example is actually a
    good idea...

    --
    Gabriel Genellina
    Gabriel Genellina, Dec 27, 2008
    #1
    1. Advertising

  2. Gabriel Genellina

    Aaron Brady Guest

    On Dec 27, 2:03 pm, "Gabriel Genellina" <>
    wrote:
    > En Sat, 27 Dec 2008 10:54:32 -0200, Hendrik van Rooyen  
    > <> escribió:
    >
    > > The c routine will actually break Python's normal string
    > > immmutability and give you back a changed ins.

    >
    > ...so don't do that!
    > If you require a mutable string to pass to C functions, use  
    > ctypes.create_string_buffer()
    >
    > > It is in general not a good idea to change the passed string
    > > like I am doing - but you wanted to know how
    > > to pass a python string, and the outs example should get
    > > you going - a string is an array of characters in c...

    >
    > Hmmm, I don't think posting a potentially harmful example is actually a  
    > good idea...
    >
    > --
    > Gabriel Genellina


    One alternative would be to return a pair of Python strings. You
    might want to wrap the C function you have already in another one,
    which allocates buffers and creates Python objects.

    Hendrik is right, though. The ctypes module allows us to break string
    immutability within Python code.
    Aaron Brady, Dec 28, 2008
    #2
    1. Advertising

  3. En Sat, 27 Dec 2008 22:06:23 -0200, Aaron Brady <>
    escribió:
    > On Dec 27, 2:03 pm, "Gabriel Genellina" <>
    > wrote:
    >> En Sat, 27 Dec 2008 10:54:32 -0200, Hendrik van Rooyen  
    >> <> escribió:
    >>
    >> > The c routine will actually break Python's normal string
    >> > immmutability and give you back a changed ins.

    >>
    >> Hmmm, I don't think posting a potentially harmful example is actually a
    >> good idea...

    >
    > Hendrik is right, though. The ctypes module allows us to break string
    > immutability within Python code.


    Ok, it *can* be done, but it's far from being a recommended practice...
    and I would never use such wizardry in production code.

    --
    Gabriel Genellina
    Gabriel Genellina, Dec 28, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Henk Punt
    Replies:
    0
    Views:
    394
    Henk Punt
    Jul 23, 2004
  2. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    755
    Malcolm
    Jun 24, 2006
  3. Replies:
    0
    Views:
    501
  4. MRAB
    Replies:
    0
    Views:
    686
  5. MRAB
    Replies:
    0
    Views:
    744
Loading...

Share This Page