pybpc / f2format Goto Github PK
View Code? Open in Web Editor NEWBackport compiler for Python 3.6 formatted string literals (f-string).
Home Page: https://pybpc.github.io/f2format/
License: MIT License
Backport compiler for Python 3.6 formatted string literals (f-string).
Home Page: https://pybpc.github.io/f2format/
License: MIT License
del with multiple values of trailing comma is not parsed correctly
echo 'a=1;del a,' | f2format -s
Traceback (most recent call last):
File "/Users/user/bin/f2format", line 8, in <module>
sys.exit(main())
File "<string>", line None
bpc_utils.parsing.BPCSyntaxError: source file '<unknown>' contains the following syntax errors:
[L1C4] SyntaxError: cannot delete starred
Describe the bug
Code with invalid quotes get successfully converted.
To Reproduce
>>> import f2format
>>> s = 'hello'
>>> code = "f'{s + ''}'"
>>> print(f2format.convert(code))
'{}'.format(s + '')
>>> eval(code)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1
SyntaxError: f-string: expecting '}'
>>>
Expected behavior
f2format.convert()
should raise an error to report that the code is invalid
I have a Python source file (see attachment) that gives the following error when I try to convert it with f2format v0.4.3 on Ubuntu 18.04 64-bit using Python 3.6.5:
AttributeError: 'FormattedValue' object has no attribute 's'
register.py.zip
echo 'type((lambda: (yield))())' | ~/.local/bin/f2format -s
Traceback (most recent call last):
File "/Users/user/.local/bin/f2format", line 8, in <module>
sys.exit(main())
File "<string>", line None
bpc_utils.parsing.BPCSyntaxError: source file '<unknown>' contains the following syntax errors:
[L1C14] SyntaxError: 'yield' outside function
๐ I wrote essentially the same thing and decided to run my testsuite against it, this test fails:
def test_with_braces():
assert f'hello {{ hi }}' == 'hello { hi }'
I have a Python source file (see attachment) that gives the following error when I try to convert it with f2format v0.4.3 on Ubuntu 18.04 64-bit using Python 3.6.5:
Traceback (most recent call last): File "/opt/design/python/lib/python3.6/multiprocessing/pool.py", line 119, in worker result = (True, func(*args, **kwds)) File "/opt/design/python/lib/python3.6/multiprocessing/pool.py", line 44, in mapstar return list(map(*args)) File "/opt/design/python/lib/python3.6/site-packages/f2format/core.py", line 244, in f2format text = convert(string, lineno) File "/opt/design/python/lib/python3.6/site-packages/f2format/core.py", line 185, in convert val = ast.parse(temp_expr).body[0].value # parse AST File "/opt/design/python/lib/python3.6/ast.py", line 35, in parse return compile(source, filename, mode, PyCF_ONLY_AST) File "<unknown>", line 1 address:0{self.axi_address_width // 4 + (1 if self.axi_address_width % 4 != 0 else 0) ^ SyntaxError: invalid syntax
I suspect it because of the ternary conditional operator in that line of code.
xilinx.py.zip
It seems that some nested f-strings with double quotes are not converted. For example:
# Code:
logger.debug(f'[{", ".join([f"0x{item:04X}" for item in data])}]')
# Gets converted to:
logger.debug('[{}]'.format(", ".join([f"0x{item:04X}" for item in data])))
# Code:
logger.error(f'FAIL: Wrong response to an invalid command 0x{command:02X} from FTDI device, port {port}: [{", ".join([f"0x{item:02X}" for item in data_r])}]')
# Gets converted to:
logger.error('FAIL: Wrong response to an invalid command 0x{:02X} from FTDI device, port {}: [{}]'.format(command, port, ", ".join([f"0x{item:02X}" for item in data_r])))
See code in the attachment
I'm running the latest f2format from Git under Python 3.6.5.
ftdi.py.zip
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.