Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add str.isspace() method #2586

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

Merged
merged 27 commits into from
Mar 14, 2024
Merged

Conversation

kmr-srbh
Copy link
Contributor

@kmr-srbh kmr-srbh commented Mar 7, 2024

Fixes #2490.

#2490 gave wrong output due to the incorrect handling of the escape sequence '\f'. Escaping the sequence was handled, but unescaping was missed. This led to the string " \t\n\v\f\r" being transformed to " \t\n\v\\f\r" in the AST. Note the extra "\" after "v". Hence, the answer in #2490 was thus 'not actually' wrong as non-whitespace and lowercase characters did exist.

Fix

def f():
    b: str = " \t\n\v\f\r"
    print(b.isspace())
    print(b.islower())
    print(b.isupper())


f()
(base) saurabh-kumar@Awadh:~/Projects/System/lpython$ ./src/bin/lpython ./examples/example.py
True
False
False

Improvement

Though the error in the above case was due to incorrect string handling, the isspace() method was actually not implemented correctly. The definition of a 'whitespace' character is very broad. The Python interpreter checks for all of them in it's implementation of the method. I incorporated checking for those characters.

@kmr-srbh
Copy link
Contributor Author

kmr-srbh commented Mar 7, 2024

I am adding the tests in a while. @Thirumalai-Shaktivel please see this.

@Thirumalai-Shaktivel
Copy link
Collaborator

Also, Let's add some tests.

@ubaidsk ubaidsk marked this pull request as draft March 9, 2024 22:23
@ubaidsk
Copy link
Collaborator

ubaidsk commented Mar 9, 2024

@kmr-srbh Please mark it as ready for review when ready.

@kmr-srbh
Copy link
Contributor Author

The failing test, though unrelated, was causing disruption here. It was the one which checked for invalid literals like 01. This was fixed in a recent merged PR, so I have removed that test.

@Shaikh-Ubaid This PR is now ready. As far as the Unicode characters are concerned, they cannot be typed, but can appear when parsing strings or related work. They were added conforming to what Python calls a whitespace character.

@kmr-srbh kmr-srbh marked this pull request as ready for review March 10, 2024 03:48
(TOKEN "identifier" i32) 28:30
(TOKEN "=") 32:32
(TOKEN "integer" 123) 34:37
(EOF) 38:38
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I think this file needs to be remove manually.

@kmr-srbh
Copy link
Contributor Author

kmr-srbh commented Mar 10, 2024

Problem persists. Why does ./run_tests.py -u not fix it?

@kmr-srbh
Copy link
Contributor Author

@Thirumalai-Shaktivel could you please look as to why the tests fail? The previous messages pointed to the null char I had declared for looping through the whitespace characters. I cannot understand why the tests fail now. Please guide me.

Copy link
Collaborator

@Thirumalai-Shaktivel Thirumalai-Shaktivel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@Thirumalai-Shaktivel Thirumalai-Shaktivel enabled auto-merge (squash) March 14, 2024 05:34
@Thirumalai-Shaktivel Thirumalai-Shaktivel merged commit 6325e8d into lcompilers:main Mar 14, 2024
@kmr-srbh kmr-srbh deleted the isspace-method branch March 19, 2024 05:34
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.

Unexpected output for string method on whitespace characters
3 participants