You've already forked python-uncompyle6
mirror of
https://github.com/rocky/python-uncompyle6.git
synced 2025-08-02 16:44:46 +08:00
Fixed bug with FORMAT_VALUE where a sub expression would not be correctly interpreted
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
# std
|
||||
import os
|
||||
# test
|
||||
import pytest
|
||||
import hypothesis
|
||||
@@ -85,19 +87,20 @@ def fstrings(draw):
|
||||
|
||||
:return: A valid f-string.
|
||||
"""
|
||||
prefix = draw(st.sampled_from('f'))
|
||||
raw = draw(st.sampled_from(list('r') + ['']))
|
||||
is_raw = draw(st.booleans())
|
||||
integer_strategy = st.integers(min_value=0, max_value=3)
|
||||
expression_count = draw(integer_strategy)
|
||||
content = []
|
||||
for _ in range(expression_count):
|
||||
expression = draw(expressions())
|
||||
#conversion = draw(st.sampled_from(('', '!s', '!r', '!a',)))
|
||||
#specifier = draw(st.sampled_from(format_specifiers(), st.just('')))
|
||||
content.append('{{{}}}'.format(expression))
|
||||
# not yet : conversion not supported
|
||||
conversion = ''#draw(st.sampled_from(('', '!s', '!r', '!a',)))
|
||||
has_specifier = draw(st.booleans())
|
||||
specifier = ':' + draw(format_specifiers()) if has_specifier else ''
|
||||
content.append('{{{}{}}}'.format(expression, conversion, specifier))
|
||||
content = ''.join(content)
|
||||
|
||||
return "{}{}'{}'".format(prefix, raw, content)
|
||||
return "f{}'{}'".format('r' if is_raw else '', content)
|
||||
|
||||
|
||||
@hypothesis.given(format_specifiers())
|
||||
@@ -110,11 +113,8 @@ def test_format_specifiers(format_specifier):
|
||||
raise
|
||||
|
||||
|
||||
@pytest.mark.skipif(PYTHON_VERSION < 3.6, reason="requires python3.6")
|
||||
@pytest.mark.skipif(PYTHON_VERSION < 3.6, reason='need at least python 3.6')
|
||||
@hypothesis.given(fstrings())
|
||||
@hypothesis.example("f'{abc}'") # BUG: strings with a single expression do not uncompyle correctly.
|
||||
@hypothesis.example("fr'{abc}{xyz}'") # BUG: no support for raw f strings.
|
||||
@hypothesis.example("f'{len(items)}{abc}'") # BUG: more complicated expressions than LOAD_NAME don't work
|
||||
def test_uncompyle_fstring(fstring):
|
||||
"""Verify uncompyling fstring bytecode"""
|
||||
|
||||
@@ -122,8 +122,14 @@ def test_uncompyle_fstring(fstring):
|
||||
# no expressions just gets compiled to a normal string.
|
||||
hypothesis.assume('{' in fstring)
|
||||
|
||||
# BUG : At the moment a single expression is not supported
|
||||
# for example f'{abc}'.
|
||||
hypothesis.assume(fstring.count('{') > 1)
|
||||
|
||||
expr = fstring + '\n'
|
||||
code = compile(expr, '<string>', 'single')
|
||||
deparsed = deparse_code(PYTHON_VERSION, code, compile_mode='single')
|
||||
recompiled = compile(deparsed.text, '<string>', 'single')
|
||||
|
||||
assert deparsed.text == expr
|
||||
if recompiled != code:
|
||||
assert deparsed.text == expr
|
||||
|
Reference in New Issue
Block a user