Skip to content

Commit b4f85c7

Browse files
committed
feat: support pyo3 v0.25
1 parent 96d223f commit b4f85c7

File tree

10 files changed

+264
-255
lines changed

10 files changed

+264
-255
lines changed

Cargo.lock

Lines changed: 165 additions & 191 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ members = [
2323
]
2424

2525
[workspace.package]
26-
version = "0.6.53-nightly.5"
26+
version = "0.6.53"
2727
authors = ["erg-lang team <[email protected]>"]
2828
license = "MIT OR Apache-2.0"
2929
edition = "2021"
@@ -75,13 +75,13 @@ log-level-error = ["erg_common/log-level-error", "erg_parser/log-level-error", "
7575
parallel = ["erg_common/parallel", "erg_parser/parallel", "erg_compiler/parallel", "erg_linter/parallel"]
7676

7777
[workspace.dependencies]
78-
erg_common = { version = "0.6.53-nightly.5", path = "./crates/erg_common" }
79-
erg_parser = { version = "0.6.53-nightly.5", path = "./crates/erg_parser" }
80-
erg_compiler = { version = "0.6.53-nightly.5", path = "./crates/erg_compiler" }
81-
erg_linter = { version = "0.6.53-nightly.5", path = "./crates/erg_linter" }
82-
els = { version = "0.1.65-nightly.5", path = "./crates/els" }
83-
erg_proc_macros = { version = "0.6.53-nightly.5", path = "./crates/erg_proc_macros" }
84-
pyo3 = { version = "0.21", features = ["extension-module"] }
78+
erg_common = { version = "0.6.53", path = "./crates/erg_common" }
79+
erg_parser = { version = "0.6.53", path = "./crates/erg_parser" }
80+
erg_compiler = { version = "0.6.53", path = "./crates/erg_compiler" }
81+
erg_linter = { version = "0.6.53", path = "./crates/erg_linter" }
82+
els = { version = "0.1.65", path = "./crates/els" }
83+
erg_proc_macros = { version = "0.6.53", path = "./crates/erg_proc_macros" }
84+
pyo3 = { version = "0.25", features = ["extension-module"] }
8585

8686
[dependencies]
8787
erg_common = { workspace = true }

crates/els/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "els"
33
description = "An Erg compiler frontend for IDEs, implements LSP."
44
documentation = "http://docs.rs/els"
5-
version = "0.1.65-nightly.5"
5+
version = "0.1.65"
66
authors.workspace = true
77
license.workspace = true
88
edition.workspace = true

crates/erg_common/error.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -308,18 +308,21 @@ impl FromPyObject<'_> for Location {
308308
}
309309

310310
#[cfg(feature = "pylib")]
311-
impl IntoPy<PyObject> for Location {
312-
fn into_py(self, py: Python<'_>) -> PyObject {
311+
impl<'py> IntoPyObject<'py> for Location {
312+
type Target = pyo3::types::PyTuple;
313+
type Output = Bound<'py, Self::Target>;
314+
type Error = pyo3::PyErr;
315+
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
313316
match self {
314-
Self::Line(l) => (l, py.None(), l, py.None()).into_py(py),
315-
Self::LineRange(lb, le) => (lb, py.None(), le, py.None()).into_py(py),
317+
Self::Line(l) => (l, py.None(), l, py.None()).into_pyobject(py),
318+
Self::LineRange(lb, le) => (lb, py.None(), le, py.None()).into_pyobject(py),
316319
Self::Range {
317320
ln_begin,
318321
col_begin,
319322
ln_end,
320323
col_end,
321-
} => (ln_begin, col_begin, ln_end, col_end).into_py(py),
322-
Self::Unknown => (py.None(), py.None(), py.None(), py.None()).into_py(py),
324+
} => (ln_begin, col_begin, ln_end, col_end).into_pyobject(py),
325+
Self::Unknown => (py.None(), py.None(), py.None(), py.None()).into_pyobject(py),
323326
}
324327
}
325328
}

crates/erg_common/set.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::{debug_fmt_iter, fmt_iter, get_hash};
1111
#[cfg(feature = "pylib")]
1212
use pyo3::prelude::PyAnyMethods;
1313
#[cfg(feature = "pylib")]
14-
use pyo3::{FromPyObject, IntoPy, PyAny, PyObject, Python};
14+
use pyo3::{Bound, FromPyObject, IntoPyObject, PyAny, Python};
1515

1616
#[macro_export]
1717
macro_rules! set {
@@ -29,9 +29,12 @@ pub struct Set<T> {
2929
}
3030

3131
#[cfg(feature = "pylib")]
32-
impl<T: Hash + Eq + IntoPy<PyObject>> IntoPy<PyObject> for Set<T> {
33-
fn into_py(self, py: Python<'_>) -> PyObject {
34-
self.elems.into_py(py)
32+
impl<'py, T: Hash + Eq + IntoPyObject<'py>> IntoPyObject<'py> for Set<T> {
33+
type Target = pyo3::types::PySet;
34+
type Output = Bound<'py, Self::Target>;
35+
type Error = pyo3::PyErr;
36+
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
37+
self.elems.into_pyobject(py)
3538
}
3639
}
3740

crates/erg_common/str.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::ops::{Add, Deref};
66
#[cfg(feature = "pylib")]
77
use pyo3::prelude::PyAnyMethods;
88
#[cfg(feature = "pylib")]
9-
use pyo3::{FromPyObject, IntoPy, PyAny, PyObject, Python};
9+
use pyo3::{FromPyObject, IntoPyObject, PyAny};
1010

1111
pub type ArcStr = std::sync::Arc<str>;
1212

@@ -28,9 +28,12 @@ impl FromPyObject<'_> for Str {
2828
}
2929

3030
#[cfg(feature = "pylib")]
31-
impl IntoPy<PyObject> for Str {
32-
fn into_py(self, py: Python<'_>) -> PyObject {
33-
(&self[..]).into_py(py)
31+
impl<'py> IntoPyObject<'py> for Str {
32+
type Target = pyo3::types::PyString;
33+
type Output = pyo3::Bound<'py, Self::Target>;
34+
type Error = std::convert::Infallible;
35+
fn into_pyobject(self, py: pyo3::Python<'py>) -> Result<Self::Output, Self::Error> {
36+
(&self[..]).into_pyobject(py)
3437
}
3538
}
3639

crates/erg_compiler/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ The overall structure is described in detail in [architecture.md(English)](../..
1111
```python
1212
import erg_compiler
1313

14-
module = erg_compiler.exec_module(".i = 1")
14+
module = erg_compiler.exec(".i = 1", None)
1515
# foo.er:
1616
# .bar = 1
1717
foo = erg_compiler.__import__("foo")
@@ -37,6 +37,7 @@ assert j == 1
3737
### Debug install (using venv)
3838

3939
```python
40+
# cd crates/erg_compiler
4041
python -m venv .venv
4142
source .venv/bin/activate
4243
maturin develop --features pylib_compiler
@@ -45,6 +46,7 @@ maturin develop --features pylib_compiler
4546
### Release install
4647

4748
```python
49+
# cd crates/erg_compiler
4850
maturin build -i python --release --features pylib_compiler
4951
pip install <output wheel>
5052
```

crates/erg_compiler/lib.rs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,11 @@ impl _Compiler {
9191
.map(|art| art.object)
9292
.map_err(|iart| iart.errors)?;
9393
let bytes = code.into_bytes(py.version().parse().unwrap());
94-
let dict = [("bytes", PyBytes::new_bound(py, &bytes))].into_py_dict_bound(py);
95-
py.run_bound("import marshal", None, None).unwrap();
96-
let code = py
97-
.eval_bound("marshal.loads(bytes)", None, Some(&dict))
94+
let dict = [("bytes", PyBytes::new(py, &bytes))]
95+
.into_py_dict(py)
9896
.unwrap();
97+
py.run(c"import marshal", None, None).unwrap();
98+
let code = py.eval(c"marshal.loads(bytes)", None, Some(&dict)).unwrap();
9999
Ok(code.into())
100100
}
101101

@@ -131,10 +131,12 @@ impl _Compiler {
131131
.map(|art| art.object)
132132
.map_err(|iart| iart.errors)?;
133133
let bytes = code.into_bytes(py.version().parse().unwrap());
134-
let dict = [("bytes", PyBytes::new_bound(py, &bytes))].into_py_dict_bound(py);
135-
py.run_bound("import marshal", None, None).unwrap();
134+
let dict = [("bytes", PyBytes::new(py, &bytes))]
135+
.into_py_dict(py)
136+
.unwrap();
137+
py.run(c"import marshal", None, None).unwrap();
136138
Ok(py
137-
.eval_bound("marshal.loads(bytes)", None, Some(&dict))
139+
.eval(c"marshal.loads(bytes)", None, Some(&dict))
138140
.unwrap()
139141
.into())
140142
}
@@ -250,9 +252,11 @@ fn _exec_with_dependencies(
250252
path: Option<String>,
251253
) -> Result<PyObject, error::CompileErrors> {
252254
let code = _compile_with_dependencies(py, code, "exec", pkgs, path)?;
253-
let module = pyo3::types::PyModule::new_bound(py, "<erg>").unwrap();
254-
let dic = [("code", code), ("dict", PyObject::from(module.dict()))].into_py_dict_bound(py);
255-
py.run_bound("exec(code, dict)", None, Some(&dic)).unwrap();
255+
let module = pyo3::types::PyModule::new(py, "<erg>").unwrap();
256+
let dic = [("code", code), ("dict", PyObject::from(module.dict()))]
257+
.into_py_dict(py)
258+
.unwrap();
259+
py.run(c"exec(code, dict)", None, Some(&dic)).unwrap();
256260
Ok(module.into())
257261
}
258262

@@ -312,9 +316,11 @@ fn _exec_ast_with_dependencies(
312316
path: Option<String>,
313317
) -> Result<PyObject, error::CompileErrors> {
314318
let code = _compile_ast_with_dependencies(py, ast, "exec", pkgs, path)?;
315-
let module = pyo3::types::PyModule::new_bound(py, "<erg>").unwrap();
316-
let dic = [("code", code), ("dict", PyObject::from(module.dict()))].into_py_dict_bound(py);
317-
py.run_bound("exec(code, dict)", None, Some(&dic)).unwrap();
319+
let module = pyo3::types::PyModule::new(py, "<erg>").unwrap();
320+
let dic = [("code", code), ("dict", PyObject::from(module.dict()))]
321+
.into_py_dict(py)
322+
.unwrap();
323+
py.run(c"exec(code, dict)", None, Some(&dic)).unwrap();
318324
Ok(module.into())
319325
}
320326

@@ -366,12 +372,12 @@ fn erg_compiler(py: Python<'_>, m: &Bound<PyModule>) -> PyResult<()> {
366372
m.add_function(wrap_pyfunction!(_import, m)?)?;
367373

368374
use crate::erg_parser::erg_parser;
369-
let parser = PyModule::new_bound(py, "erg_parser")?;
375+
let parser = PyModule::new(py, "erg_parser")?;
370376
erg_parser(py, &parser)?;
371377
m.add_submodule(&parser)?;
372378

373-
py.run_bound(
374-
"\
379+
py.run(
380+
c"\
375381
import sys
376382
sys.modules['erg_compiler.erg_parser'] = erg_parser
377383
sys.modules['erg_compiler.erg_parser.ast'] = erg_parser.ast

crates/erg_parser/ast.rs

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,15 @@ use pyo3::prelude::*;
3232
macro_rules! impl_into_py_for_enum {
3333
($Enum: ident; $($Variant: ident $(,)?)*) => {
3434
#[cfg(feature = "pylib")]
35-
impl IntoPy<PyObject> for $Enum {
36-
fn into_py(self, py: Python<'_>) -> PyObject {
35+
impl<'py> IntoPyObject<'py> for $Enum {
36+
type Target = pyo3::PyAny;
37+
type Output = pyo3::Bound<'py, Self::Target>;
38+
type Error = pyo3::PyErr;
39+
fn into_pyobject(self, py: pyo3::Python<'py>) -> Result<Self::Output, Self::Error> {
40+
use pyo3::IntoPyObjectExt;
41+
3742
match self {
38-
$(Self::$Variant(v) => v.into_py(py),)*
43+
$(Self::$Variant(v) => Ok(v.into_py_any(py)?.into_bound(py)),)*
3944
}
4045
}
4146
}
@@ -46,7 +51,7 @@ macro_rules! impl_from_py_for_enum {
4651
($Ty: ty; $($Variant: ident ($inner: ident) $(,)*)*) => {
4752
#[cfg(feature = "pylib")]
4853
impl FromPyObject<'_> for $Ty {
49-
fn extract(ob: &PyAny) -> PyResult<Self> {
54+
fn extract_bound(ob: &pyo3::Bound<'_, PyAny>) -> PyResult<Self> {
5055
$(if let Ok(extracted) = ob.extract::<$inner>() {
5156
return Ok(Self::$Variant(extracted));
5257
} else)* {
@@ -60,7 +65,7 @@ macro_rules! impl_from_py_for_enum {
6065
($Ty: ty; $($Variant: ident $(,)*)*) => {
6166
#[cfg(feature = "pylib")]
6267
impl FromPyObject<'_> for $Ty {
63-
fn extract(ob: &PyAny) -> PyResult<Self> {
68+
fn extract_bound(ob: &pyo3::Bound<'_, PyAny>) -> PyResult<Self> {
6469
$(if let Ok(extracted) = ob.extract::<$Variant>() {
6570
return Ok(Self::$Variant(extracted));
6671
} else)* {
@@ -791,18 +796,23 @@ pub enum TypeAppArgsKind {
791796
}
792797

793798
#[cfg(feature = "pylib")]
794-
impl IntoPy<PyObject> for TypeAppArgsKind {
795-
fn into_py(self, py: Python<'_>) -> PyObject {
799+
impl<'py> IntoPyObject<'py> for TypeAppArgsKind {
800+
type Target = PyAny;
801+
type Output = Bound<'py, Self::Target>;
802+
type Error = PyErr;
803+
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
804+
use pyo3::IntoPyObjectExt;
805+
796806
match self {
797-
Self::SubtypeOf(ty) => ty.into_py(py),
798-
Self::Args(args) => args.into_py(py),
807+
Self::SubtypeOf(ty) => Ok(ty.into_py_any(py)?.into_bound(py)),
808+
Self::Args(args) => Ok(args.into_py_any(py)?.into_bound(py)),
799809
}
800810
}
801811
}
802812

803813
#[cfg(feature = "pylib")]
804814
impl FromPyObject<'_> for TypeAppArgsKind {
805-
fn extract(ob: &PyAny) -> PyResult<Self> {
815+
fn extract_bound(ob: &Bound<'_, PyAny>) -> PyResult<Self> {
806816
if let Ok(ty) = ob.extract::<TypeSpecWithOp>() {
807817
Ok(Self::SubtypeOf(Box::new(ty)))
808818
} else if let Ok(args) = ob.extract::<Args>() {
@@ -4146,9 +4156,12 @@ pub enum TypeSpec {
41464156

41474157
// TODO:
41484158
#[cfg(feature = "pylib")]
4149-
impl IntoPy<PyObject> for TypeSpec {
4150-
fn into_py(self, py: Python<'_>) -> PyObject {
4151-
pyo3::types::PyNone::get_bound(py).to_object(py)
4159+
impl<'py> IntoPyObject<'py> for TypeSpec {
4160+
type Target = pyo3::types::PyNone;
4161+
type Output = Borrowed<'py, 'py, Self::Target>;
4162+
type Error = std::convert::Infallible;
4163+
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
4164+
pyo3::types::PyNone::get(py).into_pyobject(py)
41524165
}
41534166
}
41544167

@@ -4620,14 +4633,19 @@ impl Hash for VisModifierSpec {
46204633
}
46214634

46224635
#[cfg(feature = "pylib")]
4623-
impl IntoPy<PyObject> for VisModifierSpec {
4624-
fn into_py(self, py: Python<'_>) -> PyObject {
4636+
impl<'py> IntoPyObject<'py> for VisModifierSpec {
4637+
type Target = PyAny;
4638+
type Output = Bound<'py, PyAny>;
4639+
type Error = PyErr;
4640+
4641+
fn into_pyobject(self, py: Python<'py>) -> PyResult<Self::Output> {
4642+
use pyo3::IntoPyObjectExt;
4643+
46254644
match self {
4626-
Self::Private => py.None(),
4627-
Self::Auto => py.None(),
4628-
Self::Public(token) => token.into_py(py),
4629-
Self::ExplicitPrivate(token) => token.into_py(py),
4630-
Self::Restricted(rest) => rest.into_py(py),
4645+
Self::Private | Self::Auto => py.None().into_bound_py_any(py),
4646+
Self::Public(token) => token.into_bound_py_any(py),
4647+
Self::ExplicitPrivate(token) => token.into_bound_py_any(py),
4648+
Self::Restricted(rest) => rest.into_bound_py_any(py),
46314649
}
46324650
}
46334651
}

crates/erg_parser/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ fn _parse(code: String) -> Result<ast::Module, error::ParseErrors> {
3838
#[cfg_attr(feature = "pylib_parser", pymodule)]
3939
pub fn erg_parser(py: Python<'_>, m: &Bound<PyModule>) -> PyResult<()> {
4040
m.add_function(wrap_pyfunction!(_parse, m)?)?;
41-
let expr = PyModule::new_bound(py, "expr")?;
41+
let expr = PyModule::new(py, "expr")?;
4242
expr.add_class::<ast::Literal>()?;
4343
expr.add_class::<ast::NormalList>()?;
4444
expr.add_class::<ast::NormalTuple>()?;
@@ -61,7 +61,7 @@ pub fn erg_parser(py: Python<'_>, m: &Bound<PyModule>) -> PyResult<()> {
6161
expr.add_class::<ast::Dummy>()?;
6262
m.add_submodule(&expr)?;
6363

64-
let ast = PyModule::new_bound(py, "ast")?;
64+
let ast = PyModule::new(py, "ast")?;
6565
ast.add_class::<token::Token>()?;
6666
ast.add_class::<token::TokenKind>()?;
6767
ast.add_class::<ast::Literal>()?;
@@ -98,8 +98,8 @@ pub fn erg_parser(py: Python<'_>, m: &Bound<PyModule>) -> PyResult<()> {
9898
ast.add_class::<ast::AST>()?;
9999
m.add_submodule(&ast)?;
100100

101-
py.run_bound(
102-
"\
101+
py.run(
102+
c"\
103103
import sys
104104
sys.modules['erg_parser.ast'] = ast
105105
sys.modules['erg_parser.expr'] = expr

0 commit comments

Comments
 (0)