Skip to content

Commit

Permalink
C APIとPython APIの不必要なUTF-8の要求を外す
Browse files Browse the repository at this point in the history
  • Loading branch information
qryxip committed Feb 23, 2024
1 parent f9d956d commit 92c4a36
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
6 changes: 4 additions & 2 deletions crates/voicevox_core_c_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,10 @@ pub unsafe extern "C" fn voicevox_synthesizer_is_loaded_voice_model(
model_id: VoicevoxVoiceModelId,
) -> bool {
init_logger_once();
// FIXME: 不正なUTF-8文字列に対し、正式なエラーとするか黙って`false`を返す
let raw_model_id = ensure_utf8(unsafe { CStr::from_ptr(model_id) }).unwrap();
let Ok(raw_model_id) = ensure_utf8(unsafe { CStr::from_ptr(model_id) }) else {
// 与えられたIDがUTF-8ではない場合、それに対応する`VoicdModel`は確実に存在しない
return false;
};
synthesizer
.synthesizer()
.is_loaded_voice_model(&VoiceModelId::new(raw_model_id.into()))
Expand Down
32 changes: 18 additions & 14 deletions crates/voicevox_core_python_api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{marker::PhantomData, sync::Arc};
use std::{marker::PhantomData, path::PathBuf, sync::Arc};

mod convert;
use self::convert::{
Expand All @@ -13,7 +13,7 @@ use pyo3::{
create_exception,
exceptions::{PyException, PyKeyError, PyValueError},
pyclass, pyfunction, pymethods, pymodule,
types::{IntoPyDict as _, PyBytes, PyDict, PyList, PyModule},
types::{IntoPyDict as _, PyBytes, PyDict, PyList, PyModule, PyString},
wrap_pyfunction, PyAny, PyObject, PyRef, PyResult, PyTypeInfo, Python, ToPyObject,
};
use uuid::Uuid;
Expand Down Expand Up @@ -114,10 +114,7 @@ fn supported_devices(py: Python<'_>) -> PyResult<&PyAny> {
#[pymethods]
impl VoiceModel {
#[staticmethod]
fn from_path(
py: Python<'_>,
#[pyo3(from_py_with = "from_utf8_path")] path: Utf8PathBuf,
) -> PyResult<&PyAny> {
fn from_path(py: Python<'_>, path: PathBuf) -> PyResult<&PyAny> {
pyo3_asyncio::tokio::future_into_py(py, async move {
let model = voicevox_core::tokio::VoiceModel::from_path(path).await;
let model = Python::with_gil(|py| model.into_py_result(py))?;
Expand Down Expand Up @@ -247,7 +244,12 @@ impl Synthesizer {
.into_py_result(py)
}

fn is_loaded_voice_model(&self, voice_model_id: &str) -> PyResult<bool> {
// C APIの挙動と一貫性を持たせる。
fn is_loaded_voice_model(&self, voice_model_id: &PyString) -> PyResult<bool> {
let Ok(voice_model_id) = voice_model_id.to_str() else {
// 与えられたIDがUTF-8ではない場合、それに対応する`VoicdModel`は確実に存在しない
return Ok(false);
};
Ok(self
.synthesizer
.get()?
Expand Down Expand Up @@ -636,12 +638,12 @@ impl UserDict {
}

mod blocking {
use std::sync::Arc;
use std::{path::PathBuf, sync::Arc};

use camino::Utf8PathBuf;
use pyo3::{
pyclass, pymethods,
types::{IntoPyDict as _, PyBytes, PyDict, PyList},
types::{IntoPyDict as _, PyBytes, PyDict, PyList, PyString},
PyAny, PyObject, PyRef, PyResult, Python,
};
use uuid::Uuid;
Expand All @@ -661,10 +663,7 @@ mod blocking {
#[pymethods]
impl VoiceModel {
#[staticmethod]
fn from_path(
py: Python<'_>,
#[pyo3(from_py_with = "crate::convert::from_utf8_path")] path: Utf8PathBuf,
) -> PyResult<Self> {
fn from_path(py: Python<'_>, path: PathBuf) -> PyResult<Self> {
let model = voicevox_core::blocking::VoiceModel::from_path(path).into_py_result(py)?;
Ok(Self { model })
}
Expand Down Expand Up @@ -786,7 +785,12 @@ mod blocking {
.into_py_result(py)
}

fn is_loaded_voice_model(&self, voice_model_id: &str) -> PyResult<bool> {
// C APIの挙動と一貫性を持たせる。
fn is_loaded_voice_model(&self, voice_model_id: &PyString) -> PyResult<bool> {
let Ok(voice_model_id) = voice_model_id.to_str() else {
// 与えられたIDがUTF-8ではない場合、それに対応する`VoicdModel`は確実に存在しない
return Ok(false);
};
Ok(self
.synthesizer
.get()?
Expand Down

0 comments on commit 92c4a36

Please sign in to comment.