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

Fix Incorrect Python Version Detection in Source Code #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Fazzioni
Copy link

The previous implementation of the Python version detection logic incorrectly checked for Python 2 print statements by searching for the substring "print " in the source code. This approach could lead to false positives when the word print appeared inside comments or strings, causing incorrect detection of Python 2 syntax even when the code was actually using Python 3.

Example of problematic code:

# This is a comment that mentions print
print("Hello, World!")  # This is Python 3 syntax

In the above example, the previous logic would mistakenly interpret this as Python 2 code because it found "print " in the comment.

Fix Overview:
The detection mechanism was improved by utilizing Python's tokenize module to properly parse the source code into tokens, ignoring comments and strings.
Now, the code correctly differentiates between print as a Python 2 statement and print as a Python 3 function by checking if it is followed by a parenthesis (().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant