Skip to content

Commit fd8eda2

Browse files
committed
Complement _typing module
1 parent 5ff4eb0 commit fd8eda2

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

vm/src/stdlib/typing.rs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
1-
pub(crate) use _typing::make_module;
1+
use crate::{
2+
builtins::{PyModule, PyTypeRef},
3+
types::{Constructor, Representable},
4+
PyRef, PyResult, VirtualMachine,
5+
};
6+
7+
pub(crate) fn make_module(vm: &VirtualMachine) -> PyRef<PyModule> {
8+
let module = _typing::make_module(vm);
9+
module
10+
.set_attr("NoDefault", vm.ctx.no_default.clone(), vm)
11+
.unwrap();
12+
module
13+
}
214

315
#[pymodule]
416
pub(crate) mod _typing {
@@ -172,3 +184,30 @@ pub(crate) mod _typing {
172184
// }
173185
// }
174186
}
187+
188+
#[pyclass(module = false, name)]
189+
#[derive(Debug, PyPayload)]
190+
#[allow(dead_code)]
191+
pub struct NoDefaultType {}
192+
193+
#[pyclass(with(Constructor, Representable))]
194+
impl NoDefaultType {
195+
#[pymethod(magic)]
196+
fn reduce() -> String {
197+
"NoDefault".to_string()
198+
}
199+
}
200+
201+
impl Constructor for NoDefaultType {
202+
type Args = ();
203+
204+
fn py_new(_: PyTypeRef, _args: Self::Args, vm: &VirtualMachine) -> PyResult {
205+
Ok(vm.ctx.no_default.clone().into())
206+
}
207+
}
208+
209+
impl Representable for NoDefaultType {
210+
fn repr_str(_zelf: &crate::Py<Self>, _vm: &VirtualMachine) -> crate::PyResult<String> {
211+
Ok("typing.NoDefault".to_owned())
212+
}
213+
}

vm/src/types/zoo.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::{
99
union_, weakproxy, weakref, zip,
1010
},
1111
class::StaticType,
12+
stdlib::typing,
1213
vm::Context,
1314
Py,
1415
};
@@ -92,6 +93,7 @@ pub struct TypeZoo {
9293
pub generic_alias_type: &'static Py<PyType>,
9394
pub union_type: &'static Py<PyType>,
9495
pub member_descriptor_type: &'static Py<PyType>,
96+
pub no_default_type: &'static Py<PyType>,
9597

9698
// RustPython-original types
9799
pub method_def: &'static Py<PyType>,
@@ -184,6 +186,8 @@ impl TypeZoo {
184186
union_type: union_::PyUnion::init_builtin_type(),
185187
member_descriptor_type: descriptor::PyMemberDescriptor::init_builtin_type(),
186188

189+
no_default_type: typing::NoDefaultType::init_builtin_type(),
190+
187191
method_def: crate::function::HeapMethodDef::init_builtin_type(),
188192
}
189193
}

vm/src/vm/context.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use crate::{
2222
},
2323
intern::{InternableString, MaybeInternedString, StringPool},
2424
object::{Py, PyObjectPayload, PyObjectRef, PyPayload, PyRef},
25+
stdlib::typing::NoDefaultType,
2526
types::{PyTypeFlags, PyTypeSlots, TypeZoo},
2627
PyResult, VirtualMachine,
2728
};
@@ -41,6 +42,7 @@ pub struct Context {
4142
pub empty_bytes: PyRef<PyBytes>,
4243
pub ellipsis: PyRef<PyEllipsis>,
4344
pub not_implemented: PyRef<PyNotImplemented>,
45+
pub no_default: PyRef<NoDefaultType>,
4446

4547
pub types: TypeZoo,
4648
pub exceptions: exceptions::ExceptionZoo,
@@ -303,6 +305,7 @@ impl Context {
303305

304306
let empty_str = unsafe { string_pool.intern("", types.str_type.to_owned()) };
305307
let empty_bytes = create_object(PyBytes::from(Vec::new()), types.bytes_type);
308+
let no_default = create_object(NoDefaultType {}, types.no_default_type);
306309
Context {
307310
true_value,
308311
false_value,
@@ -311,6 +314,7 @@ impl Context {
311314
empty_frozenset,
312315
empty_str,
313316
empty_bytes,
317+
no_default,
314318

315319
ellipsis,
316320
not_implemented,

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy