summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--py/circuitpy_mpconfig.h1
-rw-r--r--shared-bindings/ipaddress/__init__.c29
2 files changed, 29 insertions, 1 deletions
diff --git a/py/circuitpy_mpconfig.h b/py/circuitpy_mpconfig.h
index 8ff2ddc67..f2789ee58 100644
--- a/py/circuitpy_mpconfig.h
+++ b/py/circuitpy_mpconfig.h
@@ -741,6 +741,7 @@ extern const struct _mp_obj_module_t wifi_module;
GAMEPADSHIFT_MODULE \
GNSS_MODULE \
I2CPERIPHERAL_MODULE \
+ IPADDRESS_MODULE \
JSON_MODULE \
MATH_MODULE \
_EVE_MODULE \
diff --git a/shared-bindings/ipaddress/__init__.c b/shared-bindings/ipaddress/__init__.c
index f9427ab84..b88b3afbb 100644
--- a/shared-bindings/ipaddress/__init__.c
+++ b/shared-bindings/ipaddress/__init__.c
@@ -25,6 +25,8 @@
*/
#include "py/objexcept.h"
+#include "py/objstr.h"
+#include "py/parsenum.h"
#include "py/runtime.h"
#include "shared-bindings/ipaddress/__init__.h"
#include "shared-bindings/ipaddress/IPv4Address.h"
@@ -42,7 +44,32 @@
STATIC mp_obj_t ipaddress_ip_address(mp_obj_t ip_in) {
mp_int_t value;
- if (!mp_obj_get_int_maybe(ip_in, &value)) {
+ if (mp_obj_get_int_maybe(ip_in, &value)) {
+ // We're done.
+ } else if (MP_OBJ_IS_STR(ip_in)) {
+ GET_STR_DATA_LEN(ip_in, str_data, str_len);
+ size_t period_count = 0;
+ size_t period_index[4] = {0, 0, 0, str_len};
+ for (size_t i = 0; i < str_len; i++) {
+ if (str_data[i] == '.') {
+ if (period_count < 3) {
+ period_index[period_count] = i;
+ }
+ period_count++;
+ }
+ }
+ if (period_count > 3) {
+ mp_raise_ValueError(translate("Not a valid IP string."));
+ }
+
+ size_t last_period = 0;
+ for (size_t i = 0; i < 4; i++) {
+ mp_obj_t octet = mp_parse_num_integer((const char*) str_data + last_period, period_index[i] - last_period, 10, NULL);
+ last_period = period_index[i] + 1;
+ value |= MP_OBJ_SMALL_INT_VALUE(octet) << (24 - i * 8);
+
+ }
+ } else {
mp_raise_ValueError(translate("Only raw int supported for ip."));
}