Skip to content

Commit a4e69dc

Browse files
authored
Merge pull request RustPython#3296 from DimitrisJim/internal_types_unconstructible
Mark internal types as not constructible.
2 parents 2ac85e5 + 37ba1b8 commit a4e69dc

File tree

8 files changed

+44
-21
lines changed

8 files changed

+44
-21
lines changed

Lib/test/test_xml_etree.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3028,8 +3028,6 @@ def test_getiterator(self):
30283028
self.assertEqual(summarize_list(doc.getiterator(None)), all_tags)
30293029
self.assertEqual(summarize_list(doc.getiterator('*')), all_tags)
30303030

3031-
# TODO: RUSTPYTHON
3032-
@unittest.expectedFailure
30333031
def test_copy(self):
30343032
a = ET.Element('a')
30353033
it = a.iter()

extra_tests/snippets/forbidden_instantiation.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
from typing import Type
2+
from types import (
3+
GeneratorType, CoroutineType, AsyncGeneratorType, BuiltinFunctionType,
4+
BuiltinMethodType, WrapperDescriptorType, MethodWrapperType, MethodDescriptorType,
5+
ClassMethodDescriptorType, FrameType, GetSetDescriptorType, MemberDescriptorType
6+
)
27
from testutils import assert_raises
38

49
def check_forbidden_instantiation(typ, reverse=False):
@@ -11,8 +16,26 @@ def check_forbidden_instantiation(typ, reverse=False):
1116
iter_types = [list, set, str, bytearray, bytes, dict, tuple, lambda: range(0), dict_items, dict_values]
1217
# types with custom backwards iterators
1318
reviter_types = [list, dict, lambda: range(0), dict_values, dict_items]
19+
# internal types:
20+
internal_types = [
21+
GeneratorType,
22+
CoroutineType,
23+
AsyncGeneratorType,
24+
BuiltinFunctionType,
25+
BuiltinMethodType, # same as MethodWrapperType
26+
WrapperDescriptorType,
27+
MethodWrapperType,
28+
MethodDescriptorType,
29+
ClassMethodDescriptorType,
30+
FrameType,
31+
GetSetDescriptorType, # same as MemberDescriptorType
32+
MemberDescriptorType
33+
]
1434

1535
for typ in iter_types:
1636
check_forbidden_instantiation(typ)
1737
for typ in reviter_types:
1838
check_forbidden_instantiation(typ, reverse=True)
39+
for typ in internal_types:
40+
with assert_raises(TypeError):
41+
typ()

vm/src/builtins/asyncgenerator.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
frame::FrameRef,
66
function::OptionalArg,
77
protocol::PyIterReturn,
8-
types::{IterNext, IterNextIterable},
8+
types::{Constructor, IterNext, IterNextIterable, Unconstructible},
99
IdProtocol, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol,
1010
VirtualMachine,
1111
};
@@ -26,7 +26,7 @@ impl PyValue for PyAsyncGen {
2626
}
2727
}
2828

29-
#[pyimpl]
29+
#[pyimpl(with(Constructor))]
3030
impl PyAsyncGen {
3131
pub fn as_coro(&self) -> &Coro {
3232
&self.inner
@@ -124,6 +124,7 @@ impl PyAsyncGen {
124124
self.inner.frame().code.clone()
125125
}
126126
}
127+
impl Unconstructible for PyAsyncGen {}
127128

128129
#[pyclass(module = false, name = "async_generator_wrapped_value")]
129130
#[derive(Debug)]

vm/src/builtins/builtinfunc.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::{pytype, PyClassMethod, PyStr, PyStrRef, PyTypeRef};
22
use crate::{
33
builtins::PyBoundMethod,
44
function::{FuncArgs, IntoPyNativeFunc, PyNativeFunc},
5-
types::{Callable, GetDescriptor},
5+
types::{Callable, Constructor, GetDescriptor, Unconstructible},
66
PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol, VirtualMachine,
77
};
88
use std::fmt;
@@ -99,7 +99,7 @@ impl Callable for PyBuiltinFunction {
9999
}
100100
}
101101

102-
#[pyimpl(with(Callable), flags(HAS_DICT))]
102+
#[pyimpl(with(Callable, Constructor), flags(HAS_DICT))]
103103
impl PyBuiltinFunction {
104104
#[pyproperty(magic)]
105105
fn module(&self, vm: &VirtualMachine) -> PyObjectRef {
@@ -142,6 +142,7 @@ impl PyBuiltinFunction {
142142
})
143143
}
144144
}
145+
impl Unconstructible for PyBuiltinFunction {}
145146

146147
// `PyBuiltinMethod` is similar to both `PyMethodDescrObject` in
147148
// https://github.com/python/cpython/blob/main/Include/descrobject.h
@@ -207,7 +208,7 @@ impl PyBuiltinMethod {
207208
}
208209
}
209210

210-
#[pyimpl(with(GetDescriptor, Callable), flags(METHOD_DESCR))]
211+
#[pyimpl(with(GetDescriptor, Callable, Constructor), flags(METHOD_DESCR))]
211212
impl PyBuiltinMethod {
212213
#[pyproperty(magic)]
213214
fn name(&self) -> PyStrRef {
@@ -237,6 +238,7 @@ impl PyBuiltinMethod {
237238
)
238239
}
239240
}
241+
impl Unconstructible for PyBuiltinMethod {}
240242

241243
pub fn init(context: &PyContext) {
242244
PyBuiltinFunction::extend_class(context, &context.types.builtin_function_or_method_type);

vm/src/builtins/coroutine.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::{
44
frame::FrameRef,
55
function::OptionalArg,
66
protocol::PyIterReturn,
7-
types::{IterNext, IterNextIterable},
7+
types::{Constructor, IterNext, IterNextIterable, Unconstructible},
88
IdProtocol, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine,
99
};
1010

@@ -21,7 +21,7 @@ impl PyValue for PyCoroutine {
2121
}
2222
}
2323

24-
#[pyimpl(with(IterNext))]
24+
#[pyimpl(with(Constructor, IterNext))]
2525
impl PyCoroutine {
2626
pub fn as_coro(&self) -> &Coro {
2727
&self.inner
@@ -103,6 +103,7 @@ impl PyCoroutine {
103103
None
104104
}
105105
}
106+
impl Unconstructible for PyCoroutine {}
106107

107108
impl IterNextIterable for PyCoroutine {}
108109
impl IterNext for PyCoroutine {

vm/src/builtins/frame.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,23 @@
22
33
*/
44

5-
use super::{PyCode, PyDictRef, PyStrRef, PyTypeRef};
5+
use super::{PyCode, PyDictRef, PyStrRef};
66
use crate::{
77
frame::{Frame, FrameRef},
8-
function::FuncArgs,
8+
types::{Constructor, Unconstructible},
99
IdProtocol, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, VirtualMachine,
1010
};
1111

1212
pub fn init(context: &PyContext) {
1313
FrameRef::extend_class(context, &context.types.frame_type);
1414
}
1515

16-
#[pyimpl(with(PyRef))]
16+
#[pyimpl(with(Constructor, PyRef))]
1717
impl Frame {}
18+
impl Unconstructible for Frame {}
1819

1920
#[pyimpl]
2021
impl FrameRef {
21-
#[pyslot]
22-
fn slot_new(_cls: PyTypeRef, _args: FuncArgs, vm: &VirtualMachine) -> PyResult {
23-
Err(vm.new_type_error("Cannot directly create frame object".to_owned()))
24-
}
25-
2622
#[pymethod(magic)]
2723
fn repr(self) -> String {
2824
"<frame object at .. >".to_owned()

vm/src/builtins/generator.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{
88
frame::FrameRef,
99
function::OptionalArg,
1010
protocol::PyIterReturn,
11-
types::{IterNext, IterNextIterable},
11+
types::{Constructor, IterNext, IterNextIterable, Unconstructible},
1212
IdProtocol, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine,
1313
};
1414

@@ -24,7 +24,7 @@ impl PyValue for PyGenerator {
2424
}
2525
}
2626

27-
#[pyimpl(with(IterNext))]
27+
#[pyimpl(with(Constructor, IterNext))]
2828
impl PyGenerator {
2929
pub fn as_coro(&self) -> &Coro {
3030
&self.inner
@@ -95,6 +95,7 @@ impl PyGenerator {
9595
self.inner.frame().yield_from_target()
9696
}
9797
}
98+
impl Unconstructible for PyGenerator {}
9899

99100
impl IterNextIterable for PyGenerator {}
100101
impl IterNext for PyGenerator {

vm/src/builtins/getset.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use super::PyTypeRef;
55
use crate::{
66
function::{IntoPyResult, OwnedParam, RefParam},
7-
types::GetDescriptor,
7+
types::{Constructor, GetDescriptor, Unconstructible},
88
PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyThreadingConstraint, PyValue,
99
TryFromObject, TypeProtocol, VirtualMachine,
1010
};
@@ -300,7 +300,7 @@ impl PyGetSet {
300300
}
301301
}
302302

303-
#[pyimpl(with(GetDescriptor))]
303+
#[pyimpl(with(GetDescriptor, Constructor))]
304304
impl PyGetSet {
305305
// Descriptor methods
306306

@@ -361,6 +361,7 @@ impl PyGetSet {
361361
format!("{}.{}", self.class.slot_name(), self.name.clone())
362362
}
363363
}
364+
impl Unconstructible for PyGetSet {}
364365

365366
pub(crate) fn init(context: &PyContext) {
366367
PyGetSet::extend_class(context, &context.types.getset_type);

0 commit comments

Comments
 (0)