简体   繁体   中英

How to unpack incomplete structs

A common method to make file formats extensible is to include the size of records included in the file in order to make it possible in the future to add more fields to the record.

Now assume that the field has been updated and we have a python struct format for the new record layout, but this means that this format cannot be used to unpack records of the older format as the record is too small.

What I would want is for struct.unpack to unpack those fields it can unpack (without running out of data) instead of throwing exception.

Is there an easy way to get this done? Or would one be required to have the struct format for each prior version of record layout?

There doesn't seem to be any native feature for struct allowing for some backward compatibility.

That means, if you have an exception, you need to fallback to:

  • reading the same file with the old struct
  • marshalling that old struct into a new struct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM