Re: ctypes: delay conversion from c_char_p to string

2010-04-22 Thread Zvezdan Petkovic
On Apr 22, 2010, at 10:49 AM, Zvezdan Petkovic wrote:
> libc.strdup.argtype = [ctypes.c_char_p]

Correcting my typo.  This should be in plural:

libc.strdup.argtypes = [ctypes.c_char_p]

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


Re: ctypes: delay conversion from c_char_p to string

2010-04-22 Thread Zvezdan Petkovic

On Apr 21, 2010, at 6:29 PM, Brendan Miller wrote:

> Here's the method I was using. Note that tmp_char_ptr is of type
> c_void_p. This should avoid the memory leak, assuming I am
> interpreting the semantics of the cast correctly. Is there a cleaner
> way to do this with ctypes?
> 
>def get_prop_string(self, prop_name):
># Have to work with c_void_p to prevent ctypes from copying to a string
># without giving me an opportunity to destroy the original string.
>tmp_char_ptr = _get_prop_string(self._props, prop_name)
>prop_val = cast(tmp_char_ptr, c_char_p).value
>_string_destroy(tmp_char_ptr)
>return prop_val

Is this what you want?

=

import ctypes.util


libc = ctypes.CDLL(ctypes.util.find_library('libc'))

libc.free.argtypes = [ctypes.c_void_p]
libc.free.restype = None
libc.strdup.argtype = [ctypes.c_char_p]
libc.strdup.restype = ctypes.POINTER(ctypes.c_char)


def strdup_and_free(s):
s_ptr = libc.strdup(s)
print s_ptr.contents
i = 0
while s_ptr[i] != '\0':
print s_ptr[i],
i += 1
print
libc.free(s_ptr)


if __name__ == '__main__':
strdup_and_free('My string')

=

That is an equivalent of this C program:

=

#include 
#include 
#include 


void
strdup_and_free(char *s)
{
char *s_ptr = strdup(s);

printf("%c\n", *s_ptr);
printf("%s\n", s_ptr);
free(s_ptr);
}


int
main(int argc, char *argv[])
{
strdup_and_free("My string");
return 0;
}

=

To prove that the pointer was actually freed try inserting one more
libc.free(s_ptr) at the end of the function.
You should get Abort trap because you are attempting to free storage that has 
already been freed (I did on Mac OS X 10.6)


If you insert a second free(s_ptr) call in the C program you also get an Abort 
trap:

strdup2(15785) malloc: *** error for object 0x100100080: pointer being freed 
was not allocated

(Again, this is an error message on Mac OS X 10.6)


FWIW, this technique for getting pointer *is* mentioned in ctypes documentation.

Best regards,

Zvezdan


> 
> On Wed, Apr 21, 2010 at 3:15 PM, Brendan Miller  wrote:
>> I have a function exposed through ctypes that returns a c_char_p.
>> Since I need to deallocate that c_char_p, it's inconvenient that
>> ctypes copies the c_char_p into a string instead of giving me the raw
>> pointer. I believe this will cause a memory leak, unless ctypes is
>> smart enough to free the string itself after the copy... which I
>> doubt.
>> 
>> Is there some way to tell ctypes to return an actual c_char_p, or is
>> my best bet to return a c_void_p and cast to c_char_p when I'm reading
>> to convert to a string?
>> 
>> Thanks
>> Brendan
>> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list

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


Re: 2.6.4 Mac x86_64 ?

2009-11-13 Thread Zvezdan Petkovic
On Nov 13, 2009, at 3:58 PM, chris grebeldinger wrote:

> Hi All,
> I've been having some trouble getting a x86_64/i386 universal
> readline.so to build against libedit, on MacOS 10.5.6 as Apple does.
> Does anyone have any pointers about what changes I need to make to
> setup.py or readline.c to achive this?
> Has someone already done this and would like to share it?

The fix for use of native editline (readline emulation) was done and is already 
implemented on the trunk (2.7).
Please see: http://bugs.python.org/issue6877
You can find the patch in that tracker issue or here:
http://svn.python.org/view?view=rev&revision=74970

It was marked for a backport to a future 2.6.5 release too.

> Are there any plans to provide 64 bit support in future Mac OS 2.6.x
> releases?

AFAIK, it is already supported.
Perhaps you should specify the exact problems you have.
I believe that a more appropriate place for that would be pythonmac-sig mailing 
list, though.

Best regards,

Zvezdan

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


Re: Does Class implements Interface?

2009-08-28 Thread Zvezdan Petkovic

On Aug 27, 2009, at 9:16 AM, Emanuele D'Arrigo wrote:


Are there resources such as tools, recipes, documents or strategies
that could help me deal with these issues? I've already looked into
the ABC module and the zope.interface. I'm just fishing for more
things to look at.


You say above that you looked at zope.interface.
Did you look at zope.interface.verify?
Specifically, verify.txt doctests state that:

"An object provides an interface if

- either its class declares that it implements the interfaces,
  or the object declares that it directly provides the interface

- the object defines all the methods required by the interface

- all the methods have the correct signature

- the object defines all non-method attributes required by the
  interface"

zope.interface.verify.verifyObject(I, obj) will check all of the above.
Similarly, zope.interface.verify.verifyClass(I, C) will check the class.
It is a good practice to call these functions in the regression tests.

If this is not what you are looking for, sorry for the noise.

Zvezdan

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


Re: "Battleship" style game

2009-02-25 Thread Zvezdan Petkovic

On Feb 25, 2009, at 3:54 PM, Shawn Milochik wrote:

It is true that it would be fewer lines of code with the same
functionality, but it's better practice to have that framework in
place so that any changes made in the future wouldn't break any of the
code accessing my class. Obviously this is a fairly simple game that
has a fixed set of rules, but I'm trying to cultivate good habits, and
I don't think that doing it this way is anti-Pythonic.


Well, they are trying to help you with the best practices.
You offered the code for review and they reviewed it.
Whether you'll accept what they say or deny it is your call, of course.

FWIW, the Pythonic way would be:

>>> class Ship(object):
... def __init__(self, length):
... self._length = length
... @property
... def length(self):
... return self._length
...
>>> s = Ship(5)
>>> s.length
5
>>> # notice how the property is read-only which is what you wanted
>>> s.length = 7
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: can't set attribute
>>> ^D

Using @property decorator makes a read-only property.  If you need a  
read/write property there are several ways to define setter and  
deleter methods with or without decorators.


I hope you see that

x = s.length
s.position = y

is a completely different style than

x = s.get_length()
s.set_position(y)

Most of the people who program in Python prefer the first style above.

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


Re: Python Image Library IOError - cannot find JPEG decoder?

2009-02-25 Thread Zvezdan Petkovic

On Feb 24, 9:34 am, Dario Traverso  wrote:

I've been trying to install the Python Image Library  (PIL) on my Mac
OSX Leopard laptop, but have been running into some difficulties.
...
I've followed all of the installation instructions exactly. The build
summary reported everything was "ok". What could be the problem here.
Libjpeg-6b  is not accessible?
That would be my guess.



It could be something obvious and something you have done, but it's  
worth asking:


1. Do you have the path to fink binaries, such as djpeg,
   in your shell PATH (e.g., /opt/local/bin for MacPorts)?

2. Did you set up the path to the libraries you linked with in the
   environment variable DYLD_LIBRARY_PATH?
   For example, DYLD_LIBRARY_PATH=/opt/local/lib for MacPorts

3. Did you execute your app with this variable available.

   $ env DYLD_LIBRARY_PATH=/opt/local/lib your-app

   Once you confirm what is missing you can write a Python wrapper
   to call your app with the right environment.

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


Re: Problem with environment variables and cx_Oracle

2009-02-24 Thread Zvezdan Petkovic

On Feb 24, 2009, at 4:34 PM, Brandon Taylor wrote:

Here's my setup: OS X (10.5.6 - Intel), Oracle Instant Clinet 10_2,
Python 2.6.1, Django trunk


OS X is an important detail here.


In my .bash_profile, I have ORACLE_HOME and LD_LIBRARY_PATH specified
as:

ORACLE_HOME="$HOME/Library/Oracle/instantclient_10_2"
export ORACLE_HOME

LD_LIBRARY_PATH=$ORACLE_HOME
export LD_LIBRARY_PATH


Shouldn't this be DYLD_LIBRARY_PATH for Mac?
--
http://mail.python.org/mailman/listinfo/python-list