Skip to content

Commit

Permalink
SetRuntimeEnv added in NI-Fake metadata and called in _library_interp…
Browse files Browse the repository at this point in the history
…reter (#1902)

* Added call to SetRuntimeEnvironment in library singleton get

* Update call arguments for SetRuntimeEnvironment and fix failing test

* Update comments in generated files

* Regenerated files

* Do custom tear down of patch in unit tests

* Rephrased changelog text

* Add tear down in remaining tests that fail

* Regenerated python files

* Updated character encoding

* Reworked tests

* Rename test

* Rename test and remove unnecessary calls

* Reworked test

* Reworked test to use pytest.raises

* Rework after template change and regenerate

* Call SetRuntimeEnvironment only once. Reworked tests

* Changed test name to be more descriptive

* Remove _was_runtime_env_set from files in which it's not used

* Removed abbreviations

* Replace direct initializations of LibraryInterpreter with get_initialized_library_interpreter

* Remove test abort missing in lib

---------

Co-authored-by: Victor Vădan <victor.vadan@ni.com>
  • Loading branch information
victorvadan and Victor-Vadan authored Apr 7, 2023
1 parent 09ab9b1 commit 035cb37
Show file tree
Hide file tree
Showing 20 changed files with 1,338 additions and 517 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ All notable changes to this project will be documented in this file.
* #### Removed
* ### `nidcpower` (NI-DCPower)
* #### Added
* Pass Python interpreter information if the driver runtime version supports it. This is used by NI in order to better understand client usage.
* Enums added:
* `CurrentLimitBehavior`
* #### Changed
Expand Down
26 changes: 26 additions & 0 deletions build/templates/_library_interpreter.py.mako
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ functions = helper.filter_codegen_functions(functions)
import array
import ctypes
import hightime # noqa: F401
% if 'SetRuntimeEnvironment' in functions:
import platform

% endif
import ${module_name}._library_singleton as _library_singleton
import ${module_name}._visatype as _visatype
% if config['enums']:
Expand All @@ -29,6 +33,11 @@ import ${module_name}.${c['file_name']} as ${c['file_name']} # noqa: F401
% endfor


% if 'SetRuntimeEnvironment' in functions:
_was_runtime_environment_set = None


% endif
# Helper functions for creating ctypes needed for calling into the driver DLL
def _get_ctypes_pointer_for_buffer(value=None, library_type=None, size=None):
if isinstance(value, array.array):
Expand Down Expand Up @@ -75,6 +84,23 @@ class LibraryInterpreter(object):
def __init__(self, encoding):
self._encoding = encoding
self._library = _library_singleton.get()
% if 'SetRuntimeEnvironment' in functions:
global _was_runtime_environment_set
if _was_runtime_environment_set is None:
try:
runtime_env = platform.python_implementation()
version = platform.python_version()
self.set_runtime_environment(
runtime_env,
version,
'',
''
)
except errors.DriverTooOldError:
pass
finally:
_was_runtime_environment_set = True
% endif
# Initialize _${config['session_handle_parameter_name']} to 0 for now.
# Session will directly update it once the driver runtime init function has been called and
# we have a valid session handle.
Expand Down
20 changes: 20 additions & 0 deletions generated/nidcpower/nidcpower/_library_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import array
import ctypes
import hightime # noqa: F401
import platform

import nidcpower._library_singleton as _library_singleton
import nidcpower._visatype as _visatype
import nidcpower.enums as enums # noqa: F401
Expand All @@ -14,6 +16,9 @@
import nidcpower.lcr_measurement as lcr_measurement # noqa: F401


_was_runtime_environment_set = None


# Helper functions for creating ctypes needed for calling into the driver DLL
def _get_ctypes_pointer_for_buffer(value=None, library_type=None, size=None):
if isinstance(value, array.array):
Expand Down Expand Up @@ -60,6 +65,21 @@ class LibraryInterpreter(object):
def __init__(self, encoding):
self._encoding = encoding
self._library = _library_singleton.get()
global _was_runtime_environment_set
if _was_runtime_environment_set is None:
try:
runtime_env = platform.python_implementation()
version = platform.python_version()
self.set_runtime_environment(
runtime_env,
version,
'',
''
)
except errors.DriverTooOldError:
pass
finally:
_was_runtime_environment_set = True
# Initialize _vi to 0 for now.
# Session will directly update it once the driver runtime init function has been called and
# we have a valid session handle.
Expand Down
24 changes: 20 additions & 4 deletions generated/nifake/nifake/_grpc_stub_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,12 @@ def get_a_string_of_fixed_maximum_size(self): # noqa: N802
def get_a_string_using_python_code(self, a_number): # noqa: N802
raise NotImplementedError('get_a_string_using_python_code is not supported over gRPC')

def get_an_ivi_dance_string(self): # noqa: N802
def get_an_ivi_dance_char_array(self): # noqa: N802
response = self._invoke(
self._client.GetAnIviDanceString,
grpc_types.GetAnIviDanceStringRequest(vi=self._vi),
self._client.GetAnIviDanceCharArray,
grpc_types.GetAnIviDanceCharArrayRequest(vi=self._vi),
)
return response.a_string
return response.char_array

def get_an_ivi_dance_with_a_twist_string(self): # noqa: N802
response = self._invoke(
Expand Down Expand Up @@ -284,12 +284,25 @@ def get_error(self): # noqa: N802
)
return response.error_code, response.description

def get_parameter_with_overridden_grpc_name(self, enum_parameter): # noqa: N802
response = self._invoke(
self._client.GetParameterWithOverriddenGrpcName,
grpc_types.GetParameterWithOverriddenGrpcNameRequest(vi=self._vi, enum_parameter_raw=enum_parameter.value),
)
return response.overridden_parameter

def import_attribute_configuration_buffer(self, configuration): # noqa: N802
self._invoke(
self._client.ImportAttributeConfigurationBuffer,
grpc_types.ImportAttributeConfigurationBufferRequest(vi=self._vi, configuration=configuration),
)

def import_attribute_configuration_buffer_ex(self, configuration): # noqa: N802
self._invoke(
self._client.ImportAttributeConfigurationBufferEx,
grpc_types.ImportAttributeConfigurationBufferExRequest(vi=self._vi, configuration=configuration),
)

def init_with_options(self, resource_name, id_query, reset_device, option_string): # noqa: N802
metadata = (
('ni-api-key', self._grpc_options.api_key),
Expand Down Expand Up @@ -442,6 +455,9 @@ def set_custom_type_array(self, cs): # noqa: N802
grpc_types.SetCustomTypeArrayRequest(vi=self._vi, cs=cs and [x._create_copy(grpc_types.FakeCustomStruct) for x in cs]),
)

def set_runtime_environment(self, environment, environment_version, reserved1, reserved2): # noqa: N802
raise NotImplementedError('set_runtime_environment is not supported over gRPC')

def string_valued_enum_input_function_with_defaults(self, a_mobile_os_name): # noqa: N802
self._invoke(
self._client.StringValuedEnumInputFunctionWithDefaults,
Expand Down
41 changes: 34 additions & 7 deletions generated/nifake/nifake/_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def __init__(self, ctypes_library):
self.niFake_GetANumber_cfunc = None
self.niFake_GetAStringOfFixedMaximumSize_cfunc = None
self.niFake_GetAStringUsingPythonCode_cfunc = None
self.niFake_GetAnIviDanceString_cfunc = None
self.niFake_GetAnIviDanceCharArray_cfunc = None
self.niFake_GetAnIviDanceWithATwistString_cfunc = None
self.niFake_GetArrayForPythonCodeCustomType_cfunc = None
self.niFake_GetArrayForPythonCodeDouble_cfunc = None
Expand All @@ -59,7 +59,9 @@ def __init__(self, ctypes_library):
self.niFake_GetCustomTypeTypedef_cfunc = None
self.niFake_GetEnumValue_cfunc = None
self.niFake_GetError_cfunc = None
self.niFake_GetParameterWithOverriddenGrpcName_cfunc = None
self.niFake_ImportAttributeConfigurationBuffer_cfunc = None
self.niFake_ImportAttributeConfigurationBufferEx_cfunc = None
self.niFake_InitWithOptions_cfunc = None
self.niFake_Initiate_cfunc = None
self.niFake_LockSession_cfunc = None
Expand All @@ -84,6 +86,7 @@ def __init__(self, ctypes_library):
self.niFake_SetAttributeViString_cfunc = None
self.niFake_SetCustomType_cfunc = None
self.niFake_SetCustomTypeArray_cfunc = None
self.niFake_SetRuntimeEnvironment_cfunc = None
self.niFake_StringValuedEnumInputFunctionWithDefaults_cfunc = None
self.niFake_TwoInputFunction_cfunc = None
self.niFake_UnlockSession_cfunc = None
Expand Down Expand Up @@ -220,13 +223,13 @@ def niFake_GetAStringUsingPythonCode(self, vi, a_number, a_string): # noqa: N80
self.niFake_GetAStringUsingPythonCode_cfunc.restype = ViStatus # noqa: F405
return self.niFake_GetAStringUsingPythonCode_cfunc(vi, a_number, a_string)

def niFake_GetAnIviDanceString(self, vi, buffer_size, a_string): # noqa: N802
def niFake_GetAnIviDanceCharArray(self, vi, buffer_size, char_array): # noqa: N802
with self._func_lock:
if self.niFake_GetAnIviDanceString_cfunc is None:
self.niFake_GetAnIviDanceString_cfunc = self._get_library_function('niFake_GetAnIviDanceString')
self.niFake_GetAnIviDanceString_cfunc.argtypes = [ViSession, ViInt32, ctypes.POINTER(ViChar)] # noqa: F405
self.niFake_GetAnIviDanceString_cfunc.restype = ViStatus # noqa: F405
return self.niFake_GetAnIviDanceString_cfunc(vi, buffer_size, a_string)
if self.niFake_GetAnIviDanceCharArray_cfunc is None:
self.niFake_GetAnIviDanceCharArray_cfunc = self._get_library_function('niFake_GetAnIviDanceCharArray')
self.niFake_GetAnIviDanceCharArray_cfunc.argtypes = [ViSession, ViInt32, ctypes.POINTER(ViChar)] # noqa: F405
self.niFake_GetAnIviDanceCharArray_cfunc.restype = ViStatus # noqa: F405
return self.niFake_GetAnIviDanceCharArray_cfunc(vi, buffer_size, char_array)

def niFake_GetAnIviDanceWithATwistString(self, vi, buffer_size, a_string, actual_size): # noqa: N802
with self._func_lock:
Expand Down Expand Up @@ -372,6 +375,14 @@ def niFake_GetError(self, vi, error_code, buffer_size, description): # noqa: N8
self.niFake_GetError_cfunc.restype = ViStatus # noqa: F405
return self.niFake_GetError_cfunc(vi, error_code, buffer_size, description)

def niFake_GetParameterWithOverriddenGrpcName(self, vi, original_parameter, enum_parameter): # noqa: N802
with self._func_lock:
if self.niFake_GetParameterWithOverriddenGrpcName_cfunc is None:
self.niFake_GetParameterWithOverriddenGrpcName_cfunc = self._get_library_function('niFake_GetParameterWithOverriddenGrpcName')
self.niFake_GetParameterWithOverriddenGrpcName_cfunc.argtypes = [ViSession, ctypes.POINTER(ViInt16), ViInt16] # noqa: F405
self.niFake_GetParameterWithOverriddenGrpcName_cfunc.restype = ViStatus # noqa: F405
return self.niFake_GetParameterWithOverriddenGrpcName_cfunc(vi, original_parameter, enum_parameter)

def niFake_ImportAttributeConfigurationBuffer(self, vi, size_in_bytes, configuration): # noqa: N802
with self._func_lock:
if self.niFake_ImportAttributeConfigurationBuffer_cfunc is None:
Expand All @@ -380,6 +391,14 @@ def niFake_ImportAttributeConfigurationBuffer(self, vi, size_in_bytes, configura
self.niFake_ImportAttributeConfigurationBuffer_cfunc.restype = ViStatus # noqa: F405
return self.niFake_ImportAttributeConfigurationBuffer_cfunc(vi, size_in_bytes, configuration)

def niFake_ImportAttributeConfigurationBufferEx(self, vi, size, configuration): # noqa: N802
with self._func_lock:
if self.niFake_ImportAttributeConfigurationBufferEx_cfunc is None:
self.niFake_ImportAttributeConfigurationBufferEx_cfunc = self._get_library_function('niFake_ImportAttributeConfigurationBufferEx')
self.niFake_ImportAttributeConfigurationBufferEx_cfunc.argtypes = [ViSession, ViInt32, ctypes.POINTER(ViInt8)] # noqa: F405
self.niFake_ImportAttributeConfigurationBufferEx_cfunc.restype = ViStatus # noqa: F405
return self.niFake_ImportAttributeConfigurationBufferEx_cfunc(vi, size, configuration)

def niFake_InitWithOptions(self, resource_name, id_query, reset_device, option_string, vi): # noqa: N802
with self._func_lock:
if self.niFake_InitWithOptions_cfunc is None:
Expand Down Expand Up @@ -572,6 +591,14 @@ def niFake_SetCustomTypeArray(self, vi, number_of_elements, cs): # noqa: N802
self.niFake_SetCustomTypeArray_cfunc.restype = ViStatus # noqa: F405
return self.niFake_SetCustomTypeArray_cfunc(vi, number_of_elements, cs)

def niFake_SetRuntimeEnvironment(self, environment, environment_version, reserved1, reserved2): # noqa: N802
with self._func_lock:
if self.niFake_SetRuntimeEnvironment_cfunc is None:
self.niFake_SetRuntimeEnvironment_cfunc = self._get_library_function('niFake_SetRuntimeEnvironment')
self.niFake_SetRuntimeEnvironment_cfunc.argtypes = [ctypes.POINTER(ViChar), ctypes.POINTER(ViChar), ctypes.POINTER(ViChar), ctypes.POINTER(ViChar)] # noqa: F405
self.niFake_SetRuntimeEnvironment_cfunc.restype = ViStatus # noqa: F405
return self.niFake_SetRuntimeEnvironment_cfunc(environment, environment_version, reserved1, reserved2)

def niFake_StringValuedEnumInputFunctionWithDefaults(self, vi, a_mobile_os_name): # noqa: N802
with self._func_lock:
if self.niFake_StringValuedEnumInputFunctionWithDefaults_cfunc is None:
Expand Down
57 changes: 51 additions & 6 deletions generated/nifake/nifake/_library_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import array
import ctypes
import hightime # noqa: F401
import platform

import nifake._library_singleton as _library_singleton
import nifake._visatype as _visatype
import nifake.enums as enums # noqa: F401
Expand All @@ -16,6 +18,9 @@
import nifake.custom_struct_typedef as custom_struct_typedef # noqa: F401


_was_runtime_environment_set = None


# Helper functions for creating ctypes needed for calling into the driver DLL
def _get_ctypes_pointer_for_buffer(value=None, library_type=None, size=None):
if isinstance(value, array.array):
Expand Down Expand Up @@ -62,6 +67,21 @@ class LibraryInterpreter(object):
def __init__(self, encoding):
self._encoding = encoding
self._library = _library_singleton.get()
global _was_runtime_environment_set
if _was_runtime_environment_set is None:
try:
runtime_env = platform.python_implementation()
version = platform.python_version()
self.set_runtime_environment(
runtime_env,
version,
'',
''
)
except errors.DriverTooOldError:
pass
finally:
_was_runtime_environment_set = True
# Initialize _vi to 0 for now.
# Session will directly update it once the driver runtime init function has been called and
# we have a valid session handle.
Expand Down Expand Up @@ -228,17 +248,17 @@ def get_a_string_using_python_code(self, a_number): # noqa: N802
errors.handle_error(self, error_code, ignore_warnings=False, is_error_handling=False)
return a_string_ctype.value.decode(self._encoding)

def get_an_ivi_dance_string(self): # noqa: N802
def get_an_ivi_dance_char_array(self): # noqa: N802
vi_ctype = _visatype.ViSession(self._vi) # case S110
buffer_size_ctype = _visatype.ViInt32() # case S170
a_string_ctype = None # case C050
error_code = self._library.niFake_GetAnIviDanceString(vi_ctype, buffer_size_ctype, a_string_ctype)
char_array_ctype = None # case C050
error_code = self._library.niFake_GetAnIviDanceCharArray(vi_ctype, buffer_size_ctype, char_array_ctype)
errors.handle_error(self, error_code, ignore_warnings=True, is_error_handling=False)
buffer_size_ctype = _visatype.ViInt32(error_code) # case S180
a_string_ctype = (_visatype.ViChar * buffer_size_ctype.value)() # case C060
error_code = self._library.niFake_GetAnIviDanceString(vi_ctype, buffer_size_ctype, a_string_ctype)
char_array_ctype = (_visatype.ViChar * buffer_size_ctype.value)() # case C060
error_code = self._library.niFake_GetAnIviDanceCharArray(vi_ctype, buffer_size_ctype, char_array_ctype)
errors.handle_error(self, error_code, ignore_warnings=False, is_error_handling=False)
return a_string_ctype.value.decode(self._encoding)
return char_array_ctype.value.decode(self._encoding)

def get_an_ivi_dance_with_a_twist_string(self): # noqa: N802
vi_ctype = _visatype.ViSession(self._vi) # case S110
Expand Down Expand Up @@ -418,6 +438,14 @@ def get_error(self): # noqa: N802
errors.handle_error(self, error_code, ignore_warnings=False, is_error_handling=True)
return int(error_code_ctype.value), description_ctype.value.decode(self._encoding)

def get_parameter_with_overridden_grpc_name(self, enum_parameter): # noqa: N802
vi_ctype = _visatype.ViSession(self._vi) # case S110
original_parameter_ctype = _visatype.ViInt16() # case S220
enum_parameter_ctype = _visatype.ViInt16(enum_parameter.value) # case S130
error_code = self._library.niFake_GetParameterWithOverriddenGrpcName(vi_ctype, None if original_parameter_ctype is None else (ctypes.pointer(original_parameter_ctype)), enum_parameter_ctype)
errors.handle_error(self, error_code, ignore_warnings=False, is_error_handling=False)
return int(original_parameter_ctype.value)

def import_attribute_configuration_buffer(self, configuration): # noqa: N802
vi_ctype = _visatype.ViSession(self._vi) # case S110
size_in_bytes_ctype = _visatype.ViInt32(0 if configuration is None else len(configuration)) # case S160
Expand All @@ -426,6 +454,14 @@ def import_attribute_configuration_buffer(self, configuration): # noqa: N802
errors.handle_error(self, error_code, ignore_warnings=False, is_error_handling=False)
return

def import_attribute_configuration_buffer_ex(self, configuration): # noqa: N802
vi_ctype = _visatype.ViSession(self._vi) # case S110
size_ctype = _visatype.ViInt32(0 if configuration is None else len(configuration)) # case S160
configuration_ctype = _get_ctypes_pointer_for_buffer(value=configuration, library_type=_visatype.ViInt8) # case B550
error_code = self._library.niFake_ImportAttributeConfigurationBufferEx(vi_ctype, size_ctype, configuration_ctype)
errors.handle_error(self, error_code, ignore_warnings=False, is_error_handling=False)
return

def init_with_options(self, resource_name, id_query, reset_device, option_string): # noqa: N802
resource_name_ctype = ctypes.create_string_buffer(resource_name.encode(self._encoding)) # case C020
id_query_ctype = _visatype.ViBoolean(id_query) # case S150
Expand Down Expand Up @@ -641,6 +677,15 @@ def set_custom_type_array(self, cs): # noqa: N802
errors.handle_error(self, error_code, ignore_warnings=False, is_error_handling=False)
return

def set_runtime_environment(self, environment, environment_version, reserved1, reserved2): # noqa: N802
environment_ctype = ctypes.create_string_buffer(environment.encode(self._encoding)) # case C020
environment_version_ctype = ctypes.create_string_buffer(environment_version.encode(self._encoding)) # case C020
reserved1_ctype = ctypes.create_string_buffer(reserved1.encode(self._encoding)) # case C020
reserved2_ctype = ctypes.create_string_buffer(reserved2.encode(self._encoding)) # case C020
error_code = self._library.niFake_SetRuntimeEnvironment(environment_ctype, environment_version_ctype, reserved1_ctype, reserved2_ctype)
errors.handle_error(self, error_code, ignore_warnings=False, is_error_handling=False)
return

def string_valued_enum_input_function_with_defaults(self, a_mobile_os_name): # noqa: N802
vi_ctype = _visatype.ViSession(self._vi) # case S110
a_mobile_os_name_ctype = ctypes.create_string_buffer(a_mobile_os_name.value.encode(self._encoding)) # case C030
Expand Down
810 changes: 429 additions & 381 deletions generated/nifake/nifake/nifake_pb2.py

Large diffs are not rendered by default.

Loading

0 comments on commit 035cb37

Please sign in to comment.