8000 codeformat · micropython/micropython@ce7a548 · GitHub
[go: up one dir, main page]

Skip to content

Commit ce7a548

Browse files
committed
codeformat
1 parent 01ea499 commit ce7a548

File tree

8 files changed

+104
-96
lines changed

8 files changed

+104
-96
lines changed

extmod/modussl_axtls.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ STATIC const struct ssl_errs ssl_error_tab[] = {
8282
};
8383

8484
STATIC NORETURN void ussl_raise_error(int err) {
85-
for (int i=0; ssl_error_tab[i].errnum; i++) {
85+
for (int i = 0; ssl_error_tab[i].errnum; i++) {
8686
if (ssl_error_tab[i].errnum == err) {
8787
//printf("AXTLS error: SSL_%s\n", ssl_error_tab[i].errstr);
8888
mp_raise_msg_varg(&mp_type_OSError, "AXTLS %d: %s", err, ssl_error_tab[i].errstr);

extmod/modussl_mbedtls.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,16 @@ STATIC NORETURN void mbedtls_raise_error(int err) {
8080
// err comes from underlying sockets, not mbedtls
8181
mp_raise_OSError(-err);
8282
}
83-
#if 1
83+
#if 1
8484
// Including mbedtls_strerror takes about 16KB on the esp32 due to all the strings
8585
char error_buf[80];
8686
mbedtls_strerror(err, error_buf, sizeof(error_buf));
8787
//printf("mbedtls error -0x%x : %s\n", -err, error_buf);
8888
mp_raise_msg_varg(&mp_type_OSError, "mbedtls -0x%x: %s", -err, error_buf);
89-
#else
89+
#else
9090
//printf("mbedtls error -0x%x\n", -err);
9191
mp_raise_msg_varg(&mp_type_OSError, "mbedtls -0x%x", -err);
92-
#endif
92+
#endif
9393
}
9494

9595
// _mbedtls_ssl_send is called by mbedtls to send bytes onto the underlying socket

ports/esp32/lwip_err.c

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -43,31 +43,30 @@
4343
/** Table to quickly map an lwIP error (err_t) to a socket error
4444
* by using -err as an index */
4545
static const int err_to_errno_table[] = {
46-
0, /* ERR_OK 0 No error, everything OK. */
47-
MP_ENOMEM, /* ERR_MEM -1 Out of memory error. */
48-
MP_ENOBUFS, /* ERR_BUF -2 Buffer error. */
49-
MP_EWOULDBLOCK, /* ERR_TIMEOUT -3 Timeout */
50-
MP_EHOSTUNREACH, /* ERR_RTE -4 Routing problem. */
51-
MP_EINPROGRESS, /* ERR_INPROGRESS -5 Operation in progress */
52-
MP_EINVAL, /* ERR_VAL -6 Illegal value. */
53-
MP_EWOULDBLOCK, /* ERR_WOULDBLOCK -7 Operation would block. */
54-
MP_EADDRINUSE, /* ERR_USE -8 Address in use. */
55-
MP_EALREADY, /* ERR_ALREADY -9 Already connecting. */
56-
MP_EISCONN, /* ERR_ISCONN -10 Conn already established.*/
57-
MP_ENOTCONN, /* ERR_CONN -11 Not connected. */
58-
-1, /* ERR_IF -12 Low-level netif error */
59-
MP_ECONNABORTED, /* ERR_ABRT -13 Connection aborted. */
60-
MP_ECONNRESET, /* ERR_RST -14 Connection reset. */
61-
MP_ENOTCONN, /* ERR_CLSD -15 Connection closed. */
62-
MP_EIO /* ERR_ARG -16 Illegal argument. */
46+
0, /* ERR_OK 0 No error, everything OK. */
47+
MP_ENOMEM, /* ERR_MEM -1 Out of memory error. */
48+
MP_ENOBUFS, /* ERR_BUF -2 Buffer error. */
49+
MP_EWOULDBLOCK, /* ERR_TIMEOUT -3 Timeout */
50+
MP_EHOSTUNREACH, /* ERR_RTE -4 Routing problem. */
51+
MP_EINPROGRESS, /* ERR_INPROGRESS -5 Operation in progress */
52+
MP_EINVAL, /* ERR_VAL -6 Illegal value. */
53+
MP_EWOULDBLOCK, /* ERR_WOULDBLOCK -7 Operation would block. */
54+
MP_EADDRINUSE, /* ERR_USE -8 Address in use. */
55+
MP_EALREADY, /* ERR_ALREADY -9 Already connecting. */
56+
MP_EISCONN, /* ERR_ISCONN -10 Conn already established.*/
57+
MP_ENOTCONN, /* ERR_CONN -11 Not connected. */
58+
-1, /* ERR_IF -12 Low-level netif error */
59+
MP_ECONNABORTED, /* ERR_ABRT -13 Connection aborted. */
60+
MP_ECONNRESET, /* ERR_RST -14 Connection reset. */
61+
MP_ENOTCONN, /* ERR_CLSD -15 Connection closed. */
62+
MP_EIO /* ERR_ARG -16 Illegal argument. */
6363
};
6464

6565
int
66-
err_to_errno(err_t err)
67-
{
68-
if ((err > 0) || (-err >= (err_t)LWIP_ARRAYSIZE(err_to_errno_table))) {
69-
return EIO;
70-
}
71-
//printf("lwip err_to_errno %d->%d\n", err, err_to_errno_table[-err]);
72-
return err_to_errno_table[-err];
66+
err_to_errno(err_t err) {
67+
if ((err > 0) || (-err >= (err_t)LWIP_ARRAYSIZE(err_to_errno_table))) {
68+
return EIO;
69+
}
70+
//printf("lwip err_to_errno %d->%d\n", err, err_to_errno_table[-err]);
71+
return err_to_errno_table[-err];
7372
}

tests/net_hosted/connect_nonblock.py

Lines changed: 34 additions & 30 deletions
94
Original file line numberDiff line numberDiff line change
@@ -6,77 +6,80 @@
66
except:
77
import socket, ssl, sys, time
88

9+
910
def dp(e):
10-
#print(e) # uncomment this line for dev&test to print the actual exceptions
11+
# print(e) # uncomment this line for dev&test to print the actual exceptions
1112
pass
1213

14+
1315
# do_connect establishes the socket and wraps it if requested
1416
def do_connect(peer_addr, tls, handshake):
1517
s = socket.socket()
1618
s.setblocking(False)
1719
try:
1820
s.connect(peer_addr)
1921
except OSError as er:
20-
print('connect:', er.args[0] == 115) # 115 is EINPROGRESS
22+
print("connect:", er.args[0] == 115) # 115 is EINPROGRESS
2123
# wrap with ssl/tls if desired
2224
if tls:
2325
try:
24-
if sys.implementation.name == 'micropython':
25-
s = ssl.wrap_socket(s, do_handshake=handshake )
26+
if sys.implementation.name == "micropython":
27+
s = ssl.wrap_socket(s, do_handshake=handshake)
2628
else:
2729
s = ssl.wrap_socket(s, do_handshake_on_connect=handshake)
2830
print("wrap: True")
2931
except Exception as e:
3032
dp(e)
3133
print("wrap:", e)
3234
# if handshake is set, we wait after connect() so it has time to actually happen
33-
if handshake and not tls: # with tls the handshake does it
35+
if handshake and not tls: # with tls the handshake does it
3436
time.sleep(0.2)
3537
return s
3638

39+
3740
def test(peer_addr, tls=False, handshake=False):
3841
# a fresh socket is opened for each combination because MP on linux is too fast
3942

4043
# hasRW is false in CPython for sockets: they don't have read or write methods
41-
hasRW = sys.implementation.name == 'micropython' or tls
44+
hasRW = sys.implementation.name == "micropython" or tls
4245

4346
# connect + send
4447
s = do_connect(peer_addr, tls, handshake)
4548
# send -> 4 or EAGAIN
4649
try:
47-
ret = s.send(b'1234')
48-
print('send:', handshake and ret == 4)
50+
ret = s.send(b"1234")
51+
print("send:", handshake and ret == 4)
4952
except OSError as er:
5053
dp(er)
51-
print('send:', er.args[0] == 11) # 11 is EAGAIN
54+
print("send:", er.args[0] == 11) # 11 is EAGAIN
5255
s.close()
5356

5457
# connect + write
5558
if hasRW:
5659
s = do_connect(peer_addr, tls, handshake)
5760
# write -> None
5861
try:
59-
ret = s.write(b'1234')
60-
print('write:', ret is (4 if handshake else None))
62+
ret = s.write(b"1234")
63+
print("write:", ret is (4 if handshake else None))
6164
except OSError as er:
6265
dp(er)
63-
print('write:', False) # should not raise
64-
except ValueError as er: # CPython
66+
print("write:", False) # should not raise
67+
except ValueError as er: # CPython
6568
dp(er)
66-
print('write:', er.args[0] == 'Write on closed or unwrapped SSL socket.')
69+
print("write:", er.args[0] == "Write on closed or unwrapped SSL socket.")
6770
s.close()
68-
else: # fake it...
69-
print('connect:', True);
70-
print('write:', True);
71+
else: # fake it...
72+
print("connect:", True)
73+
print("write:", True)
7174

7275
# connect + recv
7376
s = do_connect(peer_addr, tls, handshake)
7477
# recv -> EAGAIN
7578
try:
76-
print('recv:', s.recv(10))
79+
print("recv:", s.recv(10))
7780
except OSError as er:
7881
dp(er)
79-
print('recv:', er.args[0] == 11) # 11 is EAGAIN
82+
print("recv:", er.args[0] == 11) # 11 is EAGAIN
8083
s.close()
8184

8285
# connect + read
@@ -85,28 +88,29 @@ def test(peer_addr, tls=False, handshake=False):
8588
# read -> None
8689
try:
8790
ret = s.read(10)
88-
print('read:', ret is None)
91+
print("read:", ret is None)
8992
except OSError as er:
9093
dp(er)
91-
print('read:', False) # should not raise
92-
except ValueError as er: # CPython
+
print("read:", False) # should not raise
95+
except ValueError as er: # CPython
9396
dp(er)
94-
print('read:', er.args[0] == 'Read on closed or unwrapped SSL socket.')
97+
print("read:", er.args[0] == "Read on closed or unwrapped SSL socket.")
9598
s.close()
96-
else: # fake it...
97-
print('connect:', True);
98-
print('read:', True);
99+
else: # fake it...
100+
print("connect:", True)
101+
print("read:", True)
102+
99103

100104
if __name__ == "__main__":
101105
# these tests use an non-existant test IP address, this way the connect takes forever and
102106
# we can see EAGAIN/None (https://tools.ietf.org/html/rfc5737)
103107
print("--- Plain sockets to nowhere ---")
104-
test(socket.getaddrinfo('192.0.2.1', 80)[0][-1], False, False)
108+
test(socket.getaddrinfo("192.0.2.1", 80)[0][-1], False, False)
105109
print("--- SSL sockets to nowhere ---")
106110
# this test fails with AXTLS because do_handshake=False blocks on first read/write and
107111
# there it times out until the connect is aborted
108-
test(socket.getaddrinfo('192.0.2.1', 443)[0][-1], True, False)
112+
test(socket.getaddrinfo("192.0.2.1", 443)[0][-1], True, False)
109113
print("--- Plain sockets ---")
110-
test(socket.getaddrinfo('micropython.org', 80)[0][-1], False, True)
114+
test(socket.getaddrinfo("micropython.org", 80)[0][-1], False, True)
111115
print("--- SSL sockets ---")
112-
test(socket.getaddrinfo('micropython.org', 443)[0][-1], True, True)
116+
test(socket.getaddrinfo("micropython.org", 443)[0][-1], True, True)

tests/net_inet/ssl_errors.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@
66
except:
77
import socket, ssl, sys
88

9+
910
def test(addr, hostname, block=True):
1011
print("---", hostname or addr)
1112
s = socket.socket()
1213
s.setblocking(block)
1314
try:
1415
s.connect(addr)
15-
print('connected')
16+
print("connected")
1617
except OSError as e:
17-
if e.args[0] != 115: # 115 == EINPROGRESS
18+
if e.args[0] != 115: # 115 == EINPROGRESS
1819
raise
1920

2021
try:
@@ -25,19 +26,20 @@ def test(addr, hostname, block=True):
2526

2627
if not block:
2728
try:
28-
while s.write(b'0') is None:
29+
while s.write(b"0") is None:
2930
pass
3031
except OSError as e:
3132
print("write:", e)
3233
s.close()
3334

35+
3436
if __name__ == "__main__":
3537
# connect to plain HTTP port, oops!
36-
addr = socket.getaddrinfo('micropython.org', 80)[0][-1]
38+
addr = socket.getaddrinfo("micropython.org", 80)[0][-1]
3739
test(addr, None)
3840
# connect to plain HTTP port, oops!
39-
addr = socket.getaddrinfo('micropython.org', 80)[0][-1]
41+
addr = socket.getaddrinfo("micropython.org", 80)[0][-1]
4042
test(addr, None, False)
4143
# connect to server with self-signed cert, oops!
42-
addr = socket.getaddrinfo('test.mosquitto.org', 8883)[0][-1]
43-
test(addr, 'test.mosquitto.org')
44+
addr = socket.getaddrinfo("test.mosquitto.org", 8883)[0][-1]
45+
test(addr, "test.mosquitto.org")

tests/net_inet/test_tls_nonblock.py

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,27 @@ def test_one(site, opts):
1818
s.connect(addr)
1919
raise OSError(-1, "connect blocks")
2020
except OSError as e:
21-
if e.args[0] != 115: # 115=EINPROGRESS
21+
if e.args[0] != 115: # 115=EINPROGRESS
2222
raise
2323

24-
if sys.implementation.name != 'micropython':
24+
if sys.implementation.name != "micropython":
2525
# in CPython we have to wait, otherwise wrap_socket is not happy
2626
select.select([], [s], [])
2727

2828
try:
2929
# Wrap with SSL
3030
try:
31-
if sys.implementation.name == 'micropython':
31+
if sys.implementation.name == "micropython":
3232
s = ssl.wrap_socket(s, do_handshake=False)
3333
else:
3434
s = ssl.wrap_socket(s, do_handshake_on_connect=False)
3535
except OSError as e:
36-
if e.args[0] != 115: # 115=EINPROGRESS
36+
if e.args[0] != 115: # 115=EINPROGRESS
3737
raise
3838
print("wrapped")
3939

4040
# CPython needs to be told to do the handshake
41-
if sys.implementation.name != 'micropython':
41+
if sys.implementation.name != "micropython":
4242
while True:
4343
try:
4444
s.do_handshake()
@@ -51,16 +51,16 @@ def test_one(site, opts):
5151
else:
5252
raise
5353
time.sleep(0.1)
54-
#print("shook hands")
54+
# print("shook hands")
5555

5656
# Write HTTP request
57-
out = b"GET / HTTP/1.0\r\nHost: %s\r\n\r\n" % bytes(site, 'latin')
57+
out = b"GET / HTTP/1.0\r\nHost: %s\r\n\r\n" % bytes(site, "latin")
5858
if use_send:
5959
while len(out) > 0:
6060
try:
6161
n = s.send(out)
6262
except OSError as e:
63-
if e.args[0] != 11: # 11=EAGAIN
63+
if e.args[0] != 11: # 11=EAGAIN
6464
raise
6565
continue
6666
if n > 0:
@@ -79,19 +79,20 @@ def test_one(site, opts):
7979
print("wrote")
8080

8181
# Read response
82-
resp = b''
82+
resp = b""
8383
while True:
8484
if use_send:
8585
try:
8686
b = s.recv(128)
8787
except OSError as e:
88-
if e.args[0] == 11: # 11=EAGAIN
88+
if e.args[0] == 11: # 11=EAGAIN
8989
continue
90-
if e.args[0] == 2: # 2=ssl.SSL_ERROR_WANT_READ:
90+
if e.args[0] == 2: # 2=ssl.SSL_ERROR_WANT_READ:
9191
continue
9292
raise
9393
if len(b) > 0:
94-
if len(resp) < 1024: resp += b
94+
if len(resp) < 1024:
95+
resp += b
9596
elif len(b) == 0:
9697
break
9798
else:
@@ -100,35 +101,36 @@ def test_one(site, opts):
100101
try:
101102
b = s.read(128)
102103
except OSError as err:
103-
if err.args[0] == 2: # 2=ssl.SSL_ERROR_WANT_READ:
104+
if err.args[0] == 2: # 2=ssl.SSL_ERROR_WANT_READ:
104105
continue
105106
raise
106107
if b is None:
107108
continue
108109
if len(b) > 0:
109-
if len(resp) < 1024: resp += b
110+
if len(resp) < 1024:
111+
resp += b
110112
elif len(b) == 0:
111113
break
112114
print("read")
113115

114-
if resp[:7] != b'HTTP/1.':
116+
if resp[:7] != b"HTTP/1.":
115117
raise ValueError("response doesn't start with HTTP/1.")
116-
#print(resp)
118+
# print(resp)
117119

118120
finally:
119121
s.close()
120122

121123

122124
SITES = [
123125
"google.com",
124-
{"host": "www.google.com", "send":True},
125-
"micropython.org", # used in the built-in upip, it better work...
126-
"pypi.org", # ditto
126+
{"host": "www.google.com", "send": True},
127+
"micropython.org", # used in the built-in upip, it better work...
128+
"pypi.org", # ditto
127129
"api.telegram.org",
128130
{"host": "api.pushbullet.com", "sni": True},
129-
# this no longer works, not sure which special case it is supposed to test...
130-
# "w9rybpfril.execute-api.ap-southeast-2.amazonaws.com",
131-
# {"host": "w9rybpfril.execute-api.ap-southeast-2.amazonaws.com", "sni": True},
131+
# this no longer works, not sure which special case it is supposed to test...
132+
# "w9rybpfril.execute-api.ap-southeast-2.amazonaws.com",
133+
# {"host": "w9rybpfril.execute-api.ap-southeast-2.amazonaws.com", "sni": True},
132134
]
133135

134136

@@ -148,4 +150,3 @@ def main():
148150

149151

150152
main()
151-

0 commit comments

Comments
 (0)
0