Merge pull request #4792 from Neradoc/nera-fix-IPv4Address

Fix ipaddress.IPv4Address from returning invalid values
This commit is contained in:
Scott Shawcroft 2021-05-21 10:47:41 -07:00 committed by GitHub
commit 22e8b20907
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 5 additions and 4 deletions

View File

@ -1694,7 +1694,7 @@ msgid "Only one color can be transparent at a time"
msgstr "" msgstr ""
#: shared-bindings/ipaddress/__init__.c #: shared-bindings/ipaddress/__init__.c
msgid "Only raw int supported for ip" msgid "Only raw int or string supported for ip"
msgstr "" msgstr ""
#: ports/esp32s2/bindings/espidf/__init__.c ports/esp32s2/esp_error.c #: ports/esp32s2/bindings/espidf/__init__.c ports/esp32s2/esp_error.c

View File

@ -60,12 +60,13 @@ STATIC mp_obj_t ipaddress_ipv4address_make_new(const mp_obj_type_t *type, size_t
uint8_t *buf = NULL; uint8_t *buf = NULL;
if (mp_obj_get_int_maybe(address, (mp_int_t *)&value)) { if (mp_obj_get_int_maybe(address, (mp_int_t *)&value)) {
// We're done. // We're done.
buf = (uint8_t *)value; buf = (uint8_t *)&value;
} else if (mp_obj_is_str(address)) { } else if (mp_obj_is_str(address)) {
GET_STR_DATA_LEN(address, str_data, str_len); GET_STR_DATA_LEN(address, str_data, str_len);
if (!ipaddress_parse_ipv4address((const char *)str_data, str_len, &value)) { if (!ipaddress_parse_ipv4address((const char *)str_data, str_len, &value)) {
mp_raise_ValueError(translate("Not a valid IP string")); mp_raise_ValueError(translate("Not a valid IP string"));
} }
buf = (uint8_t *)&value;
} else { } else {
mp_buffer_info_t buf_info; mp_buffer_info_t buf_info;
if (mp_get_buffer(address, &buf_info, MP_BUFFER_READ)) { if (mp_get_buffer(address, &buf_info, MP_BUFFER_READ)) {

View File

@ -76,7 +76,7 @@ bool ipaddress_parse_ipv4address(const char *str_data, size_t str_len, uint32_t
return true; return true;
} }
//| def ip_address(obj: Union[int]) -> IPv4Address: //| def ip_address(obj: Union[int, str]) -> IPv4Address:
//| """Return a corresponding IP address object or raise ValueError if not possible.""" //| """Return a corresponding IP address object or raise ValueError if not possible."""
//| ... //| ...
//| //|
@ -91,7 +91,7 @@ STATIC mp_obj_t ipaddress_ip_address(mp_obj_t ip_in) {
mp_raise_ValueError(translate("Not a valid IP string")); mp_raise_ValueError(translate("Not a valid IP string"));
} }
} else { } else {
mp_raise_ValueError(translate("Only raw int supported for ip")); mp_raise_ValueError(translate("Only raw int or string supported for ip"));
} }
return common_hal_ipaddress_new_ipv4address(value); return common_hal_ipaddress_new_ipv4address(value);