stdout redirection with ctypes

Use the same C runtime that CPython 3.x uses (e.g. msvcr100.dll for 3.3). Also include a call to fflush(NULL) before and after redirecting stdout. For good measure, redirect the Windows StandardOutput handle, in case a program uses the Windows API directly. This can get complicated if the DLL uses a different C runtime, which has … Read more

How to use NumPy array with ctypes?

Your code looks like it has some confusion in it — ctypes.POINTER() creates a new ctypes pointer class, not a ctypes instance. Anyway, the easiest way to pass a NumPy array to ctypes code is to use the numpy.ndarray‘s ctypes attribute’s data_as method. Just make sure the underlying data is the right type first. For … Read more

How to enable FIPS mode for libcrypto and libssl packaged with Python?

I’ve built the OpenSSL-FIPS module using regular flags (e.g.: no-asm, shared, some ancient ciphers disabled): [cfati@cfati-ubtu16x64-0:~/Work/Dev/StackOverflow/q049320993]> ~/sopr.sh ### Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ### [064bit-prompt]> ls ssl/build/bin ssl/build/lib ssl/build/bin: c_rehash openssl ssl/build/lib: engines libcrypto.a libcrypto.so libcrypto.so.1.0.0 libssl.a libssl.so libssl.so.1.0.0 pkgconfig And started playing a little bit with … Read more

How to catch printer event in python

To be notified of new print jobs, you can use FindFirstPrinterChangeNotification, FindNextPrinterChangeNotification, and a wait function from kernel32 such as WaitForSingleObject. Here’s an example to set a filter that waits for a new print job on the local print server. There’s much more work to be done if you want the JOB_NOTIFY_FIELD_DOCUMENT value out of … Read more

Sorting list of string with specific locale in python

You could use a PyICU‘s collator to avoid changing global settings: import icu # PyICU def sorted_strings(strings, locale=None): if locale is None: return sorted(strings) collator = icu.Collator.createInstance(icu.Locale(locale)) return sorted(strings, key=collator.getSortKey) Example: >>> L = [u’sandwiches’, u’angel delight’, u’custard’, u’éclairs’, u’glühwein’] >>> sorted_strings(L) [‘angel delight’, ‘custard’, ‘glühwein’, ‘sandwiches’, ‘éclairs’] >>> sorted_strings(L, ‘en_US’) [‘angel delight’, ‘custard’, ‘éclairs’, … Read more

How to return array from C++ function to Python using ctypes

Your python code will work after some minor modifications: import ctypes f = ctypes.CDLL(‘./library.so’).function f.restype = ctypes.POINTER(ctypes.c_int * 10) print [i for i in f().contents] # output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Basically there are two changes: remove numpy-related code and ctypes.cast call since we don’t need them. specify the … Read more