Alexander Belchenko | 3 May 11:23 2010
Picon

Should I cache LoadLibrary results?

Hi,

I'm using python and ctypes to test some of my C-code (compiled to
shared libraries). Every test method starts with code like this
(pseudocode):

Class TestMyLib(unittest.TestCase):

   def test_foo:
    library_under_test = ctypes.cdll.LoadLibrary('foo')

Such test cases could be about half-hundred or more (the number is
growing as time passes, of course). The time to run my tests is very
small today (less than second), so my question is purely theorethical.

Question itself: does ctypes do any sort of caching of cdll.LoadLibrary
results (for successfully loaded libraries) or should I care about not
loading the library several times?

Thanks,
Alexander

------------------------------------------------------------------------------
Martin (gzlist | 3 May 19:15 2010

Re: Should I cache LoadLibrary results?

On 03/05/2010, Alexander Belchenko <bialix <at> ukr.net> wrote:
>
> Question itself: does ctypes do any sort of caching of cdll.LoadLibrary
> results (for successfully loaded libraries) or should I care about not
> loading the library several times?

Using `ctypes.cdll.foo` or `ctypes.cdll['foo']` will cache a
successful dlopen, but `ctypes.cdll.LoadLibrary('foo')` will not, see
the LibraryLoader code:

<http://svn.python.org/view/python/branches/release25-maint/Lib/ctypes/__init__.py?&view=markup#LibraryLoader>

Martin

------------------------------------------------------------------------------
Alexander Belchenko | 4 May 09:05 2010
Picon

Re: Should I cache LoadLibrary results?

Martin (gzlist) пишет:
> On 03/05/2010, Alexander Belchenko <bialix <at> ukr.net> wrote:
>> Question itself: does ctypes do any sort of caching of cdll.LoadLibrary
>> results (for successfully loaded libraries) or should I care about not
>> loading the library several times?
> 
> Using `ctypes.cdll.foo` or `ctypes.cdll['foo']` will cache a
> successful dlopen, but `ctypes.cdll.LoadLibrary('foo')` will not, see
> the LibraryLoader code:
> 
> <http://svn.python.org/view/python/branches/release25-maint/Lib/ctypes/__init__.py?&view=markup#LibraryLoader>

If I understand the code correctly only `ctypes.cdll.foo` is caching one.
Thanks, for the info.

> 
> Martin
> 
> 
> 

--

-- 
All the dude wanted was his rug back

------------------------------------------------------------------------------
_______________________________________________
ctypes-users mailing list
ctypes-users <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ctypes-users
(Continue reading)

Massi | 5 May 17:01 2010
Picon

Multiple library access problem

Hi everyone,
 
in my script I need to execute multiple separated loading of the same dll library, in order to handle the internal variables with different threads.
Consider the followin piece of code:
 
lib1 = cdll.LoadLibrary("MyLib.dll")) 
lib2 = cdll.LoadLibrary("MyLib.dll")) 
 
lib1.var1 = 0
lib2.var1 = 1
 
Now, if I print the value of lib1.var1 I get 1, that is lib1 and lib2 points to the same memory space. Is there a way to create "different instances" of the same library? Or, alternatively, does it exist any workaround to avoid lib1 and lib2 share the same memory space?
 
Thanks in advance.
------------------------------------------------------------------------------
_______________________________________________
ctypes-users mailing list
ctypes-users <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ctypes-users
Pavel Nikitenko | 18 May 17:09 2010
Picon

functions with: char & type of parameters

I am trying to call following C function from Windows DLL from Python 
with ctypes.

int  __stdcall BBBOUNCECHECK(   char &strMessage,
                                 char &strAddressOut,
                                 int &lAddressLenOut,
                                 char &strEmailIgnoreList,
                                 char &strKey
                             );

I tried the following:
         pEmailAddress = create_string_buffer(255)
         emailAddressLength = c_long(0)
         returnValue = self.bBounceDLL.BBBOUNCECHECK(emailMessage, 
pEmailAddress, byref(emailAddressLength), self.excludeList, 
self.licenseKey);
         self.emailAddress = repr(pEmailAddress.value)
         return returnValue

But it looks like it does not work. It returns completely different 
results in comparison if I call the same DLL function from .NET C#.
It looks like there is a problem with: char & function type parameters. 
In all examples on Internet I see that for ctypes to call DLL function 
is there used mostly char *.

Could somebody advice, how to correctly call functions with: char & type 
of parameters, with ctypes from python? I do not see it in ctypes 
documentation at all.

Pavel

------------------------------------------------------------------------------
Mads Kiilerich | 18 May 22:09 2010

Re: functions with: char & type of parameters

Pavel Nikitenko wrote, On 05/18/2010 05:09 PM:
> I am trying to call following C function from Windows DLL from Python
> with ctypes.
>
> int  __stdcall BBBOUNCECHECK(   char&strMessage,
>                                   char&strAddressOut,
>                                   int&lAddressLenOut,
>                                   char&strEmailIgnoreList,
>                                   char&strKey
>                               );
>    

AFAICS That is C++, not C.

ctypes makes C interfacing simple, but C++ is a lot harder (read: hardly 
feasible). Some work was done on 
http://code.google.com/p/ctypes-stuff/source/browse/#svn/trunk/ctypes-cpp , 
but AFAIK it hasn't been finished.

There might however be a simple hack that works for reference parameters 
in your case - I don't know.

I would consider creating a simple C wrapper for this. But if you have 
to use a cimpiler anyway then why not use one of the other methods for 
creating python extensions? See for example Roman Yakovenkos site 
http://language-binding.net/ .

/Mads

------------------------------------------------------------------------------
John Popplewell | 19 May 01:01 2010
Picon
Picon

xml2py problem with CvQualifiedType

Hi,

I've been trying to generate bindings for ImageMagick-6.6.1-Q8 on
Windows for Python 2.5.4. After installing 'gccxml 0.9' and SVN revision
81278 I managed to get h2xml.py to work using this command-line:

h2xml.py /dev/pymagick/include/wand/MagickWand.h -oMagickWand.xml -I/dev/pymagick/include -c

However, when I tried the next step I ran into problems:

xml2py.py MagickWand.xml -omagickwand.py -llib/CORE_RL_magick_.dll -llib/CORE_RL_wand_.dll
# unresolved alias: ssize_t = long
# unresolved alias: sys_errlist = _sys_errlist
# unresolved alias: environ = _environ
# unresolved alias: sys_nerr = _sys_nerr
# unresolved alias: _W64 = __w64
# unresolved alias: inline = __inline
# unresolved alias: MB_CUR_MAX = __mb_cur_max
Traceback (most recent call last):
  File "C:\Python25\scripts\xml2py.py", line 6, in <module>
    sys.exit(main())
  File "C:\Python25\Lib\site-packages\ctypeslib\xml2py.py", line 199, in main
    types=options.kind)
  File "C:\Python25\lib\site-packages\ctypeslib\codegen\codegenerator.py", line 874, in generate_code
    loops = gen.generate_code(items)
  File "C:\Python25\lib\site-packages\ctypeslib\codegen\codegenerator.py", line 785, in generate_code
    loops = self.generate_items(items)
  File "C:\Python25\lib\site-packages\ctypeslib\codegen\codegenerator.py", line 774, in generate_items
    self.generate_all(sorted(items, self.cmpitems))
  File "C:\Python25\lib\site-packages\ctypeslib\codegen\codegenerator.py", line 758, in generate_all
    self.generate(item)
  File "C:\Python25\lib\site-packages\ctypeslib\codegen\codegenerator.py", line 754, in generate
    mth(item)
  File "C:\Python25\lib\site-packages\ctypeslib\codegen\codegenerator.py", line 402, in Structure
    self.generate(struct.get_body())
  File "C:\Python25\lib\site-packages\ctypeslib\codegen\codegenerator.py", line 754, in generate
    mth(item)
  File "C:\Python25\lib\site-packages\ctypeslib\codegen\codegenerator.py", line 561, in StructureBody
    pack = calc_packing(body.struct, fields)
  File "C:\Python25\lib\site-packages\ctypeslib\codegen\codegenerator.py", line 125, in calc_packing
    _calc_packing(struct, fields, pack, isStruct)
  File "C:\Python25\lib\site-packages\ctypeslib\codegen\codegenerator.py", line 98, in _calc_packing
    s, a = storage(f.typ)
  File "C:\Python25\lib\site-packages\ctypeslib\codegen\codegenerator.py", line 76, in storage
    return int(t.size), int(t.align)
AttributeError: 'CvQualifiedType' object has no attribute 'size'

I rooted about, stuck some prints in, slept on it and found these lines in
ctypeslib/codegen/codegenerator.py in the function storage():

if isinstance(t, typedesc.Typedef):
    return storage(t.typ)

which looks like the behavior required when handling a CvQualifiedType. I'd
describe it as looking up the base-type from the fancy CvQualifiedType, but I'm
pretty much making all this up :-)

Anyway, here's my fix:

Index: ctypeslib/codegen/codegenerator.py
===================================================================
--- ctypeslib/codegen/codegenerator.py  (revision 81278)
+++ ctypeslib/codegen/codegenerator.py  (working copy)
 <at>  <at>  -65,7 +65,7  <at>  <at> 

 def storage(t):
     # return the size and alignment of a type
-    if isinstance(t, typedesc.Typedef):
+    if isinstance(t, typedesc.Typedef) or isinstance(t, typedesc.CvQualifiedType):
         return storage(t.typ)
     elif isinstance(t, typedesc.ArrayType):
         s, a = storage(t.typ)

Works for me, makes some kind of sense and now I can finally get on with using an
up to date version of ImageMagick in Python :-)

best regards,
John Popplewell.

------------------------------------------------------------------------------
Ben Stroud | 19 May 19:02 2010
Picon

in_dll string assignment question

I could use some help assigning to global C variable in DLL using ctypes.

test.c contains the following

#include <stdio.h>

char name[60];
void test(void) {
printf("Name is %s\n", name);
}

On windows (cygwin) I build a DLL (test.dll) as follows:

gcc -g -c -Wall test.c
gcc -Wall -mrtd -mno-cygwin -shared -W1,--add-stdcall-alias -o Test.dll 
test.o

Then when trying to modify the `name` variable and then calling the C 
test function using the ctypes interface I get the following...

 >>> from ctypes import *
 >>> dll = windll.Test
 >>> dll
<WinDLL 'Test', handle ... at ...>
 >>> f = c_char_p.in_dll(dll, 'name')
 >>> f
c_char_p(None)
 >>> f.value = 'foo'
 >>> f
c_char_p('foo')
 >>> dll.test()
Name is Name is 4∞┘☺
13

Why does the test function print garbage in this case?

------------------------------------------------------------------------------

_______________________________________________
ctypes-users mailing list
ctypes-users <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ctypes-users
Mark Tolonen | 22 May 04:01 2010
Picon

Re: in_dll string assignment question


"Ben Stroud" <ben.factsalive <at> gmail.com> wrote in message 
news:4BF4199E.7090602 <at> gmail.com...
>I could use some help assigning to global C variable in DLL using ctypes.
>
> test.c contains the following
>
> #include <stdio.h>
>
> char name[60];
> void test(void) {
> printf("Name is %s\n", name);
> }
>
>
> On windows (cygwin) I build a DLL (test.dll) as follows:
>
> gcc -g -c -Wall test.c
> gcc -Wall -mrtd -mno-cygwin -shared -W1,--add-stdcall-alias -o Test.dll
> test.o
>
> Then when trying to modify the `name` variable and then calling the C
> test function using the ctypes interface I get the following...
>
> >>> from ctypes import *
> >>> dll = windll.Test
> >>> dll
> <WinDLL 'Test', handle ... at ...>
> >>> f = c_char_p.in_dll(dll, 'name')
> >>> f
> c_char_p(None)
> >>> f.value = 'foo'
> >>> f
> c_char_p('foo')
> >>> dll.test()
> Name is Name is 4∞┘☺
> 13
>
> Why does the test function print garbage in this case?

"name" is an array, not a pointer.  Try:

    (c_char*60).in_dll(dll,'name').value = "foo"

-Mark

------------------------------------------------------------------------------

_______________________________________________
ctypes-users mailing list
ctypes-users <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ctypes-users

Gmane