Skip to content

Commit c10e646

Browse files
committed
add missing dir in GenericAlias
Signed-off-by: snowapril <[email protected]>
1 parent 213ffd9 commit c10e646

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

vm/src/builtins/genericalias.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
use crate::{
2-
builtins::{PyStr, PyTuple, PyTupleRef, PyTypeRef},
2+
builtins::{PyList, PyStr, PyTuple, PyTupleRef, PyTypeRef},
33
common::hash,
4+
function::IntoPyObject,
45
slots::{Hashable, SlotConstructor},
56
IdProtocol, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject,
67
TypeProtocol, VirtualMachine,
78
};
89
use std::fmt;
910

11+
static ATTR_EXCEPTIONS: [&str; 8] = [
12+
"__origin__",
13+
"__args__",
14+
"__parameters__",
15+
"__mro_entries__",
16+
"__reduce_ex__", // needed so we don't look up object.__reduce_ex__
17+
"__reduce__",
18+
"__copy__",
19+
"__deepcopy__",
20+
];
21+
1022
#[pyclass(module = "types", name = "GenericAlias")]
1123
pub struct PyGenericAlias {
1224
origin: PyTypeRef,
@@ -111,6 +123,17 @@ impl PyGenericAlias {
111123
fn origin(&self) -> PyObjectRef {
112124
self.origin.as_object().clone()
113125
}
126+
127+
#[pymethod(magic)]
128+
fn dir(&self, vm: &VirtualMachine) -> PyResult<PyList> {
129+
let dir = vm.dir(Some(self.origin()))?;
130+
for exc in ATTR_EXCEPTIONS.iter() {
131+
if !dir.contains((*exc).into_pyobject(vm), vm)? {
132+
dir.append((*exc).into_pyobject(vm));
133+
}
134+
}
135+
Ok(dir)
136+
}
114137
}
115138

116139
fn is_typevar(obj: PyObjectRef) -> bool {

vm/src/builtins/list.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ impl PyList {
265265
}
266266

267267
#[pymethod(magic)]
268-
fn contains(&self, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult<bool> {
268+
pub fn contains(&self, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult<bool> {
269269
// TODO: to_vec() cause copy which leads to cost O(N). It need to be improved.
270270
let elements = self.borrow_vec().to_vec();
271271
for elem in elements.iter() {

0 commit comments

Comments
 (0)