8000 Fix _sha512 support · RustPython/RustPython@8c7b811 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8c7b811

Browse files
committed
Fix _sha512 support
1 parent 940b879 commit 8c7b811

File tree

5 files changed

+22
-16
lines changed

5 files changed

+22
-16
lines changed

Lib/test/test_hashlib.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,8 +1118,6 @@ def _test_pbkdf2_hmac(self, pbkdf2, supported):
11181118
iterations=1, dklen=None)
11191119
self.assertEqual(out, self.pbkdf2_results['sha1'][0][0])
11201120

1121-
# TODO: RUSTPYTHON
1122-
@unittest.expectedFailure
11231121
@unittest.skipIf(builtin_hashlib is None, "test requires builtin_hashlib")
11241122
def test_pbkdf2_hmac_py(self):
11251123
with warnings_helper.check_warnings():

stdlib/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ mod md5;
2020
mod sha1;
2121
mod sha256;
2222
mod sha3;
23+
mod sha512;
2324

2425
mod json;
2526
#[cfg(not(any(target_os = "ios", target_os = "android", target_arch = "wasm32")))]
@@ -111,7 +112,7 @@ pub fn get_module_inits() -> impl Iterator<Item = (Cow<'static, str>, StdlibInit
111112
"_sha1" => sha1::make_module,
112113
"_sha3" => sha3::make_module,
113114
"_sha256" => sha256::make_module,
114-
// "_sha512" => sha512::make_module, // TODO: RUSPYTHON fix strange fail on vm: 'static type has not been initialized'
115+
"_sha512" => sha512::make_module,
115116
"_md5" => md5::make_module,
116117
"_blake2" => blake2::make_module,
117118
"_json" => json::make_module,

stdlib/src/sha256.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
pub(crate) use _sha256::make_module;
1+
use crate::vm::{builtins::PyModule, PyRef, VirtualMachine};
2+
3+
pub(crate) fn make_module(vm: &VirtualMachine) -> PyRef<PyModule> {
4+
let _ = vm.import("_hashlib", 0);
5+
_sha256::make_module(vm)
6+
}
27

38
#[pymodule]
49
mod _sha256 {

stdlib/src/sha512.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1-
// spell-checker:ignore usedforsecurity HASHXOF
1+
use crate::vm::{builtins::PyModule, PyRef, VirtualMachine};
22

3-
pub(crate) use _sha512::make_module;
3+
pub(crate) fn make_module(vm: &VirtualMachine) -> PyRef<PyModule> {
4+
let _ = vm.import("_hashlib", 0);
5+
_sha512::make_module(vm)
6+
}
47

58
#[pymodule]
69
mod _sha512 {
7-
use crate::hashlib::_hashlib::{HashArgs, HashWrapper, PyHasher};
8-
use crate::vm::{PyObjectRef, PyPayload, PyResult, VirtualMachine};
9-
use sha2::{Sha384, Sha512};
10+
use crate::hashlib::_hashlib::{local_sha384, local_sha512, HashArgs};
11+
use crate::vm::{PyPayload, PyResult, VirtualMachine};
1012

11-
#[pyfunction(name = "sha384")]
12-
fn sha384(args: HashArgs, vm: &VirtualMachine) -> PyResult<PyObjectRef> {
13-
Ok(PyHasher::new("sha384", HashWrapper::new::<Sha384>(args.string)).into_pyobject(vm))
13+
#[pyfunction]
14+
fn sha384(args: HashArgs, vm: &VirtualMachine) -> PyResult {
15+
Ok(local_sha384(args).into_pyobject(vm))
1416
}
1517

16-
#[pyfunction(name = "sha512")]
17-
fn sha512(args: HashArgs, vm: &VirtualMachine) -> PyResult<PyObjectRef> {
18-
Ok(PyHasher::new("sha512", HashWrapper::new::<Sha512>(args.string)).into_pyobject(vm))
18+
#[pyfunction]
19+
fn sha512(args: HashArgs, vm: &VirtualMachine) -> PyResult {
20+
Ok(local_sha512(args).into_pyobject(vm))
1921
}
2022
}

vm/src/class.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub trait StaticType {
2222
fn static_type() -> &'static Py<PyType> {
2323
Self::static_cell()
2424
.get()
25-
.expect("static type has not been initialized")
25+
.expect("static type has not been initialized. e.g. the native types defined in different module may be used before importing library.")
2626
}
2727
fn init_manually(typ: PyTypeRef) -> &'static Py<PyType> {
2828
let cell = Self::static_cell();

0 commit comments

Comments
 (0)
0