468,471 Members | 1,595 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,471 developers. It's quick & easy.

Re: Python String Immutability Broken!

You can also use ctypes to globally change the value of integers less
than 101. Personally, I don't particularly like the number 14. I
changed it to 9 and I am much happier now.

I love ctypes. So cool. It's not supposed to be safe.

Life is either a daring adventure or nothing. Security does not
exist in nature, nor do the children of men as a whole experience
it. Avoiding danger is no safer in the long run than exposure.
*Helen Keller <http://www.quotationspage.com/quotes/Helen_Keller/>*
/US blind & deaf educator (1880 - 1968)/

Of course I would not hire anyone who believes this quote, other than
Helen Keller, if she were still with us.

It is quite possible to write a small program that works using abused
strings. But my life better not depend on it. Among other things, if
you use the abused string as a key anywhere, you will not get correct
results. Trying to change the length of the string will cause
disasters. Lengthening a string will corrupt memory, and shortening the
string will not shorten it but rather embed '\0' in it.

Ken

Hendrik van Rooyen wrote:
>
Patrick Maupin <pmau....ail.comwrote:

>Very entertaining.


Thanks. Nice to see that there is still some sense of humour
left somewhere - its all been so serious here lately - people
seem to forget that hacking is fun!

>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?


Actually, I am not complaining - I am asking for advice on the side
effects of what I am doing, which is replacing a bunch of bits
in what is essentially an output bit field with the corresponding
input bits at the same addresses read back from a simulated i/o
bus structure. And I would also like to know if there is a better
way of doing this.

The C code actually works, doing what was intended - the \xff that
one sees appearing back comes from the pullup resistors on the
eBox's i/o. I can show that it is working by adding some resistance
and capacitance (by holding the connector against my tongue) in which
case I get a munged version of the fox back. (- evidently my tongue
is not such a perfect communications medium as I would like to believe.)

Passing the fox is actually deceptive and misleading, as in real
use there would be no such correlation sideways across bits, as
they are just representations of output lines.
(Think "coils" in PLC jargon)

>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.

This string is NOT dead - it is alive, and not even stunned -
it just looks as if it is sleeping because of the \xff - which
comes from the fact that there is no real hardware out there yet.

The C functions are very simple ones actually - they just do
what are essentially Linux I/O system calls - setting direction
bits for a port (in or out) and then reading or writing the data.

- Hendrik

--
http://mail.python.org/mailman/listinfo/python-list

Aug 25 '08 #1
2 1634
On Mon, 25 Aug 2008 03:43:01 -0700, Ken Seehart wrote:
You can also use ctypes to globally change the value of integers less
than 101. Personally, I don't particularly like the number 14. I
changed it to 9 and I am much happier now.
Okay, you've got me curious. How do you do that, and why only up to 101?

--
Steven
Aug 25 '08 #2
Steven D'Aprano wrote:
On Mon, 25 Aug 2008 03:43:01 -0700, Ken Seehart wrote:
>You can also use ctypes to globally change the value of integers less
than 101. Personally, I don't particularly like the number 14. I
changed it to 9 and I am much happier now.

Okay, you've got me curious. How do you do that, and why only up to 101?
Up to 256 in current Python. Small integers are shared to save memory.
After a quick look into the ctypes tutorial and the python source I came up
with

# 64-bit linux
>>from ctypes import *
libc = cdll.LoadLibrary("libc.so.6")
libc.memset(id(14)+16, 0, 8)
7742336
>>14
0
>>14 == 0
True

Peter
Aug 25 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

19 posts views Thread by KefX | last post: by
105 posts views Thread by Peter Hickman | last post: by
3 posts views Thread by Terry Hancock | last post: by
267 posts views Thread by Xah Lee | last post: by
reply views Thread by Kurt B. Kaiser | last post: by
reply views Thread by Roy Smith | last post: by
reply views Thread by Simon Brunning | last post: by
reply views Thread by Hendrik van Rooyen | last post: by
reply views Thread by Lie | last post: by
1 post views Thread by kmladenovski | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.