Skip to content

Commit dc6a621

Browse files
author
Fahad Zubair
committed
Upgrade pyo3, add .expect to dictionary access
1 parent 3c7b911 commit dc6a621

File tree

10 files changed

+60
-19
lines changed

10 files changed

+60
-19
lines changed

rust-runtime/aws-smithy-http-server-python/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ lambda_http = { version = "0.8.3" }
2929
num_cpus = "1.13.1"
3030
parking_lot = "0.12.1"
3131
pin-project-lite = "0.2.14"
32-
pyo3 = "0.18.2"
33-
pyo3-asyncio = { version = "0.18.0", features = ["tokio-runtime"] }
32+
pyo3 = "0.20"
33+
pyo3-asyncio = { version = "0.20.0", features = ["tokio-runtime"] }
3434
signal-hook = { version = "0.3.14", features = ["extended-siginfo"] }
3535
socket2 = { version = "0.5.5", features = ["all"] }
3636
thiserror = "1.0.40"
@@ -46,7 +46,7 @@ pretty_assertions = "1"
4646
futures-util = { version = "0.3.29", default-features = false }
4747
tower-test = "0.4"
4848
tokio-test = "0.4"
49-
pyo3-asyncio = { version = "0.18.0", features = ["testing", "attributes", "tokio-runtime", "unstable-streams"] }
49+
pyo3-asyncio = { version = "0.20.0", features = ["testing", "attributes", "tokio-runtime", "unstable-streams"] }
5050
rcgen = "0.10.0"
5151
hyper-rustls = { version = "0.24", features = ["http2"] }
5252

rust-runtime/aws-smithy-http-server-python/src/context/layer.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,16 @@ counter = ctx.counter
107107
.unwrap();
108108

109109
(
110-
locals.get_item("req_id").unwrap().to_string(),
111-
locals.get_item("counter").unwrap().to_string(),
110+
locals
111+
.get_item("req_id")
112+
.expect("Python exception occurred during dictionary lookup")
113+
.unwrap()
114+
.to_string(),
115+
locals
116+
.get_item("counter")
117+
.expect("Python exception occurred during dictionary lookup")
118+
.unwrap()
119+
.to_string(),
112120
)
113121
});
114122
Ok::<_, Infallible>(Response::new((req_id, counter)))

rust-runtime/aws-smithy-http-server-python/src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ impl From<PyError> for PyErr {
4444
/// :param status_code typing.Optional\[int\]:
4545
/// :rtype None:
4646
#[pyclass(name = "MiddlewareException", extends = BasePyException)]
47-
#[pyo3(text_signature = "($self, message, status_code=None)")]
4847
#[derive(Debug, Clone)]
4948
pub struct PyMiddlewareException {
5049
/// :type str:
@@ -59,6 +58,7 @@ pub struct PyMiddlewareException {
5958
#[pymethods]
6059
impl PyMiddlewareException {
6160
/// Create a new [PyMiddlewareException].
61+
#[pyo3(text_signature = "($self, message, status_code=None)")]
6262
#[new]
6363
fn newpy(message: String, status_code: Option<u16>) -> Self {
6464
Self {

rust-runtime/aws-smithy-http-server-python/src/logging.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,14 @@ fn setup_tracing_subscriber(
127127
/// :param format typing.Optional\[typing.Literal\['compact', 'pretty', 'json'\]\]:
128128
/// :rtype None:
129129
#[pyclass(name = "TracingHandler")]
130-
#[pyo3(text_signature = "($self, level=None, logfile=None, format=None)")]
131130
#[derive(Debug)]
132131
pub struct PyTracingHandler {
133132
_guard: Option<WorkerGuard>,
134133
}
135134

136135
#[pymethods]
137136
impl PyTracingHandler {
137+
#[pyo3(text_signature = "($self, level=None, logfile=None, format=None)")]
138138
#[new]
139139
fn newpy(
140140
py: Python,

rust-runtime/aws-smithy-http-server-python/src/middleware/pytests/layer.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ fn py_handler(code: &str) -> PyMiddlewareHandler {
321321
py.run(code, Some(globals), Some(locals))?;
322322
let handler = locals
323323
.get_item("middleware")
324+
.expect("Python exception occurred during dictionary lookup")
324325
.expect("your handler must be named `middleware`")
325326
.into();
326327
PyMiddlewareHandler::new(py, handler)

rust-runtime/aws-smithy-http-server-python/src/middleware/pytests/response.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ response = Response(200, {"Content-Type": "application/json"}, b"hello world")
2727
)
2828
.unwrap();
2929

30-
let py_response: Py<PyResponse> = locals.get_item("response").unwrap().extract().unwrap();
30+
let py_response: Py<PyResponse> = locals
31+
.get_item("response")
32+
.expect("Python exception occurred during dictionary lookup")
33+
.unwrap()
34+
.extract()
35+
.unwrap();
3136
let response = py_response.borrow_mut(py).take_inner();
3237
response.unwrap()
3338
});

rust-runtime/aws-smithy-http-server-python/src/middleware/response.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ use super::{PyHeaderMap, PyMiddlewareError};
2323
/// :param body typing.Optional[bytes]:
2424
/// :rtype None:
2525
#[pyclass(name = "Response")]
26-
#[pyo3(text_signature = "($self, status, headers=None, body=None)")]
2726
pub struct PyResponse {
2827
parts: Option<Parts>,
2928
headers: PyHeaderMap,
@@ -61,6 +60,7 @@ impl PyResponse {
6160
#[pymethods]
6261
impl PyResponse {
6362
/// Python-compatible [Response] object from the Python side.
63+
#[pyo3(text_signature = "($self, status, headers=None, body=None)")]
6464
#[new]
6565
fn newpy(
6666
status: u16,

rust-runtime/aws-smithy-http-server-python/src/socket.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use std::net::SocketAddr;
2525
/// :param port int:
2626
/// :param backlog typing.Optional\[int\]:
2727
/// :rtype None:
28-
#[pyclass(text_signature = "($self, address, port, backlog=None)")]
28+
#[pyclass]
2929
#[derive(Debug)]
3030
pub struct PySocket {
3131
pub(crate) inner: Socket,
@@ -35,6 +35,7 @@ pub struct PySocket {
3535
impl PySocket {
3636
/// Create a new UNIX `SharedSocket` from an address, port and backlog.
3737
/// If not specified, the backlog defaults to 1024 connections.
38+
#[pyo3(text_signature = "($self, address, port, backlog=None)")]
3839
#[new]
3940
pub fn new(address: String, port: i32, backlog: Option<i32>) -> PyResult<Self> {
4041
let address: SocketAddr = format!("{}:{}", address, port).parse()?;

rust-runtime/aws-smithy-http-server-python/src/tls.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@ pub mod listener;
2525
/// :param cert_path pathlib.Path:
2626
/// :param reload_secs int:
2727
/// :rtype None:
28-
#[pyclass(
29-
name = "TlsConfig",
30-
text_signature = "($self, *, key_path, cert_path, reload_secs=86400)"
31-
)]
28+
#[pyclass(name = "TlsConfig")]
3229
#[derive(Clone)]
3330
pub struct PyTlsConfig {
3431
/// Absolute path of the RSA or PKCS private key.
@@ -105,6 +102,7 @@ impl PyTlsConfig {
105102
#[pymethods]
106103
impl PyTlsConfig {
107104
#[new]
105+
#[pyo3(text_signature = "($self, *, key_path, cert_path, reload_secs=86400)")]
108106
#[pyo3(signature = (key_path, cert_path, reload_secs=86400))]
109107
fn py_new(key_path: PathBuf, cert_path: PathBuf, reload_secs: u64) -> Self {
110108
// TODO(BugOnUpstream): `reload: &PyDelta` segfaults, create an issue on PyO3
@@ -172,7 +170,11 @@ config = TlsConfig(key_path=TEST_KEY, cert_path=TEST_CERT, reload_secs=1000)
172170
Some(globals),
173171
Some(locals),
174172
)?;
175-
locals.get_item("config").unwrap().extract::<PyTlsConfig>()
173+
locals
174+
.get_item("config")
175+
.expect("Python exception occurred during dictionary lookup")
176+
.unwrap()
177+
.extract::<PyTlsConfig>()
176178
})?;
177179

178180
assert_eq!(PathBuf::from_str(TEST_KEY).unwrap(), config.key_path);

rust-runtime/aws-smithy-http-server-python/src/util.rs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,19 +158,43 @@ class Types:
158158

159159
assert_eq!(
160160
true,
161-
is_optional_of::<PyString>(py, type_hints.get_item("opt_of_str").unwrap())?
161+
is_optional_of::<PyString>(
162+
py,
163+
type_hints
164+
.get_item("opt_of_str")
165+
.expect("Python exception occurred during dictionary lookup")
166+
.unwrap()
167+
)?
162168
);
163169
assert_eq!(
164170
false,
165-
is_optional_of::<PyString>(py, type_hints.get_item("regular_str").unwrap())?
171+
is_optional_of::<PyString>(
172+
py,
173+
type_hints
174+
.get_item("regular_str")
175+
.expect("Python exception occurred during dictionary lookup")
176+
.unwrap()
177+
)?
166178
);
167179
assert_eq!(
168180
true,
169-
is_optional_of::<PyBool>(py, type_hints.get_item("opt_of_bool").unwrap())?
181+
is_optional_of::<PyBool>(
182+
py,
183+
type_hints
184+
.get_item("opt_of_bool")
185+
.expect("Python exception occurred during dictionary lookup")
186+
.unwrap()
187+
)?
170188
);
171189
assert_eq!(
172190
false,
173-
is_optional_of::<PyString>(py, type_hints.get_item("opt_of_bool").unwrap())?
191+
is_optional_of::<PyString>(
192+
py,
193+
type_hints
194+
.get_item("opt_of_bool")
195+
.expect("Python exception occurred during dictionary lookup")
196+
.unwrap()
197+
)?
174198
);
175199

176200
Ok(())

0 commit comments

Comments
 (0)