Skip to content

Add str.__rmod__() method. #190 #1262

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 7 commits into from
Aug 16, 2019

Conversation

ChJR
Copy link
Contributor

@ChJR ChJR commented Aug 15, 2019

CPython returns "NotImplemented" string when arguments supplied.

str.__rmod__(30, '%i')
except TypeError:
typeError = True
assert typeError
Copy link
Member

Choose a reason for hiding this comment

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

You can use assertRaises from testutils for this:

from testutils import assertRaises

...

with assertRaises(TypeError):
    ...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for sharing. I will use your proposal for this PR.

match do_cformat(vm, _format_string, values.clone()) {
Ok(_format_string) => Ok(vm.new_str("NotImplemented".to_string())),
Err(err) => Err(err)
}
Copy link
Member

Choose a reason for hiding this comment

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

I think you misunderstood what you were seeing in the python shell -- in CPython, str.__rmod__(str, int) doesn't return "NotImplemented", it returns the actual value NotImplemented, which you can access from Rust as vm.ctx.not_implemented().

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 misunderstood that. I will fix and re-open this pull request.

@ChJR
Copy link
Contributor Author

ChJR commented Aug 15, 2019

I improved PR code quality.
Thanks to @coolreader18

Copy link
Member

@coolreader18 coolreader18 left a comment

Choose a reason for hiding this comment

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

Nice, looks a lot better :)

fn rmod(&self, values: PyObjectRef, vm: &VirtualMachine) -> PyResult {
let format_string_text = &self.value;
let _format_string = CFormatString::from_str(format_string_text)
.map_err(|err| vm.new_value_error(format!("{}", err)))?;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
.map_err(|err| vm.new_value_error(format!("{}", err)))?;
.map_err(|err| vm.new_value_error(err.to_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.

That code found on str.mod too. I suggest that code should be moved to another PR.

Copy link
Member

Choose a reason for hiding this comment

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

Sounds good, thanks for contributing!

@ChJR ChJR closed this Aug 15, 2019
@ChJR ChJR reopened this Aug 15, 2019
.map_err(|err| vm.new_value_error(format!("{}", err)))?;

match do_cformat(vm, _format_string, values.clone()) {
Ok(_format_string) => Ok(vm.ctx.not_implemented()),
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the call to do_cformat required here? Or can we just always return NotImplemented?

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 just checked your second suggestion, and I found it works.
I used do_cformat just for argument checking, but that method is not required.
I will add a new commit for this PR.

@youknowone
Copy link
Member

@ChJR there is a tiny style glitch. There is a clippy result in travis.

@windelbouwman windelbouwman merged commit 1dceae9 into RustPython:master Aug 16, 2019
@ChJR ChJR deleted the feature/str.__rmod__ branch August 16, 2019 06:43
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.

4 participants