how to iterate over sequence and non-sequence ?

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Fri Oct 19 22:55:37 CEST 2007


stef mientki a écrit :
> Steven D'Aprano wrote:
> 
>> On Fri, 19 Oct 2007 16:19:32 +0200, stef wrote:
>>
>>  
>>
>>> Well I'm not collecting data, I'm collecting pointers to data.
>>>     
>>
>>
>> I beg to differ, you're collecting data. How that data is to be 
>> interpreted (a string, a number, a pointer...) is a separate issue.
>>
>>
>>  
>>
>>> This
>>> program simulates a user written program in JAL. As Python doesn't
>>> support pointers, instead I collect names.
>>>     
>>
>>
>> This doesn't make any sense to me. If your user-written program is 
>> supplying pointers (that is, memory addresses like 0x15A8), how do you 
>> get a name from the memory address?
>>
>>
>> If you are trying to emulate pointer-manipulation, then the usual way 
>> to simulate a pointer is with an integer offset into an array:
>>
>> # initialise your memory space to all zeroes:
>> memory = [chr(0)]*1024*64  # 64K of memory space, enough for anyone
>> NULL = 0
>> pointer = 45
>> memory[pointer:pointer + 5] = 'HELLO'
>> pointer += 6
>> memory[pointer:pointer + 5] = 'WORLD'
>>
>>
>>   
> 
> If there is a better way, I'ld like to hear it.
> I understand that execute is dangerous.
> 
> I don't have pointers, I've just names (at least I think).
> Let me explain a little bit more,
> I want to simulate / debug a user program,
> the user program might look like this:
> 
>   x = 5
>   for i in xrange(10):
>       x = x + 1
> 
> So now I want to follow the changes in "x" and "i",
> therefor in the background I change the user program a little bit, like 
> this
> 
> def user_program():
>   x = 5    ; _debug(2)
>   global x,i
>   _debug (3)
>   for i in xrange(10):
>       _debug (3)
>       x = x + 1    ; _debug (4)

You do know that Python exposes all of it's compilation / AST / whatever 
machinery, don't you ? IOW, you can take a textual program, compile it 
to a code object, play with the AST, add debug hooks, etc... Perhaps you 
should spend a little more time studying the modules index ?



More information about the Python-list mailing list