From 47ed0795b27b9676829613d82b300f22e4b9a923 Mon Sep 17 00:00:00 2001 From: rocky Date: Fri, 24 May 2019 09:53:56 -0400 Subject: [PATCH] 3.x docsting escaping works differently? --- pytest/test_docstring.py | 78 ------------------------- test/bytecode_2.7/00_docstring.pyc | Bin 1117 -> 0 bytes test/bytecode_2.7_run/00_docstring.pyc | Bin 0 -> 1437 bytes test/bytecode_3.7/00_docstring.pyc | Bin 1001 -> 0 bytes test/bytecode_3.7_run/00_docstring.pyc | Bin 0 -> 1282 bytes uncompyle6/semantics/helper.py | 42 ++++++++----- 6 files changed, 28 insertions(+), 92 deletions(-) delete mode 100644 pytest/test_docstring.py delete mode 100644 test/bytecode_2.7/00_docstring.pyc create mode 100644 test/bytecode_2.7_run/00_docstring.pyc delete mode 100644 test/bytecode_3.7/00_docstring.pyc create mode 100644 test/bytecode_3.7_run/00_docstring.pyc diff --git a/pytest/test_docstring.py b/pytest/test_docstring.py deleted file mode 100644 index 21157b5f..00000000 --- a/pytest/test_docstring.py +++ /dev/null @@ -1,78 +0,0 @@ -import sys -from uncompyle6 import PYTHON3 -if PYTHON3: - from io import StringIO - minint = -sys.maxsize-1 - maxint = sys.maxsize -else: - from StringIO import StringIO - minint = -sys.maxint-1 - maxint = sys.maxint -from uncompyle6.semantics.helper import print_docstring - -class PrintFake(): - def __init__(self): - self.pending_newlines = 0 - self.f = StringIO() - - def write(self, *data): - if (len(data) == 0) or (len(data) == 1 and data[0] == ''): - return - out = ''.join((str(j) for j in data)) - n = 0 - for i in out: - if i == '\n': - n += 1 - if n == len(out): - self.pending_newlines = max(self.pending_newlines, n) - return - elif n: - self.pending_newlines = max(self.pending_newlines, n) - out = out[n:] - break - else: - break - - if self.pending_newlines > 0: - self.f.write('\n'*self.pending_newlines) - self.pending_newlines = 0 - - for i in out[::-1]: - if i == '\n': - self.pending_newlines += 1 - else: - break - - if self.pending_newlines: - out = out[:-self.pending_newlines] - self.f.write(out) - def println(self, *data): - if data and not(len(data) == 1 and data[0] == ''): - self.write(*data) - self.pending_newlines = max(self.pending_newlines, 1) - return - pass - -def test_docstring(): - - for doc, expect in ( - ("Now is the time", - ' """Now is the time"""'), - (""" -Now is the time -""", - ''' """ - Now is the time - """''') - - # (r'''func placeholder - ' and with ("""\nstring\n """)''', - # """ r'''func placeholder - ' and with (\"\"\"\nstring\n\"\"\")'''"""), - # (r"""func placeholder - ' and with ('''\nstring\n''') and \"\"\"\nstring\n\"\"\" """, - # """ r\"\"\"func placeholder - ' and with ('''\nstring\n''') and \"\"\"\nstring\n\"\"\" \"\"\"""") - ): - - o = PrintFake() - # print(doc) - # print(expect) - print_docstring(o, ' ', doc) - assert expect == o.f.getvalue() diff --git a/test/bytecode_2.7/00_docstring.pyc b/test/bytecode_2.7/00_docstring.pyc deleted file mode 100644 index 61aa0c011f5ac6a70f534b28cd4dc8e53bef1012..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1117 zcmb_b&5qMB5O$hnp_O=tQ7V)am2A5ww4xQ#UO1p03a6@)<+a^5ki=0u;b&Leco5!^ z16Ri05V}>-N`NDeXRMiTzKLhT?~eyRR>PHK?F#68N!valp%`0{4A@sv0;YOi=`j_0 zC1mP^y-6N8_opN_O(~(uh15HfD_z4g_?Y1ik|>JSrNufcx9gIO(VcXcF8;)&3ka>} ztjTe-7(}W5gtXhH)JKu9}RIXTOg8*Zq!L8~&aBm8ef0Pulmg|3*~}pj~O2dW|T~ z=W`I&>Kb7Rvl&c}Io-LV*X(Q;tv_iAs4a3OP(W5fF-G@3bVtG-xxWi64ks6LFb0N~?=5 z=CCwO>!x&tBzuI$5VyD7S6zKTdT1EJ!j#$r+xJ;jX_a=kj>}TpCP$lWM3!qcn~Xsq z>Y_q2o{zli|LBP8{;!T$ByPg+Aqjd7BROA^%9gIZmu}|%c5oh1?2E#vCf9R6yRHdO JgMQc#e*%UY0<-`C diff --git a/test/bytecode_2.7_run/00_docstring.pyc b/test/bytecode_2.7_run/00_docstring.pyc new file mode 100644 index 0000000000000000000000000000000000000000..8f67e5662c62f4957d7428174ec82184e674011a GIT binary patch literal 1437 zcmb_c&2G~`5MJA%Ku~+A1m}@^u!Dk|_Jpb?0#!&HP$UdoP$bLoZW6b)*Yb|}6&K*n zEAVza058Do)^*YnR1xrM$1|UHX1>|oS?A|=_ujW3lY}*&MgK=MRi6ZB>@`V?eI>728~SB*9~ z`Vb?EyhJh{_RP}1?9q*yQ2qbDFfDnLt}h+BOri*PNPleZR4R)nRVAyC5EkmYZu)o{ zf7tLD2z9bMG*y=b+u615(%9M_`}%zf^&_#&WfpIk2z9F-=aSc%80pwb16$T@hX(ut Dosm$i literal 0 HcmV?d00001 diff --git a/test/bytecode_3.7/00_docstring.pyc b/test/bytecode_3.7/00_docstring.pyc deleted file mode 100644 index 2c80e86ac37a930c73112f596c5d95304eee5d62..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1001 zcmah{!H&}~5OtgGZmG2YFv=d%pptF(gjTcy?S%vC0XS8aEU)dffh3M>XMv)<@gZFK z2F^%)L$CY=C&q3`3o3LX+w+|9^YhF+J3sFcw6F2^*|#&`$lpEsPo5{2`;7E)X{Xa0VgO>|wlhxGRZ4ItX?Qvd4e&SD|q0;Z~zp4hz2s!EEa;dNSG zN>k_7^i!7QQjCWKVAjf_vIri>KJ@<;Up;6leuCaK?yS!+&5Bd%yHmS9(C*h>yZFZU WJ=1+sC{gEf;(OY4Plt4aZtx4R7UUoR diff --git a/test/bytecode_3.7_run/00_docstring.pyc b/test/bytecode_3.7_run/00_docstring.pyc new file mode 100644 index 0000000000000000000000000000000000000000..fa450ff17da739c0cf9edd56a19ad563c92d4162 GIT binary patch literal 1282 zcmb_cOOMkq5OyA=RoV8)fzv1}(FT=ly9b0STJaDP2h;;_T2+d&vD;=hi6h(LQN#tf z^B1_nPwJHe|A7-@Z#V6tqG}~C^@9=uJ|0=k~( zO>?P4lFz3(0zXFHYFOw!#U))d)Iu(M+26s{fhec{Vw`*@LKAJw??ahnMan|SOA)gp zsWK&x`u&jd_(Driyk9vp&M|x2x;nBJhp!sb^-DhzkspjpQv) z%UL<6k!YZEDNZNoia34Bq;80g!sMOX@<3UiEM=b&Y9qRBkb}rU)HTROL=fHAh`N~Y zTJ#N**M#*5g1~qveLNmRBqb9H1~3}IVAr85d-#1TtZ%b-EC$7-3TdPwNOH&qK^W2` zR$;jBw!04gE{((wg3F1->hEv8Wl-Ui%0=KJ!3Hf zUMiVThWa29428#ONf{khcm57d1)|J#Al)rNJ?N}=LlbrJMqvTKAxwDA46!^y O_3NZ=Cf8~>IN=X#S~5Za literal 0 HcmV?d00001 diff --git a/uncompyle6/semantics/helper.py b/uncompyle6/semantics/helper.py index f213e1ff..f941831e 100644 --- a/uncompyle6/semantics/helper.py +++ b/uncompyle6/semantics/helper.py @@ -99,14 +99,9 @@ def strip_quotes(str): def print_docstring(self, indent, docstring): - try: - if docstring.find('"""') == -1: - quote = '"""' - else: - quote = "'''" - docstring = docstring.replace("'''", "\\'''") - except: - return False + quote = '"""' + if docstring.find("'''") == -1: + quote = "'''" self.write(indent) if not PYTHON3 and not isinstance(docstring, str): # Must be unicode in Python2 @@ -132,18 +127,31 @@ def print_docstring(self, indent, docstring): and (docstring[-1] != '"' or docstring[-2] == '\t')): self.write('r') # raw string - # restore backslashes unescaped since raw + # Restore backslashes unescaped since raw docstring = docstring.replace('\t', '\\') else: # Escape '"' if it's the last character, so it doesn't # ruin the ending triple quote if len(docstring) and docstring[-1] == '"': docstring = docstring[:-1] + '\\"' - # Restore escaped backslashes + + # Escape triple quote when needed + if quote == '"""': + if self.version > 2.7: + replace_str = '\\"""' + else: + replace_str = '\\"\\"\\"' + docstring = docstring.replace(quote, replace_str) + else: + assert quote == "'''" + if self.version > 2.7: + replace_str = "\\'''" + else: + replace_str = "\\'\\'\\'" + docstring = docstring.replace(quote, replace_str) + docstring = docstring.replace('\t', '\\\\') - # Escape triple quote when needed - if quote == '""""': - docstring = docstring.replace('"""', '\\"\\"\\"') + lines = docstring.split('\n') calculate_indent = maxint for line in lines[1:]: @@ -152,6 +160,7 @@ def print_docstring(self, indent, docstring): calculate_indent = min(calculate_indent, len(line) - len(stripped)) calculate_indent = min(calculate_indent, len(lines[-1]) - len(lines[-1].lstrip())) # Remove indentation (first line is special): + trimmed = [lines[0]] if calculate_indent < maxint: trimmed += [line[calculate_indent:] for line in lines[1:]] @@ -164,7 +173,12 @@ def print_docstring(self, indent, docstring): else: self.println(trimmed[0]) for line in trimmed[1:-1]: - self.println( indent, line ) + if line: + self.println( indent, line ) + else: + self.println( "\n\n" ) + pass + pass self.println(indent, trimmed[-1], quote) return True