build: update pyo3 to v0.21

This commit is contained in:
Shunsuke Shibayama 2024-07-02 03:15:28 +09:00
parent d62bce689b
commit 2f6717ba4f
12 changed files with 61 additions and 41 deletions

20
Cargo.lock generated
View file

@ -379,9 +379,9 @@ dependencies = [
[[package]]
name = "pyo3"
version = "0.20.3"
version = "0.21.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53bdbb96d49157e65d45cc287af5f32ffadd5f4761438b527b055fb0d4bb8233"
checksum = "a5e00b96a521718e08e03b1a622f01c8a8deb50719335de3f60b3b3950f069d8"
dependencies = [
"cfg-if",
"indoc",
@ -397,9 +397,9 @@ dependencies = [
[[package]]
name = "pyo3-build-config"
version = "0.20.3"
version = "0.21.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "deaa5745de3f5231ce10517a1f5dd97d53e5a2fd77aa6b5842292085831d48d7"
checksum = "7883df5835fafdad87c0d888b266c8ec0f4c9ca48a5bed6bbb592e8dedee1b50"
dependencies = [
"once_cell",
"target-lexicon",
@ -407,9 +407,9 @@ dependencies = [
[[package]]
name = "pyo3-ffi"
version = "0.20.3"
version = "0.21.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62b42531d03e08d4ef1f6e85a2ed422eb678b8cd62b762e53891c05faf0d4afa"
checksum = "01be5843dc60b916ab4dad1dca6d20b9b4e6ddc8e15f50c47fe6d85f1fb97403"
dependencies = [
"libc",
"pyo3-build-config",
@ -417,9 +417,9 @@ dependencies = [
[[package]]
name = "pyo3-macros"
version = "0.20.3"
version = "0.21.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7305c720fa01b8055ec95e484a6eca7a83c841267f0dd5280f0c8b8551d2c158"
checksum = "77b34069fc0682e11b31dbd10321cbf94808394c56fd996796ce45217dfac53c"
dependencies = [
"proc-macro2",
"pyo3-macros-backend",
@ -429,9 +429,9 @@ dependencies = [
[[package]]
name = "pyo3-macros-backend"
version = "0.20.3"
version = "0.21.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7c7e9b68bb9c3149c5b0cade5d07f953d6d125eb4337723c4ccdb665f1f96185"
checksum = "08260721f32db5e1a5beae69a55553f56b99bd0e1c3e6e0a5e8851a9d0f5a85c"
dependencies = [
"heck",
"proc-macro2",

View file

@ -78,7 +78,7 @@ erg_compiler = { version = "0.6.39-nightly.1", path = "./crates/erg_compiler" }
erg_linter = { version = "0.6.39-nightly.1", path = "./crates/erg_linter" }
els = { version = "0.1.51-nightly.1", path = "./crates/els" }
erg_proc_macros = { version = "0.6.39-nightly.1", path = "./crates/erg_proc_macros" }
pyo3 = { version = "0.20", features = ["extension-module"] }
pyo3 = { version = "0.21", features = ["extension-module"] }
[dependencies]
erg_common = { workspace = true }

View file

@ -284,7 +284,7 @@ impl fmt::Display for Location {
#[cfg(feature = "pylib")]
impl FromPyObject<'_> for Location {
fn extract(ob: &'_ PyAny) -> PyResult<Self> {
fn extract_bound(ob: &Bound<'_, PyAny>) -> PyResult<Self> {
if let Ok(s) = ob.extract::<String>() {
Ok(s.parse::<Location>().unwrap())
} else if let Ok(s) = ob.extract::<u32>() {

View file

@ -7,6 +7,8 @@ use std::iter::FromIterator;
use crate::fxhash::FxHashSet;
use crate::{debug_fmt_iter, fmt_iter};
#[cfg(feature = "pylib")]
use pyo3::prelude::PyAnyMethods;
#[cfg(feature = "pylib")]
use pyo3::{FromPyObject, IntoPy, PyAny, PyObject, Python};
@ -37,7 +39,7 @@ impl<'source, T> FromPyObject<'source> for Set<T>
where
T: Hash + Eq + FromPyObject<'source>,
{
fn extract(ob: &'source PyAny) -> pyo3::PyResult<Self> {
fn extract_bound(ob: &pyo3::Bound<'source, PyAny>) -> pyo3::PyResult<Self> {
Ok(Set {
elems: ob.extract::<FxHashSet<T>>()?,
})

View file

@ -254,7 +254,11 @@ impl StdinReader {
}
pub fn reread_lines(&self, ln_begin: usize, ln_end: usize) -> Vec<String> {
self.buf[ln_begin - 1..=ln_end - 1].to_vec()
if let Some(lines) = self.buf.get(ln_begin - 1..=ln_end - 1) {
lines.to_vec()
} else {
self.buf.clone()
}
}
pub fn last_line(&mut self) -> Option<&mut String> {

View file

@ -3,6 +3,8 @@ use std::fmt;
use std::hash::{Hash, Hasher};
use std::ops::{Add, Deref};
#[cfg(feature = "pylib")]
use pyo3::prelude::PyAnyMethods;
#[cfg(feature = "pylib")]
use pyo3::{FromPyObject, IntoPy, PyAny, PyObject, Python};
@ -19,7 +21,7 @@ pub enum Str {
#[cfg(feature = "pylib")]
impl FromPyObject<'_> for Str {
fn extract(ob: &PyAny) -> pyo3::PyResult<Self> {
fn extract_bound(ob: &pyo3::Bound<'_, PyAny>) -> pyo3::PyResult<Self> {
let s = ob.extract::<String>()?;
Ok(Str::Rc(s.into()))
}

View file

@ -74,6 +74,10 @@ impl<ASTBuilder: ASTBuildable> Runnable for GenericHIRBuilder<ASTBuilder> {
// don't initialize the ownership checker
}
fn set_input(&mut self, input: erg_common::io::Input) {
self.lowerer.set_input(input);
}
fn exec(&mut self) -> Result<ExitStatus, Self::Errs> {
let mut builder = ASTBuilder::new(self.cfg().copy());
let artifact = builder

View file

@ -168,6 +168,7 @@ impl Runnable for Compiler {
fn set_input(&mut self, input: erg_common::io::Input) {
self.cfg.input = input;
self.builder.set_input(self.cfg.input.clone());
self.builder.main_builder.set_input(self.cfg.input.clone());
self.code_generator.set_input(self.cfg.input.clone());
}

View file

@ -91,9 +91,11 @@ impl _Compiler {
.map(|art| art.object)
.map_err(|iart| iart.errors)?;
let bytes = code.into_bytes(py.version().parse().unwrap());
let dict = [("bytes", PyBytes::new(py, &bytes))].into_py_dict(py);
py.run("import marshal", None, None).unwrap();
let code = py.eval("marshal.loads(bytes)", None, Some(dict)).unwrap();
let dict = [("bytes", PyBytes::new_bound(py, &bytes))].into_py_dict_bound(py);
py.run_bound("import marshal", None, None).unwrap();
let code = py
.eval_bound("marshal.loads(bytes)", None, Some(&dict))
.unwrap();
Ok(code.into())
}
@ -129,10 +131,10 @@ impl _Compiler {
.map(|art| art.object)
.map_err(|iart| iart.errors)?;
let bytes = code.into_bytes(py.version().parse().unwrap());
let dict = [("bytes", PyBytes::new(py, &bytes))].into_py_dict(py);
py.run("import marshal", None, None).unwrap();
let dict = [("bytes", PyBytes::new_bound(py, &bytes))].into_py_dict_bound(py);
py.run_bound("import marshal", None, None).unwrap();
Ok(py
.eval("marshal.loads(bytes)", None, Some(dict))
.eval_bound("marshal.loads(bytes)", None, Some(&dict))
.unwrap()
.into())
}
@ -248,9 +250,9 @@ fn _exec_with_dependencies(
path: Option<String>,
) -> Result<PyObject, error::CompileErrors> {
let code = _compile_with_dependencies(py, code, "exec", pkgs, path)?;
let module = pyo3::types::PyModule::new(py, "<erg>").unwrap();
let dic = [("code", code), ("dict", PyObject::from(module.dict()))].into_py_dict(py);
py.run("exec(code, dict)", None, Some(dic)).unwrap();
let module = pyo3::types::PyModule::new_bound(py, "<erg>").unwrap();
let dic = [("code", code), ("dict", PyObject::from(module.dict()))].into_py_dict_bound(py);
py.run_bound("exec(code, dict)", None, Some(&dic)).unwrap();
Ok(module.into())
}
@ -310,9 +312,9 @@ fn _exec_ast_with_dependencies(
path: Option<String>,
) -> Result<PyObject, error::CompileErrors> {
let code = _compile_ast_with_dependencies(py, ast, "exec", pkgs, path)?;
let module = pyo3::types::PyModule::new(py, "<erg>").unwrap();
let dic = [("code", code), ("dict", PyObject::from(module.dict()))].into_py_dict(py);
py.run("exec(code, dict)", None, Some(dic)).unwrap();
let module = pyo3::types::PyModule::new_bound(py, "<erg>").unwrap();
let dic = [("code", code), ("dict", PyObject::from(module.dict()))].into_py_dict_bound(py);
py.run_bound("exec(code, dict)", None, Some(&dic)).unwrap();
Ok(module.into())
}
@ -346,7 +348,7 @@ fn _import(py: Python<'_>, name: String) -> Result<PyObject, error::CompileError
#[cfg(feature = "pylib")]
#[pymodule]
fn erg_compiler(py: Python<'_>, m: &PyModule) -> PyResult<()> {
fn erg_compiler(py: Python<'_>, m: &Bound<PyModule>) -> PyResult<()> {
m.add_class::<Package>()?;
m.add_class::<_Compiler>()?;
m.add_function(wrap_pyfunction!(_compile, m)?)?;
@ -364,11 +366,11 @@ fn erg_compiler(py: Python<'_>, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(_import, m)?)?;
use crate::erg_parser::erg_parser;
let parser = PyModule::new(py, "erg_parser")?;
erg_parser(py, parser)?;
m.add_submodule(parser)?;
let parser = PyModule::new_bound(py, "erg_parser")?;
erg_parser(py, &parser)?;
m.add_submodule(&parser)?;
py.run(
py.run_bound(
"\
import sys
sys.modules['erg_compiler.erg_parser'] = erg_parser
@ -376,7 +378,7 @@ sys.modules['erg_compiler.erg_parser.ast'] = erg_parser.ast
sys.modules['erg_compiler.erg_parser.expr'] = erg_parser.expr
",
None,
Some(m.dict()),
Some(&m.dict()),
)?;
Ok(())

View file

@ -107,6 +107,11 @@ impl<ASTBuilder: ASTBuildable> Runnable for GenericASTLowerer<ASTBuilder> {
&mut self.cfg
}
fn set_input(&mut self, input: erg_common::io::Input) {
self.module.context.cfg.input = input.clone();
self.cfg.input = input;
}
#[inline]
fn finish(&mut self) {}

View file

@ -3572,7 +3572,7 @@ pub enum TypeSpec {
#[cfg(feature = "pylib")]
impl IntoPy<PyObject> for TypeSpec {
fn into_py(self, py: Python<'_>) -> PyObject {
pyo3::types::PyNone::get(py).into()
pyo3::types::PyNone::get_bound(py).to_object(py)
}
}

View file

@ -36,9 +36,9 @@ fn _parse(code: String) -> Result<ast::Module, error::ParseErrors> {
#[cfg(feature = "pylib")]
#[cfg_attr(feature = "pylib_parser", pymodule)]
pub fn erg_parser(py: Python<'_>, m: &PyModule) -> PyResult<()> {
pub fn erg_parser(py: Python<'_>, m: &Bound<PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(_parse, m)?)?;
let expr = PyModule::new(py, "expr")?;
let expr = PyModule::new_bound(py, "expr")?;
expr.add_class::<ast::Literal>()?;
expr.add_class::<ast::NormalList>()?;
expr.add_class::<ast::NormalTuple>()?;
@ -59,9 +59,9 @@ pub fn erg_parser(py: Python<'_>, m: &PyModule) -> PyResult<()> {
expr.add_class::<ast::Compound>()?;
expr.add_class::<ast::InlineModule>()?;
expr.add_class::<ast::Dummy>()?;
m.add_submodule(expr)?;
m.add_submodule(&expr)?;
let ast = PyModule::new(py, "ast")?;
let ast = PyModule::new_bound(py, "ast")?;
ast.add_class::<token::Token>()?;
ast.add_class::<token::TokenKind>()?;
ast.add_class::<ast::Literal>()?;
@ -96,16 +96,16 @@ pub fn erg_parser(py: Python<'_>, m: &PyModule) -> PyResult<()> {
ast.add_class::<ast::Dummy>()?;
ast.add_class::<ast::Module>()?;
ast.add_class::<ast::AST>()?;
m.add_submodule(ast)?;
m.add_submodule(&ast)?;
py.run(
py.run_bound(
"\
import sys
sys.modules['erg_parser.ast'] = ast
sys.modules['erg_parser.expr'] = expr
",
None,
Some(m.dict()),
Some(&m.dict()),
)?;
Ok(())