Skip to content

Commit

Permalink
Merge 3123f69 into 5669a60
Browse files Browse the repository at this point in the history
  • Loading branch information
dpgeorge committed Apr 23, 2021
2 parents 5669a60 + 3123f69 commit 2b8efef
Show file tree
Hide file tree
Showing 27 changed files with 77 additions and 45 deletions.
4 changes: 0 additions & 4 deletions docs/library/builtins.rst
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,6 @@ Exceptions

.. exception:: OSError

|see_cpython| `python:OSError`. MicroPython doesn't implement ``errno``
attribute, instead use the standard way to access exception arguments:
``exc.args[0]``.

.. exception:: RuntimeError

.. exception:: StopIteration
Expand Down
4 changes: 2 additions & 2 deletions docs/library/uerrno.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ Constants

Error codes, based on ANSI C/POSIX standard. All error codes start with
"E". As mentioned above, inventory of the codes depends on
:term:`MicroPython port`. Errors are usually accessible as ``exc.args[0]``
:term:`MicroPython port`. Errors are usually accessible as ``exc.errno``
where ``exc`` is an instance of `OSError`. Usage example::

try:
uos.mkdir("my_dir")
except OSError as exc:
if exc.args[0] == uerrno.EEXIST:
if exc.errno == uerrno.EEXIST:
print("Directory already exists")

.. data:: errorcode
Expand Down
2 changes: 1 addition & 1 deletion extmod/uasyncio/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ async def open_connection(host, port):
try:
s.connect(ai[-1])
except OSError as er:
if er.args[0] != EINPROGRESS:
if er.errno != EINPROGRESS:
raise er
yield core._io_queue.queue_write(s)
return ss, ss
Expand Down
4 changes: 3 additions & 1 deletion py/objexcept.c
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,9 @@ void mp_obj_exception_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
if (attr == MP_QSTR_args) {
decompress_error_text_maybe(self);
dest[0] = MP_OBJ_FROM_PTR(self->args);
} else if (self->base.type == &mp_type_StopIteration && attr == MP_QSTR_value) {
} else if (attr == MP_QSTR_value || attr == MP_QSTR_errno) {
// These are aliases for args[0]: .value for StopIteration and .errno for OSError.
// For efficiency let these attributes apply to all exception instances.
dest[0] = mp_obj_exception_get_value(self_in);
}
}
Expand Down
5 changes: 5 additions & 0 deletions tests/basics/exception1.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# test basic properties of exceptions

print(repr(IndexError()))
print(str(IndexError()))

Expand All @@ -12,3 +14,6 @@
print(s.value)
s = StopIteration(1, 2, 3)
print(s.value)

print(OSError().errno)
print(OSError(1, "msg").errno)
20 changes: 20 additions & 0 deletions tests/basics/subclass_native3.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# test subclassing a native exception


class MyExc(Exception):
pass


e = MyExc(100, "Some error")
print(e)
print(repr(e))
Expand All @@ -20,3 +24,19 @@ class MyExc(Exception):
raise MyExc("Some error2")
except:
print("Caught user exception")


class MyStopIteration(StopIteration):
pass


print(MyStopIteration().value)
print(MyStopIteration(1).value)


class MyOSError(OSError):
pass


print(MyOSError().errno)
print(MyOSError(1, "msg").errno)
9 changes: 9 additions & 0 deletions tests/cpydiff/types_exception_attrs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""
categories: Types,Exception
description: All exceptions have readable ``value`` and ``errno`` attributes, not just ``StopIteration`` and ``OSError``.
cause: MicroPython is optimised to reduce code size.
workaround: Only use ``value`` on ``StopIteration`` exceptions, and ``errno`` on ``OSError`` exceptions. Do not use or rely on these attributes on other exceptions.
"""
e = Exception(1)
print(e.value)
print(e.errno)
2 changes: 1 addition & 1 deletion tests/extmod/btree1.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
try:
db.seq(b"foo1")
except OSError as e:
print(e.args[0] == uerrno.EINVAL)
print(e.errno == uerrno.EINVAL)

print(list(db.keys()))
print(list(db.values()))
Expand Down
2 changes: 1 addition & 1 deletion tests/extmod/btree_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def ioctl(self, cmd, arg):
try:
db = btree.open(Device(), pagesize=511)
except OSError as er:
print("OSError", er.args[0] == uerrno.EINVAL)
print("OSError", er.errno == uerrno.EINVAL)

# Valid pagesize, device returns error on read; errno comes from Device.readinto
try:
Expand Down
2 changes: 1 addition & 1 deletion tests/extmod/uselect_poll_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
try:
poller.modify(s, select.POLLIN)
except OSError as e:
assert e.args[0] == errno.ENOENT
assert e.errno == errno.ENOENT

# poll after closing the socket, should return POLLNVAL
poller.register(s)
Expand Down
2 changes: 1 addition & 1 deletion tests/extmod/usocket_tcp_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
try:
s.recv(1)
except OSError as er:
print("ENOTCONN:", er.args[0] == errno.ENOTCONN)
print("ENOTCONN:", er.errno == errno.ENOTCONN)
2 changes: 1 addition & 1 deletion tests/extmod/usocket_udp_nonblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
try:
s.recv(1)
except OSError as er:
print("EAGAIN:", er.args[0] == errno.EAGAIN)
print("EAGAIN:", er.errno == errno.EAGAIN)
10 changes: 5 additions & 5 deletions tests/extmod/vfs_fat_fileio1.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,22 @@ def ioctl(self, op, arg):
try:
f.write("world!")
except OSError as e:
print(e.args[0] == uerrno.EINVAL)
print(e.errno == uerrno.EINVAL)

try:
f.read()
except OSError as e:
print(e.args[0] == uerrno.EINVAL)
print(e.errno == uerrno.EINVAL)

try:
f.flush()
except OSError as e:
print(e.args[0] == uerrno.EINVAL)
print(e.errno == uerrno.EINVAL)

try:
open("foo_file.txt", "x")
except OSError as e:
print(e.args[0] == uerrno.EEXIST)
print(e.errno == uerrno.EEXIST)

with open("foo_file.txt", "a") as f:
f.write("world!")
Expand Down Expand Up @@ -105,7 +105,7 @@ def ioctl(self, op, arg):
try:
vfs.rmdir("foo_file.txt")
except OSError as e:
print(e.args[0] == 20) # uerrno.ENOTDIR
print(e.errno == 20) # uerrno.ENOTDIR

vfs.remove("foo_file.txt")
print(list(vfs.ilistdir()))
12 changes: 6 additions & 6 deletions tests/extmod/vfs_fat_fileio2.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,22 @@ def ioctl(self, op, arg):
try:
vfs.mkdir("foo_dir")
except OSError as e:
print(e.args[0] == uerrno.EEXIST)
print(e.errno == uerrno.EEXIST)

try:
vfs.remove("foo_dir")
except OSError as e:
print(e.args[0] == uerrno.EISDIR)
print(e.errno == uerrno.EISDIR)

try:
vfs.remove("no_file.txt")
except OSError as e:
print(e.args[0] == uerrno.ENOENT)
print(e.errno == uerrno.ENOENT)

try:
vfs.rename("foo_dir", "/null/file")
except OSError as e:
print(e.args[0] == uerrno.ENOENT)
print(e.errno == uerrno.ENOENT)

# file in dir
with open("foo_dir/file-in-dir.txt", "w+t") as f:
Expand All @@ -82,7 +82,7 @@ def ioctl(self, op, arg):
try:
vfs.rmdir("foo_dir")
except OSError as e:
print(e.args[0] == uerrno.EACCES)
print(e.errno == uerrno.EACCES)

# trim full path
vfs.rename("foo_dir/file-in-dir.txt", "foo_dir/file.txt")
Expand Down Expand Up @@ -111,5 +111,5 @@ def ioctl(self, op, arg):
f = open("large_file.txt", "wb")
f.write(bytearray(bsize * free))
except OSError as e:
print("ENOSPC:", e.args[0] == 28) # uerrno.ENOSPC
print("ENOSPC:", e.errno == 28) # uerrno.ENOSPC
f.close()
2 changes: 1 addition & 1 deletion tests/extmod/vfs_fat_more.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def ioctl(self, op, arg):
try:
uos.mkdir(exist)
except OSError as er:
print("mkdir OSError", er.args[0] == 17) # EEXIST
print("mkdir OSError", er.errno == 17) # EEXIST

uos.chdir("/")
print(uos.stat("test5.txt")[:-3])
Expand Down
6 changes: 3 additions & 3 deletions tests/extmod/vfs_fat_ramdisk.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def ioctl(self, op, arg):
try:
vfs.stat("no_file.txt")
except OSError as e:
print(e.args[0] == uerrno.ENOENT)
print(e.errno == uerrno.ENOENT)

with vfs.open("foo_file.txt", "w") as f:
f.write("hello!")
Expand All @@ -80,7 +80,7 @@ def ioctl(self, op, arg):
try:
vfs.chdir("sub_file.txt")
except OSError as e:
print(e.args[0] == uerrno.ENOENT)
print(e.errno == uerrno.ENOENT)

vfs.chdir("..")
print("getcwd:", vfs.getcwd())
Expand All @@ -94,4 +94,4 @@ def ioctl(self, op, arg):
try:
vfs.ilistdir(b"no_exist")
except OSError as e:
print("ENOENT:", e.args[0] == uerrno.ENOENT)
print("ENOENT:", e.errno == uerrno.ENOENT)
2 changes: 1 addition & 1 deletion tests/extmod/websocket_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@ def ws_write(msg, sz):
try:
ws.ioctl(-1)
except OSError as e:
print("ioctl: EINVAL:", e.args[0] == uerrno.EINVAL)
print("ioctl: EINVAL:", e.errno == uerrno.EINVAL)
2 changes: 1 addition & 1 deletion tests/multi_net/tcp_accept_recv.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def instance0():
try:
print("recv", s.recv(10)) # should raise Errno 107 ENOTCONN
except OSError as er:
print(er.args[0])
print(er.errno)
s.close()


Expand Down
2 changes: 1 addition & 1 deletion tests/multi_net/tcp_client_rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def instance0():
print(s2.recv(10))
print(convert_poll_list(poll.poll(1000)))
except OSError as er:
print(er.args[0])
print(er.errno)
print(convert_poll_list(poll.poll(1000)))
# TODO lwip raises here but apparently it shouldn't
print(s2.recv(10))
Expand Down
2 changes: 1 addition & 1 deletion tests/multi_net/uasyncio_tcp_client_rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async def handle_connection(reader, writer):
writer.close()
await writer.wait_closed()
except OSError as er:
print("OSError", er.args[0])
print("OSError", er.errno)
ev.set()


Expand Down
2 changes: 1 addition & 1 deletion tests/net_hosted/accept_nonblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
try:
s.accept()
except OSError as er:
print(er.args[0] == 11) # 11 is EAGAIN
print(er.errno == 11) # 11 is EAGAIN
s.close()
2 changes: 1 addition & 1 deletion tests/net_hosted/accept_timeout.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@
try:
s.accept()
except OSError as er:
print(er.args[0] in (errno.ETIMEDOUT, "timed out")) # CPython uses a string instead of errno
print(er.errno in (errno.ETIMEDOUT, "timed out")) # CPython uses a string instead of errno
s.close()
2 changes: 1 addition & 1 deletion tests/net_hosted/connect_nonblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def test(peer_addr):
try:
s.connect(peer_addr)
except OSError as er:
print(er.args[0] == errno.EINPROGRESS)
print(er.errno == errno.EINPROGRESS)
s.close()


Expand Down
10 changes: 5 additions & 5 deletions tests/net_hosted/connect_nonblock_xfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ def do_connect(peer_addr, tls, handshake):
# print("Connecting to", peer_addr)
s.connect(peer_addr)
except OSError as er:
print("connect:", er.args[0] == errno.EINPROGRESS)
if er.args[0] != errno.EINPROGRESS:
print(" got", er.args[0])
print("connect:", er.errno == errno.EINPROGRESS)
if er.errno != errno.EINPROGRESS:
print(" got", er.errno)
# wrap with ssl/tls if desired
if tls:
try:
Expand Down Expand Up @@ -67,7 +67,7 @@ def test(peer_addr, tls=False, handshake=False):
except OSError as er:
#
dp(er)
print("send:", er.args[0] in (errno.EAGAIN, errno.EINPROGRESS))
print("send:", er.errno in (errno.EAGAIN, errno.EINPROGRESS))
s.close()
else: # fake it...
print("connect:", True)
Expand Down Expand Up @@ -103,7 +103,7 @@ def test(peer_addr, tls=False, handshake=False):
print("recv:", s.recv(10))
except OSError as er:
dp(er)
print("recv:", er.args[0] == errno.EAGAIN)
print("recv:", er.errno == errno.EAGAIN)
s.close()
else: # fake it...
print("connect:", True)
Expand Down
2 changes: 1 addition & 1 deletion tests/net_inet/ssl_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test(addr, hostname, block=True):
s.connect(addr)
print("connected")
except OSError as e:
if e.args[0] != errno.EINPROGRESS:
if e.errno != errno.EINPROGRESS:
raise
print("EINPROGRESS")

Expand Down
6 changes: 3 additions & 3 deletions tests/net_inet/test_tls_nonblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def test_one(site, opts):
s.connect(addr)
raise OSError(-1, "connect blocks")
except OSError as e:
if e.args[0] != errno.EINPROGRESS:
if e.errno != errno.EINPROGRESS:
raise

if sys.implementation.name != "micropython":
Expand All @@ -31,7 +31,7 @@ def test_one(site, opts):
else:
s = ssl.wrap_socket(s, do_handshake_on_connect=False)
except OSError as e:
if e.args[0] != errno.EINPROGRESS:
if e.errno != errno.EINPROGRESS:
raise
print("wrapped")

Expand Down Expand Up @@ -69,7 +69,7 @@ def test_one(site, opts):
try:
b = s.read(128)
except OSError as err:
if err.args[0] == 2: # 2=ssl.SSL_ERROR_WANT_READ:
if err.errno == 2: # 2=ssl.SSL_ERROR_WANT_READ:
continue
raise
if b is None:
Expand Down
2 changes: 1 addition & 1 deletion tools/upip.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def _makedirs(name, mode=0o777):
os.mkdir(s)
ret = True
except OSError as e:
if e.args[0] != errno.EEXIST and e.args[0] != errno.EISDIR:
if e.errno != errno.EEXIST and e.errno != errno.EISDIR:
raise e
ret = False
return ret
Expand Down

0 comments on commit 2b8efef

Please sign in to comment.