Skip to content

Commit 2463bdf

Browse files
authored
Fix time.strptime (RustPython#6208)
1 parent 153d0ee commit 2463bdf

File tree

3 files changed

+10
-17
lines changed

3 files changed

+10
-17
lines changed

Lib/test/test_time.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,6 @@ def test_strptime_bytes(self):
284284
self.assertRaises(TypeError, time.strptime, b'2009', "%Y")
285285
self.assertRaises(TypeError, time.strptime, '2009', b'%Y')
286286

287-
# TODO: RUSTPYTHON
288-
@unittest.expectedFailure
289287
def test_strptime_exception_context(self):
290288
# check that this doesn't chain exceptions needlessly (see #17572)
291289
with self.assertRaises(ValueError) as e:

Lib/test/test_zipfile.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,6 @@ def zip_test(self, f, compression, compresslevel=None):
123123
# Check that testzip doesn't raise an exception
124124
zipfp.testzip()
125125

126-
# TODO: RUSTPYTHON
127-
@unittest.expectedFailure
128126
def test_basic(self):
129127
for f in get_files(self):
130128
self.zip_test(f, self.compression)
@@ -394,8 +392,6 @@ def test_repr(self):
394392
self.assertIn('[closed]', repr(zipopen))
395393
self.assertIn('[closed]', repr(zipfp))
396394

397-
# TODO: RUSTPYTHON
398-
@unittest.expectedFailure
399395
def test_compresslevel_basic(self):
400396
for f in get_files(self):
401397
self.zip_test(f, self.compression, compresslevel=9)
@@ -751,8 +747,6 @@ def zip_test(self, f, compression):
751747
# Check that testzip doesn't raise an exception
752748
zipfp.testzip()
753749

754-
# TODO: RUSTPYTHON
755-
@unittest.expectedFailure
756750
def test_basic(self):
757751
for f in get_files(self):
758752
self.zip_test(f, self.compression)

vm/src/stdlib/time.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -364,15 +364,16 @@ mod decl {
364364
}
365365

366366
#[pyfunction]
367-
fn strptime(
368-
string: PyStrRef,
369-
format: OptionalArg<PyStrRef>,
370-
vm: &VirtualMachine,
371-
) -> PyResult<PyStructTime> {
372-
let format = format.as_ref().map_or("%a %b %H:%M:%S %Y", |s| s.as_str());
373-
let instant = NaiveDateTime::parse_from_str(string.as_str(), format)
374-
.map_err(|e| vm.new_value_error(format!("Parse error: {e:?}")))?;
375-
Ok(PyStructTime::new(vm, instant, -1))
367+
fn strptime(string: PyStrRef, format: OptionalArg<PyStrRef>, vm: &VirtualMachine) -> PyResult {
368+
// Call _strptime._strptime_time like CPython does
369+
let strptime_module = vm.import("_strptime", 0)?;
370+
let strptime_func = strptime_module.get_attr("_strptime_time", vm)?;
371+
372+
// Call with positional arguments
373+
match format.into_option() {
374+
Some(fmt) => strptime_func.call((string, fmt), vm),
375+
None => strptime_func.call((string,), vm),
376+
}
376377
}
377378

378379
#[cfg(not(any(

0 commit comments

Comments
 (0)