Raise syntax error when saving a value to __debug__.

This commit is contained in:
HyeockJinKim 2021-08-27 23:27:11 +09:00
parent ca37219aff
commit 94e77ef8a6

View file

@ -33,6 +33,7 @@ enum CallType {
Keyword { nargs: u32 }, Keyword { nargs: u32 },
Ex { has_kwargs: bool }, Ex { has_kwargs: bool },
} }
impl CallType { impl CallType {
fn normal_call(self) -> Instruction { fn normal_call(self) -> Instruction {
match self { match self {
@ -69,6 +70,7 @@ pub struct CompileOpts {
/// not emit assert statements /// not emit assert statements
pub optimize: u8, pub optimize: u8,
} }
impl Default for CompileOpts { impl Default for CompileOpts {
fn default() -> Self { fn default() -> Self {
CompileOpts { optimize: 0 } CompileOpts { optimize: 0 }
@ -383,8 +385,14 @@ impl Compiler {
self.compile_name(name, NameUsage::Load) self.compile_name(name, NameUsage::Load)
} }
fn store_name(&mut self, name: &str) { fn store_name(&mut self, name: &str) -> CompileResult<()> {
self.compile_name(name, NameUsage::Store) if name.eq("__debug__") {
return Err(self.error(CompileErrorType::SyntaxError(
"cannot assign to __debug__".to_owned(),
)));
}
self.compile_name(name, NameUsage::Store);
Ok(())
} }
fn mangle<'a>(&self, name: &'a str) -> Cow<'a, str> { fn mangle<'a>(&self, name: &'a str) -> Cow<'a, str> {
@ -491,9 +499,9 @@ impl Compiler {
let idx = self.name(part); let idx = self.name(part);
self.emit(Instruction::LoadAttr { idx }); self.emit(Instruction::LoadAttr { idx });
} }
self.store_name(alias); self.store_name(alias)?;
} else { } else {
self.store_name(name.name.split('.').next().unwrap()); self.store_name(name.name.split('.').next().unwrap())?;
} }
} }
} }
@ -549,9 +557,9 @@ impl Compiler {
// Store module under proper name: // Store module under proper name:
if let Some(alias) = &name.asname { if let Some(alias) = &name.asname {
self.store_name(alias); self.store_name(alias)?;
} else { } else {
self.store_name(&name.name); self.store_name(&name.name)?;
} }
} }
@ -933,7 +941,7 @@ impl Compiler {
// We have a match, store in name (except x as y) // We have a match, store in name (except x as y)
if let Some(alias) = name { if let Some(alias) = name {
self.store_name(alias); self.store_name(alias)?;
} else { } else {
// Drop exception from top of stack: // Drop exception from top of stack:
self.emit(Instruction::Pop); self.emit(Instruction::Pop);
@ -1104,7 +1112,7 @@ impl Compiler {
self.apply_decorators(decorator_list); self.apply_decorators(decorator_list);
self.store_name(name); self.store_name(name)?;
Ok(()) Ok(())
} }
@ -1268,7 +1276,7 @@ impl Compiler {
self.apply_decorators(decorator_list); self.apply_decorators(decorator_list);
self.store_name(name); self.store_name(name)?;
Ok(()) Ok(())
} }
@ -1554,7 +1562,7 @@ impl Compiler {
fn compile_store(&mut self, target: &ast::Expr) -> CompileResult<()> { fn compile_store(&mut self, target: &ast::Expr) -> CompileResult<()> {
match &target.node { match &target.node {
ast::ExprKind::Name { id, .. } => { ast::ExprKind::Name { id, .. } => {
self.store_name(id); self.store_name(id)?;
} }
ast::ExprKind::Subscript { value, slice, .. } => { ast::ExprKind::Subscript { value, slice, .. } => {
self.compile_expression(value)?; self.compile_expression(value)?;
@ -1610,7 +1618,7 @@ impl Compiler {
"starred assignment target must be in a list or tuple".to_owned(), "starred assignment target must be in a list or tuple".to_owned(),
), ),
_ => CompileErrorType::Assign(target.node.name()), _ => CompileErrorType::Assign(target.node.name()),
})) }));
} }
} }
@ -1909,10 +1917,10 @@ impl Compiler {
YieldFrom { value } => { YieldFrom { value } => {
match self.ctx.func { match self.ctx.func {
FunctionContext::NoFunction => { FunctionContext::NoFunction => {
return Err(self.error(CompileErrorType::InvalidYieldFrom)) return Err(self.error(CompileErrorType::InvalidYieldFrom));
} }
FunctionContext::AsyncFunction => { FunctionContext::AsyncFunction => {
return Err(self.error(CompileErrorType::AsyncYieldFrom)) return Err(self.error(CompileErrorType::AsyncYieldFrom));
} }
FunctionContext::Function => {} FunctionContext::Function => {}
} }