FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Fix #4157: handle error in case of unsupported format by itsankitkp · Pull Request #4474 · RustPython/RustPython · GitHub

Fix #4157: handle error in case of unsupported format - #4474

Closed
itsankitkp wants to merge 47 commits into
RustPython:mainfrom
itsankitkp:handle-panic-strftime
Closed

Fix #4157: handle error in case of unsupported format#4474
itsankitkp wants to merge 47 commits into
RustPython:mainfrom
itsankitkp:handle-panic-strftime

Conversation

Copy link
Copy Markdown
Contributor

Chrono panics in case of unsupported formats, this patch handles such cases and returns supplied format as a result.

Comment thread Lib/test/test_time.py Outdated
Comment on lines +173 to +178
# XXX this test case fails, this should have been handled in
# in in layers above fn strftime.
# self.assertRaises(ValueError, time.strftime, '%S\0', tt)

# check if unsupported arg in strftime returns arg itself
self.assertEqual(time.strftime("%4Y"), "%4Y")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Is this in CPython?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

No, CPython returns '2023'. But rust chrono doesn't support it yet

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

We prefer to keep the tests in the Lib/test directory as close as possible to what's in CPython.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Okay, should I keep it in extra_tests/snippets/stdlib_datetime.py ?

fanninpm Jan 27, 2023
edited
Loading

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

If the "%S\0" case no longer panics, then you could change the decorator to this:

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_function():
    # ...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

If the "%4Y" case no longer panics, then you could change the decorator to this:

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_function():
    # ...

I have done this, test ran fine.

itsankitkp changed the title handle error in case of unsupported format Fix #4157: handle error in case of unsupported format Jan 27, 2023
itsankitkp requested a review from fanninpm January 27, 2023 19:47
Comment thread vm/src/stdlib/time.rs Outdated
Comment on lines +213 to +225

let mut formatted_time = String::new();

/*
* chrono doesn't support all formats and it
* raised an error if unsupported format is supplied.
* If error happens, we set result as input arg
*/
let result = write!(&mut formatted_time, "{}", instant.format(format.as_str()));
match result {
Ok(r) => r,
Err(_) => formatted_time = format.to_string(),
};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Does this work?

Suggested change
let mut formatted_time = String::new();
/*
* chrono doesn't support all formats and it
* raised an error if unsupported format is supplied.
* If error happens, we set result as input arg
*/
let result = write!(&mut formatted_time, "{}", instant.format(format.as_str()));
match result {
Ok(r) => r,
Err(_) => formatted_time = format.to_string(),
};
let formatted_time = String::try_from(instant.format(format.as_str())).unwrap_or(|| format.to_string());

itsankitkp Jan 27, 2023
edited
Loading

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I am getting following error

the trait std::convert::From<DelayedFormat<StrftimeItems<'_>>> is not implemented for std::string::String

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

But I do see what you are trying to do here. I will give another pass at this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I have refactored code, it looks lot better now

Copy link
Copy Markdown
Contributor

It's fine to have that "%4Y" case in the extra_tests/.

Copy link
Copy Markdown
Contributor Author

It's fine to have that "%4Y" case in the extra_tests/.

Okay, I tried that but there is an issue
If I write assert_equal(_time.strftime("%4Y"), "%4Y"),
it runs first arg against cpython (which returns "2023") and 2nd part against rustpython (which returns "%4Y")
What I require is to run both first and second argument against rustpython only. Am I looking at wrong direction here?

Assertion Failure: <class 'str'>(2023) == <class 'str'>(%4Y)

DimitrisJim linked an issue Jan 30, 2023 that may be closed by this pull request
itsankitkp force-pushed the handle-panic-strftime branch from 0cf0b04 to d4c2fd3 Compare January 31, 2023 15:30
itsankitkp requested a review from fanninpm January 31, 2023 15:31

Copy link
Copy Markdown
Contributor Author

It's fine to have that "%4Y" case in the extra_tests/.

Okay, I tried that but there is an issue If I write assert_equal(_time.strftime("%4Y"), "%4Y"), it runs first arg against cpython (which returns "2023") and 2nd part against rustpython (which returns "%4Y") What I require is to run both first and second argument against rustpython only. Am I looking at wrong direction here?

Assertion Failure: <class 'str'>(2023) == <class 'str'>(%4Y)

I have fixed this for now by using unsupported format which returns same result in both python and rustpython.

Copy link
Copy Markdown
Member

@itsankitkp it passes tests on ubuntu, but not on macos and windows. could you check it?

----------------------------- Captured stdout call -----------------------------
Assertion Failure: <class 'str'>(?) == <class 'str'>(%?)
----------------------------- Captured stderr call -----------------------------
Traceback (most recent call last):
  File "/Users/runner/work/RustPython/RustPython/extra_tests/snippets/stdlib_datetime.py", line 135, in <module>
    assert_equal(_time.strftime("%?"), "%?")
  File "/Users/runner/work/RustPython/RustPython/extra_tests/snippets/testutils.py", line 58, in assert_equal
    _assert_print(lambda: a == b, [_typed(a), '==', _typed(b)])
  File "/Users/runner/work/RustPython/RustPython/extra_tests/snippets/testutils.py", line 47, in _assert_print
    assert f()
AssertionError

itsankitkp force-pushed the handle-panic-strftime branch from fd2f370 to 052e984 Compare February 18, 2023 08:30
itsankitkp force-pushed the handle-panic-strftime branch from be812d7 to d4a6d39 Compare February 19, 2023 05:37

Copy link
Copy Markdown
Member

rebase seems messed up. could you try:

git fetch upstream
git rebase upstream/main

when upstream is github.com/RustPython/RustPython

Copy link
Copy Markdown
Contributor Author

rebase seems messed up. could you try:

git fetch upstream
git rebase upstream/main

when upstream is github.com/RustPython/RustPython

This branch was messed up, I have created fresh PR with same changes with history fixed.
Can you please take a look #4530

Copy link
Copy Markdown
Contributor Author

Closing as I have opened other PR: #4530 with fixed git history

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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.

time.strftime(arg) fails when arg is not valid format string

Back | FazBrowse Home | New Git URL