Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Proposal to fix #154 (v2) #161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Helpers.GetDefaultEncoding is added
This function is equal to os_ops.get_default_encoding and is used in:
 - Helpers.PrepareProcessInput
 - RaiseError._TranslateDataIntoString__FromBinary
  • Loading branch information
dmitry-lipetsk committed Dec 10, 2024
commit 7b70e9e7a0b22fb999e308dab8bccc46e8e22a65
39 changes: 38 additions & 1 deletion testgres/operations/helpers.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,48 @@
import locale


class Helpers:
def _make_get_default_encoding_func():
# locale.getencoding is added in Python 3.11
if hasattr(locale, 'getencoding'):
return locale.getencoding

# It must exist
return locale.getpreferredencoding

# Prepared pointer on function to get a name of system codepage
_get_default_encoding_func = _make_get_default_encoding_func()

def GetDefaultEncoding():
#
# Original idea/source was:
#
# def os_ops.get_default_encoding():
# if not hasattr(locale, 'getencoding'):
# locale.getencoding = locale.getpreferredencoding
# return locale.getencoding() or 'UTF-8'
#

assert __class__._get_default_encoding_func is not None

r = __class__._get_default_encoding_func()

if r:
assert r is not None
assert type(r) == str # noqa: E721
assert r != ""
return r

# Is it an unexpected situation?
return 'UTF-8'

def PrepareProcessInput(input, encoding):
if not input:
return None

if type(input) == str: # noqa: E721
if encoding is None:
return input.encode()
return input.encode(__class__.GetDefaultEncoding())

assert type(encoding) == str # noqa: E721
return input.encode(encoding)
Expand Down
3 changes: 2 additions & 1 deletion testgres/operations/raise_error.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from ..exceptions import ExecUtilException
from .helpers import Helpers


class RaiseError:
Expand Down Expand Up @@ -29,7 +30,7 @@ def _TranslateDataIntoString__FromBinary(data):
assert type(data) == bytes # noqa: E721

try:
return data.decode('utf-8')
return data.decode(Helpers.GetDefaultEncoding())
except UnicodeDecodeError:
pass

Expand Down