|
4 | 4 | import array |
5 | 5 | import ctypes |
6 | 6 | import hightime # noqa: F401 |
| 7 | +import platform |
| 8 | + |
7 | 9 | import niswitch._library_singleton as _library_singleton |
8 | 10 | import niswitch._visatype as _visatype |
9 | 11 | import niswitch.enums as enums # noqa: F401 |
10 | 12 | import niswitch.errors as errors |
11 | 13 |
|
12 | 14 |
|
| 15 | +_was_runtime_environment_set = None |
| 16 | + |
| 17 | + |
13 | 18 | # Helper functions for creating ctypes needed for calling into the driver DLL |
14 | 19 | def _get_ctypes_pointer_for_buffer(value=None, library_type=None, size=None): |
15 | 20 | if isinstance(value, array.array): |
@@ -56,6 +61,21 @@ class LibraryInterpreter(object): |
56 | 61 | def __init__(self, encoding): |
57 | 62 | self._encoding = encoding |
58 | 63 | self._library = _library_singleton.get() |
| 64 | + global _was_runtime_environment_set |
| 65 | + if _was_runtime_environment_set is None: |
| 66 | + try: |
| 67 | + runtime_env = platform.python_implementation() |
| 68 | + version = platform.python_version() |
| 69 | + self.set_runtime_environment( |
| 70 | + runtime_env, |
| 71 | + version, |
| 72 | + '', |
| 73 | + '' |
| 74 | + ) |
| 75 | + except errors.DriverTooOldError: |
| 76 | + pass |
| 77 | + finally: |
| 78 | + _was_runtime_environment_set = True |
59 | 79 | # Initialize _vi to 0 for now. |
60 | 80 | # Session will directly update it once the driver runtime init function has been called and |
61 | 81 | # we have a valid session handle. |
@@ -210,16 +230,16 @@ def get_channel_name(self, index): # noqa: N802 |
210 | 230 |
|
211 | 231 | def get_error(self): # noqa: N802 |
212 | 232 | vi_ctype = _visatype.ViSession(self._vi) # case S110 |
213 | | - code_ctype = _visatype.ViStatus() # case S220 |
| 233 | + error_code_ctype = _visatype.ViStatus() # case S220 |
214 | 234 | buffer_size_ctype = _visatype.ViInt32() # case S170 |
215 | 235 | description_ctype = None # case C050 |
216 | | - error_code = self._library.niSwitch_GetError(vi_ctype, None if code_ctype is None else (ctypes.pointer(code_ctype)), buffer_size_ctype, description_ctype) |
| 236 | + error_code = self._library.niSwitch_GetError(vi_ctype, None if error_code_ctype is None else (ctypes.pointer(error_code_ctype)), buffer_size_ctype, description_ctype) |
217 | 237 | errors.handle_error(self, error_code, ignore_warnings=True, is_error_handling=True) |
218 | 238 | buffer_size_ctype = _visatype.ViInt32(error_code) # case S180 |
219 | 239 | description_ctype = (_visatype.ViChar * buffer_size_ctype.value)() # case C060 |
220 | | - error_code = self._library.niSwitch_GetError(vi_ctype, None if code_ctype is None else (ctypes.pointer(code_ctype)), buffer_size_ctype, description_ctype) |
| 240 | + error_code = self._library.niSwitch_GetError(vi_ctype, None if error_code_ctype is None else (ctypes.pointer(error_code_ctype)), buffer_size_ctype, description_ctype) |
221 | 241 | errors.handle_error(self, error_code, ignore_warnings=False, is_error_handling=True) |
222 | | - return int(code_ctype.value), description_ctype.value.decode(self._encoding) |
| 242 | + return int(error_code_ctype.value), description_ctype.value.decode(self._encoding) |
223 | 243 |
|
224 | 244 | def get_path(self, channel1, channel2): # noqa: N802 |
225 | 245 | vi_ctype = _visatype.ViSession(self._vi) # case S110 |
@@ -368,6 +388,15 @@ def set_path(self, path_list): # noqa: N802 |
368 | 388 | errors.handle_error(self, error_code, ignore_warnings=False, is_error_handling=False) |
369 | 389 | return |
370 | 390 |
|
| 391 | + def set_runtime_environment(self, environment, environment_version, reserved1, reserved2): # noqa: N802 |
| 392 | + environment_ctype = ctypes.create_string_buffer(environment.encode(self._encoding)) # case C020 |
| 393 | + environment_version_ctype = ctypes.create_string_buffer(environment_version.encode(self._encoding)) # case C020 |
| 394 | + reserved1_ctype = ctypes.create_string_buffer(reserved1.encode(self._encoding)) # case C020 |
| 395 | + reserved2_ctype = ctypes.create_string_buffer(reserved2.encode(self._encoding)) # case C020 |
| 396 | + error_code = self._library.niSwitch_SetRuntimeEnvironment(environment_ctype, environment_version_ctype, reserved1_ctype, reserved2_ctype) |
| 397 | + errors.handle_error(self, error_code, ignore_warnings=False, is_error_handling=False) |
| 398 | + return |
| 399 | + |
371 | 400 | def unlock(self): # noqa: N802 |
372 | 401 | vi_ctype = _visatype.ViSession(self._vi) # case S110 |
373 | 402 | error_code = self._library.niSwitch_UnlockSession(vi_ctype, None) |
|
0 commit comments