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

Python Minifier doesn't rename variables when eval() is used. #44

Open
Niikurasu opened this issue Feb 22, 2022 · 3 comments
Open

Python Minifier doesn't rename variables when eval() is used. #44

Niikurasu opened this issue Feb 22, 2022 · 3 comments
Labels
enhancement New feature or request

Comments

@Niikurasu
Copy link

When I try to shorten this code with all options enabled it only removes the spaces.

equation = input().split("=")[1]
grid = [["."]*10 for i in range(10)]
for y in range(10):
    x = int(eval(equation))
    if 0 <= x <= 9:
        grid[y][x] = "o"

print('\n'.join("".join(j for j in i) for i in grid))

Result: Reduction from 217 to 192

equation=input().split('=')[1]
grid=[['.']*10 for i in range(10)]
for y in range(10):
	x=int(eval(equation))
	if 0<=x<=9:grid[y][x]='o'
print('\n'.join((''.join((j for j in i))for i in grid)))

When the eval() is removed from the code it gets shortened properly (203 to 162)

D=range
E=input().split('=')[1]
A=[['.']*10 for A in D(10)]
for C in D(10):
	B=int()
	if 0<=B<=9:A[C][B]='o'
print('\n'.join((''.join((A for A in B))for B in A)))
@Niikurasu
Copy link
Author

Didn't read the documentation. Any possibility of fixing it?

@dflook
Copy link
Owner

dflook commented Feb 23, 2022

eval() executes a python expression using the local and global variables in it's current scope. We can't rename those without renaming references to them in the expression, which is not generally possible as we don't know what the expression will be.

You could technically do it if the expression was a static string, but you wouldn't need to eval it in that case.

I would try to avoid eval() as much as possible.

@dflook
Copy link
Owner

dflook commented Feb 23, 2022

I've thought about this some more, and I think there are improvements that could be made.

For usage of eval() (and var(), locals(), globals(), and maybe exec()), they only need to prevent renaming in their own local and global namespace. If a local or global mapping is provided for var() or eval() (and exec()?) then they don't need to prevent renaming at all.

This wouldn't help for your short example, but I expect it would make a difference in most situations.

@dflook dflook reopened this Feb 23, 2022
@dflook dflook added the enhancement New feature or request label Feb 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants