Skip to content

Commit 73146cc

Browse files
committed
Fix time.strptime
1 parent 153d0ee commit 73146cc

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
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:

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)