Skip to content

Commit 0167f74

Browse files
committed
Merge remote-tracking branch 'origin/master' into edge
2 parents 7cd04a9 + 0cf7a7a commit 0167f74

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

mig/shared/compat.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# --- BEGIN_HEADER ---
4+
#
5+
# support - helper functions for unit testing
6+
# Copyright (C) 2003-2024 The MiG Project by the Science HPC Center at UCPH
7+
#
8+
# This file is part of MiG.
9+
#
10+
# MiG is free software: you can redistribute it and/or modify
11+
# it under the terms of the GNU General Public License as published by
12+
# the Free Software Foundation; either version 2 of the License, or
13+
# (at your option) any later version.
14+
#
15+
# MiG is distributed in the hope that it will be useful,
16+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
# GNU General Public License for more details.
19+
#
20+
# You should have received a copy of the GNU General Public License
21+
# along with this program; if not, write to the Free Software
22+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23+
#
24+
# -- END_HEADER ---
25+
#
26+
27+
"""This file contains an assortment of compatibility functions whose
28+
lifetime is intentionally intended to be time limited. We intentionally
29+
place these in their own namespace to make them easily identifiable and
30+
ease their subsequent removal.
31+
"""
32+
33+
from __future__ import absolute_import
34+
35+
import codecs
36+
import sys
37+
38+
PY2 = sys.version_info[0] < 3
39+
40+
from mig.shared.base import STR_KIND, _force_default_coding
41+
42+
43+
def ensure_native_string(string_or_bytes):
44+
"""Given a supplied input which can be either a string or bytes
45+
return a representation providing string operations while ensuring that
46+
its contents represent a valid series of textual characters.
47+
48+
Arrange identical operation across python 2 and 3 - specifically,
49+
the presence of invalid UTF-8 bytes (thus the input not being a
50+
valid textual string) will trigger a UnicodeDecodeError on PY3.
51+
Force the same to occur on PY2.
52+
"""
53+
textual_output = _force_default_coding(string_or_bytes, STR_KIND)
54+
if PY2:
55+
# Simulate decoding done by PY3 to trigger identical exceptions
56+
# note the use of a forced "utf8" encoding value: this function
57+
# is generally used to wrap, for example, substitutions of values
58+
# into strings that are defined in the source code. In Python 3
59+
# these are mandated to be UTF-8, and thus decoding as "utf8" is
60+
# what will be attempted on supplied input. Match it.
61+
codecs.decode(textual_output, "utf8")
62+
return textual_output

tests/test_mig_shared_compat.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# --- BEGIN_HEADER ---
4+
#
5+
# test_mig_shared_compat - unit test of the corresponding mig shared module
6+
# Copyright (C) 2003-2024 The MiG Project by the Science HPC Center at UCPH
7+
#
8+
# This file is part of MiG.
9+
#
10+
# MiG is free software: you can redistribute it and/or modify
11+
# it under the terms of the GNU General Public License as published by
12+
# the Free Software Foundation; either version 2 of the License, or
13+
# (at your option) any later version.
14+
#
15+
# MiG is distributed in the hope that it will be useful,
16+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
# GNU General Public License for more details.
19+
#
20+
# You should have received a copy of the GNU General Public License
21+
# along with this program; if not, write to the Free Software
22+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23+
#
24+
# -- END_HEADER ---
25+
#
26+
27+
"""Unit test compat functions"""
28+
29+
import binascii
30+
import os
31+
import sys
32+
33+
sys.path.append(os.path.realpath(os.path.join(os.path.dirname(__file__), ".")))
34+
from support import MigTestCase, testmain
35+
36+
from mig.shared.compat import ensure_native_string
37+
38+
DUMMY_BYTECHARS = b'DEADBEEF'
39+
DUMMY_BYTESRAW = binascii.unhexlify('DEADBEEF') # 4 bytes
40+
41+
class MigSharedCompat__ensure_native_string(MigTestCase):
42+
# TODO: Add docstrings to this class and its methods
43+
def test_char_bytes_conversion(self):
44+
actual = ensure_native_string(DUMMY_BYTECHARS)
45+
self.assertEqual(actual, 'DEADBEEF')
46+
47+
def test_raw_bytes_conversion(self):
48+
with self.assertRaises(UnicodeDecodeError):
49+
ensure_native_string(DUMMY_BYTESRAW)
50+
51+
52+
if __name__ == '__main__':
53+
testmain()

0 commit comments

Comments
 (0)