From 412c725fbae24203af8515e8ee26269009e9206c Mon Sep 17 00:00:00 2001 From: hakril Date: Mon, 20 May 2024 18:45:01 +0200 Subject: [PATCH] Improve pycompat urepr_encode for non-tty stdout + fix some tests --- tests/test_crypto.py | 4 ++-- tests/test_process.py | 8 +------- tests/test_security.py | 5 +++-- windows/pycompat.py | 16 ++++++++++++---- 4 files changed, 18 insertions(+), 15 deletions(-) diff --git a/tests/test_crypto.py b/tests/test_crypto.py index c9c2ef3..1e7d057 100644 --- a/tests/test_crypto.py +++ b/tests/test_crypto.py @@ -142,8 +142,8 @@ def randomkeypair(keysize=1024): def test_certificate(rawcert): cert = windows.crypto.Certificate.from_buffer(rawcert) assert cert.serial == '1b 8e 94 cb 0b 3e eb b6 41 39 f3 c9 09 b1 6b 46' - assert cert.name == b'PythonForWindowsTest' - assert cert.issuer == b'PythonForWindowsTest' + assert cert.name == u'PythonForWindowsTest' + assert cert.issuer == u'PythonForWindowsTest' assert cert.thumbprint == 'EF 0C A8 C9 F9 E0 96 AF 74 18 56 8B C1 C9 57 27 A0 89 29 6A' assert cert.encoded == rawcert assert cert.version == 2 diff --git a/tests/test_process.py b/tests/test_process.py index 2848e7d..32ba6ef 100644 --- a/tests/test_process.py +++ b/tests/test_process.py @@ -526,10 +526,4 @@ class TestProcessWithCheckGarbage(object): p.exit() p.wait() time.sleep(0.5) # Fail on Azure CI of no sleep - os.unlink(target_programe) - - -def test_testouille(): - import locale - print(sys.stdout.isatty(), sys.stdout.encoding, locale.getpreferredencoding(), sys.getdefaultencoding(), sys.getfilesystemencoding()) - assert sys.stdout.encoding == "BADVALUE" \ No newline at end of file + os.unlink(target_programe) \ No newline at end of file diff --git a/tests/test_security.py b/tests/test_security.py index b8b1e45..6bcc92d 100644 --- a/tests/test_security.py +++ b/tests/test_security.py @@ -34,10 +34,11 @@ def test_security_descriptor_from_binary(binsd): def test_security_descriptor_from_unicode_file(tmpdir): TARGET_FILENAME = u"내 한국은 최고의 한국.txt" - TARGET_PATH = os.path.join(unicode(tmpdir), TARGET_FILENAME) + import pdb;pdb.set_trace() + TARGET_PATH = os.path.join(tmpdir, TARGET_FILENAME) with open(TARGET_PATH, "w") as f: f.write("Hello Test") - SecurityDescriptor.from_filename(TARGET_PATH) + assert SecurityDescriptor.from_filename(TARGET_PATH) def test_empty_security_descriptor(): diff --git a/windows/pycompat.py b/windows/pycompat.py index 957c5af..34a7402 100644 --- a/windows/pycompat.py +++ b/windows/pycompat.py @@ -26,10 +26,17 @@ if is_py3: return s.decode("latin1") return s - # No encoding of unicode repr + # No encoding of unicode repr if we target a TTY # Python3 handle unicode natively in string and console output - def urepr_encode(s): - return s + + if sys.stdout.isatty(): + def urepr_encode(s): + return s + else: # Not a TTY (seen in github CI) : if no explict encoding on stdout : use the locale to encode it the best we can to prevent print error + repr_encoding = sys.stdout.encoding or locale.getpreferredencoding() + + def urepr_encode_notty(s): + return ustr.encode(repr_encoding, "backslashreplace") else: # py2.7 def str_from_ascii_function(s): @@ -49,8 +56,9 @@ else: # py2.7 return s # sys.stdout.encoding may be None if not a tty - # Use sys.stdout.isatty() ? + # Use sys.stdout.isatty() ? in py2 we will never return unicode in anycase repr_encoding = sys.stdout.encoding or locale.getpreferredencoding() + repr_encoding = "cp1252" def urepr_encode(ustr): # assert isinstance(s, unicode) # Make the check explicitly ?