Skip to content

Commit

Permalink
fix: Python 3.7~3.8 bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
mtshiba committed Sep 13, 2023
1 parent 75c1ac7 commit aacdca3
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
fail-fast: false
matrix:
os: [windows-latest, ubuntu-latest, macos-latest]
python-version: ['3.9', '3.10', '3.11.3']
python-version: ['3.7', '3.8', '3.9', '3.10', '3.11.3']
runs-on: ${{ matrix.os }}
env:
RUST_BACKTRACE: full
Expand Down
18 changes: 17 additions & 1 deletion crates/erg_compiler/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ pub struct PyCodeGenerator {
union_loaded: bool,
fake_generic_loaded: bool,
abc_loaded: bool,
builtins_loaded: bool,
unit_size: usize,
units: PyCodeGenStack,
fresh_gen: SharedFreshNameGenerator,
Expand All @@ -209,6 +210,7 @@ impl PyCodeGenerator {
union_loaded: false,
fake_generic_loaded: false,
abc_loaded: false,
builtins_loaded: false,
unit_size: 0,
units: PyCodeGenStack::empty(),
fresh_gen: SharedFreshNameGenerator::new("codegen"),
Expand All @@ -231,6 +233,7 @@ impl PyCodeGenerator {
union_loaded: false,
fake_generic_loaded: false,
abc_loaded: false,
builtins_loaded: false,
unit_size: 0,
units: PyCodeGenStack::empty(),
fresh_gen: self.fresh_gen.clone(),
Expand All @@ -253,6 +256,7 @@ impl PyCodeGenerator {
self.union_loaded = false;
self.fake_generic_loaded = false;
self.abc_loaded = false;
self.builtins_loaded = false;
}

#[inline]
Expand Down Expand Up @@ -2456,13 +2460,18 @@ impl PyCodeGenerator {
Some(7) => self.emit_with_instr_307(args),
_ => todo!("not supported Python version"),
},
"sum" if self.py_version.minor <= Some(7) && args.get_kw("start").is_some() => {
self.load_builtins();
self.emit_load_name_instr(Identifier::private("#sum"));
self.emit_args_311(args, Name, true);
}
other if local.ref_t().is_poly_type_meta() && other != "classof" => {
if self.py_version.minor <= Some(9) {
self.load_fake_generic();
self.emit_load_name_instr(Identifier::private("#FakeGenericAlias"));
let mut args = args;
args.insert_pos(0, PosArg::new(Expr::Accessor(Accessor::Ident(local))));
self.emit_args_311(args, Name, false);
self.emit_args_311(args, Name, true);
} else {
self.emit_load_name_instr(local);
self.emit_index_args(args);
Expand Down Expand Up @@ -3573,6 +3582,13 @@ impl PyCodeGenerator {
);
}

fn load_builtins(&mut self) {
self.emit_global_import_items(
Identifier::public("_erg_builtins"),
vec![(Identifier::public("sum"), Some(Identifier::private("#sum")))],
);
}

pub fn emit(&mut self, hir: HIR) -> CodeObj {
log!(info "the code-generating process has started.{RESET}");
self.unit_size += 1;
Expand Down
4 changes: 2 additions & 2 deletions crates/erg_compiler/context/initialize/funcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,15 @@ impl Context {
let t_isinstance = nd_func(
vec![
kw(KW_OBJECT, Obj),
kw(KW_CLASSINFO, ClassType), // TODO: => ClassInfo
kw(KW_CLASSINFO, ClassType | unknown_len_array_t(ClassType)), // TODO: => ClassInfo
],
None,
Bool,
);
let t_issubclass = nd_func(
vec![
kw(KW_SUBCLASS, ClassType),
kw(KW_CLASSINFO, ClassType), // TODO: => ClassInfo
kw(KW_CLASSINFO, ClassType | unknown_len_array_t(ClassType)), // TODO: => ClassInfo
],
None,
Bool,
Expand Down
7 changes: 7 additions & 0 deletions crates/erg_compiler/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,13 @@ impl Args {
pub fn insert_pos(&mut self, idx: usize, pos: PosArg) {
self.pos_args.insert(idx, pos);
}

pub fn get_kw(&self, keyword: &str) -> Option<&Expr> {
self.kw_args
.iter()
.find(|kw| kw.keyword.inspect() == keyword)
.map(|kw| &kw.expr)
}
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
Expand Down
6 changes: 6 additions & 0 deletions crates/erg_compiler/lib/std/_erg_builtins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# in Python 3.7, `sum` takes no keyword arguments
def sum(iterable, start=0):
s = start
for i in iterable:
s += i
return s
2 changes: 2 additions & 0 deletions crates/erg_compiler/lib/std/_erg_contains_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ def contains_operator(y, elem) -> bool:
return True
elif hasattr(y, "try_new") and is_ok(y.try_new(elem)):
return True
elif hasattr(y, "__origin__") and hasattr(y.__origin__, "type_check"):
return y.__origin__.type_check(elem, y)
# TODO: trait check
return False
# [1] in [Int]
Expand Down

0 comments on commit aacdca3

Please sign in to comment.