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

Expose py_run! macro #512

Merged
merged 4 commits into from
Jun 15, 2019
Merged

Expose py_run! macro #512

merged 4 commits into from
Jun 15, 2019

Conversation

kngwyu
Copy link
Member

@kngwyu kngwyu commented Jun 13, 2019

Currently, we have a macro named py_run in tests/common.rs.
I think this macro is useful for users (e.g. when testing libraries).

When exposing this macro, I made a modification.
When string literal is passed, we use indoc to generate an indented string in compile time.
In other cases, we do string conversion in runtime.

@codecov
Copy link

codecov bot commented Jun 13, 2019

Codecov Report

Merging #512 into master will decrease coverage by 0.25%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #512      +/-   ##
==========================================
- Coverage    87.8%   87.54%   -0.26%     
==========================================
  Files          65       65              
  Lines        3435     3428       -7     
==========================================
- Hits         3016     3001      -15     
- Misses        419      427       +8
Impacted Files Coverage Δ
src/lib.rs 0% <ø> (ø) ⬆️
src/class/iter.rs 77.77% <0%> (-5.56%) ⬇️
src/conversion.rs 95.57% <0%> (-1.77%) ⬇️
src/types/string.rs 88.13% <0%> (-1.7%) ⬇️
src/class/macros.rs 86.8% <0%> (-1.56%) ⬇️
src/err.rs 61.11% <0%> (-1.32%) ⬇️
src/buffer.rs 69.9% <0%> (-0.49%) ⬇️
src/freelist.rs 82.22% <0%> (-0.39%) ⬇️
src/class/sequence.rs 80.61% <0%> (-0.2%) ⬇️
src/type_object.rs 89.09% <0%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 787f5d3...9073956. Read the comment docs.

src/lib.rs Outdated
@@ -210,6 +213,96 @@ macro_rules! wrap_pymodule {
}};
}

/// A convinient macro to execute a Python code snippet, with some local variables set.
Copy link
Member

Choose a reason for hiding this comment

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

typo: convenient

src/lib.rs Show resolved Hide resolved
src/lib.rs Outdated
/// format!("{}時{}分{}秒", self.hour, self.minute, self.second)
/// }
/// #[getter]
/// fn hour(&self) -> PyResult<u32> {
Copy link
Member

Choose a reason for hiding this comment

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

(PyResult isn't necessary here)

src/lib.rs Outdated
///
/// # Example
/// ```
/// use pyo3::{prelude::*, PyRawObject, py_run};
Copy link
Member

Choose a reason for hiding this comment

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

why is PyRawObject imported?

src/lib.rs Show resolved Hide resolved
$py.run("import sys; sys.stderr.flush()", None, None)
.unwrap();
})
.expect($code)
Copy link
Member

Choose a reason for hiding this comment

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

To be generally useful, I think this should not panic on exceptions, or reflect this in the name?

src/lib.rs Outdated
#[doc(hidden)]
pub fn _indoc_runtime(commands: &str) -> String {
let indent;
if let Some(second) = commands.lines().nth(1) {
Copy link
Member

Choose a reason for hiding this comment

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

better: let indent = if ...

src/lib.rs Outdated
pub fn _indoc_runtime(commands: &str) -> String {
let indent;
if let Some(second) = commands.lines().nth(1) {
indent = second
Copy link
Member

Choose a reason for hiding this comment

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

Some('\n').iter().chain(chars...) gets rid of the additional allocation "\n".to_string() below.

Copy link
Member

Choose a reason for hiding this comment

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

We should optimize the examples for legibility, not for performance

@kngwyu
Copy link
Member Author

kngwyu commented Jun 14, 2019

@birkenfeld
Thanks for your review!

To be generally useful, I think this should not panic on exceptions, or reflect this in the name?

Since this macro is mainly for testing, I think unwrapping is OK and more convenient.
In the latest commit I added a note about it in the document.

@kngwyu
Copy link
Member Author

kngwyu commented Jun 14, 2019

And I rewrote _indoc_runtime(formerly called indoc) so that it behaves the same as indoc! macro.

Copy link
Member

@konstin konstin left a comment

Choose a reason for hiding this comment

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

Left one comment, otherwise it's looking fine

src/lib.rs Outdated
pyo3::py_run_impl!($py, $($val)+, pyo3::indoc::indoc!($code))
}};
($py:expr, $($val:ident)+, $code:expr) => {{
pyo3::py_run_impl!($py, $($val)+, &pyo3::_indoc_runtime($code))
Copy link
Member

Choose a reason for hiding this comment

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

Could you explain why we're sometimes using indoc and sometimes don't?

Copy link
Member Author

Choose a reason for hiding this comment

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

indoc only accepts string literals so it doesn't work for the cases like

let code = generates_code();
py_run!(py, obj, code)

Copy link
Member

@konstin konstin Jun 14, 2019

Choose a reason for hiding this comment

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

Then you can use unindent, which is what indoc uses internally.

Copy link
Member Author

Choose a reason for hiding this comment

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

OK, I'll try that

@kngwyu kngwyu merged commit 3e69389 into PyO3:master Jun 15, 2019
@kngwyu kngwyu deleted the expose-py-run branch June 15, 2019 14:50
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.

3 participants