Files
2025-04-02 13:41:38 +00:00

260 lines
62 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="A marker token that represents holding the GIL."><title>Python in pyo3::marker - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-Medium-e1aa3f0a.woff2,SourceCodePro-Regular-8badfe75.ttf.woff2,SourceCodePro-Semibold-aa29a496.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2" crossorigin href="../../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../../static.files/rustdoc-46132b98.css"><meta name="rustdoc-vars" data-root-path="../../" data-static-root-path="../../static.files/" data-current-crate="pyo3" data-themes="" data-resource-suffix="" data-rustdoc-version="1.85.1 (4eb161250 2025-03-15)" data-channel="1.85.1" data-search-js="search-75f5ac3e.js" data-settings-js="settings-0f613d39.js" ><script src="../../static.files/storage-59e33391.js"></script><script defer src="sidebar-items.js"></script><script defer src="../../static.files/main-5f194d8c.js"></script><noscript><link rel="stylesheet" href="../../static.files/noscript-893ab5e7.css"></noscript><link rel="alternate icon" type="image/png" href="../../static.files/favicon-32x32-6580c154.png"><link rel="icon" type="image/svg+xml" href="../../static.files/favicon-044be391.svg"></head><body class="rustdoc struct"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle" title="show sidebar"></button></nav><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../../pyo3/index.html">pyo3</a><span class="version">0.24.1</span></h2></div><div class="sidebar-elems"><section id="rustdoc-toc"><h2 class="location"><a href="#">Python</a></h2><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#obtaining-a-python-token" title="Obtaining a Python token">Obtaining a Python token</a></li><li><a href="#deadlocks" title="Deadlocks">Deadlocks</a></li><li><a href="#releasing-and-freeing-memory" title="Releasing and freeing memory">Releasing and freeing memory</a></li></ul><h3><a href="#implementations">Methods</a></h3><ul class="block method"><li><a href="#method.Ellipsis" title="Ellipsis">Ellipsis</a></li><li><a href="#method.None" title="None">None</a></li><li><a href="#method.NotImplemented" title="NotImplemented">NotImplemented</a></li><li><a href="#method.allow_threads" title="allow_threads">allow_threads</a></li><li><a href="#method.assume_gil_acquired" title="assume_gil_acquired">assume_gil_acquired</a></li><li><a href="#method.check_signals" title="check_signals">check_signals</a></li><li><a href="#method.eval" title="eval">eval</a></li><li><a href="#method.eval_bound" title="eval_bound">eval_bound</a></li><li><a href="#method.get_type" title="get_type">get_type</a></li><li><a href="#method.get_type_bound" title="get_type_bound">get_type_bound</a></li><li><a href="#method.import" title="import">import</a></li><li><a href="#method.import_bound" title="import_bound">import_bound</a></li><li><a href="#method.run" title="run">run</a></li><li><a href="#method.run_bound" title="run_bound">run_bound</a></li><li><a href="#method.version" title="version">version</a></li><li><a href="#method.version_info" title="version_info">version_info</a></li><li><a href="#method.with_gil" title="with_gil">with_gil</a></li><li><a href="#method.with_gil_unchecked" title="with_gil_unchecked">with_gil_unchecked</a></li></ul><h3><a href="#trait-implementations">Trait Implementations</a></h3><ul class="block trait-implementation"><li><a href="#impl-Clone-for-Python%3C'py%3E" title="Clone">Clone</a></li><li><a href="#impl-Copy-for-Python%3C'py%3E" title="Copy">Copy</a></li></ul><h3><a href="#synthetic-implementations">Auto Trait Implementations</a></h3><ul class="block synthetic-implementation"><li><a href="#impl-Send-for-Python%3C'py%3E" title="!Send">!Send</a></li><li><a href="#impl-Sync-for-Python%3C'py%3E" title="!Sync">!Sync</a></li><li><a href="#impl-Freeze-for-Python%3C'py%3E" title="Freeze">Freeze</a></li><li><a href="#impl-RefUnwindSafe-for-Python%3C'py%3E" title="RefUnwindSafe">RefUnwindSafe</a></li><li><a href="#impl-Unpin-for-Python%3C'py%3E" title="Unpin">Unpin</a></li><li><a href="#impl-UnwindSafe-for-Python%3C'py%3E" title="UnwindSafe">UnwindSafe</a></li></ul><h3><a href="#blanket-implementations">Blanket Implementations</a></h3><ul class="block blanket-implementation"><li><a href="#impl-Any-for-T" title="Any">Any</a></li><li><a href="#impl-Borrow%3CT%3E-for-T" title="Borrow&#60;T&#62;">Borrow&#60;T&#62;</a></li><li><a href="#impl-BorrowMut%3CT%3E-for-T" title="BorrowMut&#60;T&#62;">BorrowMut&#60;T&#62;</a></li><li><a href="#impl-CloneToUninit-for-T" title="CloneToUninit">CloneToUninit</a></li><li><a href="#impl-From%3CT%3E-for-T" title="From&#60;T&#62;">From&#60;T&#62;</a></li><li><a href="#impl-Into%3CU%3E-for-T" title="Into&#60;U&#62;">Into&#60;U&#62;</a></li><li><a href="#impl-ToOwned-for-T" title="ToOwned">ToOwned</a></li><li><a href="#impl-TryFrom%3CU%3E-for-T" title="TryFrom&#60;U&#62;">TryFrom&#60;U&#62;</a></li><li><a href="#impl-TryInto%3CU%3E-for-T" title="TryInto&#60;U&#62;">TryInto&#60;U&#62;</a></li></ul></section><div id="rustdoc-modnav"><h2><a href="index.html">In pyo3::<wbr>marker</a></h2></div></div></nav><div class="sidebar-resizer"></div><main><div class="width-limiter"><rustdoc-search></rustdoc-search><section id="main-content" class="content"><div class="main-heading"><span class="rustdoc-breadcrumbs"><a href="../index.html">pyo3</a>::<wbr><a href="index.html">marker</a></span><h1>Struct <span class="struct">Python</span><button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><rustdoc-toolbar></rustdoc-toolbar><span class="sub-heading"><a class="src" href="../../src/pyo3/marker.rs.html#359">Source</a> </span></div><pre class="rust item-decl"><code>pub struct Python&lt;'py&gt;(<span class="comment">/* private fields */</span>);</code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>A marker token that represents holding the GIL.</p>
<p>It serves three main purposes:</p>
<ul>
<li>It provides a global API for the Python interpreter, such as <a href="struct.Python.html#method.eval_bound" title="method pyo3::marker::Python::eval_bound"><code>Python::eval_bound</code></a>.</li>
<li>It can be passed to functions that require a proof of holding the GIL, such as
<a href="../struct.Py.html#method.clone_ref" title="method pyo3::Py::clone_ref"><code>Py::clone_ref</code></a>.</li>
<li>Its lifetime represents the scope of holding the GIL which can be used to create Rust
references that are bound to it, such as [<code>Bound&lt;'py, PyAny&gt;</code>].</li>
</ul>
<p>Note that there are some caveats to using it that you might need to be aware of. See the
<a href="#deadlocks">Deadlocks</a> and <a href="#releasing-and-freeing-memory">Releasing and freeing memory</a>
paragraphs for more information about that.</p>
<h2 id="obtaining-a-python-token"><a class="doc-anchor" href="#obtaining-a-python-token">§</a>Obtaining a Python token</h2>
<p>The following are the recommended ways to obtain a [<code>Python&lt;'py&gt;</code>] token, in order of preference:</p>
<ul>
<li>If you already have something with a lifetime bound to the GIL, such as [<code>Bound&lt;'py, PyAny&gt;</code>], you can
use its <code>.py()</code> method to get a token.</li>
<li>In a function or method annotated with <a href="../attr.pyfunction.html" title="attr pyo3::pyfunction"><code>#[pyfunction]</code></a> or <a href="../attr.pymethods.html" title="attr pyo3::pymethods"><code>#[pymethods]</code></a> you can declare it
as a parameter, and PyO3 will pass in the token when Python code calls it.</li>
<li>When you need to acquire the GIL yourself, such as when calling Python code from Rust, you
should call <a href="struct.Python.html#method.with_gil" title="associated function pyo3::marker::Python::with_gil"><code>Python::with_gil</code></a> to do that and pass your code as a closure to it.</li>
</ul>
<p>The first two options are zero-cost; <a href="struct.Python.html#method.with_gil" title="associated function pyo3::marker::Python::with_gil"><code>Python::with_gil</code></a> requires runtime checking and may need to block
to acquire the GIL.</p>
<h2 id="deadlocks"><a class="doc-anchor" href="#deadlocks">§</a>Deadlocks</h2>
<p>Note that the GIL can be temporarily released by the Python interpreter during a function call
(e.g. importing a module). In general, you dont need to worry about this because the GIL is
reacquired before returning to the Rust code:</p>
<div class="example-wrap"><pre class="language-text"><code>`Python` exists |=====================================|
GIL actually held |==========| |================|
Rust code running |=======| |==| |======|</code></pre></div>
<p>This behaviour can cause deadlocks when trying to lock a Rust mutex while holding the GIL:</p>
<ul>
<li>Thread 1 acquires the GIL</li>
<li>Thread 1 locks a mutex</li>
<li>Thread 1 makes a call into the Python interpreter which releases the GIL</li>
<li>Thread 2 acquires the GIL</li>
<li>Thread 2 tries to locks the mutex, blocks</li>
<li>Thread 1s Python interpreter call blocks trying to reacquire the GIL held by thread 2</li>
</ul>
<p>To avoid deadlocking, you should release the GIL before trying to lock a mutex or <code>await</code>ing in
asynchronous code, e.g. with <a href="struct.Python.html#method.allow_threads" title="method pyo3::marker::Python::allow_threads"><code>Python::allow_threads</code></a>.</p>
<h2 id="releasing-and-freeing-memory"><a class="doc-anchor" href="#releasing-and-freeing-memory">§</a>Releasing and freeing memory</h2>
<p>The [<code>Python&lt;'py&gt;</code>] type can be used to create references to variables owned by the Python
interpreter, using functions such as <a href="struct.Python.html#method.eval_bound" title="method pyo3::marker::Python::eval_bound"><code>Python::eval_bound</code></a> and <a href="../types/struct.PyModule.html#method.import" title="associated function pyo3::types::PyModule::import"><code>PyModule::import</code></a>.</p>
</div></details><h2 id="implementations" class="section-header">Implementations<a href="#implementations" class="anchor">§</a></h2><div id="implementations-list"><details class="toggle implementors-toggle" open><summary><section id="impl-Python%3C'_%3E" class="impl"><a class="src rightside" href="../../src/pyo3/marker.rs.html#361-446">Source</a><a href="#impl-Python%3C'_%3E" class="anchor">§</a><h3 class="code-header">impl <a class="struct" href="struct.Python.html" title="struct pyo3::marker::Python">Python</a>&lt;'_&gt;</h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.with_gil" class="method"><a class="src rightside" href="../../src/pyo3/marker.rs.html#404-412">Source</a><h4 class="code-header">pub fn <a href="#method.with_gil" class="fn">with_gil</a>&lt;F, R&gt;(f: F) -&gt; R<div class="where">where
F: for&lt;'py&gt; <a class="trait" href="https://doc.rust-lang.org/1.85.1/core/ops/function/trait.FnOnce.html" title="trait core::ops::function::FnOnce">FnOnce</a>(<a class="struct" href="struct.Python.html" title="struct pyo3::marker::Python">Python</a>&lt;'py&gt;) -&gt; R,</div></h4></section></summary><div class="docblock"><p>Acquires the global interpreter lock, allowing access to the Python interpreter. The
provided closure <code>F</code> will be executed with the acquired <code>Python</code> marker token.</p>
<p>If implementing <a href="../attr.pymethods.html" title="attr pyo3::pymethods"><code>#[pymethods]</code></a> or <a href="../attr.pyfunction.html" title="attr pyo3::pyfunction"><code>#[pyfunction]</code></a>,
declare <code>py: Python</code> as an argument. PyO3 will pass in the token to grant access to the GIL
context in which the function is running, avoiding the need to call <code>with_gil</code>.</p>
<p>If the <a href="https://pyo3.rs/main/features.html#auto-initialize"><code>auto-initialize</code></a> feature is enabled and the Python runtime is not already
initialized, this function will initialize it. See
<a href="../fn.prepare_freethreaded_python.html" title="fn pyo3::prepare_freethreaded_python"><code>prepare_freethreaded_python</code></a>
for details.</p>
<p>If the current thread does not yet have a Python “thread state” associated with it,
a new one will be automatically created before <code>F</code> is executed and destroyed after <code>F</code>
completes.</p>
<h5 id="panics"><a class="doc-anchor" href="#panics">§</a>Panics</h5>
<ul>
<li>If the <a href="https://pyo3.rs/main/features.html#auto-initialize"><code>auto-initialize</code></a> feature is not enabled and the Python interpreter is not
initialized.</li>
</ul>
<h5 id="examples"><a class="doc-anchor" href="#examples">§</a>Examples</h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>pyo3::prelude::<span class="kw-2">*</span>;
<span class="kw">use </span>pyo3::ffi::c_str;
Python::with_gil(|py| -&gt; PyResult&lt;()&gt; {
<span class="kw">let </span>x: i32 = py.eval(<span class="macro">c_str!</span>(<span class="string">"5"</span>), <span class="prelude-val">None</span>, <span class="prelude-val">None</span>)<span class="question-mark">?</span>.extract()<span class="question-mark">?</span>;
<span class="macro">assert_eq!</span>(x, <span class="number">5</span>);
<span class="prelude-val">Ok</span>(())
})</code></pre></div>
</div></details><details class="toggle method-toggle" open><summary><section id="method.with_gil_unchecked" class="method"><a class="src rightside" href="../../src/pyo3/marker.rs.html#438-445">Source</a><h4 class="code-header">pub unsafe fn <a href="#method.with_gil_unchecked" class="fn">with_gil_unchecked</a>&lt;F, R&gt;(f: F) -&gt; R<div class="where">where
F: for&lt;'py&gt; <a class="trait" href="https://doc.rust-lang.org/1.85.1/core/ops/function/trait.FnOnce.html" title="trait core::ops::function::FnOnce">FnOnce</a>(<a class="struct" href="struct.Python.html" title="struct pyo3::marker::Python">Python</a>&lt;'py&gt;) -&gt; R,</div></h4></section></summary><div class="docblock"><p>Like <a href="struct.Python.html#method.with_gil" title="associated function pyo3::marker::Python::with_gil"><code>Python::with_gil</code></a> except Python interpreter state checking is skipped.</p>
<p>Normally when the GIL is acquired, we check that the Python interpreter is an
appropriate state (e.g. it is fully initialized). This function skips those
checks.</p>
<h5 id="safety"><a class="doc-anchor" href="#safety">§</a>Safety</h5>
<p>If <a href="struct.Python.html#method.with_gil" title="associated function pyo3::marker::Python::with_gil"><code>Python::with_gil</code></a> would succeed, it is safe to call this function.</p>
<p>In most cases, you should use <a href="struct.Python.html#method.with_gil" title="associated function pyo3::marker::Python::with_gil"><code>Python::with_gil</code></a>.</p>
<p>A justified scenario for calling this function is during multi-phase interpreter
initialization when <a href="struct.Python.html#method.with_gil" title="associated function pyo3::marker::Python::with_gil"><code>Python::with_gil</code></a> would fail before
<a href="../ffi/fn._Py_InitializeMain.html" title="fn pyo3::ffi::_Py_InitializeMain"><code>_Py_InitializeMain</code></a>
is called because the interpreter is only partially initialized.</p>
<p>Behavior in other scenarios is not documented.</p>
</div></details></div></details><details class="toggle implementors-toggle" open><summary><section id="impl-Python%3C'py%3E" class="impl"><a class="src rightside" href="../../src/pyo3/marker.rs.html#448-840">Source</a><a href="#impl-Python%3C'py%3E" class="anchor">§</a><h3 class="code-header">impl&lt;'py&gt; <a class="struct" href="struct.Python.html" title="struct pyo3::marker::Python">Python</a>&lt;'py&gt;</h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.allow_threads" class="method"><a class="src rightside" href="../../src/pyo3/marker.rs.html#510-521">Source</a><h4 class="code-header">pub fn <a href="#method.allow_threads" class="fn">allow_threads</a>&lt;T, F&gt;(self, f: F) -&gt; T<div class="where">where
F: <a class="trait" href="trait.Ungil.html" title="trait pyo3::marker::Ungil">Ungil</a> + <a class="trait" href="https://doc.rust-lang.org/1.85.1/core/ops/function/trait.FnOnce.html" title="trait core::ops::function::FnOnce">FnOnce</a>() -&gt; T,
T: <a class="trait" href="trait.Ungil.html" title="trait pyo3::marker::Ungil">Ungil</a>,</div></h4></section></summary><div class="docblock"><p>Temporarily releases the GIL, thus allowing other Python threads to run. The GIL will be
reacquired when <code>F</code>s scope ends.</p>
<p>If you dont need to touch the Python
interpreter for some time and have other Python threads around, this will let you run
Rust-only code while letting those other Python threads make progress.</p>
<p>Only types that implement <a href="trait.Ungil.html" title="trait pyo3::marker::Ungil"><code>Ungil</code></a> can cross the closure. See the
<a href="index.html" title="mod pyo3::marker">module level documentation</a> for more information.</p>
<p>If you need to pass Python objects into the closure you can use <a href="../struct.Py.html" title="struct pyo3::Py"><code>Py</code></a><code>&lt;T&gt;</code>to create a
reference independent of the GIL lifetime. However, you cannot do much with those without a
<a href="struct.Python.html" title="struct pyo3::marker::Python"><code>Python</code></a> token, for which youd need to reacquire the GIL.</p>
<h5 id="example-releasing-the-gil-while-running-a-computation-in-rust-only-code"><a class="doc-anchor" href="#example-releasing-the-gil-while-running-a-computation-in-rust-only-code">§</a>Example: Releasing the GIL while running a computation in Rust-only code</h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>pyo3::prelude::<span class="kw-2">*</span>;
<span class="attr">#[pyfunction]
</span><span class="kw">fn </span>sum_numbers(py: Python&lt;<span class="lifetime">'_</span>&gt;, numbers: Vec&lt;u32&gt;) -&gt; PyResult&lt;u32&gt; {
<span class="comment">// We release the GIL here so any other Python threads get a chance to run.
</span>py.allow_threads(<span class="kw">move </span>|| {
<span class="comment">// An example of an "expensive" Rust calculation
</span><span class="kw">let </span>sum = numbers.iter().sum();
<span class="prelude-val">Ok</span>(sum)
})
}</code></pre></div>
<p>Please see the <a href="https://pyo3.rs/main/parallelism.html">Parallelism</a> chapter of the guide for a thorough discussion of using
<a href="struct.Python.html#method.allow_threads" title="method pyo3::marker::Python::allow_threads"><code>Python::allow_threads</code></a> in this manner.</p>
<h5 id="example-passing-borrowed-python-references-into-the-closure-is-not-allowed"><a class="doc-anchor" href="#example-passing-borrowed-python-references-into-the-closure-is-not-allowed">§</a>Example: Passing borrowed Python references into the closure is not allowed</h5>
<div class="example-wrap compile_fail"><a href="#" class="tooltip" title="This example deliberately fails to compile"></a><pre class="rust rust-example-rendered"><code><span class="kw">use </span>pyo3::prelude::<span class="kw-2">*</span>;
<span class="kw">use </span>pyo3::types::PyString;
<span class="kw">fn </span>parallel_print(py: Python&lt;<span class="lifetime">'_</span>&gt;) {
<span class="kw">let </span>s = PyString::new_bound(py, <span class="string">"This object cannot be accessed without holding the GIL &gt;_&lt;"</span>);
py.allow_threads(<span class="kw">move </span>|| {
<span class="macro">println!</span>(<span class="string">"{:?}"</span>, s); <span class="comment">// This causes a compile error.
</span>});
}</code></pre></div>
</div></details><details class="toggle method-toggle" open><summary><section id="method.eval" class="method"><a class="src rightside" href="../../src/pyo3/marker.rs.html#542-549">Source</a><h4 class="code-header">pub fn <a href="#method.eval" class="fn">eval</a>(
self,
code: &amp;<a class="struct" href="https://doc.rust-lang.org/1.85.1/core/ffi/c_str/struct.CStr.html" title="struct core::ffi::c_str::CStr">CStr</a>,
globals: <a class="enum" href="https://doc.rust-lang.org/1.85.1/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;&amp;<a class="struct" href="../struct.Bound.html" title="struct pyo3::Bound">Bound</a>&lt;'py, <a class="struct" href="../types/struct.PyDict.html" title="struct pyo3::types::PyDict">PyDict</a>&gt;&gt;,
locals: <a class="enum" href="https://doc.rust-lang.org/1.85.1/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;&amp;<a class="struct" href="../struct.Bound.html" title="struct pyo3::Bound">Bound</a>&lt;'py, <a class="struct" href="../types/struct.PyDict.html" title="struct pyo3::types::PyDict">PyDict</a>&gt;&gt;,
) -&gt; <a class="type" href="../type.PyResult.html" title="type pyo3::PyResult">PyResult</a>&lt;<a class="struct" href="../struct.Bound.html" title="struct pyo3::Bound">Bound</a>&lt;'py, <a class="struct" href="../types/struct.PyAny.html" title="struct pyo3::types::PyAny">PyAny</a>&gt;&gt;</h4></section></summary><div class="docblock"><p>Evaluates a Python expression in the given context and returns the result.</p>
<p>If <code>globals</code> is <code>None</code>, it defaults to Python module <code>__main__</code>.
If <code>locals</code> is <code>None</code>, it defaults to the value of <code>globals</code>.</p>
<p>If <code>globals</code> doesnt contain <code>__builtins__</code>, default <code>__builtins__</code>
will be added automatically.</p>
<h5 id="examples-1"><a class="doc-anchor" href="#examples-1">§</a>Examples</h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">let </span>result = py.eval(<span class="macro">c_str!</span>(<span class="string">"[i * 10 for i in range(5)]"</span>), <span class="prelude-val">None</span>, <span class="prelude-val">None</span>).unwrap();
<span class="kw">let </span>res: Vec&lt;i64&gt; = result.extract().unwrap();
<span class="macro">assert_eq!</span>(res, <span class="macro">vec!</span>[<span class="number">0</span>, <span class="number">10</span>, <span class="number">20</span>, <span class="number">30</span>, <span class="number">40</span>])</code></pre></div>
</div></details><details class="toggle method-toggle" open><summary><section id="method.eval_bound" class="method"><a class="src rightside" href="../../src/pyo3/marker.rs.html#555-563">Source</a><h4 class="code-header">pub fn <a href="#method.eval_bound" class="fn">eval_bound</a>(
self,
code: &amp;<a class="primitive" href="https://doc.rust-lang.org/1.85.1/std/primitive.str.html">str</a>,
globals: <a class="enum" href="https://doc.rust-lang.org/1.85.1/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;&amp;<a class="struct" href="../struct.Bound.html" title="struct pyo3::Bound">Bound</a>&lt;'py, <a class="struct" href="../types/struct.PyDict.html" title="struct pyo3::types::PyDict">PyDict</a>&gt;&gt;,
locals: <a class="enum" href="https://doc.rust-lang.org/1.85.1/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;&amp;<a class="struct" href="../struct.Bound.html" title="struct pyo3::Bound">Bound</a>&lt;'py, <a class="struct" href="../types/struct.PyDict.html" title="struct pyo3::types::PyDict">PyDict</a>&gt;&gt;,
) -&gt; <a class="type" href="../type.PyResult.html" title="type pyo3::PyResult">PyResult</a>&lt;<a class="struct" href="../struct.Bound.html" title="struct pyo3::Bound">Bound</a>&lt;'py, <a class="struct" href="../types/struct.PyAny.html" title="struct pyo3::types::PyAny">PyAny</a>&gt;&gt;</h4></section><span class="item-info"><div class="stab deprecated"><span class="emoji">👎</span><span>Deprecated since 0.23.0: renamed to <code>Python::eval</code></span></div></span></summary><div class="docblock"><p>Deprecated name for <a href="struct.Python.html#method.eval" title="method pyo3::marker::Python::eval"><code>Python::eval</code></a>.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.run" class="method"><a class="src rightside" href="../../src/pyo3/marker.rs.html#600-610">Source</a><h4 class="code-header">pub fn <a href="#method.run" class="fn">run</a>(
self,
code: &amp;<a class="struct" href="https://doc.rust-lang.org/1.85.1/core/ffi/c_str/struct.CStr.html" title="struct core::ffi::c_str::CStr">CStr</a>,
globals: <a class="enum" href="https://doc.rust-lang.org/1.85.1/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;&amp;<a class="struct" href="../struct.Bound.html" title="struct pyo3::Bound">Bound</a>&lt;'py, <a class="struct" href="../types/struct.PyDict.html" title="struct pyo3::types::PyDict">PyDict</a>&gt;&gt;,
locals: <a class="enum" href="https://doc.rust-lang.org/1.85.1/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;&amp;<a class="struct" href="../struct.Bound.html" title="struct pyo3::Bound">Bound</a>&lt;'py, <a class="struct" href="../types/struct.PyDict.html" title="struct pyo3::types::PyDict">PyDict</a>&gt;&gt;,
) -&gt; <a class="type" href="../type.PyResult.html" title="type pyo3::PyResult">PyResult</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/1.85.1/std/primitive.unit.html">()</a>&gt;</h4></section></summary><div class="docblock"><p>Executes one or more Python statements in the given context.</p>
<p>If <code>globals</code> is <code>None</code>, it defaults to Python module <code>__main__</code>.
If <code>locals</code> is <code>None</code>, it defaults to the value of <code>globals</code>.</p>
<p>If <code>globals</code> doesnt contain <code>__builtins__</code>, default <code>__builtins__</code>
will be added automatically.</p>
<h5 id="examples-2"><a class="doc-anchor" href="#examples-2">§</a>Examples</h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>pyo3::{
prelude::<span class="kw-2">*</span>,
types::{PyBytes, PyDict},
ffi::c_str,
};
Python::with_gil(|py| {
<span class="kw">let </span>locals = PyDict::new(py);
py.run(<span class="macro">c_str!</span>(
<span class="string">r#"
import base64
s = 'Hello Rust!'
ret = base64.b64encode(s.encode('utf-8'))
"#</span>),
<span class="prelude-val">None</span>,
<span class="prelude-val">Some</span>(<span class="kw-2">&amp;</span>locals),
)
.unwrap();
<span class="kw">let </span>ret = locals.get_item(<span class="string">"ret"</span>).unwrap().unwrap();
<span class="kw">let </span>b64 = ret.downcast::&lt;PyBytes&gt;().unwrap();
<span class="macro">assert_eq!</span>(b64.as_bytes(), <span class="string">b"SGVsbG8gUnVzdCE="</span>);
});</code></pre></div>
<p>You can use <a href="macro.py_run.html"><code>py_run!</code></a> for a handy alternative of <code>run</code>
if you dont need <code>globals</code> and unwrapping is OK.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.run_bound" class="method"><a class="src rightside" href="../../src/pyo3/marker.rs.html#616-624">Source</a><h4 class="code-header">pub fn <a href="#method.run_bound" class="fn">run_bound</a>(
self,
code: &amp;<a class="primitive" href="https://doc.rust-lang.org/1.85.1/std/primitive.str.html">str</a>,
globals: <a class="enum" href="https://doc.rust-lang.org/1.85.1/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;&amp;<a class="struct" href="../struct.Bound.html" title="struct pyo3::Bound">Bound</a>&lt;'py, <a class="struct" href="../types/struct.PyDict.html" title="struct pyo3::types::PyDict">PyDict</a>&gt;&gt;,
locals: <a class="enum" href="https://doc.rust-lang.org/1.85.1/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;&amp;<a class="struct" href="../struct.Bound.html" title="struct pyo3::Bound">Bound</a>&lt;'py, <a class="struct" href="../types/struct.PyDict.html" title="struct pyo3::types::PyDict">PyDict</a>&gt;&gt;,
) -&gt; <a class="type" href="../type.PyResult.html" title="type pyo3::PyResult">PyResult</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/1.85.1/std/primitive.unit.html">()</a>&gt;</h4></section><span class="item-info"><div class="stab deprecated"><span class="emoji">👎</span><span>Deprecated since 0.23.0: renamed to <code>Python::run</code></span></div></span></summary><div class="docblock"><p>Deprecated name for <a href="struct.Python.html#method.run" title="method pyo3::marker::Python::run"><code>Python::run</code></a>.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.get_type" class="method"><a class="src rightside" href="../../src/pyo3/marker.rs.html#695-700">Source</a><h4 class="code-header">pub fn <a href="#method.get_type" class="fn">get_type</a>&lt;T&gt;(self) -&gt; <a class="struct" href="../struct.Bound.html" title="struct pyo3::Bound">Bound</a>&lt;'py, <a class="struct" href="../types/struct.PyType.html" title="struct pyo3::types::PyType">PyType</a>&gt;<div class="where">where
T: <a class="trait" href="../type_object/trait.PyTypeInfo.html" title="trait pyo3::type_object::PyTypeInfo">PyTypeInfo</a>,</div></h4></section></summary><div class="docblock"><p>Gets the Python type object for type <code>T</code>.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.get_type_bound" class="method"><a class="src rightside" href="../../src/pyo3/marker.rs.html#706-711">Source</a><h4 class="code-header">pub fn <a href="#method.get_type_bound" class="fn">get_type_bound</a>&lt;T&gt;(self) -&gt; <a class="struct" href="../struct.Bound.html" title="struct pyo3::Bound">Bound</a>&lt;'py, <a class="struct" href="../types/struct.PyType.html" title="struct pyo3::types::PyType">PyType</a>&gt;<div class="where">where
T: <a class="trait" href="../type_object/trait.PyTypeInfo.html" title="trait pyo3::type_object::PyTypeInfo">PyTypeInfo</a>,</div></h4></section><span class="item-info"><div class="stab deprecated"><span class="emoji">👎</span><span>Deprecated since 0.23.0: renamed to <code>Python::get_type</code></span></div></span></summary><div class="docblock"><p>Deprecated name for <a href="struct.Python.html#method.get_type" title="method pyo3::marker::Python::get_type"><code>Python::get_type</code></a>.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.import" class="method"><a class="src rightside" href="../../src/pyo3/marker.rs.html#714-719">Source</a><h4 class="code-header">pub fn <a href="#method.import" class="fn">import</a>&lt;N&gt;(self, name: N) -&gt; <a class="type" href="../type.PyResult.html" title="type pyo3::PyResult">PyResult</a>&lt;<a class="struct" href="../struct.Bound.html" title="struct pyo3::Bound">Bound</a>&lt;'py, <a class="struct" href="../types/struct.PyModule.html" title="struct pyo3::types::PyModule">PyModule</a>&gt;&gt;<div class="where">where
N: <a class="trait" href="../conversion/trait.IntoPyObject.html" title="trait pyo3::conversion::IntoPyObject">IntoPyObject</a>&lt;'py, Target = <a class="struct" href="../types/struct.PyString.html" title="struct pyo3::types::PyString">PyString</a>&gt;,</div></h4></section></summary><div class="docblock"><p>Imports the Python module with the specified name.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.import_bound" class="method"><a class="src rightside" href="../../src/pyo3/marker.rs.html#726-731">Source</a><h4 class="code-header">pub fn <a href="#method.import_bound" class="fn">import_bound</a>&lt;N&gt;(self, name: N) -&gt; <a class="type" href="../type.PyResult.html" title="type pyo3::PyResult">PyResult</a>&lt;<a class="struct" href="../struct.Bound.html" title="struct pyo3::Bound">Bound</a>&lt;'py, <a class="struct" href="../types/struct.PyModule.html" title="struct pyo3::types::PyModule">PyModule</a>&gt;&gt;<div class="where">where
N: <a class="trait" href="../conversion/trait.IntoPy.html" title="trait pyo3::conversion::IntoPy">IntoPy</a>&lt;<a class="struct" href="../struct.Py.html" title="struct pyo3::Py">Py</a>&lt;<a class="struct" href="../types/struct.PyString.html" title="struct pyo3::types::PyString">PyString</a>&gt;&gt;,</div></h4></section><span class="item-info"><div class="stab deprecated"><span class="emoji">👎</span><span>Deprecated since 0.23.0: renamed to <code>Python::import</code></span></div></span></summary><div class="docblock"><p>Deprecated name for <a href="struct.Python.html#method.import" title="method pyo3::marker::Python::import"><code>Python::import</code></a>.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.None" class="method"><a class="src rightside" href="../../src/pyo3/marker.rs.html#736-738">Source</a><h4 class="code-header">pub fn <a href="#method.None" class="fn">None</a>(self) -&gt; <a class="type" href="../type.PyObject.html" title="type pyo3::PyObject">PyObject</a></h4></section></summary><div class="docblock"><p>Gets the Python builtin value <code>None</code>.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.Ellipsis" class="method"><a class="src rightside" href="../../src/pyo3/marker.rs.html#743-745">Source</a><h4 class="code-header">pub fn <a href="#method.Ellipsis" class="fn">Ellipsis</a>(self) -&gt; <a class="type" href="../type.PyObject.html" title="type pyo3::PyObject">PyObject</a></h4></section></summary><div class="docblock"><p>Gets the Python builtin value <code>Ellipsis</code>, or <code>...</code>.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.NotImplemented" class="method"><a class="src rightside" href="../../src/pyo3/marker.rs.html#750-752">Source</a><h4 class="code-header">pub fn <a href="#method.NotImplemented" class="fn">NotImplemented</a>(self) -&gt; <a class="type" href="../type.PyObject.html" title="type pyo3::PyObject">PyObject</a></h4></section></summary><div class="docblock"><p>Gets the Python builtin value <code>NotImplemented</code>.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.version" class="method"><a class="src rightside" href="../../src/pyo3/marker.rs.html#765-771">Source</a><h4 class="code-header">pub fn <a href="#method.version" class="fn">version</a>(self) -&gt; &amp;'py <a class="primitive" href="https://doc.rust-lang.org/1.85.1/std/primitive.str.html">str</a></h4></section></summary><div class="docblock"><p>Gets the running Python interpreter version as a string.</p>
<h5 id="examples-3"><a class="doc-anchor" href="#examples-3">§</a>Examples</h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code>Python::with_gil(|py| {
<span class="comment">// The full string could be, for example:
// "3.10.0 (tags/v3.10.0:b494f59, Oct 4 2021, 19:00:18) [MSC v.1929 64 bit (AMD64)]"
</span><span class="macro">assert!</span>(py.version().starts_with(<span class="string">"3."</span>));
});</code></pre></div>
</div></details><details class="toggle method-toggle" open><summary><section id="method.version_info" class="method"><a class="src rightside" href="../../src/pyo3/marker.rs.html#785-793">Source</a><h4 class="code-header">pub fn <a href="#method.version_info" class="fn">version_info</a>(self) -&gt; <a class="struct" href="../struct.PythonVersionInfo.html" title="struct pyo3::PythonVersionInfo">PythonVersionInfo</a>&lt;'py&gt;</h4></section></summary><div class="docblock"><p>Gets the running Python interpreter version as a struct similar to
<code>sys.version_info</code>.</p>
<h5 id="examples-4"><a class="doc-anchor" href="#examples-4">§</a>Examples</h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code>Python::with_gil(|py| {
<span class="comment">// PyO3 supports Python 3.7 and up.
</span><span class="macro">assert!</span>(py.version_info() &gt;= (<span class="number">3</span>, <span class="number">7</span>));
<span class="macro">assert!</span>(py.version_info() &gt;= (<span class="number">3</span>, <span class="number">7</span>, <span class="number">0</span>));
});</code></pre></div>
</div></details><details class="toggle method-toggle" open><summary><section id="method.check_signals" class="method"><a class="src rightside" href="../../src/pyo3/marker.rs.html#837-839">Source</a><h4 class="code-header">pub fn <a href="#method.check_signals" class="fn">check_signals</a>(self) -&gt; <a class="type" href="../type.PyResult.html" title="type pyo3::PyResult">PyResult</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/1.85.1/std/primitive.unit.html">()</a>&gt;</h4></section></summary><div class="docblock"><p>Lets the Python interpreter check and handle any pending signals. This will invoke the
corresponding signal handlers registered in Python (if any).</p>
<p>Returns <code>Err(</code><a href="../struct.PyErr.html" title="struct pyo3::PyErr"><code>PyErr</code></a><code>)</code> if any signal handler raises an exception.</p>
<p>These signals include <code>SIGINT</code> (normally raised by CTRL + C), which by default raises
<code>KeyboardInterrupt</code>. For this reason it is good practice to call this function regularly
as part of long-running Rust functions so that users can cancel it.</p>
<h5 id="example"><a class="doc-anchor" href="#example">§</a>Example</h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>pyo3::prelude::<span class="kw-2">*</span>;
<span class="attr">#[pyfunction]
</span><span class="kw">fn </span>loop_forever(py: Python&lt;<span class="lifetime">'_</span>&gt;) -&gt; PyResult&lt;()&gt; {
<span class="kw">loop </span>{
<span class="comment">// As this loop is infinite it should check for signals every once in a while.
// Using `?` causes any `PyErr` (potentially containing `KeyboardInterrupt`)
// to break out of the loop.
</span>py.check_signals()<span class="question-mark">?</span>;
<span class="comment">// do work here
</span>}
}</code></pre></div>
<h5 id="note"><a class="doc-anchor" href="#note">§</a>Note</h5>
<p>This function calls <a href="https://docs.python.org/3/c-api/exceptions.html?highlight=pyerr_checksignals#c.PyErr_CheckSignals"><code>PyErr_CheckSignals()</code></a> which in turn may call signal handlers.
As Pythons <a href="https://docs.python.org/3/library/signal.html"><code>signal</code></a> API allows users to define custom signal handlers, calling this
function allows arbitrary Python code inside signal handlers to run.</p>
<p>If the function is called from a non-main thread, or under a non-main Python interpreter,
it does nothing yet still returns <code>Ok(())</code>.</p>
</div></details></div></details><details class="toggle implementors-toggle" open><summary><section id="impl-Python%3C'unbound%3E" class="impl"><a class="src rightside" href="../../src/pyo3/marker.rs.html#842-863">Source</a><a href="#impl-Python%3C'unbound%3E" class="anchor">§</a><h3 class="code-header">impl&lt;'unbound&gt; <a class="struct" href="struct.Python.html" title="struct pyo3::marker::Python">Python</a>&lt;'unbound&gt;</h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.assume_gil_acquired" class="method"><a class="src rightside" href="../../src/pyo3/marker.rs.html#860-862">Source</a><h4 class="code-header">pub unsafe fn <a href="#method.assume_gil_acquired" class="fn">assume_gil_acquired</a>() -&gt; <a class="struct" href="struct.Python.html" title="struct pyo3::marker::Python">Python</a>&lt;'unbound&gt;</h4></section></summary><div class="docblock"><p>Unsafely creates a Python token with an unbounded lifetime.</p>
<p>Many of PyO3 APIs use <code>Python&lt;'_&gt;</code> as proof that the GIL is held, but this function can be
used to call them unsafely.</p>
<h5 id="safety-1"><a class="doc-anchor" href="#safety-1">§</a>Safety</h5>
<ul>
<li>This token and any borrowed Python references derived from it can only be safely used
whilst the currently executing thread is actually holding the GIL.</li>
<li>This function creates a token with an <em>unbounded</em> lifetime. Safe code can assume that
holding a <code>Python&lt;'py&gt;</code> token means the GIL is and stays acquired for the lifetime <code>'py</code>.
If you let it or borrowed Python references escape to safe code you are
responsible for bounding the lifetime <code>'unbound</code> appropriately. For more on unbounded
lifetimes, see the <a href="https://doc.rust-lang.org/nomicon/unbounded-lifetimes.html">nomicon</a>.</li>
</ul>
</div></details></div></details></div><h2 id="trait-implementations" class="section-header">Trait Implementations<a href="#trait-implementations" class="anchor">§</a></h2><div id="trait-implementations-list"><details class="toggle implementors-toggle" open><summary><section id="impl-Clone-for-Python%3C'py%3E" class="impl"><a class="src rightside" href="../../src/pyo3/marker.rs.html#358">Source</a><a href="#impl-Clone-for-Python%3C'py%3E" class="anchor">§</a><h3 class="code-header">impl&lt;'py&gt; <a class="trait" href="https://doc.rust-lang.org/1.85.1/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a> for <a class="struct" href="struct.Python.html" title="struct pyo3::marker::Python">Python</a>&lt;'py&gt;</h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.clone" class="method trait-impl"><a class="src rightside" href="../../src/pyo3/marker.rs.html#358">Source</a><a href="#method.clone" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.85.1/core/clone/trait.Clone.html#tymethod.clone" class="fn">clone</a>(&amp;self) -&gt; <a class="struct" href="struct.Python.html" title="struct pyo3::marker::Python">Python</a>&lt;'py&gt;</h4></section></summary><div class='docblock'>Returns a copy of the value. <a href="https://doc.rust-lang.org/1.85.1/core/clone/trait.Clone.html#tymethod.clone">Read more</a></div></details><details class="toggle method-toggle" open><summary><section id="method.clone_from" class="method trait-impl"><span class="rightside"><span class="since" title="Stable since Rust version 1.0.0">1.0.0</span> · <a class="src" href="https://doc.rust-lang.org/1.85.1/src/core/clone.rs.html#174">Source</a></span><a href="#method.clone_from" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.85.1/core/clone/trait.Clone.html#method.clone_from" class="fn">clone_from</a>(&amp;mut self, source: &amp;Self)</h4></section></summary><div class='docblock'>Performs copy-assignment from <code>source</code>. <a href="https://doc.rust-lang.org/1.85.1/core/clone/trait.Clone.html#method.clone_from">Read more</a></div></details></div></details><section id="impl-Copy-for-Python%3C'py%3E" class="impl"><a class="src rightside" href="../../src/pyo3/marker.rs.html#358">Source</a><a href="#impl-Copy-for-Python%3C'py%3E" class="anchor">§</a><h3 class="code-header">impl&lt;'py&gt; <a class="trait" href="https://doc.rust-lang.org/1.85.1/core/marker/trait.Copy.html" title="trait core::marker::Copy">Copy</a> for <a class="struct" href="struct.Python.html" title="struct pyo3::marker::Python">Python</a>&lt;'py&gt;</h3></section></div><h2 id="synthetic-implementations" class="section-header">Auto Trait Implementations<a href="#synthetic-implementations" class="anchor">§</a></h2><div id="synthetic-implementations-list"><section id="impl-Freeze-for-Python%3C'py%3E" class="impl"><a href="#impl-Freeze-for-Python%3C'py%3E" class="anchor">§</a><h3 class="code-header">impl&lt;'py&gt; <a class="trait" href="https://doc.rust-lang.org/1.85.1/core/marker/trait.Freeze.html" title="trait core::marker::Freeze">Freeze</a> for <a class="struct" href="struct.Python.html" title="struct pyo3::marker::Python">Python</a>&lt;'py&gt;</h3></section><section id="impl-RefUnwindSafe-for-Python%3C'py%3E" class="impl"><a href="#impl-RefUnwindSafe-for-Python%3C'py%3E" class="anchor">§</a><h3 class="code-header">impl&lt;'py&gt; <a class="trait" href="https://doc.rust-lang.org/1.85.1/core/panic/unwind_safe/trait.RefUnwindSafe.html" title="trait core::panic::unwind_safe::RefUnwindSafe">RefUnwindSafe</a> for <a class="struct" href="struct.Python.html" title="struct pyo3::marker::Python">Python</a>&lt;'py&gt;</h3></section><section id="impl-Send-for-Python%3C'py%3E" class="impl"><a href="#impl-Send-for-Python%3C'py%3E" class="anchor">§</a><h3 class="code-header">impl&lt;'py&gt; !<a class="trait" href="https://doc.rust-lang.org/1.85.1/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> for <a class="struct" href="struct.Python.html" title="struct pyo3::marker::Python">Python</a>&lt;'py&gt;</h3></section><section id="impl-Sync-for-Python%3C'py%3E" class="impl"><a href="#impl-Sync-for-Python%3C'py%3E" class="anchor">§</a><h3 class="code-header">impl&lt;'py&gt; !<a class="trait" href="https://doc.rust-lang.org/1.85.1/core/marker/trait.Sync.html" title="trait core::marker::Sync">Sync</a> for <a class="struct" href="struct.Python.html" title="struct pyo3::marker::Python">Python</a>&lt;'py&gt;</h3></section><section id="impl-Unpin-for-Python%3C'py%3E" class="impl"><a href="#impl-Unpin-for-Python%3C'py%3E" class="anchor">§</a><h3 class="code-header">impl&lt;'py&gt; <a class="trait" href="https://doc.rust-lang.org/1.85.1/core/marker/trait.Unpin.html" title="trait core::marker::Unpin">Unpin</a> for <a class="struct" href="struct.Python.html" title="struct pyo3::marker::Python">Python</a>&lt;'py&gt;</h3></section><section id="impl-UnwindSafe-for-Python%3C'py%3E" class="impl"><a href="#impl-UnwindSafe-for-Python%3C'py%3E" class="anchor">§</a><h3 class="code-header">impl&lt;'py&gt; <a class="trait" href="https://doc.rust-lang.org/1.85.1/core/panic/unwind_safe/trait.UnwindSafe.html" title="trait core::panic::unwind_safe::UnwindSafe">UnwindSafe</a> for <a class="struct" href="struct.Python.html" title="struct pyo3::marker::Python">Python</a>&lt;'py&gt;</h3></section></div><h2 id="blanket-implementations" class="section-header">Blanket Implementations<a href="#blanket-implementations" class="anchor">§</a></h2><div id="blanket-implementations-list"><details class="toggle implementors-toggle"><summary><section id="impl-Any-for-T" class="impl"><a class="src rightside" href="https://doc.rust-lang.org/1.85.1/src/core/any.rs.html#138">Source</a><a href="#impl-Any-for-T" class="anchor">§</a><h3 class="code-header">impl&lt;T&gt; <a class="trait" href="https://doc.rust-lang.org/1.85.1/core/any/trait.Any.html" title="trait core::any::Any">Any</a> for T<div class="where">where
T: 'static + ?<a class="trait" href="https://doc.rust-lang.org/1.85.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</div></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.type_id" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.85.1/src/core/any.rs.html#139">Source</a><a href="#method.type_id" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.85.1/core/any/trait.Any.html#tymethod.type_id" class="fn">type_id</a>(&amp;self) -&gt; <a class="struct" href="https://doc.rust-lang.org/1.85.1/core/any/struct.TypeId.html" title="struct core::any::TypeId">TypeId</a></h4></section></summary><div class='docblock'>Gets the <code>TypeId</code> of <code>self</code>. <a href="https://doc.rust-lang.org/1.85.1/core/any/trait.Any.html#tymethod.type_id">Read more</a></div></details></div></details><details class="toggle implementors-toggle"><summary><section id="impl-Borrow%3CT%3E-for-T" class="impl"><a class="src rightside" href="https://doc.rust-lang.org/1.85.1/src/core/borrow.rs.html#209">Source</a><a href="#impl-Borrow%3CT%3E-for-T" class="anchor">§</a><h3 class="code-header">impl&lt;T&gt; <a class="trait" href="https://doc.rust-lang.org/1.85.1/core/borrow/trait.Borrow.html" title="trait core::borrow::Borrow">Borrow</a>&lt;T&gt; for T<div class="where">where
T: ?<a class="trait" href="https://doc.rust-lang.org/1.85.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</div></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.borrow" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.85.1/src/core/borrow.rs.html#211">Source</a><a href="#method.borrow" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.85.1/core/borrow/trait.Borrow.html#tymethod.borrow" class="fn">borrow</a>(&amp;self) -&gt; <a class="primitive" href="https://doc.rust-lang.org/1.85.1/std/primitive.reference.html">&amp;T</a></h4></section></summary><div class='docblock'>Immutably borrows from an owned value. <a href="https://doc.rust-lang.org/1.85.1/core/borrow/trait.Borrow.html#tymethod.borrow">Read more</a></div></details></div></details><details class="toggle implementors-toggle"><summary><section id="impl-BorrowMut%3CT%3E-for-T" class="impl"><a class="src rightside" href="https://doc.rust-lang.org/1.85.1/src/core/borrow.rs.html#217">Source</a><a href="#impl-BorrowMut%3CT%3E-for-T" class="anchor">§</a><h3 class="code-header">impl&lt;T&gt; <a class="trait" href="https://doc.rust-lang.org/1.85.1/core/borrow/trait.BorrowMut.html" title="trait core::borrow::BorrowMut">BorrowMut</a>&lt;T&gt; for T<div class="where">where
T: ?<a class="trait" href="https://doc.rust-lang.org/1.85.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</div></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.borrow_mut" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.85.1/src/core/borrow.rs.html#218">Source</a><a href="#method.borrow_mut" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.85.1/core/borrow/trait.BorrowMut.html#tymethod.borrow_mut" class="fn">borrow_mut</a>(&amp;mut self) -&gt; <a class="primitive" href="https://doc.rust-lang.org/1.85.1/std/primitive.reference.html">&amp;mut T</a></h4></section></summary><div class='docblock'>Mutably borrows from an owned value. <a href="https://doc.rust-lang.org/1.85.1/core/borrow/trait.BorrowMut.html#tymethod.borrow_mut">Read more</a></div></details></div></details><details class="toggle implementors-toggle"><summary><section id="impl-CloneToUninit-for-T" class="impl"><a class="src rightside" href="https://doc.rust-lang.org/1.85.1/src/core/clone.rs.html#273">Source</a><a href="#impl-CloneToUninit-for-T" class="anchor">§</a><h3 class="code-header">impl&lt;T&gt; <a class="trait" href="https://doc.rust-lang.org/1.85.1/core/clone/trait.CloneToUninit.html" title="trait core::clone::CloneToUninit">CloneToUninit</a> for T<div class="where">where
T: <a class="trait" href="https://doc.rust-lang.org/1.85.1/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a>,</div></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.clone_to_uninit" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.85.1/src/core/clone.rs.html#275">Source</a><a href="#method.clone_to_uninit" class="anchor">§</a><h4 class="code-header">unsafe fn <a href="https://doc.rust-lang.org/1.85.1/core/clone/trait.CloneToUninit.html#tymethod.clone_to_uninit" class="fn">clone_to_uninit</a>(&amp;self, dst: <a class="primitive" href="https://doc.rust-lang.org/1.85.1/std/primitive.pointer.html">*mut </a><a class="primitive" href="https://doc.rust-lang.org/1.85.1/std/primitive.u8.html">u8</a>)</h4></section></summary><span class="item-info"><div class="stab unstable"><span class="emoji">🔬</span><span>This is a nightly-only experimental API. (<code>clone_to_uninit</code>)</span></div></span><div class='docblock'>Performs copy-assignment from <code>self</code> to <code>dst</code>. <a href="https://doc.rust-lang.org/1.85.1/core/clone/trait.CloneToUninit.html#tymethod.clone_to_uninit">Read more</a></div></details></div></details><details class="toggle implementors-toggle"><summary><section id="impl-From%3CT%3E-for-T" class="impl"><a class="src rightside" href="https://doc.rust-lang.org/1.85.1/src/core/convert/mod.rs.html#767">Source</a><a href="#impl-From%3CT%3E-for-T" class="anchor">§</a><h3 class="code-header">impl&lt;T&gt; <a class="trait" href="https://doc.rust-lang.org/1.85.1/core/convert/trait.From.html" title="trait core::convert::From">From</a>&lt;T&gt; for T</h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.from" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.85.1/src/core/convert/mod.rs.html#770">Source</a><a href="#method.from" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.85.1/core/convert/trait.From.html#tymethod.from" class="fn">from</a>(t: T) -&gt; T</h4></section></summary><div class="docblock"><p>Returns the argument unchanged.</p>
</div></details></div></details><details class="toggle implementors-toggle"><summary><section id="impl-Into%3CU%3E-for-T" class="impl"><a class="src rightside" href="https://doc.rust-lang.org/1.85.1/src/core/convert/mod.rs.html#750-752">Source</a><a href="#impl-Into%3CU%3E-for-T" class="anchor">§</a><h3 class="code-header">impl&lt;T, U&gt; <a class="trait" href="https://doc.rust-lang.org/1.85.1/core/convert/trait.Into.html" title="trait core::convert::Into">Into</a>&lt;U&gt; for T<div class="where">where
U: <a class="trait" href="https://doc.rust-lang.org/1.85.1/core/convert/trait.From.html" title="trait core::convert::From">From</a>&lt;T&gt;,</div></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.into" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.85.1/src/core/convert/mod.rs.html#760">Source</a><a href="#method.into" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.85.1/core/convert/trait.Into.html#tymethod.into" class="fn">into</a>(self) -&gt; U</h4></section></summary><div class="docblock"><p>Calls <code>U::from(self)</code>.</p>
<p>That is, this conversion is whatever the implementation of
<code><a href="https://doc.rust-lang.org/1.85.1/core/convert/trait.From.html" title="trait core::convert::From">From</a>&lt;T&gt; for U</code> chooses to do.</p>
</div></details></div></details><details class="toggle implementors-toggle"><summary><section id="impl-ToOwned-for-T" class="impl"><a class="src rightside" href="https://doc.rust-lang.org/1.85.1/src/alloc/borrow.rs.html#82-84">Source</a><a href="#impl-ToOwned-for-T" class="anchor">§</a><h3 class="code-header">impl&lt;T&gt; <a class="trait" href="https://doc.rust-lang.org/1.85.1/alloc/borrow/trait.ToOwned.html" title="trait alloc::borrow::ToOwned">ToOwned</a> for T<div class="where">where
T: <a class="trait" href="https://doc.rust-lang.org/1.85.1/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a>,</div></h3></section></summary><div class="impl-items"><details class="toggle" open><summary><section id="associatedtype.Owned" class="associatedtype trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.85.1/src/alloc/borrow.rs.html#86">Source</a><a href="#associatedtype.Owned" class="anchor">§</a><h4 class="code-header">type <a href="https://doc.rust-lang.org/1.85.1/alloc/borrow/trait.ToOwned.html#associatedtype.Owned" class="associatedtype">Owned</a> = T</h4></section></summary><div class='docblock'>The resulting type after obtaining ownership.</div></details><details class="toggle method-toggle" open><summary><section id="method.to_owned" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.85.1/src/alloc/borrow.rs.html#87">Source</a><a href="#method.to_owned" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.85.1/alloc/borrow/trait.ToOwned.html#tymethod.to_owned" class="fn">to_owned</a>(&amp;self) -&gt; T</h4></section></summary><div class='docblock'>Creates owned data from borrowed data, usually by cloning. <a href="https://doc.rust-lang.org/1.85.1/alloc/borrow/trait.ToOwned.html#tymethod.to_owned">Read more</a></div></details><details class="toggle method-toggle" open><summary><section id="method.clone_into" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.85.1/src/alloc/borrow.rs.html#91">Source</a><a href="#method.clone_into" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.85.1/alloc/borrow/trait.ToOwned.html#method.clone_into" class="fn">clone_into</a>(&amp;self, target: <a class="primitive" href="https://doc.rust-lang.org/1.85.1/std/primitive.reference.html">&amp;mut T</a>)</h4></section></summary><div class='docblock'>Uses borrowed data to replace owned data, usually by cloning. <a href="https://doc.rust-lang.org/1.85.1/alloc/borrow/trait.ToOwned.html#method.clone_into">Read more</a></div></details></div></details><details class="toggle implementors-toggle"><summary><section id="impl-TryFrom%3CU%3E-for-T" class="impl"><a class="src rightside" href="https://doc.rust-lang.org/1.85.1/src/core/convert/mod.rs.html#807-809">Source</a><a href="#impl-TryFrom%3CU%3E-for-T" class="anchor">§</a><h3 class="code-header">impl&lt;T, U&gt; <a class="trait" href="https://doc.rust-lang.org/1.85.1/core/convert/trait.TryFrom.html" title="trait core::convert::TryFrom">TryFrom</a>&lt;U&gt; for T<div class="where">where
U: <a class="trait" href="https://doc.rust-lang.org/1.85.1/core/convert/trait.Into.html" title="trait core::convert::Into">Into</a>&lt;T&gt;,</div></h3></section></summary><div class="impl-items"><details class="toggle" open><summary><section id="associatedtype.Error-1" class="associatedtype trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.85.1/src/core/convert/mod.rs.html#811">Source</a><a href="#associatedtype.Error-1" class="anchor">§</a><h4 class="code-header">type <a href="https://doc.rust-lang.org/1.85.1/core/convert/trait.TryFrom.html#associatedtype.Error" class="associatedtype">Error</a> = <a class="enum" href="https://doc.rust-lang.org/1.85.1/core/convert/enum.Infallible.html" title="enum core::convert::Infallible">Infallible</a></h4></section></summary><div class='docblock'>The type returned in the event of a conversion error.</div></details><details class="toggle method-toggle" open><summary><section id="method.try_from" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.85.1/src/core/convert/mod.rs.html#814">Source</a><a href="#method.try_from" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.85.1/core/convert/trait.TryFrom.html#tymethod.try_from" class="fn">try_from</a>(value: U) -&gt; <a class="enum" href="https://doc.rust-lang.org/1.85.1/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;T, &lt;T as <a class="trait" href="https://doc.rust-lang.org/1.85.1/core/convert/trait.TryFrom.html" title="trait core::convert::TryFrom">TryFrom</a>&lt;U&gt;&gt;::<a class="associatedtype" href="https://doc.rust-lang.org/1.85.1/core/convert/trait.TryFrom.html#associatedtype.Error" title="type core::convert::TryFrom::Error">Error</a>&gt;</h4></section></summary><div class='docblock'>Performs the conversion.</div></details></div></details><details class="toggle implementors-toggle"><summary><section id="impl-TryInto%3CU%3E-for-T" class="impl"><a class="src rightside" href="https://doc.rust-lang.org/1.85.1/src/core/convert/mod.rs.html#792-794">Source</a><a href="#impl-TryInto%3CU%3E-for-T" class="anchor">§</a><h3 class="code-header">impl&lt;T, U&gt; <a class="trait" href="https://doc.rust-lang.org/1.85.1/core/convert/trait.TryInto.html" title="trait core::convert::TryInto">TryInto</a>&lt;U&gt; for T<div class="where">where
U: <a class="trait" href="https://doc.rust-lang.org/1.85.1/core/convert/trait.TryFrom.html" title="trait core::convert::TryFrom">TryFrom</a>&lt;T&gt;,</div></h3></section></summary><div class="impl-items"><details class="toggle" open><summary><section id="associatedtype.Error" class="associatedtype trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.85.1/src/core/convert/mod.rs.html#796">Source</a><a href="#associatedtype.Error" class="anchor">§</a><h4 class="code-header">type <a href="https://doc.rust-lang.org/1.85.1/core/convert/trait.TryInto.html#associatedtype.Error" class="associatedtype">Error</a> = &lt;U as <a class="trait" href="https://doc.rust-lang.org/1.85.1/core/convert/trait.TryFrom.html" title="trait core::convert::TryFrom">TryFrom</a>&lt;T&gt;&gt;::<a class="associatedtype" href="https://doc.rust-lang.org/1.85.1/core/convert/trait.TryFrom.html#associatedtype.Error" title="type core::convert::TryFrom::Error">Error</a></h4></section></summary><div class='docblock'>The type returned in the event of a conversion error.</div></details><details class="toggle method-toggle" open><summary><section id="method.try_into" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.85.1/src/core/convert/mod.rs.html#799">Source</a><a href="#method.try_into" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.85.1/core/convert/trait.TryInto.html#tymethod.try_into" class="fn">try_into</a>(self) -&gt; <a class="enum" href="https://doc.rust-lang.org/1.85.1/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;U, &lt;U as <a class="trait" href="https://doc.rust-lang.org/1.85.1/core/convert/trait.TryFrom.html" title="trait core::convert::TryFrom">TryFrom</a>&lt;T&gt;&gt;::<a class="associatedtype" href="https://doc.rust-lang.org/1.85.1/core/convert/trait.TryFrom.html#associatedtype.Error" title="type core::convert::TryFrom::Error">Error</a>&gt;</h4></section></summary><div class='docblock'>Performs the conversion.</div></details></div></details></div></section></div></main></body></html>