Skip to content
Open
Changes from 2 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
16 changes: 16 additions & 0 deletions pygbx/gbx.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ def __init__(self, obj):
self.root_parser = ByteReader(obj)

self.magic = self.root_parser.read(3, '3s')
try:
self.magic.decode('utf-8')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because magic is used only temporarily in the constructor, drop self and reuse it:

magic = self.root_parser.read(3, '3s')
try:
    magic = magic.decode('utf-8')
except:
    raise GbxLoadError(f'obj is not a valid Gbx data: can not decode unicode')

if magic != 'GBX':
   ...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good one. Pushed another version, which includes this change.

except:
raise GbxLoadError(f'obj is not a valid Gbx data: can not decode unicode')

if self.magic.decode('utf-8') != 'GBX':
raise GbxLoadError(f'obj is not a valid Gbx data: magic string is incorrect')
self.version = self.root_parser.read(2, 'H')
Expand Down Expand Up @@ -135,6 +140,17 @@ def __init__(self, obj):
bp = ByteReader(self.data)
self._read_node(self.class_id, -1, bp)

def __enter__(self):
return self

def __exit__(self, exception_type, exception_value, exception_traceback):
self.close()

"""Closes the current file connection."""
def close(self):
if self.f:
self.f.close()

def __read_sub_folder(self):
num_sub_folders = self.root_parser.read_uint32()
for folder in range(num_sub_folders):
Expand Down