Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change in behaviour with %-formatted aligned string with tuple interpreted vs compiled #3476

Closed
LordAro opened this issue Mar 31, 2020 · 2 comments

Comments

@LordAro
Copy link
Contributor

LordAro commented Mar 31, 2020

With the following testcase:

print("str: '%08s'" % (""))
print("str: '%08s'" % ("",))

When run with python (3.8):

 $ python3 foo.py
str: '        '
str: '        '

When compiled with cython (0.29.15):

 $ cython --embed -3 foo.py && gcc foo.c -lpython3.8 -o foo
 $ ./foo
str: '        '
Traceback (most recent call last):
  File "foo.py", line 2, in init foo
    print("str: '%08s'" % ("",))
ValueError: '=' alignment not allowed in string format specifier

Something to do with the tuple is making it very unhappy...
Possibly related to #3092 ?

@scoder
Copy link
Contributor

scoder commented Mar 31, 2020

Yes, related to #3092.

The difference between the two lines is that only the tuple case is being optimised (into an f-string), because the single-argument-non-tuple case is not easy to optimise (it might still receive a tuple at runtime, especially since the real type is usually unknown and the argument is almost never a constant as in your simple example).

Both seem easy to fix. PR welcome.
Worst case, the f-string optimisation could be disabled for these formats. Or preferably converted correctly.

@nanlliu
Copy link

nanlliu commented Apr 3, 2020

is it something a beginner can do? any help tho? can I work on this? if possible, I would love to work on both issues! much appreciated! @scoder

@scoder scoder closed this as completed in dcf6b82 May 9, 2020
@scoder scoder added this to the 0.29.18 milestone May 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants