Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitfc3ffa4

Browse files
Merge pull request#284 from WorksApplications/feature/280-pyo3-23
update pyo3 to v0.23
2 parents287f3d7 +97aec81 commitfc3ffa4

File tree

9 files changed

+72
-77
lines changed

9 files changed

+72
-77
lines changed

‎python/Cargo.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ name = "sudachipy"
1515
crate-type = ["cdylib"]
1616

1717
[dependencies]
18-
pyo3 = {version ="0.22",features = ["extension-module"] }
18+
pyo3 = {version ="0.23",features = ["extension-module"] }
1919
scopeguard ="1"# Apache 2.0/MIT
2020
thread_local ="1.1"# Apache 2.0/MIT
2121

‎python/build-wheels-manylinux-pgo.sh‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ export CARGO_BUILD_TARGET=x86_64-unknown-linux-gnu
3535

3636
# see following link for the list of cpython bin
3737
# https://github.com/pypa/manylinux?tab=readme-ov-file#image-content
38-
# TODO: after supporting py313t, "/opt/python/cp{37,38,39,310,311,312,313}-*/bin" would suffice.
39-
forPYBINin /opt/python/cp*-cp{37m,38,39,310,311,312,313}/bin;do
38+
forPYBINin /opt/python/cp{37,38,39,310,311,312,313}-*/bin;do
4039
"${PYBIN}/pip" install -U setuptools wheel setuptools-rust
4140
find. -iname'sudachipy*.so'
4241
rm -f build/lib/sudachipy/sudachipy*.so

‎python/src/build.rs‎

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use std::io::BufWriter;
1919
use std::path::Path;
2020

2121
use pyo3::prelude::*;
22-
use pyo3::types::{PyBytes,PyList,PyString,PyTuple,PyType};
22+
use pyo3::types::{PyBytes,PyList,PyString,PyType};
2323

2424
use sudachi::analysis::stateless_tokenizer::DictionaryAccess;
2525
use sudachi::config::Config;
@@ -36,18 +36,11 @@ pub fn register_functions(m: &Bound<PyModule>) -> PyResult<()> {
3636
}
3737

3838
fnto_stats<T:DictionaryAccess>(py:Python,builder:DictBuilder<T>) ->PyResult<Bound<PyList>>{
39-
let stats =PyList::empty_bound(py);
39+
let stats =PyList::empty(py);
4040

4141
for pin builder.report(){
42-
let t =PyTuple::new_bound(
43-
py,
44-
[
45-
p.part().into_py(py),
46-
p.size().into_py(py),
47-
p.time().as_secs_f64().into_py(py),
48-
],
49-
);
50-
stats.append(t)?;
42+
let values =(p.part(), p.size(), p.time().as_secs_f64());
43+
stats.append(values.into_pyobject(py)?)?;
5144
}
5245

5346
Ok(stats)
@@ -174,7 +167,7 @@ fn resolve_as_pypathstr<'py>(
174167
py:Python<'py>,
175168
data:&Bound<'py,PyAny>,
176169
) ->PyResult<Option<Bound<'py,PyString>>>{
177-
let binding = py.import_bound("pathlib")?.getattr("Path")?;
170+
let binding = py.import("pathlib")?.getattr("Path")?;
178171
let path = binding.downcast::<PyType>()?;
179172
if data.is_instance(path)?{
180173
Ok(Some(data.call_method0("resolve")?.str()?))

‎python/src/dictionary.rs‎

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use std::path::{Path, PathBuf};
2020
use std::str::FromStr;
2121
use std::sync::Arc;
2222

23+
use pyo3::ffi::c_str;
2324
use pyo3::prelude::*;
2425
use pyo3::types::{PySet,PyString,PyTuple};
2526

@@ -160,7 +161,7 @@ impl PyDictionary {
160161
if dict_type.is_some(){
161162
errors::warn_deprecation(
162163
py,
163-
"Parameter dict_type of Dictionary() is deprecated, use dict instead",
164+
c_str!("Parameter dict_type of Dictionary() is deprecated, use dict instead"),
164165
)?
165166
}
166167

@@ -211,7 +212,9 @@ impl PyDictionary {
211212
.pos_list
212213
.iter()
213214
.map(|pos|{
214-
let tuple:Py<PyTuple> =PyTuple::new_bound(py, pos).into_py(py);
215+
let tuple:Py<PyTuple> =PyTuple::new(py, pos)
216+
.expect("failed to convert POS tuple")
217+
.unbind();
215218
tuple
216219
})
217220
.collect();
@@ -288,12 +291,8 @@ impl PyDictionary {
288291
/// :param target: can be either a list of POS partial tuples or a callable which maps POS to bool.
289292
///
290293
/// :type target: Iterable[PartialPOS] | Callable[[POS], bool]
291-
fnpos_matcher<'py>(
292-
&'pyself,
293-
py:Python<'py>,
294-
target:&Bound<'py,PyAny>,
295-
) ->PyResult<PyPosMatcher>{
296-
PyPosMatcher::create(py,self.dictionary.as_ref().unwrap(), target)
294+
fnpos_matcher<'py>(&'pyself,target:&Bound<'py,PyAny>) ->PyResult<PyPosMatcher>{
295+
PyPosMatcher::create(self.dictionary.as_ref().unwrap(), target)
297296
}
298297

299298
/// Creates HuggingFace Tokenizers-compatible PreTokenizer.
@@ -367,13 +366,12 @@ impl PyDictionary {
367366
)
368367
};
369368

370-
let internal =PyPretokenizer::new(dict, mode, required_fields, handler, projection);
371-
let internal_cell =Bound::new(py, internal)?;
372-
let module = py.import_bound("tokenizers.pre_tokenizers")?;
369+
let pretokenizer =PyPretokenizer::new(dict, mode, required_fields, handler, projection);
370+
let module = py.import("tokenizers.pre_tokenizers")?;
373371
module
374372
.getattr("PreTokenizer")?
375373
.getattr("custom")?
376-
.call1(PyTuple::new_bound(py,[internal_cell]))
374+
.call1((pretokenizer,))
377375
}
378376

379377
/// Look up morphemes in the binary dictionary without performing the analysis.
@@ -507,7 +505,7 @@ fn read_config(config_opt: &Bound<PyAny>) -> PyResult<ConfigBuilder> {
507505
)));
508506
}
509507
let py = config_opt.py();
510-
let cfg_type = py.import_bound("sudachipy.config")?.getattr("Config")?;
508+
let cfg_type = py.import("sudachipy.config")?.getattr("Config")?;
511509
if config_opt.is_instance(&cfg_type)?{
512510
let cfg_as_str = config_opt.call_method0("as_jsons")?;
513511
returnread_config(&cfg_as_str);
@@ -520,24 +518,20 @@ fn read_config(config_opt: &Bound<PyAny>) -> PyResult<ConfigBuilder> {
520518
}
521519

522520
pub(crate)fnread_default_config(py:Python) ->PyResult<ConfigBuilder>{
523-
let path = py
524-
.import_bound("sudachipy")?
525-
.getattr("_DEFAULT_SETTINGFILE")?;
521+
let path = py.import("sudachipy")?.getattr("_DEFAULT_SETTINGFILE")?;
526522
let path = path.downcast::<PyString>()?.to_str()?;
527523
let path =PathBuf::from(path);
528524
errors::wrap_ctx(ConfigBuilder::from_opt_file(Some(&path)),&path)
529525
}
530526

531527
pub(crate)fnget_default_resource_dir(py:Python) ->PyResult<PathBuf>{
532-
let path = py
533-
.import_bound("sudachipy")?
534-
.getattr("_DEFAULT_RESOURCEDIR")?;
528+
let path = py.import("sudachipy")?.getattr("_DEFAULT_RESOURCEDIR")?;
535529
let path = path.downcast::<PyString>()?.to_str()?;
536530
Ok(PathBuf::from(path))
537531
}
538532

539533
fnfind_dict_path(py:Python,dict_type:&str) ->PyResult<PathBuf>{
540-
let pyfunc = py.import_bound("sudachipy")?.getattr("_find_dict_path")?;
534+
let pyfunc = py.import("sudachipy")?.getattr("_find_dict_path")?;
541535
let path = pyfunc.call1((dict_type,))?;
542536
let path = path.downcast::<PyString>()?.to_str()?;
543537
Ok(PathBuf::from(path))

‎python/src/errors.rs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17+
use core::ffi::CStr;
1718
use std::fmt::{Debug,Display};
1819

1920
use pyo3::exceptions::PyDeprecationWarning;
@@ -37,6 +38,6 @@ pub fn wrap_ctx<T, E: Display, C: Debug + ?Sized>(v: Result<T, E>, ctx: &C) -> P
3738
}
3839
}
3940

40-
pubfnwarn_deprecation(py:Python<'_>,msg:&str) ->PyResult<()>{
41-
PyErr::warn_bound(py,&py.get_type_bound::<PyDeprecationWarning>(), msg,1)
41+
pubfnwarn_deprecation(py:Python<'_>,msg:&CStr) ->PyResult<()>{
42+
PyErr::warn(py,&py.get_type::<PyDeprecationWarning>(), msg,1)
4243
}

‎python/src/morpheme.rs‎

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use std::ops::Deref;
1919
use std::sync::Arc;
2020

2121
use pyo3::exceptions::PyIndexError;
22+
use pyo3::ffi::c_str;
2223
use pyo3::prelude::*;
2324
use pyo3::types::{PyList,PyString,PyTuple,PyType};
2425

@@ -101,7 +102,7 @@ impl PyMorphemeListWrapper {
101102
fnempty(_cls:&Bound<PyType>,py:Python,dict:&PyDictionary) ->PyResult<Self>{
102103
errors::warn_deprecation(
103104
py,
104-
"Use Tokenizer.tokenize(\"\") if you need an empty MorphemeList.",
105+
c_str!("Use Tokenizer.tokenize(\"\") if you need an empty MorphemeList."),
105106
)?;
106107

107108
let cloned = dict.dictionary.as_ref().unwrap().clone();
@@ -165,7 +166,7 @@ impl PyMorphemeListWrapper {
165166
result.push(' ');
166167
}
167168
}
168-
PyString::new_bound(py, result.as_str())
169+
PyString::new(py, result.as_str())
169170
}
170171

171172
fn__repr__(slf:Py<PyMorphemeListWrapper>,py:Python) ->PyResult<Bound<PyString>>{
@@ -184,7 +185,7 @@ impl PyMorphemeListWrapper {
184185
result.push_str(",\n");
185186
}
186187
result.push_str("]>");
187-
Ok(PyString::new_bound(py, result.as_str()))
188+
Ok(PyString::new(py, result.as_str()))
188189
}
189190

190191
fn__iter__(slf:Py<Self>) ->PyMorphemeIter{
@@ -301,7 +302,7 @@ impl PyMorpheme {
301302
let list =self.list(py);
302303
let morph =self.morph(py);
303304
match list.projection(){
304-
None =>PyString::new_bound(py, morph.surface().deref()),
305+
None =>PyString::new(py, morph.surface().deref()),
305306
Some(proj) => proj.project(morph.deref(), py),
306307
}
307308
}
@@ -311,7 +312,7 @@ impl PyMorpheme {
311312
/// See `Config.projection`.
312313
#[pyo3(text_signature ="(self, /) -> str")]
313314
fnraw_surface<'py>(&'pyself,py:Python<'py>) ->Bound<'py,PyString>{
314-
PyString::new_bound(py,self.morph(py).surface().deref())
315+
PyString::new(py,self.morph(py).surface().deref())
315316
}
316317

317318
/// Returns the part of speech as a six-element tuple.
@@ -334,20 +335,32 @@ impl PyMorpheme {
334335

335336
/// Returns the dictionary form.
336337
#[pyo3(text_signature ="(self, /) -> str")]
337-
fndictionary_form<'py>(&'pyself,py:Python<'py>) ->PyObject{
338-
self.morph(py).get_word_info().dictionary_form().into_py(py)
338+
fndictionary_form<'py>(&'pyself,py:Python<'py>) ->PyResult<Bound<PyString>>{
339+
Ok(self
340+
.morph(py)
341+
.get_word_info()
342+
.dictionary_form()
343+
.into_pyobject(py)?)
339344
}
340345

341346
/// Returns the normalized form.
342347
#[pyo3(text_signature ="(self, /) -> str")]
343-
fnnormalized_form<'py>(&'pyself,py:Python<'py>) ->PyObject{
344-
self.morph(py).get_word_info().normalized_form().into_py(py)
348+
fnnormalized_form<'py>(&'pyself,py:Python<'py>) ->PyResult<Bound<PyString>>{
349+
Ok(self
350+
.morph(py)
351+
.get_word_info()
352+
.normalized_form()
353+
.into_pyobject(py)?)
345354
}
346355

347356
/// Returns the reading form.
348357
#[pyo3(text_signature ="(self, /) -> str")]
349-
fnreading_form<'py>(&'pyself,py:Python<'py>) ->PyObject{
350-
self.morph(py).get_word_info().reading_form().into_py(py)
358+
fnreading_form<'py>(&'pyself,py:Python<'py>) ->PyResult<Bound<PyString>>{
359+
Ok(self
360+
.morph(py)
361+
.get_word_info()
362+
.reading_form()
363+
.into_pyobject(py)?)
351364
}
352365

353366
/// Returns sub-morphemes in the provided split mode.
@@ -431,10 +444,10 @@ impl PyMorpheme {
431444

432445
/// Returns the list of synonym group ids.
433446
#[pyo3(text_signature ="(self, /) -> List[int]")]
434-
fnsynonym_group_ids<'py>(&'pyself,py:Python<'py>) ->Bound<PyList>{
447+
fnsynonym_group_ids<'py>(&'pyself,py:Python<'py>) ->PyResult<Bound<PyList>>{
435448
let mref =self.morph(py);
436449
let ids = mref.get_word_info().synonym_group_ids();
437-
PyList::new_bound(py, ids)
450+
PyList::new(py, ids)
438451
}
439452

440453
/// Returns the word info.
@@ -443,7 +456,7 @@ impl PyMorpheme {
443456
/// Users should not touch the raw WordInfo.
444457
#[pyo3(text_signature ="(self, /) -> WordInfo")]
445458
fnget_word_info(&self,py:Python) ->PyResult<PyWordInfo>{
446-
errors::warn_deprecation(py,"Users should not touch the raw WordInfo.")?;
459+
errors::warn_deprecation(py,c_str!("Users should not touch the raw WordInfo."))?;
447460
Ok(self.morph(py).get_word_info().clone().into())
448461
}
449462

‎python/src/pos_matcher.rs‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,21 @@ pub struct PyPosMatcher {
3939

4040
implPyPosMatcher{
4141
pub(crate)fncreate<'py>(
42-
py:Python<'py>,
4342
dic:&'pyArc<PyDicData>,
4443
data:&Bound<'py,PyAny>,
4544
) ->PyResult<PyPosMatcher>{
4645
if data.is_callable(){
47-
Self::create_from_fn(dic, data, py)
46+
Self::create_from_fn(dic, data)
4847
}else{
49-
let iter = data.iter()?;
48+
let iter = data.try_iter()?;
5049
Self::create_from_iter(dic,&iter)
5150
}
5251
}
5352

54-
fncreate_from_fn(dic:&Arc<PyDicData>,func:&Bound<PyAny>,py:Python) ->PyResult<Self>{
53+
fncreate_from_fn(dic:&Arc<PyDicData>,func:&Bound<PyAny>) ->PyResult<Self>{
5554
letmut data =Vec::new();
5655
for(pos_id, pos)in dic.pos.iter().enumerate(){
57-
let args =PyTuple::new_bound(py,[pos]);
58-
if func.call1(args)?.downcast::<PyBool>()?.is_true(){
56+
if func.call1((pos,))?.downcast::<PyBool>()?.is_true(){
5957
data.push(pos_idasu16);
6058
}
6159
}

‎python/src/pretokenizer.rs‎

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use std::sync::Arc;
2020
use pyo3::intern;
2121
use pyo3::prelude::*;
2222
use pyo3::sync::GILOnceCell;
23-
use pyo3::types::{PyList,PySlice,PyTuple,PyType};
23+
use pyo3::types::{PyList,PySlice,PyType};
2424
use thread_local::ThreadLocal;
2525

2626
use sudachi::analysis::stateful_tokenizer::StatefulTokenizer;
@@ -154,8 +154,7 @@ impl PyPretokenizer {
154154
}
155155
Some(h) =>{
156156
let mrp:&Bound<PyAny> = morphs.bind(py);
157-
let args =PyTuple::new_bound(py,[index, string, mrp]);
158-
h.bind(py).call1(args)
157+
h.bind(py).call1((index, string, mrp))
159158
}
160159
}
161160
}
@@ -166,7 +165,7 @@ impl PyPretokenizer {
166165
py:Python<'py>,
167166
data:&Bound<'py,PyAny>,
168167
) ->PyResult<Bound<'py,PyAny>>{
169-
data.call_method1(intern!(py,"split"),PyTuple::new_bound(py,[self_]))
168+
data.call_method1(intern!(py,"split"),(self_,))
170169
}
171170
}
172171

@@ -175,12 +174,11 @@ fn make_result_for_surface<'py>(
175174
morphs:&PyMorphemeList,
176175
string:&Bound<'py,PyAny>,
177176
) ->PyResult<Bound<'py,PyList>>{
178-
let result =PyList::empty_bound(py);
177+
let result =PyList::empty(py);
179178
for idxin0..morphs.len(){
180179
let node = morphs.get(idx);
181-
let slice =PySlice::new_bound(py, node.begin_c()asisize, node.end_c()asisize,1);
182-
let args =PyTuple::new_bound(py,[slice]);
183-
let substring = string.call_method1(intern!(py,"slice"), args)?;
180+
let slice =PySlice::new(py, node.begin_c()asisize, node.end_c()asisize,1);
181+
let substring = string.call_method1(intern!(py,"slice"),(slice,))?;
184182
result.append(substring)?;
185183
}
186184
Ok(result)
@@ -191,20 +189,19 @@ fn make_result_for_projection<'py>(
191189
morphs:&PyMorphemeList,
192190
proj:&dynMorphemeProjection,
193191
) ->PyResult<Bound<'py,PyList>>{
194-
let result =PyList::empty_bound(py);
192+
let result =PyList::empty(py);
195193
let nstring ={
196194
staticNORMALIZED_STRING:GILOnceCell<Py<PyType>> =GILOnceCell::new();
197195
NORMALIZED_STRING.get_or_try_init(py, || ->PyResult<Py<PyType>>{
198-
let ns = py.import_bound("tokenizers")?.getattr("NormalizedString")?;
196+
let ns = py.import("tokenizers")?.getattr("NormalizedString")?;
199197
let tpe = ns.downcast::<PyType>()?;
200198
Ok(tpe.clone().unbind())
201199
})?
202200
};
203201
for idxin0..morphs.len(){
204202
let node = morphs.get(idx);
205203
let value = proj.project(&node, py);
206-
let args =PyTuple::new_bound(py,[value]);
207-
let substring = nstring.call1(py, args)?;
204+
let substring = nstring.call1(py,(value,))?;
208205
result.append(substring)?;
209206
}
210207
Ok(result)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp