From 4078a9381da2b5cb473c4e9ebdaa6a34a7f1cda4 Mon Sep 17 00:00:00 2001 From: divinity76 Date: Mon, 19 Sep 2022 14:33:41 +0200 Subject: [PATCH 1/2] fix \x9F crash-on-windows with hex encoding previously var_dump('\x9F'); may cause a crash on windows cmd.exe , fix that by hex-encoding instead of crashing. for more info, see https://github.com/sha256/python-var-dump/issues/19 --- var_dump/_var_dump.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/var_dump/_var_dump.py b/var_dump/_var_dump.py index 0abb951..8dba917 100644 --- a/var_dump/_var_dump.py +++ b/var_dump/_var_dump.py @@ -65,8 +65,21 @@ def display(o, space, num, key, typ, proret): l.append(str(o)) if proret: - print(st % tuple(l)) - + try: + print(st % tuple(l)) + except UnicodeEncodeError as err: + # may happen on Windows when stdout is piped to a file + # for unknown reasons... ref https://github.com/sha256/python-var-dump/issues/19 + # lets replace all non-ascii characters with \xHEX + lc = list(l) + fixedstr = '' + for i in range(len(lc[3])): + try: + fixedstr += lc[3][i].encode('ascii', 'strict').decode('ascii', 'strict') + except UnicodeEncodeError: + fixedstr += "\\x"+ (hex(ord(lc[3][i]))[2:].zfill(2)) + lc[3] = fixedstr + print(st % tuple(lc)) return st % tuple(l) From d1bd938602552a773247158de7649e1140848988 Mon Sep 17 00:00:00 2001 From: divinity76 Date: Mon, 19 Sep 2022 14:58:51 +0200 Subject: [PATCH 2/2] simplify code --- var_dump/_var_dump.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/var_dump/_var_dump.py b/var_dump/_var_dump.py index 8dba917..304f967 100644 --- a/var_dump/_var_dump.py +++ b/var_dump/_var_dump.py @@ -72,13 +72,7 @@ def display(o, space, num, key, typ, proret): # for unknown reasons... ref https://github.com/sha256/python-var-dump/issues/19 # lets replace all non-ascii characters with \xHEX lc = list(l) - fixedstr = '' - for i in range(len(lc[3])): - try: - fixedstr += lc[3][i].encode('ascii', 'strict').decode('ascii', 'strict') - except UnicodeEncodeError: - fixedstr += "\\x"+ (hex(ord(lc[3][i]))[2:].zfill(2)) - lc[3] = fixedstr + lc[3] = lc[3].encode('ascii', 'backslashreplace').decode('ascii', 'backslashreplace') print(st % tuple(lc)) return st % tuple(l)