def closeLibrary(): handle = ssl._lib._handle if "FreeLibrary" in dir(_ctypes): _ctypes.FreeLibrary(handle) _ctypes.FreeLibrary(handle) print "OpenSSL closed, handle:", handle else: _ctypes.dlclose(handle) _ctypes.dlclose(handle) print "OpenSSL dlclosed, handle:", handle
def take_fake_data(self, runtime): base = ctypes.CDLL(working_directory + '/' + lib_name) extra = ctypes.CDLL(working_directory + '/' + extra_lib_name) BUFFER_SIZE = 1000000 # We're going to use these in a bit timestamps = np.array([]) channels = np.array([]) # For the ctypes stuff timestamps_dummy = (ctypes.c_longlong * BUFFER_SIZE)() channels_dummy = (ctypes.c_byte * BUFFER_SIZE)() tags_valid = ctypes.c_int(0) # Needs to be run to initialise the TDC, will pick up any TDC base.TDC_init(-1) # Figure out what channels need enabling and tell the TDC to do that thing channels_to_enable = (1 << (self.channel_trig - 1)) + ( 1 << (self.channel_spcm - 1)) base.TDC_enableChannels(channels_to_enable) # Set the size of the buffer for the TDC, set to max here base.TDC_setTimestampBufferSize(BUFFER_SIZE) # This is the simulated data distribution simPar = (ctypes.c_double * 2)() # Let's go with a mean value of 5us between events simPar[0] = 32000 simPar[1] = 32000 # Figure out how many tags gets us to around the runtime num_loops = int(np.round(runtime / (5e-6 * 1000))) for _ in range(0, num_loops): # Generate tags extra.TDC_generateTimestamps_flat(simPar, 1000) # Grab tags and stick them into the arrays we've made base.TDC_getLastTimestamps(1, timestamps_dummy, channels_dummy, ctypes.byref(tags_valid)) self.timestamps = np.append(self.timestamps, timestamps_dummy[0:tags_valid.value]) self.channels = np.append(self.channels, channels_dummy[0:tags_valid.value]) # Release TDC base.TDC_deInit() # Free the library, do this or windows loses it's shit _ctypes.FreeLibrary(base._handle) _ctypes.FreeLibrary(extra._handle)
def close(self): if self.system == 'windows': _ctypes.FreeLibrary(self.kunpeng._handle) else: handle = self.kunpeng._handle del self.kunpeng _ctypes.dlclose(handle)
def g2ToFile_nostreams(folder_name, file_out_name, max_time, bin_width, pulse_spacing, max_pulse_distance): file_list = os.listdir(folder_name) int_bin_width = round(bin_width / tagger_resolution) * tagger_resolution int_max_time = round(max_time / int_bin_width) * int_bin_width int_pulse_spacing = round(pulse_spacing / int_bin_width) * int_bin_width max_bin = int(round(int_max_time / int_bin_width)) num_files = len(file_list) numer_list = [int(0)] * (2 * max_bin + 1) denom_ctypes = ctypes.c_int(0) ctypes_file_list = file_list_to_ctypes(file_list, folder_name) lib = ctypes.CDLL(working_directory + '/' + lib_name) lib.getG2Correlations_nostreams.argtypes = [ ctypes.c_char_p * num_files, ctypes.c_int, ctypes.c_double, ctypes.c_double, ctypes.c_double, ctypes.c_int, ctypes.py_object, ctypes.POINTER(ctypes.c_int) ] start_time = time.time() #with wurlitzer.sys_pipes(): lib.getG2Correlations_nostreams(ctypes_file_list, num_files, int_max_time, int_bin_width, int_pulse_spacing, max_pulse_distance, numer_list, ctypes.byref(denom_ctypes)) print("Finished in " + str(time.time() - start_time) + "s") if os.name == 'nt': _ctypes.FreeLibrary(lib._handle) else: _ctypes.dlclose(lib._handle) tau = np.arange(-max_bin, max_bin + 1) * int_bin_width scipy.io.savemat(file_out_name, { 'numer': np.array(numer_list), 'denom': denom_ctypes.value, 'tau': tau })
def unload_lib(self): # Unload the currently loaded dynamic linked library to be secure if self._lib is not None and self._loaded and _ctypes is not None: _ctypes.FreeLibrary(self._lib._handle) if platform == 'win32' else _ctypes.dlclose(self._lib._handle) del self._lib self._lib = None self._loaded = False
def remove_lib(self): # Unload the currently loaded dynamic linked library to be secure if self._lib is not None: _ctypes.FreeLibrary(self._lib._handle) if platform == 'win32' else _ctypes.dlclose(self._lib._handle) del self._lib self._lib = None # deactivate the cleanup finalizers for the current set of files if self._cleanup_files is not None: self._cleanup_files.detach() if self._cleanup_lib is not None: self._cleanup_lib.detach() # If file already exists, pull new names. This is necessary on a Windows machine, because # Python's ctype does not deal in any sort of manner well with dynamic linked libraries on this OS. if path.isfile(self.lib_file): [remove(s) for s in [self.src_file, self.lib_file, self.log_file]] if MPI: mpi_comm = MPI.COMM_WORLD mpi_rank = mpi_comm.Get_rank() basename = path.join(get_cache_dir(), self._cache_key) if mpi_rank == 0 else None basename = mpi_comm.bcast(basename, root=0) basename = basename + "_%d" % mpi_rank else: basename = path.join(get_cache_dir(), "%s_0" % self._cache_key) self.src_file = "%s.c" % basename self.lib_file = "%s.%s" % (basename, 'dll' if platform == 'win32' else 'so') self.log_file = "%s.log" % basename
def cleanup_unload_lib(lib): # Clean-up the in-memory dynamic linked libraries. # This is not really necessary, as these programs are not that large, but with the new random # naming scheme which is required on Windows OS'es to deal with updates to a Parcels' kernel. if lib is not None: _ctypes.FreeLibrary( lib._handle) if platform == 'win32' else _ctypes.dlclose( lib._handle)
def __del__(self): # Clean-up the in-memory dynamic linked libraries. # This is not really necessary, as these programs are not that large, but with the new random # naming scheme which is required on Windows OS'es to deal with updates to a Parcels' kernel. if self._lib is not None: _ctypes.FreeLibrary(self._lib._handle) if platform == 'win32' else _ctypes.dlclose(self._lib._handle) del self._lib self._lib = None map(remove, [self.src_file, self.lib_file, self.log_file]) if path.isfile(self.lib_file) else None
def dlclose(obj): """ Close/unload a PIE binary or a shared library. :param obj: object returned by ctypes.CDLL when the resource was loaded """ if platform == "win32": _ctypes.FreeLibrary(obj._handle) # pylint:disable=protected-access,no-member else: _ctypes.dlclose(obj._handle) # pylint:disable=protected-access,no-member
def unload_library(self): if self.libc is not None and self.compiled and self.loaded: _ctypes.FreeLibrary( self.libc._handle ) if sys.platform == 'win32' else _ctypes.dlclose( self.libc._handle) del self.libc self.libc = None self.loaded = False
def close_dll(dll): """Closes dewesoft dll""" result = dll.DWDeInit() if result != DWStatus.DWSTAT_OK.value: raise RuntimeError("DWDeInit() failed: {}".format(result)) if platform.system() == 'Windows': _ctypes.FreeLibrary(dll._handle) else: _ctypes.dlclose(dll._handle)
def freeLibrary(self): ''' Call FMU destructor before being destructed. Just cleaning up. ''' if hasattr(self, '_library'): self.freeModelInstance() if platform.system() == 'Linux': _ctypes.dlclose(self._libraryHandle) elif platform.system() == 'Windows': _ctypes.FreeLibrary(self._libraryHandle) #only for windows else: raise FMUError.FMUError('Unknown platform: %s\n' % platform.system())
def g2ToFile_pulse(folder_name, file_out_name, min_tau_1, max_tau_1, min_tau_2, max_tau_2, bin_width): file_list = os.listdir(folder_name) int_bin_width = round(bin_width / tagger_resolution) * tagger_resolution int_min_tau_1 = round(min_tau_1 / int_bin_width) * int_bin_width min_tau_1_bin = int(round(int_min_tau_1 / int_bin_width)) int_min_tau_2 = round(min_tau_2 / int_bin_width) * int_bin_width min_tau_2_bin = int(round(int_min_tau_2 / int_bin_width)) int_max_tau_1 = round(max_tau_1 / int_bin_width) * int_bin_width max_tau_1_bin = int(round(int_max_tau_1 / int_bin_width)) int_max_tau_2 = round(max_tau_2 / int_bin_width) * int_bin_width max_tau_2_bin = int(round(int_max_tau_2 / int_bin_width)) num_files = len(file_list) numer_list = [int(0)] * (max_tau_1_bin - min_tau_1_bin + 1) * (max_tau_2_bin - min_tau_2_bin + 1) denom_ctypes = ctypes.c_int(0) ctypes_file_list = file_list_to_ctypes(file_list, folder_name) lib = ctypes.CDLL(working_directory + '/' + lib_name) lib.getG2Correlations_pulse.argtypes = [ ctypes.c_char_p * num_files, ctypes.c_int, ctypes.c_double, ctypes.c_double, ctypes.c_double, ctypes.c_double, ctypes.c_double, ctypes.py_object, ctypes.POINTER(ctypes.c_int), ctypes.c_int ] start_time = time.time() #with wurlitzer.sys_pipes(): lib.getG2Correlations_pulse(ctypes_file_list, num_files, int_min_tau_1, int_max_tau_1, int_min_tau_2, int_max_tau_2, int_bin_width, numer_list, ctypes.byref(denom_ctypes), 4) print("Finished in " + str(time.time() - start_time) + "s") time.sleep(1) if os.name == 'nt': _ctypes.FreeLibrary(lib._handle) else: _ctypes.dlclose(lib._handle) tau_1 = np.arange(min_tau_1_bin, max_tau_1_bin + 1) * int_bin_width tau_2 = np.arange(min_tau_2_bin, max_tau_2_bin + 1) * int_bin_width scipy.io.savemat( file_out_name, { 'numer': np.array(numer_list).reshape(max_tau_2_bin - min_tau_2_bin + 1, max_tau_1_bin - min_tau_1_bin + 1), 'tau_1': tau_1, 'tau_2': tau_2 })
def _close_library(): global _library global _library_wrapper print("Unloading library.") _ctypes.FreeLibrary(_library._handle) _library = None _library_wrapper = None print("Library unloaded.")
def __del__(self): """Handle removal of the DLL when the simulator is deleted.""" if self._dll is not None: handle = self._dll._handle if platform.system() == 'Windows': _ctypes.FreeLibrary(handle) # pylint: disable=no-member else: _ctypes.dlclose(handle) # pylint: disable=no-member self._dll = None if self._dir is not None: shutil.rmtree(self._dir) self._dir = None
def _unload_lib_obj(olib): if utils.is_windows(): import _ctypes _ctypes.FreeLibrary(olib._handle) del olib elif utils.is_linux(): import _ctypes _ctypes.dlclose(olib._handle) del olib elif utils.is_mac(): import _ctypes _ctypes.dlclose(olib._handle) del olib
def remove_lib(self): # Unload the currently loaded dynamic linked library to be secure if self._lib is not None: _ctypes.FreeLibrary(self._lib._handle) if platform == 'win32' else _ctypes.dlclose(self._lib._handle) del self._lib self._lib = None # If file already exists, pull new names. This is necessary on a Windows machine, because # Python's ctype does not deal in any sort of manner well with dynamic linked libraries on this OS. if path.isfile(self.lib_file): map(remove, [self.src_file, self.lib_file, self.log_file]) basename = path.join(get_cache_dir(), self._cache_key) self.src_file = "%s.c" % basename self.lib_file = "%s.%s" % (basename, 'dll' if platform == 'win32' else 'so') self.log_file = "%s.log" % basename
def unload(self): global yaya if yaya: yaya.unload() # force Python to unload the library # else the next load would not function properly try: _ctypes.FreeLibrary(yaya._handle) except: pass try: _ctypes.dlclose(yaya._handle) except: pass yaya = cdll.LoadLibrary("libaya5.so")
def close_devices(self): if self.devices_open: import _ctypes import gc self.pyaudio.Pa_Terminate() self.pypm.Pm_Terminate() for x in (self.pyaudio._handle, self.pypm._handle): if pydaw_util.IS_WINDOWS: _ctypes.FreeLibrary(x) else: _ctypes.dlclose(x) del self.pyaudio del self.pypm gc.collect() self.devices_open = False time.sleep(0.5) # Give the kernel audio API time to close else: pass
def _delete(library, tempdir): try: # clean up the espeak library allocated memory library.espeak_Terminate() except AttributeError: # library not loaded pass # on Windows it is required to unload the library or the .dll file # cannot be erased from the temporary directory if sys.platform == 'win32': # pragma: nocover # pylint: disable=import-outside-toplevel # pylint: disable=protected-access # pylint: disable=no-member import _ctypes _ctypes.FreeLibrary(library._handle) # clean up the tempdir containing the copy of the library shutil.rmtree(tempdir)
def take_data(runtime, channel_trig, channel_spcm): base = ctypes.CDLL(working_directory + '/' + lib_name) BUFFER_SIZE = 1000000 # We're going to use these in a bit timestamps = np.array([]) channels = np.array([]) # For the ctypes stuff timestamps_dummy = (ctypes.c_longlong*BUFFER_SIZE)() channels_dummy = (ctypes.c_byte*BUFFER_SIZE)() tags_valid = ctypes.c_int(0) # Needs to be run to initialise the TDC, will pick up any TDC base.TDC_init(-1) # Figure out what channels need enabling and tell the TDC to do that thing channels_to_enable = (1 << (channel_trig-1)) + (1 << (channel_spcm-1)) base.TDC_enableChannels(channels_to_enable) # Set the size of the buffer for the TDC, set to max here base.TDC_setTimestampBufferSize( BUFFER_SIZE ) # Loop to accumulate tags start = time.time() while time.time() - start < runtime: # Wait to accumualte tags time.sleep(0.05) # Grab tags and stick them into the arrays we've made base.TDC_getLastTimestamps( 1, timestamps_dummy, channels_dummy, ctypes.byref(tags_valid)) timestamps = np.append(timestamps,timestamps_dummy[0:tags_valid.value]) channels = np.append(channels,channels_dummy[0:tags_valid.value]) # Release TDC base.TDC_deInit() # Free the library, do this or windows loses it's shit _ctypes.FreeLibrary(base._handle) # Return tags and channels return timestamps, channels
def _cleanup(): """ Remove all temporary dll files from file system on interpreter termination. Helper function which removes all temporary dll files from the file system which have been created by the JMIModel constructor and have not been deleted when Python interpreter is terminated. Uses the class attribute _temp_dlls which holds a list of all temporary dll file names and handles created during the Python session. """ for tmp in _temp_dlls: tmpfile = tmp.get('name') if os.path.exists(tmpfile) and os.path.isfile(tmpfile): if sys.platform == 'win32': _ctypes.FreeLibrary(tmp.get('handle')) #else: # _ctypes.dlclose(tmp.get('handle')) os.remove(tmpfile)
def Solution(self):# Функция взаимодействия с динамической библиотекой для нахождения решения data = self.data sizet = self.sizet sizex = self.sizex sizey = self.sizey u0 = self.u0 f = self.f arr2 = [sizet, sizex - 1, sizey - 1] arr =[self.R, self.r, self.time, self.a] ctypes_arrays = [np.ctypeslib.as_ctypes(array) for array in data] point = (ctypes.c_double * 4)(*arr) point2 = (ctypes.c_int * 3)(*arr2) pointer_ar = (ctypes.POINTER(ctypes.c_double) * sizet)(*ctypes_arrays) callback_type = ctypes.CFUNCTYPE(ctypes.c_double, ctypes.c_double, ctypes.c_double) callback_type2 = ctypes.CFUNCTYPE(ctypes.c_double, ctypes.c_double, ctypes.c_double, ctypes.c_double) mydll = ctypes.CDLL("./DipDll.dll") mydll.Solution(pointer_ar, callback_type(u0), callback_type2(f), point, point2) _ctypes.FreeLibrary(mydll._handle) del mydll
def __del__(self): # Clean-up the in-memory dynamic linked libraries. # This is not really necessary, as these programs are not that large, but with the new random # naming scheme which is required on Windows OS'es to deal with updates to a Parcels' kernel. if self._lib is not None: _ctypes.FreeLibrary( self._lib._handle) if platform == 'win32' else _ctypes.dlclose( self._lib._handle) del self._lib self._lib = None if path.isfile(self.lib_file) and self.delete_cfiles: [ remove(s) for s in [self.src_file, self.lib_file, self.log_file] ] self.fieldset = None self.field_args = None self.const_args = None self.funcvars = None self.funccode = None
def run(self, inputs, **kwargs): """ Run TensorflowRep. :param inputs: Given inputs. :param kwargs: Other args. :return: Outputs. """ super(QumicoRep, self).run(inputs, **kwargs) # generate c code if self._compiled is False: self.convert_c() # execute NodeDLL = ctypes.CDLL(path.join(self._out_c_path, QUMICO_MAIN + ".so")) # convert extension outputs = [] for i in range(len(self.graph[-1].op.output_tensor)): outputs.append( np.zeros(dtype=self.graph[-1].op.output_tensor_dtypes[i], shape=self.graph[-1].op.output_tensor_shapes[i])) inputs_ndpointers = [] inputs_ndpointers.extend(self._get_ndpointers(inputs)) inputs_ndpointers.extend(self._get_ndpointers(outputs)) NodeDLL.qumico.argtypes = inputs_ndpointers NodeDLL.qumico.restype = ctypes.c_int NodeDLL.qumico(*(inputs + outputs)) # todo move to postrun decorator method if platform.system() == "Windows": _ctypes.FreeLibrary(NodeDLL._handle) # unload dll that is cached elif platform.system() == "Linux": _ctypes.dlclose(NodeDLL._handle) # unload dll that is cached for n in self.graph: n.op.reset_for_run() return outputs
def reset(self): if self.lib_object is None: return try: if self.is_remote: disconnect() else: finalize() except NxLibException: # Ignore any errors while finalizing the NxLib. We will unload the library anyway. pass # ctypes does not unload the library even when the CDLL object gets garbage collected, # so we unload it manually. if os.name == "posix": _ctypes.dlclose(self.lib_object._handle) elif os.name == "nt": _ctypes.FreeLibrary(self.lib_object._handle) del self.lib_object self.lib_object = None
def g3ToDict(folder_name, file_out_name, max_time, bin_width, pulse_spacing, max_pulse_distance, calc_norm=True, update=False): #Convert various parameters to their integer values for the DLL int_bin_width = round(bin_width / tagger_resolution) * tagger_resolution int_max_time = round(max_time / int_bin_width) * int_bin_width int_pulse_spacing = round(pulse_spacing / int_bin_width) * int_bin_width max_bin = int(round(int_max_time / int_bin_width)) numer_list = [int(0)] * (2 * max_bin + 1) * (2 * max_bin + 1) #Calculate what tau should look like tau = np.arange(-max_bin, max_bin + 1) * int_bin_width #Get list of files to process from the data folder dir_file_list = os.listdir(folder_name) #Previous values to add to new calculated total old_denom = 0 old_numer = [] updating = False #Check if the output file already exists, and if we should be updating the old file if (os.path.isfile(file_out_name) or os.path.isfile(file_out_name + ".mat")) and update: #Load old matrix old_mat = scipy.io.loadmat(file_out_name) #Try and fetch old file list, try included for backwards compatibility try: #Grab old file list, denominator and numerator old_file_list = [ filename.strip() for filename in old_mat['file_list'] ] old_denom = old_mat['denom_g3'][0][0] old_numer = old_mat['numer_g3'] old_tau = old_mat['tau'][0] if not np.array_equal(tau, old_tau): print( "Can't update as the new and old tau values are different") raise Exception("Different tau values") #If so only process files that are different from last processing file_list = [ filename for filename in dir_file_list if (filename not in old_file_list) ] updating = True except: #Throw an error if the old stuff couldn't be grabbed and just default to non-update behaviour print("Error thrown, falling back on using whole file list") file_list = dir_file_list else: #Otherwise process everything file_list = dir_file_list num_files = len(file_list) #Convert things to C versions for the DLL denom_ctypes = ctypes.c_int(0) ctypes_file_list = file_list_to_ctypes(file_list, folder_name) #Setup the DLL lib = ctypes.CDLL(working_directory + '/' + lib_name) lib.getG3Correlations.argtypes = [ ctypes.c_char_p * num_files, ctypes.c_int, ctypes.c_double, ctypes.c_double, ctypes.c_double, ctypes.c_int64, ctypes.py_object, ctypes.POINTER(ctypes.c_int), ctypes.c_int, ctypes.c_int ] start_time = time.time() #Call the DLL lib.getG3Correlations(ctypes_file_list, num_files, int_max_time, int_bin_width, int_pulse_spacing, max_pulse_distance, numer_list, ctypes.byref(denom_ctypes), calc_norm, 4, 4) print("Finished in " + str(time.time() - start_time) + "s") time.sleep(1) #This is required in Windows as otherwise the DLL can't be re-used without rebooting the computer if os.name == 'nt': _ctypes.FreeLibrary(lib._handle) else: _ctypes.dlclose(lib._handle) #Check if we have old values to update output_dict = {} if updating: try: #Try and add the newly calculated values to the old ones may f**k up if you ask for numerators of different sizes output_dict = { 'numer_g3': np.array(numer_list).reshape((2 * max_bin + 1), (2 * max_bin + 1)) + old_numer, 'denom_g3': denom_ctypes.value + old_denom, 'tau': tau, 'file_list': dir_file_list } except: print("Could not update values") else: output_dict = { 'numer_g3': np.array(numer_list).reshape((2 * max_bin + 1), (2 * max_bin + 1)), 'denom_g3': denom_ctypes.value, 'tau': tau, 'file_list': dir_file_list } return output_dict
def closeLibrary(): import _ctypes if "FreeLibrary" in dir(_ctypes): _ctypes.FreeLibrary(OpenSSL._lib._handle) else: _ctypes.dlclose(OpenSSL._lib._handle)
def unload_library(lib): _ctypes.FreeLibrary(lib._handle)
def finish_tests(): global dll dll.finish_tests() _ctypes.FreeLibrary(dll._handle) dll = 0