Skip to content

Commit

Permalink
nidaqmx-python Reports Python as Runtime Environment (#574)
Browse files Browse the repository at this point in the history
* Add set runtime environment

* Address comments
  • Loading branch information
charitylxy authored May 7, 2024
1 parent af9e449 commit fcc8c13
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 4 deletions.
5 changes: 5 additions & 0 deletions generated/nidaqmx/_base_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1530,6 +1530,11 @@ def set_read_attribute_uint32(self, task, attribute, value):
def set_read_attribute_uint64(self, task, attribute, value):
raise NotImplementedError

@abc.abstractmethod
def set_runtime_environment(
self, environment, environment_version, reserved_1, reserved_2):
raise NotImplementedError

@abc.abstractmethod
def set_scale_attribute_double(self, scale_name, attribute, value):
raise NotImplementedError
Expand Down
4 changes: 4 additions & 0 deletions generated/nidaqmx/_grpc_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3574,6 +3574,10 @@ def get_error_string(self, error_code):
_logger.exception('Failed to get error string for error code %d.', error_code)
return 'Failed to retrieve error description.'

def set_runtime_environment(
self, environment, environment_version, reserved_1, reserved_2):
raise NotImplementedError


def _assign_numpy_array(numpy_array, grpc_array):
"""
Expand Down
35 changes: 33 additions & 2 deletions generated/nidaqmx/_library_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import ctypes
import logging
import platform
import warnings
from typing import Optional

Expand All @@ -11,11 +12,12 @@
from nidaqmx._base_interpreter import BaseEventHandler, BaseInterpreter
from nidaqmx._lib import lib_importer, ctypes_byte_str, c_bool32, wrapped_ndpointer
from nidaqmx.error_codes import DAQmxErrors, DAQmxWarnings
from nidaqmx.errors import DaqError, DaqReadError, DaqWarning, DaqWriteError
from nidaqmx.errors import DaqError, DaqFunctionNotSupportedError, DaqReadError, DaqWarning, DaqWriteError
from nidaqmx._lib_time import AbsoluteTime


_logger = logging.getLogger(__name__)
_was_runtime_environment_set = None


class LibraryEventHandler(BaseEventHandler):
Expand Down Expand Up @@ -43,7 +45,22 @@ class LibraryInterpreter(BaseInterpreter):
__slots__ = ()

def __init__(self):
pass
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 DaqFunctionNotSupportedError:
pass
finally:
_was_runtime_environment_set = True


def add_cdaq_sync_connection(self, port_list):
cfunc = lib_importer.windll.DAQmxAddCDAQSyncConnection
Expand Down Expand Up @@ -5288,6 +5305,20 @@ def set_read_attribute_uint64(self, task, attribute, value):
task, attribute, ctypes.c_uint64(value))
self.check_for_error(error_code)

def set_runtime_environment(
self, environment, environment_version, reserved_1, reserved_2):
cfunc = lib_importer.windll.DAQmxSetRuntimeEnvironment
if cfunc.argtypes is None:
with cfunc.arglock:
if cfunc.argtypes is None:
cfunc.argtypes = [
ctypes_byte_str, ctypes_byte_str, ctypes_byte_str,
ctypes_byte_str]

error_code = cfunc(
environment, environment_version, reserved_1, reserved_2)
self.check_for_error(error_code)

def set_scale_attribute_double(self, scale_name, attribute, value):
cfunc = lib_importer.cdll.DAQmxSetScaleAttribute
if cfunc.argtypes is None:
Expand Down
36 changes: 36 additions & 0 deletions src/codegen/metadata/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -21934,6 +21934,42 @@
'python_codegen_method': 'CustomCode',
'returns': 'int32'
},
'SetRuntimeEnvironment': {
'calling_convention': 'StdCall',
'codegen_method': 'private',
'parameters': [
{
'ctypes_data_type': 'ctypes.c_char_p',
'direction': 'in',
'name': 'environment',
'python_data_type': 'str',
'type': 'const char[]'
},
{
'ctypes_data_type': 'ctypes.c_char_p',
'direction': 'in',
'name': 'environmentVersion',
'python_data_type': 'str',
'type': 'const char[]'
},
{
'ctypes_data_type': 'ctypes.c_char_p',
'direction': 'in',
'name': 'reserved1',
'python_data_type': 'str',
'type': 'const char[]'
},
{
'ctypes_data_type': 'ctypes.c_char_p',
'direction': 'in',
'name': 'reserved2',
'python_data_type': 'str',
'type': 'const char[]'
}
],
'python_codegen_method': 'CustomCode',
'returns': 'int32'
},
'SetScaleAttributeDouble': {
'calling_convention': 'Cdecl',
'cname': 'DAQmxSetScaleAttribute',
Expand Down
4 changes: 4 additions & 0 deletions src/codegen/templates/_grpc_interpreter.py.mako
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ class GrpcStubInterpreter(BaseInterpreter):
_logger.exception('Failed to get error string for error code %d.', error_code)
return 'Failed to retrieve error description.'

def set_runtime_environment(
self, environment, environment_version, reserved_1, reserved_2):
raise NotImplementedError


def _assign_numpy_array(numpy_array, grpc_array):
"""
Expand Down
21 changes: 19 additions & 2 deletions src/codegen/templates/_library_interpreter.py.mako
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import ctypes
import logging
import platform
import warnings
from typing import Optional

Expand All @@ -28,11 +29,12 @@ from typing import List
from nidaqmx._base_interpreter import BaseEventHandler, BaseInterpreter
from nidaqmx._lib import lib_importer, ctypes_byte_str, c_bool32, wrapped_ndpointer
from nidaqmx.error_codes import DAQmxErrors, DAQmxWarnings
from nidaqmx.errors import DaqError, DaqReadError, DaqWarning, DaqWriteError
from nidaqmx.errors import DaqError, DaqFunctionNotSupportedError, DaqReadError, DaqWarning, DaqWriteError
from nidaqmx._lib_time import AbsoluteTime


_logger = logging.getLogger(__name__)
_was_runtime_environment_set = None


class LibraryEventHandler(BaseEventHandler):
Expand Down Expand Up @@ -60,7 +62,22 @@ class LibraryInterpreter(BaseInterpreter):
__slots__ = ()

def __init__(self):
pass
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 DaqFunctionNotSupportedError:
pass
finally:
_was_runtime_environment_set = True


% for func in functions:
<%
Expand Down
1 change: 1 addition & 0 deletions src/codegen/utilities/interpreter_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@

GRPC_INTERPRETER_IGNORED_FUNCTIONS = [
"get_error_string",
"set_runtime_environment",
]

LIBRARY_INTERPRETER_IGNORED_FUNCTIONS = [
Expand Down

0 comments on commit fcc8c13

Please sign in to comment.