|
| 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 |
0 commit comments