Skip to content

Commit f783149

Browse files
committed
Add stub implementation of atexit
1 parent ae0a343 commit f783149

File tree

4 files changed

+59
-9
lines changed

4 files changed

+59
-9
lines changed

Lib/atexit.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Dummy implementation of atexit
2+
3+
4+
def register(func, *args, **kwargs):
5+
return func
6+
7+
8+
def unregister(func):
9+
pass

vm/src/macros.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,22 +116,22 @@ macro_rules! no_kwargs {
116116

117117
#[macro_export]
118118
macro_rules! py_module {
119-
( $vm:expr, $module_name:expr, { $($name:expr => $value:expr),* $(,)* }) => {{
119+
( $vm:expr, $module_name:expr, { $($name:expr => $value:expr),* $(,)? }) => {{
120120
let module = $vm.new_module($module_name, $vm.ctx.new_dict());
121-
$(
122-
$vm.set_attr(&module, $name, $value).unwrap();
123-
)*
121+
$crate::extend_module!($vm, module, { $($name => $value),* });
124122
module
125123
}};
126124
}
127125

128126
#[macro_export]
129127
macro_rules! extend_module {
130-
( $vm:expr, $module:expr, { $($name:expr => $value:expr),* $(,)* }) => {
128+
( $vm:expr, $module:expr, { $($name:expr => $value:expr),* $(,)? }) => {{
129+
#[allow(unused_variables)]
130+
let module: &$crate::pyobject::PyObjectRef = &$module;
131131
$(
132-
$vm.set_attr(&$module, $name, $value).unwrap();
132+
$vm.__module_set_attr(&module, $name, $value).unwrap();
133133
)*
134-
}
134+
}};
135135
}
136136

137137
#[macro_export]

vm/src/obj/objfunction.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,24 @@ pub struct PyMethod {
6464
// TODO: these shouldn't be public
6565
pub object: PyObjectRef,
6666
pub function: PyObjectRef,
67+
pub actually_bind: bool,
6768
}
6869

6970
impl PyMethod {
7071
pub fn new(object: PyObjectRef, function: PyObjectRef) -> Self {
71-
PyMethod { object, function }
72+
PyMethod {
73+
object,
74+
function,
75+
actually_bind: true,
76+
}
77+
}
78+
79+
pub fn new_nobind(object: PyObjectRef, function: PyObjectRef) -> Self {
80+
PyMethod {
81+
object,
82+
function,
83+
actually_bind: false,
84+
}
7285
}
7386

7487
fn getattribute(&self, name: PyStringRef, vm: &VirtualMachine) -> PyResult {

vm/src/vm.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,9 +580,15 @@ impl VirtualMachine {
580580
} else if let Some(PyMethod {
581581
ref function,
582582
ref object,
583+
actually_bind,
583584
}) = func_ref.payload()
584585
{
585-
self.invoke(&function, args.insert(object.clone()))
586+
let args = if *actually_bind {
587+
args.insert(object.clone())
588+
} else {
589+
args
590+
};
591+
self.invoke(&function, args)
586592
} else if let Some(PyBuiltinFunction { ref value }) = func_ref.payload() {
587593
value(self, args)
588594
} else if self.is_callable(&func_ref) {
@@ -1303,6 +1309,28 @@ impl VirtualMachine {
13031309
};
13041310
Ok(value)
13051311
}
1312+
1313+
#[doc(hidden)]
1314+
pub fn __module_set_attr(
1315+
&self,
1316+
module: &PyObjectRef,
1317+
attr_name: impl TryIntoRef<PyString>,
1318+
attr_value: impl Into<PyObjectRef>,
1319+
) -> PyResult<()> {
1320+
let val = attr_value.into();
1321+
let val = if val
1322+
.class()
1323+
.is(&self.ctx.types.builtin_function_or_method_type)
1324+
{
1325+
PyMethod::new_nobind(module.clone(), val)
1326+
.into_ref(self)
1327+
.into_object()
1328+
} else {
1329+
val
1330+
};
1331+
self.set_attr(module, attr_name, val)?;
1332+
Ok(())
1333+
}
13061334
}
13071335

13081336
impl Default for VirtualMachine {

0 commit comments

Comments
 (0)