@@ -33,12 +33,11 @@ and return the evaluated value as a `Bound<'py, PyAny>` object.
3333
3434``` rust
3535use pyo3 :: prelude :: * ;
36- use pyo3 :: ffi :: c_str;
3736
3837# fn main () -> Result <(), ()> {
3938Python :: attach (| py | {
4039 let result = py
41- . eval (c_str! ( " [i * 10 for i in range(5)]" ) , None , None )
40+ . eval (c " [i * 10 for i in range(5)]" , None , None )
4241 . map_err (| e | {
4342 e . print_and_set_sys_last_vars (py );
4443 })? ;
@@ -108,22 +107,21 @@ to this function!
108107
109108``` rust
110109use pyo3 :: {prelude :: * , types :: IntoPyDict };
111- use pyo3_ffi :: c_str;
112110
113111# fn main () -> PyResult <()> {
114112Python :: attach (| py | {
115113 let activators = PyModule :: from_code (
116114 py ,
117- c_str! ( r # "
115+ cr #"
118116def relu(x):
119117 """ see https : // en.wikipedia.org/wiki/Rectifier_(neural_networks)"""
120118 return max (0.0 , x )
121119
122120def leaky_relu (x , slope = 0.01 ):
123121 return x if x >= 0 else x * slope
124- " # ) ,
125- c_str! ( " activators.py" ) ,
126- c_str! ( " activators" ) ,
122+ " #,
123+ c " activators . py" ,
124+ c " activators " ,
127125 )?;
128126
129127 let relu_result: f64 = activators.getattr(" relu " )?.call1((-1.0,))?.extract()?;
@@ -155,7 +153,6 @@ As an example, the below adds the module `foo` to the embedded interpreter:
155153
156154```rust
157155use pyo3::prelude::*;
158- use pyo3 :: ffi :: c_str;
159156
160157#[pymodule]
161158mod foo {
@@ -169,7 +166,7 @@ mod foo {
169166
170167fn main() -> PyResult<()> {
171168 pyo3::append_to_inittab!(foo);
172- Python :: attach (| py | Python :: run (py , c_str! ( " import foo; foo.add_one(6)" ) , None , None ))
169+ Python::attach(|py| Python::run(py, c " import foo ; foo . add_one (6 )" , None, None))
173170}
174171```
175172
@@ -180,7 +177,6 @@ and insert it manually into `sys.modules`:
180177```rust
181178use pyo3::prelude::*;
182179use pyo3::types::PyDict;
183- use pyo3 :: ffi :: c_str;
184180
185181#[pyfunction]
186182pub fn add_one(x: i64) -> i64 {
@@ -201,7 +197,7 @@ fn main() -> PyResult<()> {
201197 py_modules.set_item(" foo " , foo_module)?;
202198
203199 // Now we can import + run our python code
204- Python :: run (py , c_str! ( " import foo; foo.add_one(6)" ) , None , None )
200+ Python::run(py, c " import foo ; foo . add_one (6 )" , None, None)
205201 })
206202}
207203```
@@ -267,8 +263,8 @@ fn main() -> PyResult<()> {
267263 )));
268264 let py_app = c_str!(include_str!(concat!(env!(" CARGO_MANIFEST_DIR " ), " / python_app / app . py" )));
269265 let from_python = Python::attach(|py| -> PyResult<Py<PyAny>> {
270- PyModule::from_code(py, py_foo, c_str!( "foo.py"), c_str!( "utils.foo") )?;
271- let app: Py<PyAny> = PyModule::from_code(py, py_app, c_str!( "app.py"), c_str!("") )?
266+ PyModule::from_code(py, py_foo, c " foo . py" , c " utils . foo" )?;
267+ let app: Py<PyAny> = PyModule::from_code(py, py_app, c " app . py" , c "" )?
272268 .getattr(" run " )?
273269 .into();
274270 app.call0(py)
@@ -295,7 +291,6 @@ that directory is `/usr/share/python_app`).
295291```rust,no_run
296292use pyo3::prelude::*;
297293use pyo3::types::PyList;
298- use pyo3_ffi::c_str;
299294use std::fs;
300295use std::path::Path;
301296use std::ffi::CString;
@@ -309,7 +304,7 @@ fn main() -> PyResult<()> {
309304 .getattr(" path " )?
310305 .cast_into::<PyList>()?;
311306 syspath.insert(0, path)?;
312- let app: Py<PyAny> = PyModule::from_code(py, py_app.as_c_str(), c_str!( "app.py"), c_str!("") )?
307+ let app: Py<PyAny> = PyModule::from_code(py, py_app.as_c_str(), c " app . py" , c "" )?
313308 .getattr(" run " )?
314309 .into();
315310 app.call0(py)
@@ -326,13 +321,12 @@ Use context managers by directly invoking `__enter__` and `__exit__`.
326321
327322```rust
328323use pyo3::prelude::*;
329- use pyo3 :: ffi :: c_str;
330324
331325fn main() {
332326 Python::attach(|py| {
333327 let custom_manager = PyModule::from_code(
334328 py,
335- c_str! ( r # "
329+ cr #"
336330class House (object ):
337331 def __init__ (self , address ):
338332 self . address = address
@@ -344,9 +338,9 @@ class House(object):
344338 else :
345339 print (f " Thank you for visiting {self.address}, come again soon!" )
346340
347- " # ) ,
348- c_str! ( " house.py" ) ,
349- c_str! ( " house" ) ,
341+ " #,
342+ c " house . py" ,
343+ c " house " ,
350344 )
351345 .unwrap();
352346
@@ -355,7 +349,7 @@ class House(object):
355349
356350 house.call_method0(" __enter__ " ).unwrap();
357351
358- let result = py . eval (c_str! ( " undefined_variable + 1" ) , None , None );
352+ let result = py.eval(c " undefined_variable + 1 " , None, None);
359353
360354 // If the eval threw an exception we'll pass it through to the context manager.
361355 // Otherwise, __exit__ is called with empty arguments (Python " None " ).
0 commit comments