Skip to content

Commit 41cb6ba

Browse files
authored
Use ascii encoding when locale is not set (#643)
* return ascii encoding when locale is not set
1 parent 12ef705 commit 41cb6ba

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

generated/nidaqmx/_lib.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,14 @@ def __getattr__(self, function):
120120
"""
121121

122122

123+
def get_encoding_from_locale() -> str:
124+
"""
125+
Gets the current locale encoding handling cases where it is unset.
126+
"""
127+
_, encoding = locale.getlocale()
128+
return encoding or 'ascii'
129+
130+
123131
class DaqLibImporter:
124132
"""
125133
Encapsulates NI-DAQmx library importing and handle type parsing logic.
@@ -157,7 +165,7 @@ def encoding(self):
157165
if self._encoding is None:
158166
self._import_lib()
159167
return self._encoding
160-
168+
161169
def _import_lib(self):
162170
"""
163171
Determines the location of and loads the NI-DAQmx CAI DLL.
@@ -185,7 +193,7 @@ def _load_lib(libname: str):
185193
try:
186194
if nidaqmx_c_library=="nicaiu":
187195
windll, cdll = _load_lib("nicaiu")
188-
encoding = locale.getlocale()[1]
196+
encoding = get_encoding_from_locale()
189197
elif nidaqmx_c_library=="nicai_utf8":
190198
windll, cdll = _load_lib("nicai_utf8")
191199
encoding = 'utf-8'
@@ -201,15 +209,15 @@ def _load_lib(libname: str):
201209
# Fallback to nicaiu.dll if nicai_utf8.dll cannot be loaded
202210
try:
203211
windll, cdll = _load_lib("nicaiu")
204-
encoding = locale.getlocale()[1]
212+
encoding = get_encoding_from_locale()
205213
except (OSError, WindowsError) as e:
206214
raise DaqNotFoundError(_DAQ_NOT_FOUND_MESSAGE) from e
207215
elif sys.platform.startswith('linux'):
208216
library_path = find_library('nidaqmx')
209217
if library_path is not None:
210218
cdll = ctypes.cdll.LoadLibrary(library_path)
211219
windll = cdll
212-
encoding = locale.getlocale()[1]
220+
encoding = get_encoding_from_locale()
213221
else:
214222
raise DaqNotFoundError(_DAQ_NOT_FOUND_MESSAGE)
215223
else:

src/handwritten/_lib.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,14 @@ def __getattr__(self, function):
120120
"""
121121

122122

123+
def get_encoding_from_locale() -> str:
124+
"""
125+
Gets the current locale encoding handling cases where it is unset.
126+
"""
127+
_, encoding = locale.getlocale()
128+
return encoding or 'ascii'
129+
130+
123131
class DaqLibImporter:
124132
"""
125133
Encapsulates NI-DAQmx library importing and handle type parsing logic.
@@ -157,7 +165,7 @@ def encoding(self):
157165
if self._encoding is None:
158166
self._import_lib()
159167
return self._encoding
160-
168+
161169
def _import_lib(self):
162170
"""
163171
Determines the location of and loads the NI-DAQmx CAI DLL.
@@ -185,7 +193,7 @@ def _load_lib(libname: str):
185193
try:
186194
if nidaqmx_c_library=="nicaiu":
187195
windll, cdll = _load_lib("nicaiu")
188-
encoding = locale.getlocale()[1]
196+
encoding = get_encoding_from_locale()
189197
elif nidaqmx_c_library=="nicai_utf8":
190198
windll, cdll = _load_lib("nicai_utf8")
191199
encoding = 'utf-8'
@@ -201,15 +209,15 @@ def _load_lib(libname: str):
201209
# Fallback to nicaiu.dll if nicai_utf8.dll cannot be loaded
202210
try:
203211
windll, cdll = _load_lib("nicaiu")
204-
encoding = locale.getlocale()[1]
212+
encoding = get_encoding_from_locale()
205213
except (OSError, WindowsError) as e:
206214
raise DaqNotFoundError(_DAQ_NOT_FOUND_MESSAGE) from e
207215
elif sys.platform.startswith('linux'):
208216
library_path = find_library('nidaqmx')
209217
if library_path is not None:
210218
cdll = ctypes.cdll.LoadLibrary(library_path)
211219
windll = cdll
212-
encoding = locale.getlocale()[1]
220+
encoding = get_encoding_from_locale()
213221
else:
214222
raise DaqNotFoundError(_DAQ_NOT_FOUND_MESSAGE)
215223
else:

tests/acceptance/test_internationalization.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import locale
21
import pathlib
32
from typing import Any, Dict, List, Optional, Union
43

54
import pytest
65

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

0 commit comments

Comments
 (0)