E5E4 make_class -> make_static_type by youknowone · Pull Request #7331 · RustPython/RustPython · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/derive-impl/src/pymodule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@ impl ModuleItem for ClassItem {
class_meta.class_name()?
};
let class_new = quote_spanned!(ident.span() =>
let new_class = <#ident as ::rustpython_vm::class::PyClassImpl>::make_class(ctx);
let new_class = <#ident as ::rustpython_vm::class::PyClassImpl>::make_static_type();
// Only set __module__ string if the class doesn't already have a
// getset descriptor for __module__ (which provides instance-level
// module resolution, e.g. TypeAliasType)
Expand Down Expand Up @@ -850,7 +850,7 @@ impl ModuleItem for StructSequenceItem {

// Generate the class creation code
let class_new = quote_spanned!(pytype_ident.span() =>
let new_class = <#pytype_ident as ::rustpython_vm::class::PyClassImpl>::make_class(ctx);
let new_class = <#pytype_ident as ::rustpython_vm::class::PyClassImpl>::make_static_type();
{
let module_key = rustpython_vm::identifier!(ctx, __module__);
let has_module_getset = new_class.attributes.read()
Expand Down
6 changes: 3 additions & 3 deletions crates/stdlib/src/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ mod decl {
#[cfg(unix)]
{
use crate::vm::class::PyClassImpl;
poll::PyPoll::make_class(&vm.ctx);
poll::PyPoll::make_static_type();
}

__module_exec(vm, module);
Expand Down Expand Up @@ -527,9 +527,9 @@ mod decl {

#[cfg(any(target_os = "linux", target_os = "android", target_os = "redox"))]
#[pyattr(name = "epoll", once)]
fn epoll(vm: &VirtualMachine) -> PyTypeRef {
fn epoll(_vm: &VirtualMachine) -> PyTypeRef {
use crate::vm::class::PyClassImpl;
epoll::PyEpoll::make_class(&vm.ctx)
epoll::PyEpoll::make_static_type()
}

#[cfg(any(target_os = "linux", target_os = "android", target_os = "redox"))]
Expand Down
7 changes: 2 additions & 5 deletions crates/vm/src/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,16 +206,13 @@ pub trait PyClassImpl: PyClassDef {
class.extend_methods(class.slots.methods, ctx);
}

fn make_class(ctx: &Context) -> PyTypeRef
fn make_static_type() -> PyTypeRef
where
Self: StaticType + Sized,
{
(*Self::static_cell().get_or_init(|| {
let typ = Self::create_static_type();
// SAFETY: Context is heap-allocated via PyRc and stored in a static cell
// (Context::genesis), so it lives for 'static.
let ctx: &'static Context = unsafe { &*(ctx as *const Context) };
Self::extend_class(ctx, unsafe {
Self::extend_class(Context::genesis(), unsafe {
// typ will be saved in static_cell
let r: &Py<PyType> = &typ;
let r: &'static Py<PyType> = core::mem::transmute(r);
Expand Down
16 changes: 8 additions & 8 deletions crates/vm/src/stdlib/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,15 +301,15 @@ fn node_add_location(
}

/// Return the expected AST mod type class for a compile() mode string.
pub(crate) fn mode_type_and_name(
ctx: &Context,
mode: &str,
) -> Option<(PyRef<PyType>, &'static str)> {
pub(crate) fn mode_type_and_name(mode: &str) -> Option<(PyRef<PyType>, &'static str)> {
match mode {
"exec" => Some((pyast::NodeModModule::make_class(ctx), "Module")),
"eval" => Some((pyast::NodeModExpression::make_class(ctx), "Expression")),
"single" => Some((pyast::NodeModInteractive::make_class(ctx), "Interactive")),
"func_type" => Some((pyast::NodeModFunctionType::make_class(ctx), "FunctionType")),
"exec" => Some((pyast::NodeModModule::make_static_type(), "Module")),
"eval" => Some((pyast::NodeModExpression::make_static_type(), "Expression")),
"single" => Some((pyast::NodeModInteractive::make_static_type(), "Interactive")),
"func_type" => Some((
pyast::NodeModFunctionType::make_static_type(),
"FunctionType",
)),
_ => None,
}
}
Expand Down
252 changes: 126 additions & 126 deletions crates/vm/src/stdlib/ast/pyast.rs
2366
Original file line number Diff line number Diff line change
Expand Up @@ -1523,132 +1523,132 @@ const FIELD_TYPES: &[(&str, &[(&str, FieldType)])] = &[

pub fn extend_module_nodes(vm: &VirtualMachine, module: &Py<PyModule>) {
extend_module!(vm, module, {
"AST" => NodeAst::make_class(&vm.ctx),
"mod" => NodeMod::make_class(&vm.ctx),
"Module" => NodeModModule::make_class(&vm.ctx),
"Interactive" => NodeModInteractive::make_class(&vm.ctx),
"Expression" => NodeModExpression::make_class(&vm.ctx),
"FunctionType" => NodeModFunctionType::make_class(&vm.ctx),
"stmt" => NodeStmt::make_class(&vm.ctx),
"FunctionDef" => NodeStmtFunctionDef::make_class(&vm.ctx),
"AsyncFunctionDef" => NodeStmtAsyncFunctionDef::make_class(&vm.ctx),
"ClassDef" => NodeStmtClassDef::make_class(&vm.ctx),
"Return" => NodeStmtReturn::make_class(&vm.ctx),
"Delete" => NodeStmtDelete::make_class(&vm.ctx),
"Assign" => NodeStmtAssign::make_class(&vm.ctx),
"TypeAlias" => NodeStmtTypeAlias::make_class(&vm.ctx),
"AugAssign" => NodeStmtAugAssign::make_class(&vm.ctx),
"AnnAssign" => NodeStmtAnnAssign::make_class(&vm.ctx),
"For" => NodeStmtFor::make_class(&vm.ctx),
"AsyncFor" => NodeStmtAsyncFor::make_class(&vm.ctx),
"While" => NodeStmtWhile::make_class(&vm.ctx),
"If" => NodeStmtIf::make_class(&vm.ctx),
"With" => NodeStmtWith::make_class(&vm.ctx),
"AsyncWith" => NodeStmtAsyncWith::make_class(&vm.ctx),
"Match" => NodeStmtMatch::make_class(&vm.ctx),
"Raise" => NodeStmtRaise::make_class(&vm.ctx),
"Try" => NodeStmtTry::make_class(&vm.ctx),
"TryStar" => NodeStmtTryStar::make_class(&vm.ctx),
"Assert" => NodeStmtAssert::make_class(&vm.ctx),
"Import" => NodeStmtImport::make_class(&vm.ctx),
"ImportFrom" => NodeStmtImportFrom::make_class(&vm.ctx),
"Global" => NodeStmtGlobal::make_class(&vm.ctx),
"Nonlocal" => NodeStmtNonlocal::make_class(&vm.ctx),
"Expr" => NodeStmtExpr::make_class(&vm.ctx),
"Pass" => NodeStmtPass::make_class(&vm.ctx),
"Break" => NodeStmtBreak::make_class(&vm.ctx),
"Continue" => NodeStmtContinue::make_class(&vm.ctx),
"expr" => NodeExpr::make_class(&vm.ctx),
"BoolOp" => NodeExprBoolOp::make_class(&vm.ctx),
"NamedExpr" => NodeExprNamedExpr::make_class(&vm.ctx),
"BinOp" => NodeExprBinOp::make_class(&vm.ctx),
"UnaryOp" => NodeExprUnaryOp::make_class(&vm.ctx),
"Lambda" => NodeExprLambda::make_class(&vm.ctx),
"IfExp" => NodeExprIfExp::make_class(&vm.ctx),
"Dict" => NodeExprDict::make_class(&vm.ctx),
"Set" => NodeExprSet::make_class(&vm.ctx),
"ListComp" => NodeExprListComp::make_class(&vm.ctx),
"SetComp" => NodeExprSetComp::make_class(&vm.ctx),
"DictComp" => NodeExprDictComp::make_class(&vm.ctx),
"GeneratorExp" => NodeExprGeneratorExp::make_class(&vm.ctx),
"Await" => NodeExprAwait::make_class(&vm.ctx),
"Yield" => NodeExprYield::make_class(&vm.ctx),
"YieldFrom" => NodeExprYieldFrom::make_class(&vm.ctx),
"Compare" => NodeExprCompare::make_class(&vm.ctx),
"Call" => NodeExprCall::make_class(&vm.ctx),
"FormattedValue" => NodeExprFormattedValue::make_class(&vm.ctx),
"JoinedStr" => NodeExprJoinedStr::make_class(&vm.ctx),
"TemplateStr" => NodeExprTemplateStr::make_class(&vm.ctx),
"Interpolation" => NodeExprInterpolation::make_class(&vm.ctx),
"Constant" => NodeExprConstant::make_class(&vm.ctx),
"Attribute" => NodeExprAttribute::make_class(&vm.ctx),
"Subscript" => NodeExprSubscript::make_class(&vm.ctx),
"Starred" => NodeExprStarred::make_class(&vm.ctx),
"Name" => NodeExprName::make_class(&vm.ctx),
"List" => NodeExprList::make_class(&vm.ctx),
"Tuple" => NodeExprTuple::make_class(&vm.ctx),
"Slice" => NodeExprSlice::make_class(&vm.ctx),
"expr_context" => NodeExprContext::make_class(&vm.ctx),
"Load" => NodeExprContextLoad::make_class(&vm.ctx),
"Store" => NodeExprContextStore::make_class(&vm.ctx),
"Del" => NodeExprContextDel::make_class(&vm.ctx),
"boolop" => NodeBoolOp::make_class(&vm.ctx),
"And" => NodeBoolOpAnd::make_class(&vm.ctx),
"Or" => NodeBoolOpOr::make_class(&vm.ctx),
"operator" => NodeOperator::make_class(&vm.ctx),
"Add" => NodeOperatorAdd::make_class(&vm.ctx),
"Sub" => NodeOperatorSub::make_class(&vm.ctx),
"Mult" => NodeOperatorMult::make_class(&vm.ctx),
"MatMult" => NodeOperatorMatMult::make_class(&vm.ctx),
"Div" => NodeOperatorDiv::make_class(&vm.ctx),
"Mod" => NodeOperatorMod::make_class(&vm.ctx),
"Pow" => NodeOperatorPow::make_class(&vm.ctx),
"LShift" => NodeOperatorLShift::make_class(&vm.ctx),
"RShift" => NodeOperatorRShift::make_class(&vm.ctx),
"BitOr" => NodeOperatorBitOr::make_class(&vm.ctx),
"BitXor" => NodeOperatorBitXor::make_class(&vm.ctx),
"BitAnd" => NodeOperatorBitAnd::make_class(&vm.ctx),
"FloorDiv" => NodeOperatorFloorDiv::make_class(&vm.ctx),
"unaryop" => NodeUnaryOp::make_class(&vm.ctx),
"Invert" => NodeUnaryOpInvert::make_class(&vm.ctx),
"Not" => NodeUnaryOpNot::make_class(&vm.ctx),
"UAdd" => NodeUnaryOpUAdd::make_class(&vm.ctx),
"USub" => NodeUnaryOpUSub::make_class(&vm.ctx),
"cmpop" => NodeCmpOp::make_class(&vm.ctx),
"Eq" => NodeCmpOpEq::make_class(&vm.ctx),
"NotEq" => NodeCmpOpNotEq::make_class(&vm.ctx),
"Lt" => NodeCmpOpLt::make_class(&vm.ctx),
"LtE" => NodeCmpOpLtE::make_class(&vm.ctx),
"Gt" => NodeCmpOpGt::make_class(&vm.ctx),
"GtE" => NodeCmpOpGtE::make_class(&vm.ctx),
"Is" => NodeCmpOpIs::make_class(&vm.ctx),
"IsNot" => NodeCmpOpIsNot::make_class(&vm.ctx),
"In" => NodeCmpOpIn::make_class(&vm.ctx),
"NotIn" => NodeCmpOpNotIn::make_class(&vm.ctx),
"comprehension" => NodeComprehension::make_class(&vm.ctx),
"excepthandler" => NodeExceptHandler::make_class(&vm.ctx),
"ExceptHandler" => NodeExceptHandlerExceptHandler::make_class(&vm.ctx),
"arguments" => NodeArguments::make_class(&vm.ctx),
"arg" => NodeArg::make_class(&vm.ctx),
"keyword" => NodeKeyword::make_class(&vm.ctx),
"alias" => NodeAlias::make_class(&vm.ctx),
"withitem" => NodeWithItem::make_class(&vm.ctx),
"match_case" => NodeMatchCase::make_class(&vm.ctx),
"pattern" => NodePattern::make_class(&vm.ctx),
"MatchValue" => NodePatternMatchValue::make_class(&vm.ctx),
"MatchSingleton" => NodePatternMatchSingleton::make_class(&vm.ctx),
"MatchSequence" => NodePatternMatchSequence::make_class(&vm.ctx),
"MatchMapping" => NodePatternMatchMapping::make_class(&vm.ctx),
"MatchClass" => NodePatternMatchClass::make_class(&vm.ctx),
"MatchStar" => NodePatternMatchStar::make_class(&vm.ctx),
"MatchAs" => NodePatternMatchAs::make_class(&vm.ctx),
"MatchOr" => NodePatternMatchOr::make_class(&vm.ctx),
"type_ignore" => NodeTypeIgnore::make_class(&vm.ctx),
"TypeIgnore" => NodeTypeIgnoreTypeIgnore::make_class(&vm.ctx),
"type_param" => NodeTypeParam::make_class(&vm.ctx),
"TypeVar" => NodeTypeParamTypeVar::make_class(&vm.ctx),
"ParamSpec" => NodeTypeParamParamSpec::make_class(&vm.ctx),
"TypeVarTuple" => NodeTypeParamTypeVarTuple::make_class(&vm.ctx),
"AST" => NodeAst::make_static_type(),
"mod" => NodeMod::make_static_type(),
"Module" => NodeModModule::make_static_type(),
"Interactive" => NodeModInteractive::make_static_type(),
"Expression" => NodeModExpression::make_static_type(),
"FunctionType" => NodeModFunctionType::make_static_type(),
"stmt" => NodeStmt::make_static_type(),
"FunctionDef" => NodeStmtFunctionDef::make_static_type(),
"AsyncFunctionDef" => NodeStmtAsyncFunctionDef::make_static_type(),
"ClassDef" => NodeStmtClassDef::make_static_type(),
"Return" => NodeStmtReturn::make_static_type(),
"Delete" => NodeStmtDelete::make_static_type(),
"Assign" => NodeStmtAssign::make_static_type(),
"TypeAlias" => NodeStmtTypeAlias::make_static_type(),
"AugAssign" => NodeStmtAugAssign::make_static_type(),
"AnnAssign" => NodeStmtAnnAssign::make_static_type(),
"For" => NodeStmtFor::make_static_type(),
"AsyncFor" => NodeStmtAsync E85A For::make_static_type(),
"While" => NodeStmtWhile::make_static_type(),
"If" => NodeStmtIf::make_static_type(),
"With" => NodeStmtWith::make_static_type(),
"AsyncWith" => NodeStmtAsyncWith::make_static_type(),
"Match" => NodeStmtMatch::make_static_type(),
"Raise" => NodeStmtRaise::make_static_type(),
"Try" => NodeStmtTry::make_static_type(),
"TryStar" => NodeStmtTryStar::make_static_type(),
"Assert" => NodeStmtAssert::make_static_type(),
"Import" => NodeStmtImport::make_static_type(),
"ImportFrom" => NodeStmtImportFrom::make_static_type(),
"Global" => NodeStmtGlobal::make_static_type(),
"Nonlocal" => NodeStmtNonlocal::make_static_type(),
"Expr" => NodeStmtExpr::make_static_type(),
"Pass" => NodeStmtPass::make_static_type(),
"Break" => NodeStmtBreak::make_static_type(),
"Continue" => NodeStmtContinue::make_static_type(),
"expr" => NodeExpr::make_static_type(),
"BoolOp" => NodeExprBoolOp::make_static_type(),
"NamedExpr" => NodeExprNamedExpr::make_static_type(),
"BinOp" => NodeExprBinOp::make_static_type(),
"UnaryOp" => NodeExprUnaryOp::make_static_type(),
"Lambda" => NodeExprLambda::make_static_type(),
"IfExp" => NodeExprIfExp::make_static_type(),
"Dict" => NodeExprDict::make_static_type(),
"Set" => NodeExprSet::make_static_type(),
"ListComp" => NodeExprListComp::make_static_type(),
"SetComp" => NodeExprSetComp::make_static_type(),
"DictComp" => NodeExprDictComp::make_static_type(),
"GeneratorExp" => NodeExprGeneratorExp::make_static_type(),
"Await" => NodeExprAwait::make_static_type(),
"Yield" => NodeExprYield::make_static_type(),
"YieldFrom" => NodeExprYieldFrom::make_static_type(),
"Compare" => NodeExprCompare::make_static_type(),
"Call" => NodeExprCall::make_static_type(),
"FormattedValue" => NodeExprFormattedValue::make_static_type(),
"JoinedStr" => NodeExprJoinedStr::make_static_type(),
"TemplateStr" => NodeExprTemplateStr::make_static_type(),
"Interpolation" => NodeExprInterpolation::make_static_type(),
"Constant" => NodeExprConstant::make_static_type(),
"Attribute" => NodeExprAttribute::make_static_type(),
"Subscript" => NodeExprSubscript::make_static_type(),
"Starred" => NodeExprStarred::make_static_type(),
"Name" => NodeExprName::make_static_type(),
"List" => NodeExprList::make_static_type(),
"Tuple" => NodeExprTuple::make_static_type(),
"Slice" => NodeExprSlice::make_static_type(),
"expr_context" => NodeExprContext::make_static_type(),
"Load" => NodeExprContextLoad::make_static_type(),
"Store" => NodeExprContextStore::make_static_type(),
"Del" => NodeExprContextDel::make_static_type(),
"boolop" => NodeBoolOp::make_static_type(),
"And" => NodeBoolOpAnd::make_static_type(),
"Or" => NodeBoolOpOr::make_static_type(),
"operator" => NodeOperator::make_static_type(),
"Add" => NodeOperatorAdd::make_static_type(),
"Sub" => NodeOperatorSub::make_static_type(),
"Mult" => NodeOperatorMult::make_static_type(),
"MatMult" => NodeOperatorMatMult::make_static_type(),
"Div" => NodeOperatorDiv::make_static_type(),
"Mod" => NodeOperatorMod::make_static_type(),
"Pow" => NodeOperatorPow::make_static_type(),
"LShift" => NodeOperatorLShift::make_static_type(),
"RShift" => NodeOperatorRShift::make_static_type(),
"BitOr" => NodeOperatorBitOr::make_static_type(),
"BitXor" => NodeOperatorBitXor::make_static_type(),
"BitAnd" => NodeOperatorBitAnd::make_static_type(),
"FloorDiv" => NodeOperatorFloorDiv::make_static_type(),
"unaryop" => NodeUnaryOp::make_static_type(),
"Invert" => NodeUnaryOpInvert::make_static_type(),
"Not" => NodeUnaryOpNot::make_static_type(),
"UAdd" => NodeUnaryOpUAdd::make_static_type(),
"USub" => NodeUnaryOpUSub::make_static_type(),
"cmpop" => NodeCmpOp::make_static_type(),
"Eq" => NodeCmpOpEq::make_static_type(),
"NotEq" => NodeCmpOpNotEq::make_static_type(),
"Lt" => NodeCmpOpLt::make_static_type(),
"LtE" => NodeCmpOpLtE::make_static_type(),
"Gt" => NodeCmpOpGt::make_static_type(),
"GtE" => NodeCmpOpGtE::make_static_type(),
"Is" => NodeCmpOpIs::make_static_type(),
"IsNot" => NodeCmpOpIsNot::make_static_type(),
"In" => NodeCmpOpIn::make_static_type(),
"NotIn" => NodeCmpOpNotIn::make_static_type(),
"comprehension" => NodeComprehension::make_static_type(),
"excepthandler" => NodeExceptHandler::make_static_type(),
"ExceptHandler" => NodeExceptHandlerExceptHandler::make_static_type(),
"arguments" => NodeArguments::make_static_type(),
"arg" => NodeArg::make_static_type(),
"keyword" => NodeKeyword::make_static_type(),
"alias" => NodeAlias::make_static_type(),
"withitem" => NodeWithItem::make_static_type(),
"match_case" => NodeMatchCase::make_static_type(),
"pattern" => NodePattern::make_static_type(),
"MatchValue" => NodePatternMatchValue::make_static_type(),
"MatchSingleton" => NodePatternMatchSingleton::make_static_type(),
"MatchSequence" => NodePatternMatchSequence::make_static_type(),
"MatchMapping" => NodePatternMatchMapping::make_static_type(),
"MatchClass" => NodePatternMatchClass::make_static_type(),
"MatchStar" => NodePatternMatchStar::make_static_type(),
"MatchAs" => NodePatternMatchAs::make_static_type(),
"MatchOr" => NodePatternMatchOr::make_static_type(),
"type_ignore" => NodeTypeIgnore::make_static_type(),
"TypeIgnore" => NodeTypeIgnoreTypeIgnore::make_static_type(),
"type_param" => NodeTypeParam::make_static_type(),
"TypeVar" => NodeTypeParamTypeVar::make_static_type(),
"ParamSpec" => NodeTypeParamParamSpec::make_static_type(),
"TypeVarTuple" => NodeTypeParamTypeVarTuple::make_static_type(),
});

// Populate _field_types with real Python type objects
Expand Down
4 changes: 2 additions & 2 deletions crates/vm/src/stdlib/ast/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ Support for arbitrary keyword arguments is deprecated and will be removed in Pyt
field_types.map(|ft| ft.downcast::<crate::builtins::PyDict>())
{
let expr_ctx_type: PyObjectRef =
super::super::pyast::NodeExprContext::make_class(&vm.ctx).into();
super::super::pyast::NodeExprContext::make_static_type().into();

for field in &fields {
if set_fields.contains(field.as_str()) {
Expand All @@ -382,7 +382,7 @@ Support for arbitrary keyword arguments is deprecated and will be removed in Pyt
} else if ftype.is(&expr_ctx_type) {
// expr_context — default to Load()
let load_type =
super::super::pyast::NodeExprContextLoad::make_class(&vm.ctx);
super::super::pyast::NodeExprContextLoad::make_static_type();
let load_instance = load_type
.get_attr(vm.ctx.intern_str("_instance"))
.unwrap_or_else(|| {
Expand Down
Loading
0