Skip to content

Commit

Permalink
Merge pull request #45 from a1phyr/pyo3_0.21
Browse files Browse the repository at this point in the history
Update to pyo3 0.21
  • Loading branch information
vorner authored Apr 21, 2024
2 parents 7081938 + 76ff388 commit 36254e2
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ jobs:
uses: actions-rs/toolchain@v1
with:
# MSRV as set in Cargo.toml
toolchain: 1.48.0
toolchain: 1.56.0
default: true
profile: minimal

Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.10.0

* Update to pyo3 0.21.

# 0.9.0

* Bump lowest allowed version of pyo3 to 0.15 ‒ this prevents linking multiple
Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ keywords = ["pyo3", "python", "logging"]
categories = ["development-tools::debugging"]
edition = "2018"
license = "Apache-2.0 OR MIT"
rust-version = "1.48.0"
rust-version = "1.56.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
arc-swap = "~1"
# It's OK to ask for std on log, because pyo3 needs it too.
log = { version = "~0.4.4", default-features = false, features = ["std"] }
pyo3 = { version = ">=0.15, <0.21", default-features = false }
pyo3 = { version = ">=0.21, <0.22", default-features = false }

[dev-dependencies]
pyo3 = { version = ">=0.15, <0.21", default-features = false, features = ["auto-initialize", "macros"] }
pyo3 = { version = ">=0.21, <0.22", default-features = false, features = ["auto-initialize", "macros"] }

# `pyo3-macros` is lying about the minimal version for its `syn` dependency.
# Because we're testing with `-Zminimal-versions`, we need to explicitly set it here.
Expand Down
30 changes: 14 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#![doc(
html_root_url = "https://docs.rs/pyo3-log/0.2.1/pyo3-log/",
test(attr(deny(warnings))),
test(attr(allow(unknown_lints, non_local_definitions))),
test(attr(allow(unknown_lints, non_local_definitions)))
)]
#![warn(missing_docs)]

Expand Down Expand Up @@ -38,7 +38,7 @@
//! }
//!
//! #[pymodule]
//! fn my_module(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
//! fn my_module(m: Bound<'_, PyModule>) -> PyResult<()> {
//! pyo3_log::init();
//!
//! m.add_wrapped(wrap_pyfunction!(log_something))?;
Expand Down Expand Up @@ -286,7 +286,7 @@ impl Logger {
///
/// It defaults to having a filter for [`Debug`][LevelFilter::Debug].
pub fn new(py: Python<'_>, caching: Caching) -> PyResult<Self> {
let logging = py.import("logging")?;
let logging = py.import_bound("logging")?;
Ok(Self {
top_filter: LevelFilter::Debug,
filters: HashMap::new(),
Expand Down Expand Up @@ -400,18 +400,18 @@ impl Logger {
.and_then(|node| node.local.as_ref())
.map(|local| &local.logger);
let (logger, cached) = match cached_logger {
Some(cached) => (cached.as_ref(py), true),
Some(cached) => (cached.bind(py).clone(), true),
None => (
self.logging
.as_ref(py)
.bind(py)
.getattr("getLogger")?
.call1((&target,))?,
false,
),
};
// We need to check for this ourselves. For some reason, the logger.handle does not check
// it. And besides, we can save ourselves few python calls if it's turned off.
if is_enabled_for(logger, record.level())? {
if is_enabled_for(&logger, record.level())? {
let none = py.None();
// TODO: kv pairs, if enabled as a feature?
let record = logger.call_method1(
Expand All @@ -422,8 +422,8 @@ impl Logger {
record.file(),
record.line().unwrap_or_default(),
msg,
PyTuple::empty(py), // args
&none, // exc_info
PyTuple::empty_bound(py), // args
&none, // exc_info
),
)?;
logger.call_method1("handle", (record,))?;
Expand Down Expand Up @@ -504,12 +504,11 @@ impl Log for Logger {
let filter = match self.caching {
Caching::Nothing => unreachable!(),
Caching::Loggers => LevelFilter::max(),
Caching::LoggersAndLevels => {
extract_max_level(py, &logger).unwrap_or_else(|e| {
Caching::LoggersAndLevels => extract_max_level(logger.bind(py))
.unwrap_or_else(|e| {
e.print(py);
LevelFilter::max()
})
}
}),
};
store_to_cache = Some((logger, filter));
}
Expand Down Expand Up @@ -538,14 +537,13 @@ fn map_level(level: Level) -> usize {
}
}

fn is_enabled_for(logger: &PyAny, level: Level) -> PyResult<bool> {
fn is_enabled_for(logger: &Bound<'_, PyAny>, level: Level) -> PyResult<bool> {
let level = map_level(level);
logger.call_method1("isEnabledFor", (level,))?.is_true()
logger.call_method1("isEnabledFor", (level,))?.is_truthy()
}

fn extract_max_level(py: Python<'_>, logger: &PyObject) -> PyResult<LevelFilter> {
fn extract_max_level(logger: &Bound<'_, PyAny>) -> PyResult<LevelFilter> {
use Level::*;
let logger = logger.as_ref(py);
for l in &[Trace, Debug, Info, Warn, Error] {
if is_enabled_for(logger, *l)? {
return Ok(l.to_level_filter());
Expand Down

0 comments on commit 36254e2

Please sign in to comment.