Skip to content

Commit 3193163

Browse files
committed
faster bool extract
1 parent fb694d0 commit 3193163

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

src/input/input_python.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,16 +244,16 @@ impl<'a> Input<'a> for PyAny {
244244
}
245245

246246
fn strict_bool(&self) -> ValResult<bool> {
247-
if let Ok(bool) = self.extract::<bool>() {
248-
Ok(bool)
247+
if let Ok(bool) = self.downcast::<PyBool>() {
248+
Ok(bool.is_true())
249249
} else {
250250
Err(ValError::new(ErrorType::BoolType, self))
251251
}
252252
}
253253

254254
fn lax_bool(&self) -> ValResult<bool> {
255-
if let Ok(bool) = self.extract::<bool>() {
256-
Ok(bool)
255+
if let Ok(bool) = self.downcast::<PyBool>() {
256+
Ok(bool.is_true())
257257
} else if let Some(cow_str) = maybe_as_string(self, ErrorType::BoolParsing)? {
258258
str_as_bool(self, &cow_str)
259259
} else if let Ok(int) = extract_i64(self) {

src/serializers/type_serializers/literal.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use std::borrow::Cow;
22

3-
use pyo3::intern;
43
use pyo3::prelude::*;
5-
use pyo3::types::{PyDict, PyList, PyString};
4+
use pyo3::types::{PyBool, PyDict, PyList, PyString};
5+
use pyo3::{intern, PyTypeInfo};
66

77
use ahash::AHashSet;
88
use serde::Serialize;
@@ -76,7 +76,7 @@ enum OutputValue<'a> {
7676
impl LiteralSerializer {
7777
fn check<'a>(&self, value: &'a PyAny, extra: &Extra) -> PyResult<OutputValue<'a>> {
7878
if extra.check.enabled() {
79-
if !self.expected_int.is_empty() && value.extract::<bool>().is_err() {
79+
if !self.expected_int.is_empty() && !PyBool::is_type_of(value) {
8080
if let Ok(int) = extract_i64(value) {
8181
if self.expected_int.contains(&int) {
8282
return Ok(OutputValue::OkInt(int));

0 commit comments

Comments
 (0)