Popular recipes tagged "meta:requires=ctypes"http://code.activestate.com/recipes/tags/meta:requires=ctypes/2017-01-03T10:31:26-08:00ActiveState Code Recipesctypes CDLL with automatic errno checking (Python) 2017-01-03T10:31:26-08:00Oren Tiroshhttp://code.activestate.com/recipes/users/2033964/http://code.activestate.com/recipes/580741-ctypes-cdll-with-automatic-errno-checking/ <p style="color: grey"> Python recipe 580741 by <a href="/recipes/users/2033964/">Oren Tirosh</a> . </p> <p>This class extends ctypes.CDLL with automatic checking of errno and automatically raises an exception when set by the function.</p> Using ctypes to call C code from Python (Python) 2016-05-09T18:00:44-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580660-using-ctypes-to-call-c-code-from-python/ <p style="color: grey"> Python recipe 580660 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/ctypes/">ctypes</a>, <a href="/recipes/tags/ffi/">ffi</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/time/">time</a>). </p> <p>This recipe shows basic usage of the ctypes module to call C code from Python code.</p> How to execute x86 64-bit assembly code directly from Python on Linux (requires Nasm) (Python) 2015-03-24T18:23:19-07:00Simon Harrisonhttp://code.activestate.com/recipes/users/4191738/http://code.activestate.com/recipes/579037-how-to-execute-x86-64-bit-assembly-code-directly-f/ <p style="color: grey"> Python recipe 579037 by <a href="/recipes/users/4191738/">Simon Harrison</a> (<a href="/recipes/tags/nasm/">nasm</a>). Revision 4. </p> <p>This particular example pertains to running 64-bit assembly code under 64-bit Python, however if you are fortunate enough to be running on a 32-bit Linux platform you may get away with doing a lot less. Since the introduction of DEP you can no longer just shove some code in a buffer and execute it. You must first allow execute permission. Further, you cannot change memory protection settings for anything other than an entire page, so I've padded the memory allocated to include a page before and a page after, so I can seek back to the start of page for the start of my assembler, then change memory protection from there secure in the knowledge that I won't change protection for anything beyond the end of my allocated region. I could probably have used valloc() instead.</p> <p>After that it's simply a matter of changing protection. There is some doubt over whether you can leave this memory read/writable after you set the execute bit, I haven't played with that.</p> <p>Finally, it's important to reset the memory protection before the memory gets freed/reused by Python because Python has no knowledge of the hackery you've just been up to.</p> <p>For a Windows version, use VirtualProtect() to achieve similar results. I could have conditionally added this and made the code portable, however I rarely do any assembler on Windows, so it's left as an exercise for the reader.</p> Simple video with ctypes and Xvid (Tkinter,pygame,...) (Python) 2015-06-29T13:59:38-07:00Jiri Justrahttp://code.activestate.com/recipes/users/4192188/http://code.activestate.com/recipes/579073-simple-video-with-ctypes-and-xvid-tkinterpygame/ <p style="color: grey"> Python recipe 579073 by <a href="/recipes/users/4192188/">Jiri Justra</a> (<a href="/recipes/tags/ctypes/">ctypes</a>, <a href="/recipes/tags/video/">video</a>, <a href="/recipes/tags/xvid/">xvid</a>). </p> <p>This is simple library for playing avi video clips. Videos must be SOUNDLESS (and maybe Xvid encoded wont hurt), to work it properly. I am using it to play intro. It suites good to this purpose, since it can play one clip at a time only.</p> Simple audio with ctypes and SDL also for Tkinter (Python) 2015-08-17T18:54:17-07:00Jiri Justrahttp://code.activestate.com/recipes/users/4192188/http://code.activestate.com/recipes/579070-simple-audio-with-ctypes-and-sdl-also-for-tkinter/ <p style="color: grey"> Python recipe 579070 by <a href="/recipes/users/4192188/">Jiri Justra</a> (<a href="/recipes/tags/audio/">audio</a>, <a href="/recipes/tags/sdl/">sdl</a>, <a href="/recipes/tags/sdl_mixer/">sdl_mixer</a>, <a href="/recipes/tags/tkinter/">tkinter</a>). Revision 3. </p> <p>I've needed just to play audio in my Tkinter application, but it seems there is no easy way to do this, so I have made this simple code. It is small ctypes wrapper around SDL_mixer.</p> <p>It should work for Win and *nix also .. I've tested it on ubuntu-14.04.3-desktop-i386</p> Reimplementation of rmtree supporting Windows reparse points (Python) 2014-03-08T21:17:41-08:00Charles Grunwaldhttp://code.activestate.com/recipes/users/4175823/http://code.activestate.com/recipes/578849-reimplementation-of-rmtree-supporting-windows-repa/ <p style="color: grey"> Python recipe 578849 by <a href="/recipes/users/4175823/">Charles Grunwald</a> (<a href="/recipes/tags/ctypes/">ctypes</a>, <a href="/recipes/tags/directories/">directories</a>, <a href="/recipes/tags/files/">files</a>, <a href="/recipes/tags/remove/">remove</a>, <a href="/recipes/tags/win32/">win32</a>). Revision 3. </p> <p>Ctypes-based implementation of shutil.rmtree that correctly handles Windows reparse point folders. (symbolic links, junctions, etc)</p> Sticky window (Python) 2013-12-16T17:10:25-08:00winandhttp://code.activestate.com/recipes/users/4188769/http://code.activestate.com/recipes/578789-sticky-window/ <p style="color: grey"> Python recipe 578789 by <a href="/recipes/users/4188769/">winand</a> (<a href="/recipes/tags/windows/">windows</a>). </p> <p>Create a window which sticks to screen edges using PyQt4 under Windows.</p> Windows directory walk using ctypes (Python) 2013-08-09T00:17:00-07:00Shao-chuan Wanghttp://code.activestate.com/recipes/users/4168519/http://code.activestate.com/recipes/578629-windows-directory-walk-using-ctypes/ <p style="color: grey"> Python recipe 578629 by <a href="/recipes/users/4168519/">Shao-chuan Wang</a> (<a href="/recipes/tags/ctypes/">ctypes</a>, <a href="/recipes/tags/system/">system</a>). </p> <p>Windows shell/explorer has a limit size of full path, but both NTFS and ReFS can support full path longer than the limit; this is making os.walk on Windows bad if files are in deeply nested folders, and therefore this recipe.</p> Get memory usage of Windows processes using GetProcessMemoryInfo (via ctypes) (Python) 2013-04-25T01:26:19-07:00Ben Hoythttp://code.activestate.com/recipes/users/4170919/http://code.activestate.com/recipes/578513-get-memory-usage-of-windows-processes-using-getpro/ <p style="color: grey"> Python recipe 578513 by <a href="/recipes/users/4170919/">Ben Hoyt</a> (<a href="/recipes/tags/ctypes/">ctypes</a>, <a href="/recipes/tags/memory/">memory</a>, <a href="/recipes/tags/process/">process</a>, <a href="/recipes/tags/win32/">win32</a>, <a href="/recipes/tags/windows/">windows</a>). </p> <p>These functions call the Win32 function GetProcessMemoryInfo() using ctypes to get the memory usage of the current process. Works on both 32-bit and 64-bit Windows and Python 2.6+ (including Python 3.x).</p> car.py (Ctypes ARchiver in PYthon [libarchive ffi wrapper]) (Python) 2013-05-28T04:02:15-07:00Mike 'Fuzzy' Partinhttp://code.activestate.com/recipes/users/4179778/http://code.activestate.com/recipes/578531-carpy-ctypes-archiver-in-python-libarchive-ffi-wra/ <p style="color: grey"> Python recipe 578531 by <a href="/recipes/users/4179778/">Mike 'Fuzzy' Partin</a> (<a href="/recipes/tags/bzip2/">bzip2</a>, <a href="/recipes/tags/cpio/">cpio</a>, <a href="/recipes/tags/ctypes/">ctypes</a>, <a href="/recipes/tags/ffi/">ffi</a>, <a href="/recipes/tags/gzip/">gzip</a>, <a href="/recipes/tags/lzma/">lzma</a>, <a href="/recipes/tags/pax/">pax</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/tar/">tar</a>). Revision 3. </p> <p>CTypes libarchive wrapper, handles extraction only with no arguments.</p> Enable Clear Type font smoothing on Windows (ctypes version) (Python) 2013-05-07T09:17:56-07:00ccpizzahttp://code.activestate.com/recipes/users/4170754/http://code.activestate.com/recipes/578500-enable-clear-type-font-smoothing-on-windows-ctypes/ <p style="color: grey"> Python recipe 578500 by <a href="/recipes/users/4170754/">ccpizza</a> (<a href="/recipes/tags/fontsmoothing/">fontsmoothing</a>, <a href="/recipes/tags/windows/">windows</a>). Revision 3. </p> <p>Running the script without parameters will enable Clear Type font smoothing. Pass 0, false, off, or disable to turn off Clear Type.</p> <p>This version requires the <code>ctypes</code> module.</p> <p>For a version that uses pywin32 see <a href="http://code.activestate.com/recipes/578499-enable-clear-type-font-smoothing-on-windows-pywin3/" rel="nofollow">http://code.activestate.com/recipes/578499-enable-clear-type-font-smoothing-on-windows-pywin3/</a> </p> Save and restore SHA-512 internal state (Python) 2013-03-03T18:05:42-08:00Dima Tisnekhttp://code.activestate.com/recipes/users/4068698/http://code.activestate.com/recipes/578479-save-and-restore-sha-512-internal-state/ <p style="color: grey"> Python recipe 578479 by <a href="/recipes/users/4068698/">Dima Tisnek</a> (<a href="/recipes/tags/database/">database</a>, <a href="/recipes/tags/inner/">inner</a>, <a href="/recipes/tags/pickle/">pickle</a>, <a href="/recipes/tags/restore/">restore</a>, <a href="/recipes/tags/sha512/">sha512</a>, <a href="/recipes/tags/state/">state</a>). </p> <p>If you have a very long input to hash, you may want to save your progress.</p> <p>CPython doesn't normally let you, but it's easy to hack around via ctypes</p> Peek at Python value stack (Python) 2013-01-08T22:15:21-08:00Dima Tisnekhttp://code.activestate.com/recipes/users/4068698/http://code.activestate.com/recipes/578412-peek-at-python-value-stack/ <p style="color: grey"> Python recipe 578412 by <a href="/recipes/users/4068698/">Dima Tisnek</a> (<a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python2/">python2</a>, <a href="/recipes/tags/stack/">stack</a>). </p> <p>Poking at value stack, a.k.a. evaluation stack is not allowed in Python, however everything is doable with ctypes.</p> <p>I think this could be useful in debugging complex statements.</p> Eject\close CDRom door with IronPython using CPython libraries (Text) 2012-12-21T15:54:56-08:00greg zakharovhttp://code.activestate.com/recipes/users/4184115/http://code.activestate.com/recipes/578388-ejectclose-cdrom-door-with-ironpython-using-cpytho/ <p style="color: grey"> Text recipe 578388 by <a href="/recipes/users/4184115/">greg zakharov</a> (<a href="/recipes/tags/cdrom/">cdrom</a>, <a href="/recipes/tags/close/">close</a>, <a href="/recipes/tags/eject/">eject</a>). </p> <p>Of course, ActivePython is a great stuff and there are not problems which it can not solve. But I do like experiment with different technologies. The following script demonstrates how you can eject\close CDRom door with IronPython using standard CPython libraries.</p> Python reader + writer for spss sav files (Linux, Mac & Windows) (Python) 2013-02-20T22:07:27-08:00Albert-Jan Roskamhttp://code.activestate.com/recipes/users/4177640/http://code.activestate.com/recipes/577811-python-reader-writer-for-spss-sav-files-linux-mac-/ <p style="color: grey"> Python recipe 577811 by <a href="/recipes/users/4177640/">Albert-Jan Roskam</a> (<a href="/recipes/tags/reader/">reader</a>, <a href="/recipes/tags/sav/">sav</a>, <a href="/recipes/tags/spss/">spss</a>, <a href="/recipes/tags/writer/">writer</a>). Revision 12. </p> <p><strong>Python Program to READ &amp; WRITE Spss system files (.sav) (Linux,Mac or Windows)</strong></p> <ul> <li><em>Check <a href="https://pypi.python.org/pypi/savReaderWriter/" rel="nofollow">https://pypi.python.org/pypi/savReaderWriter/</a> for the latest version (including the libraries!)</em></li> <li>Requires libspssdio.so.1 (LINUX) or libspssdio.dylib (MAC) or spssio32.dll (WINDOWS) plus associated libaries, which can be freely downloaded from: <a href="https://www.ibm.com/developerworks/mydeveloperworks/wikis/home/wiki/We70df3195ec8_4f95_9773_42e448fa9029/page/Downloads%2520for%2520IBM%25C2%25AE%2520SPSS%25C2%25AE%2520Statistics?lang=en" rel="nofollow">https://www.ibm.com/developerworks/mydeveloperworks/wikis/home/wiki/We70df3195ec8_4f95_9773_42e448fa9029/page/Downloads%20for%20IBM%C2%AE%20SPSS%C2%AE%20Statistics?lang=en</a></li> <li>It is recommended to download the v21 I/O files (required for writing zlib (.zsav) compressed files)</li> <li>December 2012 (complete rewrite):</li> <li>Added support for slicing, indexing, array slicing + other special methods</li> <li>Added support for writing spss date fields</li> <li>Added support for almost all meta data (missing values, sets, roles, etc.)</li> <li>Added support for 64 bit Windows (tested with Win7) and other OSs (z/Linux, Solaris, HP Linux, IBM AIX (untested though)</li> <li>Added support for reading and writing zlib compressed (.zsav) files</li> <li>Removed pesky segfault error when freeing memory</li> <li>Removed errors related to encoding</li> <li>Changed some Reader defaults (verbose=False, returnHeader=False)</li> <li>Renamed SavDataDictionaryReader into SavHeaderReader</li> </ul> <p><strong>LINUX:</strong></p> <p><em>Installation (tested on Linux Ubuntu 10):</em></p> <ul> <li>additional packages/files needed are: intel-icc8-libs_8.0-1_i386.deb,libicu32_3.2-3_i386.deb, libstdc++5_3.3.6-20_i386.deb, libirc.so.</li> <li>Run the following commands in your terminal: sudo apt-get install intel-icc8-libs; sudo apt-get install libicu32; sudo apt-get install libstdc++5.</li> <li>Then convert libirc.a (static) to libirc.so (dynamic), save in same location as libspssdio.so.1: ar vx intel-icc8-libs_8.0-1_i386.deb; tar -xzvf data.tar.gz ./usr/lib/libirc.a; ar -x libirc.a.</li> </ul> <p><em>Calling the program:</em></p> <ul> <li>In the TERMINAL type: export LD_LIBRARY_PATH=/path/of/additional/sofiles; python /some/path/wrapperToProgram.py. You may also add ld_library_path to .bashrc</li> <li>The wrapper starts with "from SavReaderWriter import *", followed by e.g. stuff from the if __name__ == '__main__' section</li> </ul> <p><strong>MAC OS:</strong></p> <ul> <li>you must put all the dylib files that come with the IBM SPSS_Statistics_InputOutput_Modules_* package in the macos directory somewhere that OS X can find them</li> <li>one simple way to accomplish this is to copy them to /usr/lib</li> </ul> <p><strong>WINDOWS:</strong></p> <ul> <li>You can also find this dll in the installation directory of SPSS (although SPSS is _not_ needed!)</li> <li>The .dll should be saved in the same location as this program.</li> </ul> <p><strong>USAGE:</strong> See docstrings + __main__ section</p> Disable File System Redirector (Python) 2012-02-06T16:34:02-08:00zxwhttp://code.activestate.com/recipes/users/4180790/http://code.activestate.com/recipes/578035-disable-file-system-redirector/ <p style="color: grey"> Python recipe 578035 by <a href="/recipes/users/4180790/">zxw</a> (<a href="/recipes/tags/32bit/">32bit</a>, <a href="/recipes/tags/64bit/">64bit</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/redirector/">redirector</a>, <a href="/recipes/tags/windows/">windows</a>). Revision 5. </p> <p>This disables the <a href="http://msdn.microsoft.com/en-us/library/aa384187(v=vs.85).aspx">Windows File System Redirector</a>.</p> <p>When a 32 bit program runs on a 64 bit operating system the paths to C:/Windows/System32 automatically get redirected to the 32 bit version (C:/Windows/SysWow64), if you really do need to access the contents of System32, you need to disable the file system redirector first.</p> Multithreading Downloader Class (Python) 2012-07-22T07:44:20-07:00Itay Brandeshttp://code.activestate.com/recipes/users/4182927/http://code.activestate.com/recipes/578220-multithreading-downloader-class/ <p style="color: grey"> Python recipe 578220 by <a href="/recipes/users/4182927/">Itay Brandes</a> (<a href="/recipes/tags/downloader/">downloader</a>, <a href="/recipes/tags/multithread/">multithread</a>, <a href="/recipes/tags/multithreading/">multithreading</a>, <a href="/recipes/tags/urllib/">urllib</a>, <a href="/recipes/tags/urllib2/">urllib2</a>, <a href="/recipes/tags/urlopen/">urlopen</a>). </p> <p>Garbs files from the web using multithreading in an attempt to enhance transfer rate.</p> DDE Client (Python) 2011-04-14T16:33:19-07:00David Naylorhttp://code.activestate.com/recipes/users/4177661/http://code.activestate.com/recipes/577654-dde-client/ <p style="color: grey"> Python recipe 577654 by <a href="/recipes/users/4177661/">David Naylor</a> (<a href="/recipes/tags/ctypes/">ctypes</a>, <a href="/recipes/tags/dde/">dde</a>, <a href="/recipes/tags/win32/">win32</a>). </p> <p>A DDE Client using ctypes for Windows. Unlike the pywin32 implementation this code supports advice requests. </p> Get parent process pid win32 (Python) 2012-05-15T18:01:22-07:00nuumiohttp://code.activestate.com/recipes/users/4182080/http://code.activestate.com/recipes/578133-get-parent-process-pid-win32/ <p style="color: grey"> Python recipe 578133 by <a href="/recipes/users/4182080/">nuumio</a> (<a href="/recipes/tags/process/">process</a>, <a href="/recipes/tags/process_management/">process_management</a>). </p> <p>Use ctypes + Win32 functions to enumerate processes and find parent process id.</p> <p>Thanks to winterTTr Dong for the original recipe.</p> How to Mutate a Float (Python) 2012-05-14T16:49:13-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/578132-how-to-mutate-a-float/ <p style="color: grey"> Python recipe 578132 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/ctypes/">ctypes</a>, <a href="/recipes/tags/experiment/">experiment</a>, <a href="/recipes/tags/float/">float</a>, <a href="/recipes/tags/structure/">structure</a>). </p> <p>This is just an experiment to test my mental understanding of Python. It should not be used in any code as is violates the design principle that floats are to be immutable. The code also abuses <code>ctypes</code> and an understanding of how <code>floats</code> are currently arranged in memory. <code>set_float</code> is not guaranteed to work properly on any system, and may fail to work in the future if the data's arrangement changes.</p>