Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NMEASentence/__getattr__: Graceful fail #143

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions pynmea2/nmea.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import re
import operator
from functools import reduce
from logging import getLogger


log = getLogger()


class ParseError(ValueError):
Expand Down Expand Up @@ -149,10 +153,12 @@ def parse(line, check=False):
def __getattr__(self, name):
#pylint: disable=invalid-name
t = type(self)
try:
i = t.name_to_idx[name]
except KeyError:
raise AttributeError(name)

if name not in t.name_to_idx:
log.debug("%s has no attribute %s", self.__class__.__name__, name)
return None
i = t.name_to_idx[name]

f = t.fields[i]
if i < len(self.data):
v = self.data[i]
Expand Down
3 changes: 1 addition & 2 deletions test/test_pynmea.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ def test_checksum():

def test_attribute():
msg = pynmea2.parse(data)
with pytest.raises(AttributeError):
msg.foobar
assert msg.foobar is None


def test_fail():
Expand Down