Python String Immutability Broken!

Discussion in 'Python' started by Roy Smith, Aug 24, 2008.

  1. Roy Smith

    Roy Smith Guest

    In article <>,
    "Hendrik van Rooyen" <> wrote:

    > It is reputed to belong to a programmer who was flayed alive


    Reminds me of that great old song from "Saturday Night Hacker":

    Oh, oh, oh, oh.
    Flaying alive, flaying alive.
    Oh, oh, oh, oh.
    Flaying ali-i-i-i-i-ive!
    Roy Smith, Aug 24, 2008
    #1
    1. Advertising

  2. South Africa. Sunday 24th August 2008.

    Our South African correspondent, Waffling Swiftly, reports the
    discovery of a corpse in the local cyberspace.

    It is reputed to belong to a programmer who was flayed alive
    by the C.L.P. group, because he had violated the immutability
    of a python string.

    Rumour has it that the attack was led, and the killing blow struck,
    by the "KILL GIL" girl who left her lair on Irmen "Mr. Pyro"'s blog
    at http://www.razorvine.net/frog/user/irmen/article/2005-02-13/45 in
    order to perform the hit, using her katana.

    When asked to comment, the BDFL shrugged and said:

    The guy had it coming.
    He was the architect of his own misfortune.
    Python strings _are_ immutable.


    Here is the evidence:

    root@dmp:/tmp/disk/ebox/iotest/lib# python
    Python 2.5.2 (r252:60911, Mar 1 2008, 13:52:45)
    [GCC 4.2.3] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> s = 'The quick brown fox jumps over the lazy dog and now is the time!'
    >>> s

    'The quick brown fox jumps over the lazy dog and now is the time!'
    >>> import ctypes as c
    >>> io = c.cdll.LoadLibrary('./lib_gpio.a')
    >>> io.io_en()

    0
    >>> io.read_write(s,len(s))

    255
    >>> s

    '\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff
    \xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff
    \xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff
    \xff\xff\xff\xff\xff\xff\xff'
    >>>g = 'boo'
    >>> io.read_write(g,len(g))

    255
    >>> g

    '\xff\xff\xff'
    >>>


    And here is the incriminating code:

    WARNING: Don't do this at home. It does low level
    i/o and unless you run it on a DMP Vortex processor,
    it might turn your printer into a pumpkin, or something.

    YOU HAVE BEEN WARNED!


    </code>

    #include <stdlib.h>
    #include <stdio.h>
    #include <sys/io.h>
    #include <unistd.h>

    /* We define our read and write routines */

    #define write_port(a,b) outb(b,a)
    #define read_port(a) inb(a)

    /* Then we hard code the port numbers we need */

    #define data_0 (0x78)
    #define data_1 (0x79)
    #define data_2 (0x7a)

    #define dir_0 (0x98)
    #define dir_1 (0x99)
    #define dir_2 (0x9a)


    /* Here we organise permission to use the ports*/

    int io_en()
    {
    int rv = 0;
    rv = iopl(3);
    return rv;
    }

    /* Then we make some dumb i/o routines for testing */

    /* first we make outputs */

    unsigned char put_0 (char x)
    {
    write_port (dir_0, 0xff);
    write_port (data_0, x);
    return 0x00;
    }

    unsigned char put_1 (char x)
    {
    write_port (dir_1, 0xff);
    write_port (data_1, x);
    return 0x00;
    }

    unsigned char put_2 (char x)
    {
    write_port (dir_2, 0xff);
    write_port (data_2, x);
    return 0x00;
    }

    /* Then we make inputs */

    unsigned char rval = 0x00;

    unsigned char get_0 ()
    {
    write_port (dir_0, 0x00);
    rval = read_port(data_0) & 0xff;
    return rval;
    }

    unsigned char get_1 ()
    {
    write_port (dir_1, 0x00);
    rval = read_port(data_1) & 0xff;
    return rval;
    }

    unsigned char get_2 ()
    {
    write_port (dir_2, 0x00);
    rval = read_port(data_2) & 0xff;
    return rval;
    }

    /* This routine outputs and inputs a symmetric block of bytes, writing
    the outputs out and replacing them by the corresponding inputs */

    unsigned char read_write (unsigned char *outputs, int len)
    {
    int i = 0;
    char rv;
    while (i < len)
    {
    rv = put_1(i); /* put out the addy */
    rv = put_0(outputs); /* put out the char */
    rv = put_2('\x03'); /* make a write strobe */
    rv = put_2('\x02');
    rv = put_2('\x03');
    rv = get_0(); /* turn the bus around */
    rv = put_2('\x05'); /* make a read strobe */
    rv = put_2('\x04');
    outputs = get_0(); /* read the char */
    rv = put_2('\x05'); /* 3-state bus again */
    i++;
    }
    return *outputs;
    }
    </end code>

    The man's dying words were supposed to be:

    You often see this pattern of inputs replacing
    outputs in DSP serial port code.
    I suppose I should have used array.array...
    aaahhrgh...!!

    Flowers, comments, pitfalls, advice and money are welcome!

    - Hendrik
    Hendrik van Rooyen, Aug 25, 2008
    #2
    1. Advertising

  3. Re: Python String Immutability Broken!

    On Aug 24, 8:49 pm, "Hendrik van Rooyen" <> wrote:
    > (a lot of stuff related to using a string with a C library via ctypes)


    Very entertaining.

    But let me get this straight: Are you just complaining that if you
    pass a string to an arbitrary C function using ctypes, that that
    arbitrary function can modify the string?

    Because if you are, then I think you share a great deal of
    responsibility for the death of that string -- sending the poor thing
    to its grave through some unknown C function.

    What would you have ctypes do instead?
    Patrick Maupin, Aug 25, 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. Terry Hancock

    Easy immutability in python?

    Terry Hancock, Mar 4, 2006, in forum: Python
    Replies:
    3
    Views:
    378
    Terry Hancock
    Mar 4, 2006
  2. Terry Hancock

    Re: Easy immutability in python?

    Terry Hancock, Mar 4, 2006, in forum: Python
    Replies:
    2
    Views:
    272
    Alex Martelli
    Mar 4, 2006
  3. John Nagle

    The future of Python immutability

    John Nagle, Sep 3, 2009, in forum: Python
    Replies:
    50
    Views:
    1,203
    Steven D'Aprano
    Sep 8, 2009
  4. andrea crotti

    Immutability and Python

    andrea crotti, Oct 29, 2012, in forum: Python
    Replies:
    8
    Views:
    206
    Thomas Rachel
    Nov 8, 2012
  5. andrea crotti

    Re: Immutability and Python

    andrea crotti, Oct 29, 2012, in forum: Python
    Replies:
    0
    Views:
    163
    andrea crotti
    Oct 29, 2012
Loading...

Share This Page