Skip to content

Commit f22aed2

Browse files
authored
Fix PyCode constructor/replace (#6193)
* Fix PyCode constructor * Reuse MarshalError
1 parent 0fb7d0f commit f22aed2

File tree

6 files changed

+296
-99
lines changed

6 files changed

+296
-99
lines changed

Lib/test/test_code.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,6 @@ class List(list):
222222
obj = List([1, 2, 3])
223223
self.assertEqual(obj[0], "Foreign getitem: 1")
224224

225-
# TODO: RUSTPYTHON
226-
@unittest.expectedFailure
227225
def test_constructor(self):
228226
def func(): pass
229227
co = func.__code__
@@ -255,8 +253,6 @@ def test_qualname(self):
255253
CodeTest.test_qualname.__qualname__
256254
)
257255

258-
# TODO: RUSTPYTHON
259-
@unittest.expectedFailure
260256
def test_replace(self):
261257
def func():
262258
x = 1
@@ -297,8 +293,6 @@ def func2():
297293
self.assertEqual(new_code.co_varnames, code2.co_varnames)
298294
self.assertEqual(new_code.co_nlocals, code2.co_nlocals)
299295

300-
# TODO: RUSTPYTHON
301-
@unittest.expectedFailure
302296
def test_nlocals_mismatch(self):
303297
def func():
304298
x = 1

Lib/test/test_funcattrs.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@ def duplicate():
6565
return 3
6666
self.assertNotEqual(self.b, duplicate)
6767

68-
# TODO: RUSTPYTHON
69-
@unittest.expectedFailure
7068
def test_copying___code__(self):
7169
def test(): pass
7270
self.assertEqual(test(), None)

compiler/core/src/marshal.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,19 @@ impl<'a> ReadBorrowed<'a> for &'a [u8] {
165165
}
166166
}
167167

168+
/// Parses bytecode bytes into CodeUnit instructions.
169+
/// Each instruction is 2 bytes: opcode and argument.
170+
pub fn parse_instructions_from_bytes(bytes: &[u8]) -> Result<Box<[CodeUnit]>> {
171+
bytes
172+
.chunks_exact(2)
173+
.map(|cu| {
174+
let op = Instruction::try_from(cu[0])?;
175+
let arg = OpArgByte(cu[1]);
176+
Ok(CodeUnit { op, arg })
177+
})
178+
.collect()
179+
}
180+
168181
pub struct Cursor<B> {
169182
pub data: B,
170183
pub position: usize,
@@ -185,14 +198,7 @@ pub fn deserialize_code<R: Read, Bag: ConstantBag>(
185198
) -> Result<CodeObject<Bag::Constant>> {
186199
let len = rdr.read_u32()?;
187200
let instructions = rdr.read_slice(len * 2)?;
188-
let instructions = instructions
189-
.chunks_exact(2)
190-
.map(|cu| {
191-
let op = Instruction::try_from(cu[0])?;
192-
let arg = OpArgByte(cu[1]);
193-
Ok(CodeUnit { op, arg })
194-
})
195-
.collect::<Result<Box<[CodeUnit]>>>()?;
201+
let instructions = parse_instructions_from_bytes(instructions)?;
196202

197203
let len = rdr.read_u32()?;
198204
let locations = (0..len)

0 commit comments

Comments
 (0)