By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,050 Members | 1,019 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,050 IT Pros & Developers. It's quick & easy.

C pointers/Python

P: n/a
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

Jul 18 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On 21 Mar 2005 15:52:44 -0800, jo*@gmail.com <do*****@gmail.com> wrote:
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.

--
Stephen Thorne
Development Engineer, NetBoxBlue.com
Jul 18 '05 #2

P: n/a
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

Jul 18 '05 #3

P: n/a
On 21 Mar 2005 17:16:18 -0800, integer <in*****@gmail.com> wrote:
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.

--
Stephen Thorne
Development Engineer, NetBoxBlue.com
Jul 18 '05 #4

P: n/a
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.

Jul 18 '05 #5

P: n/a
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

Jul 18 '05 #6

P: n/a
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.

Jul 18 '05 #7

P: n/a
"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)

Jul 18 '05 #8

P: n/a
On 21 Mar 2005 19:32:20 -0800, jo*@gmail.com <do*****@gmail.com> wrote:
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.

--
Stephen Thorne
Jul 18 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.