C pointers/Python

J

joe

Hello,

I am trying to convert some C code into python.
Here's the C code...

typedef enum {
CODE1 = 0x1,
CODE2 = 0x2
} CODE
testit(unsigned char *buffer,
unsigned long length,
CODE code)
{
unsigned long data
switch (code)
{
case CODE1:
while(len--)
{
*buffer++ = (unsigned long)data++
}
break
case CODE2 :
.....

}
How does python take care of pointers? Any help with python pointer
code appreciated.
-Joe
 
S

Stephen Thorne

Hello,

I am trying to convert some C code into python.
Here's the C code...

typedef enum {
CODE1 = 0x1,
CODE2 = 0x2
} CODE
testit(unsigned char *buffer,
unsigned long length,
CODE code)
{
unsigned long data
switch (code)
{
case CODE1:
while(len--)
{
*buffer++ = (unsigned long)data++
}
break

CODE1 = 1
CODE2 = 2
def testit(code):
data = 'somedata'
if code == CODE1:
return data

In C, you pass a memory location to copy the result into. In Python,
we return the result as an object.

As for pointers, we don't need them.
 
I

integer

you never deal directly with pointers in python.
in your case, you need to pass a mutable object as your 'buffer'
argument and perform operations on it such that 'buffer==data'. (for
example, you could use a list here.)
def testit( buffer ):
for i in range( len ):
buffer[A+i]=data[B+i]
for some constants A,B
 
S

Stephen Thorne

you never deal directly with pointers in python.
in your case, you need to pass a mutable object as your 'buffer'
argument and perform operations on it such that 'buffer==data'. (for
example, you could use a list here.)
def testit( buffer ):
for i in range( len ):
buffer[A+i]=data[B+i]
for some constants A,B

That's unpythonic.

The correct solution is to return the result. Anything else is trying
to squeeze a C idiom into python for no gain.
 
M

Marcin Mika

agreed.
you might say i was trying to translate his C code "word for word",
rather than properly "pythonizing" the entire chunk of code as a whole.
 
J

joe

Can i do something like this?

if code == CODE1:
data = 0x0
While True:
len = len - 1
if len == -1:
break
buffer = data

Do i need to initialze the buffer?

-Thanks,
Joe
 
M

Marcin Mika

Can i do something like this?

if code == CODE1:
data = 0x0
While True:
len = len - 1
if len == -1:
break
buffer = data

certainly not!
there are many things wrong with that.
first of all, as was pointed out already: this is highly un-pythonic.
secondly, its completely wrong. if you bind 'buffer' to another object,
then you have no chance of modifying its original contents. thats why i
stated in my previous post that IF you insist on persuing the
"un-pythonic" path then your function argument 'buffer' _must_ be a
mutable object which you must operate on through its methods, but you
CANNOT modify it with: buffer=data.
doing that will only re-bind 'buffer', it will never change anything
outside the function.
 
L

Lonnie Princehouse

"len" is a built-in function in Python, so you probably don't want to
use it as a variable name.

What is this C code actually trying to do? Don't try to transliterate
it; instead, read up on how lists and slicing work, and rewrite it in
Python starting from a higher level of abstraction.

One hint- you can copy the same datum many times without a while
loop...

# reassign sub-list interval [i, j-1]
buffer[i:j] = [data] * (j-i)

# reassign the entire list
buffer[:] = [data] * len(buffer)
 
S

Stephen Thorne

Can i do something like this?

if code == CODE1:
data = 0x0
While True:
len = len - 1
if len == -1:
break
buffer = data

Do i need to initialze the buffer?

Please, forget everything you know about memory, pointers, and C idiom.
Also, considering doing the python tutorial. There is one provided
here: http://python.org/doc/tut

Basically, you're not approaching this in a python-like way, and thus
what should be a single line (i.e. 'return data') you're trying to
turn into a painful process of copying data from one memory location
to another. This is acceptable in the C world, and not appropriate in
the python world.

I would suggest you define your problem in broader terms (i.e. "I am
trying to interpret data that is coming over a socket, my packet
structure looks like this ..., how do I marshall dispatch that to
callbacks so I can talk the protocol properly? Here is a link to the
working C code http://...") and there is a large group of wonderful
people here that would love to show you the pythonic way of achieving
your goal.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top