Fix global behavior on class (#4234)

This commit is contained in:
Kuan-Chun Wang authored and GitHub committed 2022-10-21 13:06:50 +09:00
1 parent 2868e874c9
commit dda6f86ba3
5 files changed
+12 -25

No files matched your search

-2
View File
@@ -1925,8 +1925,6 @@ class TestCase(unittest.TestCase):
# Check MRO resolution.
self.assertEqual(Child.__mro__, (Child, Parent, Generic, object))
# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_dataclasses_pickleable(self):
global P, Q, R
@dataclass
-10
View File
@@ -110,11 +110,6 @@ class PyPicklerTests(AbstractPickleTests, unittest.TestCase):
def test_in_band_buffers(self): # TODO: RUSTPYTHON, remove when this passes
super().test_in_band_buffers() # TODO: RUSTPYTHON, remove when this passes
# TODO: RUSTPYTHON, pickle.PicklingError
@unittest.expectedFailure
def test_nested_names(self): # TODO: RUSTPYTHON, remove when this passes
super().test_nested_names() # TODO: RUSTPYTHON, remove when this passes
# TODO: RUSTPYTHON, AttributeError: module 'pickle' has no attribute 'PickleBuffer'
@unittest.expectedFailure
def test_oob_buffers(self): # TODO: RUSTPYTHON, remove when this passes
@@ -201,11 +196,6 @@ class InMemoryPickleTests(AbstractPickleTests, AbstractUnpickleTests,
def test_load_python2_str_as_bytes(self): # TODO: RUSTPYTHON, remove when this passes
super().test_load_python2_str_as_bytes() # TODO: RUSTPYTHON, remove when this passes
# TODO: RUSTPYTHON, pickle.PicklingError
@unittest.expectedFailure
def test_nested_names(self): # TODO: RUSTPYTHON, remove when this passes
super().test_nested_names() # TODO: RUSTPYTHON, remove when this passes
# TODO: RUSTPYTHON, AttributeError: module 'pickle' has no attribute 'PickleBuffer'
@unittest.expectedFailure
def test_oob_buffers(self): # TODO: RUSTPYTHON, remove when this passes
-5
View File
@@ -35,11 +35,6 @@ class OptimizedPickleTests(AbstractPickleTests, unittest.TestCase):
def test_in_band_buffers(self): # TODO: RUSTPYTHON, remove when this passes
super().test_in_band_buffers()
# TODO: RUSTPYTHON, pickle.PicklingError
@unittest.expectedFailure
def test_nested_names(self): # TODO: RUSTPYTHON, remove when this passes
super().test_nested_names()
def test_notimplemented(self): # TODO: RUSTPYTHON, remove when this passes
super().test_notimplemented()
-6
View File
@@ -1581,8 +1581,6 @@ class ProtocolTests(BaseTestCase):
Alias2 = typing.Union[P, typing.Iterable]
self.assertEqual(Alias, Alias2)
# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_protocols_pickleable(self):
global P, CP # pickle wants to reference the class by name
T = TypeVar('T')
@@ -2186,8 +2184,6 @@ class GenericTests(BaseTestCase):
self.assertNotEqual(repr(base), '')
self.assertEqual(base, base)
# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_pickle(self):
global C # pickle wants to reference the class by name
T = TypeVar('T')
@@ -4802,8 +4798,6 @@ class AnnotatedTests(BaseTestCase):
with self.assertRaisesRegex(TypeError, 'at least two arguments'):
Annotated[int]
# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_pickle(self):
samples = [typing.Any, typing.Union[int, str],
typing.Optional[str], Tuple[int, ...],
+12 -2
View File
@@ -1275,6 +1275,15 @@ impl Compiler {
let prev_class_name = std::mem::replace(&mut self.class_name, Some(name.to_owned()));
// Check if the class is declared global
let symbol_table = self.symbol_table_stack.last().unwrap();
let symbol = symbol_table.lookup(name.as_ref()).expect(
"The symbol must be present in the symbol table, even when it is undefined in python.",
);
let mut global_path_prefix = Vec::new();
if symbol.scope == SymbolScope::GlobalExplicit {
global_path_prefix.append(&mut self.qualified_path);
}
self.push_qualified_path(name);
let qualified_name = self.qualified_path.join(".");
@@ -1287,7 +1296,7 @@ impl Compiler {
let dunder_module = self.name("__module__");
self.emit(Instruction::StoreLocal(dunder_module));
self.emit_constant(ConstantData::Str {
value: qualified_name.clone(),
value: qualified_name,
});
let qualname = self.name("__qualname__");
self.emit(Instruction::StoreLocal(qualname));
@@ -1323,6 +1332,7 @@ impl Compiler {
self.class_name = prev_class_name;
self.qualified_path.pop();
self.qualified_path.append(global_path_prefix.as_mut());
self.ctx = prev_ctx;
let mut funcflags = bytecode::MakeFunctionFlags::empty();
@@ -1342,7 +1352,7 @@ impl Compiler {
self.emit(Instruction::MakeFunction(funcflags));
self.emit_constant(ConstantData::Str {
value: qualified_name,
value: name.to_owned(),
});
let call = self.compile_call_inner(2, bases, keywords)?;