Skip to content

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

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

Closed
wants to merge 47 commits into from

Conversation

itsankitkp
Copy link
Contributor

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

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
Contributor

Choose a reason for hiding this comment

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

Is this in CPython?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

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

Copy link
Contributor

Choose a reason for hiding this comment

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

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

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

Copy link
Contributor

@fanninpm fanninpm Jan 27, 2023

Choose a reason for hiding this comment

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

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
Contributor Author

Choose a reason for hiding this comment

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

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 itsankitkp changed the title handle error in case of unsupported format Fix #4157: handle error in case of unsupported format Jan 27, 2023
@itsankitkp itsankitkp requested a review from fanninpm January 27, 2023 19:47
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
Contributor

Choose a reason for hiding this comment

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

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());

Copy link
Contributor Author

@itsankitkp itsankitkp Jan 27, 2023

Choose a reason for hiding this comment

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

I am getting following error

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have refactored code, it looks lot better now

@fanninpm
Copy link
Contributor

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

@itsankitkp
Copy link
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 DimitrisJim linked an issue Jan 30, 2023 that may be closed by this pull request
@itsankitkp itsankitkp force-pushed the handle-panic-strftime branch from 0cf0b04 to d4c2fd3 Compare January 31, 2023 15:30
@itsankitkp itsankitkp requested a review from fanninpm January 31, 2023 15:31
@itsankitkp
Copy link
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.

@youknowone
Copy link
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 itsankitkp force-pushed the handle-panic-strftime branch from fd2f370 to 052e984 Compare February 18, 2023 08:30
@itsankitkp itsankitkp force-pushed the handle-panic-strftime branch from be812d7 to d4a6d39 Compare February 19, 2023 05:37
@youknowone
Copy link
Member

rebase seems messed up. could you try:

git fetch upstream
git rebase upstream/main

when upstream is github.com/RustPython/RustPython

@itsankitkp
Copy link
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

@itsankitkp
Copy link
Contributor Author

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

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