8000 Merge pull request #806 from jepler/hashlib-py3 · godlygeek/circuitpython@d42c83f · GitHub
[go: up one dir, main page]

Skip to content

Commit d42c83f

Browse files
authored
Merge pull request adafruit#806 from jepler/hashlib-py3
Rename uhashlib->hashlib, improve python3 compatibility
2 parents c4cfd11 + 2955ada commit d42c83f

File tree

7 files changed

+33
-8
lines changed

7 files changed

+33
-8
lines changed

docs/library/uhashlib.rst renamed to docs/library/hashlib.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
:mod:`uhashlib` -- hashing algorithms
1+
:mod:`hashlib` -- hashing algorithms
22
=====================================
33

44
.. include:: ../templates/unsupported_in_circuitpython.inc
55

6-
.. module:: uhashlib
6+
.. module:: hashlib
77
:synopsis: hashing algorithms
88

99
|see_cpython_module| :mod:`cpython:hashlib`.
@@ -29,15 +29,15 @@ be implemented:
2929
Constructors
3030
------------
3131

32-
.. class:: uhashlib.sha256([data])
32+
.. class:: hashlib.sha256([data])
3333

3434
Create an SHA256 hasher object and optionally feed ``data`` into it.
3535

36-
.. class:: uhashlib.sha1([data])
36+
.. class:: hashlib.sha1([data])
3737

3838
Create an SHA1 hasher object and optionally feed ``data`` into it.
3939

40-
.. class:: uhashlib.md5([data])
40+
.. class:: hashlib.md5([data])
4141

4242
Create an MD5 hasher object and optionally feed ``data`` into it.
4343

docs/library/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Python standard libraries and micro-libraries
2323
binascii.rst
2424
ucollections.rst
2525
uerrno.rst
26-
uhashlib.rst
26+
hashlib.rst
2727
uheapq.rst
2828
uio.rst
2929
ujson.rst

extmod/moduhashlib.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@
3636
#include "lib/axtls/crypto/crypto.h"
3737
#endif
3838

39+
static void check_not_unicode(const mp_obj_t arg) {
40+
#if MICROPY_CPYTHON_COMPAT
41+
if (MP_OBJ_IS_STR(arg)) {
42+
mp_raise_TypeError("a bytes-like object is required");
43+
}
44+
#endif
45+
}
46+
3947
typedef struct _mp_obj_hash_t {
4048
mp_obj_base_t base;
4149
char state[0];
@@ -70,6 +78,7 @@ STATIC mp_obj_t sha1_make_new(const mp_obj_type_t *type, size_t n_args, size_t n
7078
#endif
7179

7280
STATIC mp_obj_t hash_update(mp_obj_t self_in, mp_obj_t arg) {
81+
check_not_unicode(arg);
7382
mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in);
7483
mp_buffer_info_t bufinfo;
7584
mp_get_buffer_raise(arg, &bufinfo, MP_BUFFER_READ);
@@ -80,6 +89,7 @@ MP_DEFINE_CONST_FUN_OBJ_2(hash_update_obj, hash_update);
8089

8190
#if MICROPY_PY_UHASHLIB_SHA1
8291
STATIC mp_obj_t sha1_update(mp_obj_t self_in, mp_obj_t arg) {
92+
check_not_unicode(arg);
8393
mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in);
8494
mp_buffer_info_t bufinfo;
8595
mp_get_buffer_raise(arg, &bufinfo, MP_BUFFER_READ);
@@ -139,7 +149,7 @@ STATIC const mp_obj_type_t sha1_type = {
139149
#endif
140150

141151
STATIC const mp_rom_map_elem_t mp_module_hashlib_globals_table[] = {
142-
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_uhashlib) },
152+
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_hashlib) },
143153
{ MP_ROM_QSTR(MP_QSTR_sha256), MP_ROM_PTR(&sha256_type) },
144154
#if MICROPY_PY_UHASHLIB_SHA1
145155
{ MP_ROM_QSTR(MP_QSTR_sha1), MP_ROM_PTR(&sha1_type) },

py/objmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ STATIC const mp_rom_map_elem_t mp_builtin_module_table[] = {
199199
{ MP_ROM_QSTR(MP_QSTR_utimeq), MP_ROM_PTR(&mp_module_utimeq) },
200200
#endif
201201
#if MICROPY_PY_UHASHLIB
202-
{ MP_ROM_QSTR(MP_QSTR_uhashlib), MP_ROM_PTR(&mp_module_uhashlib) },
202+
{ MP_ROM_QSTR(MP_QSTR_hashlib), MP_ROM_PTR(&mp_module_uhashlib) },
203203
#endif
204204
#if MICROPY_PY_UBINASCII
205205
{ MP_ROM_QSTR(MP_QSTR_binascii), MP_ROM_PTR(&mp_module_ubinascii) },

shared-bindings/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Module Supported Ports
2929
`busio` **All Supported**
3030
`digitalio` **All Supported**
3131
`gamepad` **SAMD Express, nRF**
32+
`hashlib` **ESP8266**
3233
`math` **All Supported**
3334
`microcontroller` **All Supported**
3435
`multiterminal` **ESP8266**

tests/extmod/uhashlib_sha1.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,10 @@
1919
sha1 = hashlib.sha1(b'hello')
2020
sha1.update(b'world')
2121
print(sha1.digest())
22+
23+
sha1 = hashlib.sha1(b'hello')
24+
try:
25+
sha1.update(u'world')
26+
except TypeError as e:
27+
print("TypeError")
28+
print(sha1.digest())

tests/extmod/uhashlib_sha256.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@
2323

2424
print(hashlib.sha256(b"\xff" * 64).digest())
2525

26+
sha256 = hashlib.sha256(b'hello')
27+
try:
28+
sha256.update(u'world')
29+
except TypeError as e:
30+
print("TypeError")
31+
print(sha256.digest())
32+
2633
# TODO: running .digest() several times in row is not supported()
2734
#h = hashlib.sha256(b'123')
2835
#print(h.digest())

0 commit comments

Comments
 (0)
0