Skip to content

Commit c0c1d9f

Browse files
committed
Wrap sys.stdout.flush() in try block
MicroPython does not include sys.stdout.flush()
1 parent b38fa04 commit c0c1d9f

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

examples/qwiic_keypad_ex1.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,13 @@ def runExample():
8181
else:
8282
print(charButton, end="")
8383

84-
# Flush the stdout buffer to give immediate user feedback
85-
sys.stdout.flush()
84+
# Some platforms won't print until newline character, so a flush is
85+
# needed to update. However some platforms (eg. MicroPython) don't
86+
# include sys.stdout.flush(), so wrap in a try block
87+
try:
88+
sys.stdout.flush()
89+
except:
90+
pass
8691

8792
time.sleep(.25)
8893

examples/qwiic_keypad_ex3.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,13 @@ def clearEntry(userEntry):
140140
def printEntry(userEntry):
141141

142142
print("\rUserEntry:%s" % (''.join(userEntry)), end="")
143-
sys.stdout.flush()
143+
# Some platforms won't print until newline character, so a flush is
144+
# needed to update. However some platforms (eg. MicroPython) don't
145+
# include sys.stdout.flush(), so wrap in a try block
146+
try:
147+
sys.stdout.flush()
148+
except:
149+
pass
144150

145151

146152
if __name__ == '__main__':

0 commit comments

Comments
 (0)