summaryrefslogtreecommitdiff
path: root/py/moduerrno.c
diff options
context:
space:
mode:
authorNoralf Trønnes <noralf@tronnes.org>2018-11-12 16:16:47 +0100
committerNoralf Trønnes <noralf@tronnes.org>2018-11-13 22:04:44 +0100
commitc5aa2e93001436f6d63e3e9b7dff3c535397db1c (patch)
tree99c25b5cefc5d6ff2c3e41ef820295d595c86fc2 /py/moduerrno.c
parent50f5d27c436cf71d14bf893709375e5bcf62717b (diff)
Support OSError attributes
This adds support for the OSError attributes : errno, strerror, filename and filename2. CPython only sets errno if 2 arguments has been passed in. This has not been implemented here. CPython OSError.args is capped at 2 items for backward compatibility reasons. This has not been implemented here. MICROPY_CPYTHON_COMPAT has to be enabled to get these attributes. mp_common_errno_to_str() has been extended to check mp_errno_to_str() as well. This is done to ease reuse for the strerror argument.
Diffstat (limited to 'py/moduerrno.c')
-rw-r--r--py/moduerrno.c37
1 files changed, 23 insertions, 14 deletions
diff --git a/py/moduerrno.c b/py/moduerrno.c
index 255c1a73e..c1feafaa1 100644
--- a/py/moduerrno.c
+++ b/py/moduerrno.c
@@ -136,19 +136,28 @@ const char* mp_errno_to_str(mp_obj_t errno_val) {
#endif //MICROPY_PY_UERRNO
-// For commonly encountered errors, return human readable strings
-const compressed_string_t* mp_common_errno_to_str(mp_obj_t errno_val) {
- if (MP_OBJ_IS_SMALL_INT(errno_val)) {
- switch (MP_OBJ_SMALL_INT_VALUE(errno_val)) {
- case EPERM: return translate("Permission denied");
- case ENOENT: return translate("No such file/directory");
- case EIO: return translate("Input/output error");
- case EACCES: return translate("Permission denied");
- case EEXIST: return translate("File exists");
- case ENODEV: return translate("Unsupported operation");
- case EINVAL: return translate("Invalid argument");
- case EROFS: return translate("Read-only filesystem");
- }
+// For commonly encountered errors, return human readable strings, otherwise try errno name
+const char *mp_common_errno_to_str(mp_obj_t errno_val, char *buf, size_t len) {
+ if (!MP_OBJ_IS_SMALL_INT(errno_val)) {
+ return NULL;
+ }
+
+ const compressed_string_t* desc = NULL;
+ switch (MP_OBJ_SMALL_INT_VALUE(errno_val)) {
+ case EPERM: desc = translate("Permission denied"); break;
+ case ENOENT: desc = translate("No such file/directory"); break;
+ case EIO: desc = translate("Input/output error"); break;
+ case EACCES: desc = translate("Permission denied"); break;
+ case EEXIST: desc = translate("File exists"); break;
+ case ENODEV: desc = translate("Unsupported operation"); break;
+ case EINVAL: desc = translate("Invalid argument"); break;
+ case EROFS: desc = translate("Read-only filesystem"); break;
}
- return NULL;
+ if (desc != NULL && desc->length <= len) {
+ decompress(desc, buf);
+ return buf;
+ }
+
+ const char *msg = mp_errno_to_str(errno_val);
+ return msg[0] != '\0' ? msg : NULL;
}