Skip to content

Commit 7b70e9e

Browse files
Helpers.GetDefaultEncoding is added
This function is equal to os_ops.get_default_encoding and is used in: - Helpers.PrepareProcessInput - RaiseError._TranslateDataIntoString__FromBinary
1 parent 3cc19d2 commit 7b70e9e

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

testgres/operations/helpers.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,48 @@
1+
import locale
2+
3+
14
class Helpers:
5+
def _make_get_default_encoding_func():
6+
# locale.getencoding is added in Python 3.11
7+
if hasattr(locale, 'getencoding'):
8+
return locale.getencoding
9+
10+
# It must exist
11+
return locale.getpreferredencoding
12+
13+
# Prepared pointer on function to get a name of system codepage
14+
_get_default_encoding_func = _make_get_default_encoding_func()
15+
16+
def GetDefaultEncoding():
17+
#
18+
# Original idea/source was:
19+
#
20+
# def os_ops.get_default_encoding():
21+
# if not hasattr(locale, 'getencoding'):
22+
# locale.getencoding = locale.getpreferredencoding
23+
# return locale.getencoding() or 'UTF-8'
24+
#
25+
26+
assert __class__._get_default_encoding_func is not None
27+
28+
r = __class__._get_default_encoding_func()
29+
30+
if r:
31+
assert r is not None
32+
assert type(r) == str # noqa: E721
33+
assert r != ""
34+
return r
35+
36+
# Is it an unexpected situation?
37+
return 'UTF-8'
38+
239
def PrepareProcessInput(input, encoding):
340
if not input:
441
return None
542

643
if type(input) == str: # noqa: E721
744
if encoding is None:
8-
return input.encode()
45+
return input.encode(__class__.GetDefaultEncoding())
946

1047
assert type(encoding) == str # noqa: E721
1148
return input.encode(encoding)

testgres/operations/raise_error.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from ..exceptions import ExecUtilException
2+
from .helpers import Helpers
23

34

45
class RaiseError:
@@ -29,7 +30,7 @@ def _TranslateDataIntoString__FromBinary(data):
2930
assert type(data) == bytes # noqa: E721
3031

3132
try:
32-
return data.decode('utf-8')
33+
return data.decode(Helpers.GetDefaultEncoding())
3334
except UnicodeDecodeError:
3435
pass
3536

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy