From the course: Using Rust with Python
Unlock this course with a free trial
Join today to access over 23,200 courses taught by industry experts.
PyO3 exceptions - Rust Tutorial
From the course: Using Rust with Python
PyO3 exceptions
- [Instructor] Python exception handling in Rust is an interesting problem to deal with because of the fact that you are able to get a different type of response in Rust, right, where you have errors, but in Python you have exceptions. So how do you do this? Well, first up, in raising exceptions here, you can see here that you can use a new PyErr instance or PyErr from type to create a Python exception, and then PyErr raise to then raise it in Python. On the other hand, to handle the exceptions, you can use .MapError to move a Rust result error into a Python exception, or use Py function attribute and return Py result. And you can see this in this function from Rust below. So let's go ahead and take a look at how this works in action here with some real code. I have a project here called rust-exceptions, and inside of this rust-exceptions here I have some Python code. But first, let's look at the lib here. So we are using PyO3, and I'm using PyO3 exceptions here, and I have a…
Contents
-
-
Introduction: Instructor45s
-
Introduction: Rust and Python5m
-
PyO3 installation4m 15s
-
(Locked)
Basic Rust library4m 46s
-
(Locked)
Rust to Python3m 33s
-
(Locked)
Rust ownership model4m 59s
-
(Locked)
Diagram of a PyO3 project3m 40s
-
(Locked)
Python calculator CLI3m 54s
-
(Locked)
PyO3 features2m 38s
-
(Locked)
PyO3 exceptions4m 21s
-
(Locked)
Call Python from Rust5m 53s
-
(Locked)
Run Python with embedded Rust4m 47s
-
(Locked)
Embedded Rust CLI diagram3m 34s
-
(Locked)
Embedded Rust CLI4m 31s
-
(Locked)
Embedded Rust CLI test4m 59s
-
(Locked)
Rust-built Python tools3m 52s
-
(Locked)
Using the Rust Ruff linter5m 15s
-
(Locked)
Using Polars in Python with Rust4m 9s
-
(Locked)
Polars CLI in Rust4m
-
(Locked)
Polars CLI test in Rust4m 21s
-
(Locked)
Polars CLI integration test3m 29s
-
(Locked)
Polars criterion benchmarking5m 39s
-