473,386 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 2008
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: KefX | last post by:
I've been following the group a bit (somewhat loosely; discussions involving other languages or advanced concepts kind of lose me), and I see all this negativity (OMG Python's lambda is borken...
105
by: Peter Hickman | last post by:
Well after all this discussion it would appear that a 'Python like' language has appeared => Prothon. http://www.prothon.org/index.html Very alpha, sort of like Python (if you consider the...
3
by: Terry Hancock | last post by:
Is there an *easy* way to make an object immutable in python? Or perhaps I should say "one obvious way to do it"? Oughtn't there to be one? I've found a thread on how to do this, which...
267
by: Xah Lee | last post by:
Python, Lambda, and Guido van Rossum Xah Lee, 2006-05-05 In this post, i'd like to deconstruct one of Guido's recent blog about lambda in Python. In Guido's blog written in 2006-02-10 at...
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 380 open (-36) / 3658 closed (+65) / 4038 total (+29) Bugs : 965 open ( -9) / 6555 closed (+35) / 7520 total (+26) RFE : 272 open...
0
by: Roy Smith | last post by:
In article <mailman.2119.1219612653.922.python-list@python.org>, "Hendrik van Rooyen" <mail@microcorp.co.zawrote: Reminds me of that great old song from "Saturday Night Hacker": Oh, oh, oh,...
0
by: Simon Brunning | last post by:
2008/8/25 Hendrik van Rooyen <mail@microcorp.co.za>: You can indeed use ctypes to modify the value of a string - see <http://tinyurl.com/5hcnwl>. You can use it to crash the OS, too. My advice...
0
by: Hendrik van Rooyen | last post by:
"Gabriel Genellina": Thanks Gabriel – looks like I really have to spend more time with that excellent document. Patrick Maupin:
0
by: Lie | last post by:
On Oct 11, 5:27 am, "Hendrik van Rooyen" <m...@microcorp.co.zawrote: Not necessarily. You've mentioned two ways. PS: Umm, a little bit off note: set is a built-in name, I'm a little confused...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.