diff options
author | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2024-11-16 14:16:39 +0100 |
---|---|---|
committer | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2024-11-16 14:16:39 +0100 |
commit | b17d7fe27a68c0ea93fb0d6e5b81fe3c166dcee0 (patch) | |
tree | 855f6d1f96908ba210043cd0c069c2aa8941adae /crates/yt_dlp/src | |
parent | fix(yt/select/add): Avoid crash on adding a video (diff) | |
download | yt-b17d7fe27a68c0ea93fb0d6e5b81fe3c166dcee0.tar.gz yt-b17d7fe27a68c0ea93fb0d6e5b81fe3c166dcee0.zip |
build(treewide): Update dependencies
Diffstat (limited to 'crates/yt_dlp/src')
-rw-r--r-- | crates/yt_dlp/src/lib.rs | 26 | ||||
-rw-r--r-- | crates/yt_dlp/src/logging.rs | 16 |
2 files changed, 24 insertions, 18 deletions
diff --git a/crates/yt_dlp/src/lib.rs b/crates/yt_dlp/src/lib.rs index 980d807..970bfe2 100644 --- a/crates/yt_dlp/src/lib.rs +++ b/crates/yt_dlp/src/lib.rs @@ -26,7 +26,7 @@ use pyo3::types::{PyString, PyTuple, PyTupleMethods}; use pyo3::{ pyfunction, types::{PyAnyMethods, PyDict, PyDictMethods, PyList, PyListMethods, PyModule}, - wrap_pyfunction_bound, Bound, PyAny, PyResult, Python, + wrap_pyfunction, Bound, PyAny, PyResult, Python, }; use serde::Serialize; use serde_json::{Map, Value}; @@ -53,7 +53,7 @@ pub fn add_logger_and_sig_handler<'a>( ) -> PyResult<Bound<'a, PyDict>> { setup_logging(py, "yt_dlp")?; - let logging = PyModule::import_bound(py, "logging")?; + let logging = PyModule::import(py, "logging")?; let ytdl_logger = logging.call_method1("getLogger", ("yt_dlp",))?; // Ensure that all events are logged by setting the log level to NOTSET (we filter on rust's side) @@ -62,8 +62,8 @@ pub fn add_logger_and_sig_handler<'a>( // Disable the SIGINT (Ctrl+C) handler, python installs. // This allows the user to actually stop the application with Ctrl+C. // This is here because it can only be run in the main thread and this was here already. - py.run_bound( - "\ + py.run( + c"\ import signal signal.signal(signal.SIGINT, signal.SIG_DFL)", None, @@ -71,7 +71,7 @@ signal.signal(signal.SIGINT, signal.SIG_DFL)", ) .expect("This code should always work"); - let config_opts = PyDict::new_bound(py); + let config_opts = PyDict::new(py); config_opts .set_item("level", 0) .expect("Setting this item should always work"); @@ -285,12 +285,12 @@ pub fn progress_hook(py: Python<'_>, input: &Bound<'_, PyDict>) -> PyResult<()> pub fn add_hooks<'a>(opts: Bound<'a, PyDict>, py: Python<'_>) -> PyResult<Bound<'a, PyDict>> { if let Some(hooks) = opts.get_item("progress_hooks")? { let hooks = hooks.downcast::<PyList>()?; - hooks.append(wrap_pyfunction_bound!(progress_hook, py)?)?; + hooks.append(wrap_pyfunction!(progress_hook, py)?)?; opts.set_item("progress_hooks", hooks)?; } else { // No hooks are set yet - let hooks_list = PyList::new_bound(py, &[wrap_pyfunction_bound!(progress_hook, py)?]); + let hooks_list = PyList::new(py, &[wrap_pyfunction!(progress_hook, py)?])?; opts.set_item("progress_hooks", hooks_list)?; } @@ -327,7 +327,7 @@ pub async fn extract_info( let instance = get_yt_dlp(py, opts)?; let args = (url.as_str(),); - let kwargs = PyDict::new_bound(py); + let kwargs = PyDict::new(py); kwargs.set_item("download", download)?; kwargs.set_item("process", process)?; @@ -417,10 +417,10 @@ fn json_map_to_py_dict<'a>( fn json_dumps(py: Python<'_>, input: Bound<'_, PyAny>) -> PyResult<String> { // json.dumps(yt_dlp.sanitize_info(input)) - let yt_dlp = get_yt_dlp(py, PyDict::new_bound(py))?; + let yt_dlp = get_yt_dlp(py, PyDict::new(py))?; let sanitized_result = yt_dlp.call_method1("sanitize_info", (input,))?; - let json = PyModule::import_bound(py, "json")?; + let json = PyModule::import(py, "json")?; let dumps = json.getattr("dumps")?; let output = dumps.call1((sanitized_result,))?; @@ -439,7 +439,7 @@ fn json_loads_str<T: Serialize>(py: Python<'_>, input: T) -> PyResult<Bound<'_, fn json_loads(py: Python<'_>, input: String) -> PyResult<Bound<'_, PyDict>> { // json.loads(input) - let json = PyModule::import_bound(py, "json")?; + let json = PyModule::import(py, "json")?; let dumps = json.getattr("loads")?; let output = dumps.call1((input,))?; @@ -451,7 +451,7 @@ fn json_loads(py: Python<'_>, input: String) -> PyResult<Bound<'_, PyDict>> { } fn get_yt_dlp_utils(py: Python<'_>) -> PyResult<Bound<'_, PyAny>> { - let yt_dlp = PyModule::import_bound(py, "yt_dlp")?; + let yt_dlp = PyModule::import(py, "yt_dlp")?; let utils = yt_dlp.getattr("utils")?; Ok(utils) @@ -461,7 +461,7 @@ fn get_yt_dlp<'a>(py: Python<'a>, opts: Bound<'a, PyDict>) -> PyResult<Bound<'a, let opts = add_logger_and_sig_handler(opts, py)?; let opts = add_hooks(opts, py)?; - let yt_dlp = PyModule::import_bound(py, "yt_dlp")?; + let yt_dlp = PyModule::import(py, "yt_dlp")?; let youtube_dl = yt_dlp.call_method1("YoutubeDL", (opts,))?; Ok(youtube_dl) diff --git a/crates/yt_dlp/src/logging.rs b/crates/yt_dlp/src/logging.rs index 385255d..670fc1c 100644 --- a/crates/yt_dlp/src/logging.rs +++ b/crates/yt_dlp/src/logging.rs @@ -15,6 +15,8 @@ // The pyo3 `pyfunction` proc-macros call unsafe functions internally, which trigger this lint. #![allow(unsafe_op_in_unsafe_fn)] +use std::ffi::CString; + use log::{logger, Level, MetadataBuilder, Record}; use pyo3::{ prelude::{PyAnyMethods, PyListMethods, PyModuleMethods}, @@ -91,14 +93,17 @@ fn host_log(record: Bound<'_, PyAny>, rust_target: &str) -> PyResult<()> { /// Registers the `host_log` function in rust as the event handler for Python's logging logger /// This function needs to be called from within a pyo3 context as early as possible to ensure logging messages /// arrive to the rust consumer. +/// +/// # Panics +/// Only if internal assertions fail. #[allow(clippy::module_name_repetitions)] pub fn setup_logging(py: Python<'_>, target: &str) -> PyResult<()> { - let logging = py.import_bound("logging")?; + let logging = py.import("logging")?; logging.setattr("host_log", wrap_pyfunction!(host_log, &logging)?)?; - py.run_bound( - format!( + py.run( + CString::new(format!( r#" class HostHandler(Handler): def __init__(self, level=0): @@ -113,8 +118,9 @@ def basicConfig(*pargs, **kwargs): kwargs["handlers"] = [HostHandler()] return oldBasicConfig(*pargs, **kwargs) "# - ) - .as_str(), + )) + .expect("This is hardcoded") + .as_c_str(), Some(&logging.dict()), None, )?; |