Skip to content

Commit

Permalink
build: update pyo3 to v0.21
Browse files Browse the repository at this point in the history
  • Loading branch information
mtshiba committed Jul 1, 2024
1 parent d62bce6 commit 2f6717b
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 41 deletions.
20 changes: 10 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
2 changes: 1 addition & 1 deletion crates/erg_common/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>() {
Expand Down
4 changes: 3 additions & 1 deletion crates/erg_common/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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>>()?,
})
Expand Down
6 changes: 5 additions & 1 deletion crates/erg_common/stdin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
4 changes: 3 additions & 1 deletion crates/erg_common/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -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()))
}
Expand Down
4 changes: 4 additions & 0 deletions crates/erg_compiler/build_hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions crates/erg_compiler/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down
38 changes: 20 additions & 18 deletions crates/erg_compiler/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

Expand Down Expand Up @@ -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())
}
Expand Down Expand Up @@ -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())
}

Expand Down Expand Up @@ -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())
}

Expand Down Expand Up @@ -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)?)?;
Expand All @@ -364,19 +366,19 @@ 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
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(())
Expand Down
5 changes: 5 additions & 0 deletions crates/erg_compiler/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}

Expand Down
2 changes: 1 addition & 1 deletion crates/erg_parser/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
14 changes: 7 additions & 7 deletions crates/erg_parser/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>()?;
Expand All @@ -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>()?;
Expand Down Expand Up @@ -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(())
Expand Down

0 comments on commit 2f6717b

Please sign in to comment.