[Tutor] (no subject)
Hey I just need help plz! Sent via the Samsung GALAXY S®4 Active™, an AT&T 4G LTE smartphone___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] (no subject)
On 4 March 2014 20:43, coffer.nick wrote: > Hey I just need help plz! OK, what would you like help with? Walter ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] reg: How to import dll in python
Hi, Please find code used to create dll: add_1.cpp #include "add_1.h" using namespace std; Mathematics::Mathematics() { } void Mathematics::input() { cout << "Input two inetegers\n"; cin >> x >> y; } void Mathematics::add() { cout << "Result = " << x + y; } add_1.h #include class Mathematics { int x, y; public: __declspec(dllexport) Mathematics();//constructor __declspec(dllexport) void input(); __declspec(dllexport) void add(); }; Dll created: Addition.dll Python Code to import above dll: import ctypes From ctypes import * testDll = cdll.LoadLibrary("D:\Python Test Codes\DLL_Test\Addition.dll") test1 = ctypes.WINFUNCTYPE(None) test2 = test1 (("add", testDll)) Error Displayed Traceback (most recent call last): File "", line 1, in AttributeError: function 'add' not found ::DISCLAIMER:: The contents of this e-mail and any attachment(s) are confidential and intended for the named recipient(s) only. E-mail transmission is not guaranteed to be secure or error-free as information could be intercepted, corrupted, lost, destroyed, arrive late or incomplete, or may contain viruses in transmission. The e mail and its contents (with or without referred errors) shall therefore not attach any liability on the originator or HCL or its affiliates. Views or opinions, if any, presented in this email are solely those of the author and may not necessarily reflect the views or opinions of HCL or its affiliates. Any form of reproduction, dissemination, copying, disclosure, modification, distribution and / or publication of this message without the prior written consent of authorized representative of HCL is strictly prohibited. If you have received this email in error please delete it and notify the sender immediately. Before opening any email and/or attachments, please check them for viruses and other defects. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] reg: How to import dll in python
Hi Shweta, You'll likely get a better response posting this question on the main python list -- the Tutor list is primarily for teaching python Emile On 3/5/2014 4:49 AM, Shweta Kaushik wrote: Hi, Please find code used to create dll: *_add_1.cpp_* #include "add_1.h" using namespace std; Mathematics::Mathematics() { } void Mathematics::input() { cout << "Input two inetegers\n"; cin >> x >> y; } void Mathematics::add() { cout << "Result = " << x + y; } *_add_1.h_* #include class Mathematics { int x, y; public: __declspec(dllexport) Mathematics();//constructor __declspec(dllexport) void input(); __declspec(dllexport) void add(); }; *_Dll created_*: Addition.dll *_Python Code to import above dll_*: import ctypes From ctypes import * testDll = cdll.LoadLibrary("D:\Python Test Codes\DLL_Test\Addition.dll") test1 = ctypes.WINFUNCTYPE(None) test2 = test1 (("add", testDll)) Error Displayed Traceback (most recent call last): File "", line 1, in AttributeError: function 'add' not found ::DISCLAIMER:: The contents of this e-mail and any attachment(s) are confidential and intended for the named recipient(s) only. E-mail transmission is not guaranteed to be secure or error-free as information could be intercepted, corrupted, lost, destroyed, arrive late or incomplete, or may contain viruses in transmission. The e mail and its contents (with or without referred errors) shall therefore not attach any liability on the originator or HCL or its affiliates. Views or opinions, if any, presented in this email are solely those of the author and may not necessarily reflect the views or opinions of HCL or its affiliates. Any form of reproduction, dissemination, copying, disclosure, modification, distribution and / or publication of this message without the prior written consent of authorized representative of HCL is strictly prohibited. If you have received this email in error please delete it and notify the sender immediately. Before opening any email and/or attachments, please check them for viruses and other defects. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] "Lab Three" query [was: Re: (no subject)]
Hi, Please reply to the mailing list post and not to me directly so others can benefit/chime in. I'll send this response to the mailing list instead of directly to you, so you should receive it via that route. On 5 March 2014 13:20, nick coffer wrote: > Ok. I need help setting this up. I know how to mostly. Just I need to know > how to get the variables. Firstly please note, this is homework so I/we cannot provide a direct solution. Secondly, you're still not being very specific about what you're struggling with or indeed what you've tried. It usually works best if you're as concrete as possible, e.g. post the actual code that you've tried, and explain what you're struggling with or what errors you're running into that you cannot resolve. If you run into errors, always quote the exact error message and stack trace. Don't paraphrase code or error messages. So, what exactly have you written so far and how is it not working? I don't really understand what you mean by "get the variables", do you mean input them from the user, read them from a file, or what? Also, what version of Python are you using and what operating system are you using? (Windows, Mac, Ubuntu, BSD, something else?) Good luck, Walter ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] reg: How to import dll in python
> > From: Shweta Kaushik >To: "tutor@python.org" >Sent: Wednesday, March 5, 2014 1:49 PM >Subject: [Tutor] reg: How to import dll in python >import ctypes >From ctypes import * >testDll = cdll.LoadLibrary("D:\Python Test Codes\DLL_Test\Addition.dll") >test1 = ctypes.WINFUNCTYPE(None) >test2 = test1 (("add", testDll)) > >Error Displayed >Traceback (most recent call last): > File "", line 1, in >AttributeError: function 'add' not found perhaps: from ctypes import * cppAdd = cdll.LoadLibrary(r"D:\Python Test Codes\DLL_Test\Addition.dll") # also note the r' add = cppAdd.add add.argtypes = [c_double, c_double] add(1.0, 2.0) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] reg: How to import dll in python
On Wed, Mar 5, 2014 at 7:49 AM, Shweta Kaushik wrote: > > Please find code used to create dll: I hardly ever use C++, so C++ gurus feel free to correct any errors here. I just wanted a basic C API to work with ctypes -- since that's the Python aspect of this question. I've also only targeted Microsoft's compiler since the question is using a Windows DLL. First and foremost, in a C++ project you need to use `extern "C"` to get C linkage without C++ name mangling of exported symbols. My example uses the default __cdecl calling convention. You can switch to __stdcall if you wish. ctypes automatically handles stdcall name decoration. A C++ method uses the __thiscall convention that implicitly passes the `this` pointer (e.g. for x86 MSVC passes it in the ECX register). I handle this in the __cdecl C API by using stub functions and a pointer to an opaque structure. FYI, native x64 code only has one calling convention (for now). For an x64 target, MSVC ignores calling convention keywords such as __stdcall and __thiscall. Creating the stub functions manually is tedious, and prone to introducing bugs. Big projects automate this with SWIG, SIP, etc. You could also use Boost.Python or Cython. But probably all of the latter are off topic for python-tutor. If we're sticking with CPython's standard library, then ctypes is the only game in town. OK, enough blabber. mathematics.h: #ifndef MATHEMATICS_H #define MATHEMATICS_H #ifdef BUILD_MATHEMATICS #define DLLAPI __declspec(dllexport) #else #define DLLAPI __declspec(dllimport) #endif #ifdef __cplusplus class Mathematics { int x, y; public: void input(); int add(); }; #else /* !__cpluplus */ typedef struct _Mathematics Mathematics; #endif #ifdef __cplusplus extern "C" { #endif DLLAPI Mathematics *math_create(); DLLAPI void math_free(Mathematics *); DLLAPI void math_input(Mathematics *); DLLAPI int math_add(Mathematics *); #ifdef __cplusplus } #endif #endif /* !MATHEMATICS_H */ mathematics.cpp: #include #include #include "mathematics.h" using namespace std; void Mathematics::input() { cout << "Input two integers" << endl; cin >> x >> y; } int Mathematics::add() { return x + y; } Mathematics *math_create() { Mathematics *self = nullptr; try { self = new Mathematics; } catch(bad_alloc &ba) { cerr << "bad_alloc: " << ba.what() << endl; } return self; } void math_free(Mathematics *self) { delete self; } void math_input(Mathematics *self) { self->input(); } int math_add(Mathematics *self) { return self->add(); } mathematics.dll, mathematics.lib: cl mathematics.cpp /EHsc /MD /LD /DBUILD_MATHEMATICS mathtest.c: #include #include #include "mathematics.h" #pragma comment(lib, "mathematics.lib") int main() { Mathematics *m = math_create(); if (m == NULL) return EXIT_FAILURE; math_input(m); printf("Result = %d\n", math_add(m)); math_free(m); return EXIT_SUCCESS; } Test run: C:\>mathtest.exe Input two integers -10 15 Result = 5 mathematics.py: import os import ctypes libpath = os.path.join(os.path.dirname(__file__), 'mathematics.dll') lib = ctypes.CDLL(libpath) class _CMathematics(ctypes.Structure): pass CMathematics = ctypes.POINTER(_CMathematics) def check_alloc(result, func, args): if not result: raise MemoryError return args lib.math_create.restype = CMathematics lib.math_create.errcheck = check_alloc lib.math_free.restype = None lib.math_free.argtypes = [CMathematics] lib.math_input.restype = None lib.math_input.argtypes = [CMathematics] lib.math_add.restype = ctypes.c_int lib.math_add.argtypes = [CMathematics] class Mathematics(object): _lib = lib # for __del__ _handle = None def __init__(self): self._handle = lib.math_create() def __del__(self): if self._handle: self._lib.math_free(self._handle) def input(self): lib.math_input(self._handle) def add(self): return lib.math_add(self._handle) del os, ctypes if __name__ == '__main__': m = Mathematics() m.input() print("Result = %d" % m.add()) I cached lib in the class to enable Mathematics.__del__ to work during shutdown. Without that, the __del__ method might try to use lib after Python has already set it to None. This way avoids spurious errors. Generally avoid using global names in __del__. CDLL calls the platform loader, such as Windows LoadLibrary or POSI