-
-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enhance exception handling for ERR_LOG and ERR_IGNORE
- Loading branch information
Showing
2 changed files
with
34 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -540,7 +540,7 @@ def testUBXITERATE_ERR4( | |
self.assertEqual(EXPECTED_RESULT, res) | ||
|
||
def testBADHDR_FAIL(self): # invalid header in data with quitonerror = 2 | ||
EXPECTED_ERROR = "Unknown protocol b'\\xb5w'" | ||
EXPECTED_ERROR = "Unknown protocol header b'\\xb5w'" | ||
with self.assertRaises(UBXParseError) as context: | ||
i = 0 | ||
with open(os.path.join(DIRNAME, "pygpsdata-BADHDR.log"), "rb") as stream: | ||
|
@@ -551,11 +551,14 @@ def testBADHDR_FAIL(self): # invalid header in data with quitonerror = 2 | |
|
||
def testBADHDR_LOG(self): # invalid header in data with quitonerror = 1 | ||
i = 0 | ||
self.catchio() | ||
with open(os.path.join(DIRNAME, "pygpsdata-BADHDR.log"), "rb") as stream: | ||
|
||
ubr = UBXReader(stream, quitonerror=ERR_LOG) | ||
for raw, parsed in ubr: | ||
i += 1 | ||
self.assertEqual(parsed, "<UNKNOWN PROTOCOL(header=b'\\xb5w')>") | ||
output = self.restoreio().split("\n") | ||
self.assertEqual(output, ["Unknown protocol header b'\\xb5w'."]) | ||
This comment has been minimized.
Sorry, something went wrong.
carstenandrich
|
||
|
||
def testBADHDR_IGNORE(self): # invalid header in data with quitonerror = 0 | ||
i = 0 | ||
|
1 comment
on commit 8fa0bcf
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Quick git diff to use logging instead of printing to stdout. Worked for the, but didn't do the unittests.
diff --git a/src/pyubx2/ubxreader.py b/src/pyubx2/ubxreader.py
index cf8294a..5f3b979 100644
--- a/src/pyubx2/ubxreader.py
+++ b/src/pyubx2/ubxreader.py
@@ -21,6 +21,7 @@ Created on 2 Oct 2020
"""
from socket import socket
+import logging
import pynmeagps.exceptions as nme
import pyrtcm.exceptions as rte
@@ -101,6 +102,7 @@ class UBXReader:
self._labelmsm = labelmsm
self._msgmode = msgmode
self._parsing = parsing
+ self._logger = logging.getLogger(__name__)
if self._msgmode not in (GET, SET, POLL, SETPOLL):
raise UBXStreamError(
@@ -344,7 +346,7 @@ class UBXReader:
# pass to error handler if there is one
# else just print to stdout
if self._errorhandler is None:
- print(err)
+ self._logger.error(err)
else:
self._errorhandler(err)
@@ -446,5 +448,5 @@ class UBXReader:
if quitonerror == ERR_RAISE:
raise UBXParseError(errmsg) from err
if quitonerror == ERR_LOG:
- print(errmsg)
+ self._logger.error(errmsg)
return None
IMHO a library should never print to stdout, as you never know if the application using the library is using stdout to pipe precious data to another process or write it to a file. An imported library writing to stdout would clobber this data. If you really have to write error messages, use stderr, but I would still frown upon that.
Better solution, is to use the logging module, which defaults to printing to stderr, when not configured. See comment below for details.