Skip to content

Commit

Permalink
Use ascii encoding when locale is not set (#643) (#652)
Browse files Browse the repository at this point in the history
* return ascii encoding when locale is not set

Co-authored-by: Zach Hindes <zach.hindes@ni.com>
  • Loading branch information
WayneDroid and zhindes authored Nov 6, 2024
1 parent 8c58899 commit a87e689
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
16 changes: 12 additions & 4 deletions generated/nidaqmx/_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ def __getattr__(self, function):
"""


def get_encoding_from_locale() -> str:
"""
Gets the current locale encoding handling cases where it is unset.
"""
_, encoding = locale.getlocale()
return encoding or 'ascii'


class DaqLibImporter:
"""
Encapsulates NI-DAQmx library importing and handle type parsing logic.
Expand Down Expand Up @@ -157,7 +165,7 @@ def encoding(self):
if self._encoding is None:
self._import_lib()
return self._encoding

def _import_lib(self):
"""
Determines the location of and loads the NI-DAQmx CAI DLL.
Expand Down Expand Up @@ -185,7 +193,7 @@ def _load_lib(libname: str):
try:
if nidaqmx_c_library=="nicaiu":
windll, cdll = _load_lib("nicaiu")
encoding = locale.getlocale()[1]
encoding = get_encoding_from_locale()
elif nidaqmx_c_library=="nicai_utf8":
windll, cdll = _load_lib("nicai_utf8")
encoding = 'utf-8'
Expand All @@ -201,15 +209,15 @@ def _load_lib(libname: str):
# Fallback to nicaiu.dll if nicai_utf8.dll cannot be loaded
try:
windll, cdll = _load_lib("nicaiu")
encoding = locale.getlocale()[1]
encoding = get_encoding_from_locale()
except (OSError, WindowsError) as e:
raise DaqNotFoundError(_DAQ_NOT_FOUND_MESSAGE) from e
elif sys.platform.startswith('linux'):
library_path = find_library('nidaqmx')
if library_path is not None:
cdll = ctypes.cdll.LoadLibrary(library_path)
windll = cdll
encoding = locale.getlocale()[1]
encoding = get_encoding_from_locale()
else:
raise DaqNotFoundError(_DAQ_NOT_FOUND_MESSAGE)
else:
Expand Down
16 changes: 12 additions & 4 deletions src/handwritten/_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ def __getattr__(self, function):
"""


def get_encoding_from_locale() -> str:
"""
Gets the current locale encoding handling cases where it is unset.
"""
_, encoding = locale.getlocale()
return encoding or 'ascii'


class DaqLibImporter:
"""
Encapsulates NI-DAQmx library importing and handle type parsing logic.
Expand Down Expand Up @@ -157,7 +165,7 @@ def encoding(self):
if self._encoding is None:
self._import_lib()
return self._encoding

def _import_lib(self):
"""
Determines the location of and loads the NI-DAQmx CAI DLL.
Expand Down Expand Up @@ -185,7 +193,7 @@ def _load_lib(libname: str):
try:
if nidaqmx_c_library=="nicaiu":
windll, cdll = _load_lib("nicaiu")
encoding = locale.getlocale()[1]
encoding = get_encoding_from_locale()
elif nidaqmx_c_library=="nicai_utf8":
windll, cdll = _load_lib("nicai_utf8")
encoding = 'utf-8'
Expand All @@ -201,15 +209,15 @@ def _load_lib(libname: str):
# Fallback to nicaiu.dll if nicai_utf8.dll cannot be loaded
try:
windll, cdll = _load_lib("nicaiu")
encoding = locale.getlocale()[1]
encoding = get_encoding_from_locale()
except (OSError, WindowsError) as e:
raise DaqNotFoundError(_DAQ_NOT_FOUND_MESSAGE) from e
elif sys.platform.startswith('linux'):
library_path = find_library('nidaqmx')
if library_path is not None:
cdll = ctypes.cdll.LoadLibrary(library_path)
windll = cdll
encoding = locale.getlocale()[1]
encoding = get_encoding_from_locale()
else:
raise DaqNotFoundError(_DAQ_NOT_FOUND_MESSAGE)
else:
Expand Down
5 changes: 2 additions & 3 deletions tests/acceptance/test_internationalization.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import locale
import pathlib
from typing import Any, Dict, List, Optional, Union

import pytest

from nidaqmx._lib import lib_importer
from nidaqmx._lib import lib_importer, get_encoding_from_locale
from nidaqmx.error_codes import DAQmxErrors
from nidaqmx.errors import DaqError
from nidaqmx.system import Device
Expand All @@ -20,7 +19,7 @@ def ai_task(task, sim_6363_device):
def _get_encoding(obj: Union[Task, Dict[str, Any]]) -> Optional[str]:
if getattr(obj, "_grpc_options", None) or (isinstance(obj, dict) and "grpc_options" in obj):
# gRPC server limited to MBCS encoding
return locale.getlocale()[1]
return get_encoding_from_locale()
else:
return lib_importer.encoding

Expand Down

0 comments on commit a87e689

Please sign in to comment.