release_notes.txt 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524
  1. --------------------------------
  2. bitstring module version history
  3. --------------------------------
  4. ---------------------------------------
  5. March 4th 2014: version 3.1.3 released
  6. ---------------------------------------
  7. This is another bug fix release.
  8. * Fix for problem with prepend for bitstrings with byte offsets in their data store.
  9. ---------------------------------------
  10. April 18th 2013: version 3.1.2 released
  11. ---------------------------------------
  12. This is another bug fix release.
  13. * Fix for problem where unpacking bytes would by eight times too long
  14. ---------------------------------------
  15. March 21st 2013: version 3.1.1 released
  16. ---------------------------------------
  17. This is a bug fix release.
  18. * Fix for problem where concatenating bitstrings sometimes modified method's arguments
  19. ------------------------------------------
  20. February 26th 2013: version 3.1.0 released
  21. ------------------------------------------
  22. This is a minor release with a couple of new features and some bug fixes.
  23. New 'pad' token
  24. ---------------
  25. This token can be used in reads and when packing/unpacking to indicate that
  26. you don't care about the contents of these bits. Any padding bits will just
  27. be skipped over when reading/unpacking or zero-filled when packing.
  28. >>> a, b = s.readlist('pad:5, uint:3, pad:1, uint:3')
  29. Here only two items are returned in the list - the padding bits are ignored.
  30. New clear and copy convenience methods
  31. --------------------------------------
  32. These methods have been introduced in Python 3.3 for lists and bytearrays,
  33. as more obvious ways of clearing and copying, and we mirror that change here.
  34. t = s.copy() is equivalent to t = s[:], and s.clear() is equivalent to del s[:].
  35. Other changes
  36. -------------
  37. * Some bug fixes.
  38. -----------------------------------------
  39. February 7th 2012: version 3.0.2 released
  40. -----------------------------------------
  41. This is a minor update that fixes a few bugs.
  42. * Fix for subclasses of bitstring classes behaving strangely (Issue 121).
  43. * Fix for excessive memory usage in rare cases (Issue 120).
  44. * Fixes for slicing edge cases.
  45. There has also been a reorganisation of the code to return it to a single
  46. 'bitstring.py' file rather than the package that has been used for the past
  47. several releases. This change shouldn't affect users directly.
  48. ------------------------------------------
  49. November 21st 2011: version 3.0.1 released
  50. ------------------------------------------
  51. This release fixed a small but very visible bug in bitstring printing.
  52. ------------------------------------------
  53. November 21st 2011: version 3.0.0 released
  54. ------------------------------------------
  55. This is a major release which breaks backward compatibility in a few places.
  56. Backwardly incompatible changes
  57. ===============================
  58. Hex, oct and bin properties don't have leading 0x, 0o and 0b
  59. ------------------------------------------------------------
  60. If you ask for the hex, octal or binary representations of a bitstring then
  61. they will no longer be prefixed with '0x', 0o' or '0b'. This was done as it
  62. was noticed that the first thing a lot of user code does after getting these
  63. representations was to cut off the first two characters before further
  64. processing.
  65. >>> a = BitArray('0x123')
  66. >>> a.hex, a.oct, a.bin
  67. ('123', '0443', '000100100011')
  68. Previously this would have returned ('0x123', '0o0443', '0b000100100011')
  69. This change might require some recoding, but it should all be simplifications.
  70. ConstBitArray renamed to Bits
  71. -----------------------------
  72. Previously Bits was an alias for ConstBitStream (for backward compatibility).
  73. This has now changed so that Bits and BitArray loosely correspond to the
  74. built-in types bytes and bytearray.
  75. If you were using streaming/reading methods on a Bits object then you will
  76. have to change it to a ConstBitStream.
  77. The ConstBitArray name is kept as an alias for Bits.
  78. Stepping in slices has conventional meaning
  79. -------------------------------------------
  80. The step parameter in __getitem__, __setitem__ and __delitem__ used to act
  81. as a multiplier for the start and stop parameters. No one seemed to use it
  82. though and so it has now reverted to the convential meaning for containers.
  83. If you are using step then recoding is simple: s[a:b:c] becomes s[a*c:b*c].
  84. Some examples of the new usage:
  85. >>> s = BitArray('0x0000')
  86. s[::4] = [1, 1, 1, 1]
  87. >>> s.hex
  88. '8888'
  89. >>> del s[8::2]
  90. >>> s.hex
  91. '880'
  92. New features
  93. ============
  94. New readto method
  95. -----------------
  96. This method is a mix between a find and a read - it searches for a bitstring
  97. and then reads up to and including it. For example:
  98. >>> s = ConstBitStream('0x47000102034704050647')
  99. >>> s.readto('0x47', bytealigned=True)
  100. BitStream('0x47')
  101. >>> s.readto('0x47', bytealigned=True)
  102. BitStream('0x0001020347')
  103. >>> s.readto('0x47', bytealigned=True)
  104. BitStream('0x04050647')
  105. pack function accepts an iterable as its format
  106. -----------------------------------------------
  107. Previously only a string was accepted as the format in the pack function.
  108. This was an oversight as it broke the symmetry between pack and unpack.
  109. Now you can use formats like this:
  110. fmt = ['hex:8', 'bin:3']
  111. a = pack(fmt, '47', '001')
  112. a.unpack(fmt)
  113. --------------------------------------
  114. June 18th 2011: version 2.2.0 released
  115. --------------------------------------
  116. This is a minor upgrade with a couple of new features.
  117. New interleaved exponential-Golomb interpretations
  118. --------------------------------------------------
  119. New bit interpretations for interleaved exponential-Golomb (as used in the
  120. Dirac video codec) are supplied via 'uie' and 'sie':
  121. >>> s = BitArray(uie=41)
  122. >>> s.uie
  123. 41
  124. >>> s.bin
  125. '0b00010001001'
  126. These are pretty similar to the non-interleaved versions - see the manual
  127. for more details. Credit goes to Paul Sargent for the patch.
  128. New package-level bytealigned variable
  129. --------------------------------------
  130. A number of methods take a 'bytealigned' parameter to indicate that they
  131. should only work on byte boundaries (e.g. find, replace, split). Previously
  132. this parameter defaulted to 'False'. Instead it now defaults to
  133. 'bitstring.bytealigned', which itself defaults to 'False', but can be changed
  134. to modify the default behaviour of the methods. For example:
  135. >>> a = BitArray('0x00 ff 0f ff')
  136. >>> a.find('0x0f')
  137. (4,) # found first not on a byte boundary
  138. >>> a.find('0x0f', bytealigned=True)
  139. (16,) # forced looking only on byte boundaries
  140. >>> bitstring.bytealigned = True # Change default behaviour
  141. >>> a.find('0x0f')
  142. (16,)
  143. >>> a.find('0x0f', bytealigned=False)
  144. (4,)
  145. If you're only working with bytes then this can help avoid some errors and
  146. save some typing!
  147. Other changes
  148. -------------
  149. * Fix for Python 3.2, correcting for a change to the binascii module.
  150. * Fix for bool initialisation from 0 or 1.
  151. * Efficiency improvements, including interning strategy.
  152. ------------------------------------------
  153. February 23rd 2011: version 2.1.1 released
  154. ------------------------------------------
  155. This is a release to fix a couple of bugs that were introduced in 2.1.0.
  156. * Bug fix: Reading using the 'bytes' token had been broken (Issue 102).
  157. * Fixed problem using some methods on ConstBitArrays.
  158. * Better exception handling for tokens missing values.
  159. * Some performance improvements.
  160. -----------------------------------------
  161. January 23rd 2011: version 2.1.0 released
  162. -----------------------------------------
  163. New class hierarchy introduced with simpler classes
  164. ---------------------------------------------------
  165. Previously there were just two classes, the immutable Bits which was the base
  166. class for the mutable BitString class. Both of these classes have the concept
  167. of a bit position, from which reads etc. take place so that the bitstring could
  168. be treated as if it were a file or stream.
  169. Two simpler classes have now been added which are purely bit containers and
  170. don't have a bit position. These are called ConstBitArray and BitArray. As you
  171. can guess the former is an immutable version of the latter.
  172. The other classes have also been renamed to better reflect their capabilities.
  173. Instead of BitString you can use BitStream, and instead of Bits you can use
  174. ConstBitStream. The old names are kept as aliases for backward compatibility.
  175. The classes hierarchy is:
  176. ConstBitArray
  177. / \
  178. / \
  179. BitArray ConstBitStream (formerly Bits)
  180. \ /
  181. \ /
  182. BitStream (formerly BitString)
  183. Other changes
  184. -------------
  185. A lot of internal reorganisation has taken place since the previous version,
  186. most of which won't be noticed by the end user. Some things you might see are:
  187. * New package structure. Previous versions have been a single file for the
  188. module and another for the unit tests. The module is now split into many
  189. more files so it can't be used just by copying bitstring.py any more.
  190. * To run the unit tests there is now a script called runtests.py in the test
  191. directory.
  192. * File based bitstring are now implemented in terms of an mmap. This should
  193. be just an implementation detail, but unfortunately for 32-bit versions of
  194. Python this creates a limit of 4GB on the files that can be used. The work
  195. around is either to get a 64-bit Python, or just stick with version 2.0.
  196. * The ConstBitArray and ConstBitStream classes no longer copy byte data when
  197. a slice or a read takes place, they just take a reference. This is mostly
  198. a very nice optimisation, but there are occassions where it could have an
  199. adverse effect. For example if a very large bitstring is created, a small
  200. slice taken and the original deleted. The byte data from the large
  201. bitstring would still be retained in memory.
  202. * Optimisations. Once again this version should be faster than the last.
  203. The module is still pure Python but some of the reorganisation was to make
  204. it more feasible to put some of the code into Cython or similar, so
  205. hopefully more speed will be on the way.
  206. --------------------------------------
  207. July 26th 2010: version 2.0.3 released
  208. --------------------------------------
  209. * Bug fix: Using peek and read for a single bit now returns a new bitstring
  210. as was intended, rather than the old behaviour of returning a bool.
  211. * Removed HTML docs from source archive - better to use the online version.
  212. --------------------------------------
  213. July 25th 2010: version 2.0.2 released
  214. --------------------------------------
  215. This is a major release, with a number of backwardly incompatible changes.
  216. The main change is the removal of many methods, all of which have simple
  217. alternatives. Other changes are quite minor but may need some recoding.
  218. There are a few new features, most of which have been made to help the
  219. stream-lining of the API. As always there are performance improvements and
  220. some API changes were made purely with future performance in mind.
  221. The backwardly incompatible changes are:
  222. -----------------------------------------
  223. * Methods removed.
  224. About half of the class methods have been removed from the API. They all have
  225. simple alternatives, so what remains is more powerful and easier to remember.
  226. The removed methods are listed here on the left, with their equivalent
  227. replacements on the right:
  228. s.advancebit() -> s.pos += 1
  229. s.advancebits(bits) -> s.pos += bits
  230. s.advancebyte() -> s.pos += 8
  231. s.advancebytes(bytes) -> s.pos += 8*bytes
  232. s.allunset([a, b]) -> s.all(False, [a, b])
  233. s.anyunset([a, b]) -> s.any(False, [a, b])
  234. s.delete(bits, pos) -> del s[pos:pos+bits]
  235. s.peekbit() -> s.peek(1)
  236. s.peekbitlist(a, b) -> s.peeklist([a, b])
  237. s.peekbits(bits) -> s.peek(bits)
  238. s.peekbyte() -> s.peek(8)
  239. s.peekbytelist(a, b) -> s.peeklist([8*a, 8*b])
  240. s.peekbytes(bytes) -> s.peek(8*bytes)
  241. s.readbit() -> s.read(1)
  242. s.readbitlist(a, b) -> s.readlist([a, b])
  243. s.readbits(bits) -> s.read(bits)
  244. s.readbyte() -> s.read(8)
  245. s.readbytelist(a, b) -> s.readlist([8*a, 8*b])
  246. s.readbytes(bytes) -> s.read(8*bytes)
  247. s.retreatbit() -> s.pos -= 1
  248. s.retreatbits(bits) -> s.pos -= bits
  249. s.retreatbyte() -> s.pos -= 8
  250. s.retreatbytes(bytes) -> s.pos -= 8*bytes
  251. s.reversebytes(start, end) -> s.byteswap(0, start, end)
  252. s.seek(pos) -> s.pos = pos
  253. s.seekbyte(bytepos) -> s.bytepos = bytepos
  254. s.slice(start, end, step) -> s[start:end:step]
  255. s.tell() -> s.pos
  256. s.tellbyte() -> s.bytepos
  257. s.truncateend(bits) -> del s[-bits:]
  258. s.truncatestart(bits) -> del s[:bits]
  259. s.unset([a, b]) -> s.set(False, [a, b])
  260. Many of these methods have been deprecated for the last few releases, but
  261. there are some new removals too. Any recoding needed should be quite
  262. straightforward, so while I apologise for the hassle, I had to take the
  263. opportunity to streamline and rationalise what was becoming a bit of an
  264. overblown API.
  265. * set / unset methods combined.
  266. The set/unset methods have been combined in a single method, which now
  267. takes a boolean as its first argument:
  268. s.set([a, b]) -> s.set(1, [a, b])
  269. s.unset([a, b]) -> s.set(0, [a, b])
  270. s.allset([a, b]) -> s.all(1, [a, b])
  271. s.allunset([a, b]) -> s.all(0, [a, b])
  272. s.anyset([a, b]) -> s.any(1, [a, b])
  273. s.anyunset([a, b]) -> s.any(0, [a, b])
  274. * all / any only accept iterables.
  275. The all and any methods (previously called allset, allunset, anyset and
  276. anyunset) no longer accept a single bit position. The recommended way of
  277. testing a single bit is just to index it, for example instead of:
  278. >>> if s.all(True, i):
  279. just use
  280. >>> if s[i]:
  281. If you really want to you can of course use an iterable with a single
  282. element, such as 's.any(False, [i])', but it's clearer just to write
  283. 'not s[i]'.
  284. * Exception raised on reading off end of bitstring.
  285. If a read or peek goes beyond the end of the bitstring then a ReadError
  286. will be raised. The previous behaviour was that the rest of the bitstring
  287. would be returned and no exception raised.
  288. * BitStringError renamed to Error.
  289. The base class for errors in the bitstring module is now just Error, so
  290. it will likely appears in your code as bitstring.Error instead of
  291. the rather repetitive bitstring.BitStringError.
  292. * Single bit slices and reads return a bool.
  293. A single index slice (such as s[5]) will now return a bool (i.e. True or
  294. False) rather than a single bit bitstring. This is partly to reflect the
  295. style of the bytearray type, which returns an integer for single items, but
  296. mostly to avoid common errors like:
  297. >>> if s[0]:
  298. ... do_something()
  299. While the intent of this code snippet is quite clear (i.e. do_something if
  300. the first bit of s is set) under the old rules s[0] would be true as long
  301. as s wasn't empty. That's because any one-bit bitstring was true as it was a
  302. non-empty container. Under the new rule s[0] is True if s starts with a '1'
  303. bit and False if s starts with a '0' bit.
  304. The change does not affect reads and peeks, so s.peek(1) will still return
  305. a single bit bitstring, which leads on to the next item...
  306. * Empty bitstrings or bitstrings with only zero bits are considered False.
  307. Previously a bitstring was False if it had no elements, otherwise it was True.
  308. This is standard behaviour for containers, but wasn't very useful for a container
  309. of just 0s and 1s. The new behaviour means that the bitstring is False if it
  310. has no 1 bits. This means that code like this:
  311. >>> if s.peek(1):
  312. ... do_something()
  313. should work as you'd expect. It also means that Bits(1000), Bits(0x00) and
  314. Bits('uint:12=0') are all also False. If you need to check for the emptiness of
  315. a bitstring then instead check the len property:
  316. if s -> if s.len
  317. if not s -> if not s.len
  318. * Length and offset disallowed for some initialisers.
  319. Previously you could create bitstring using expressions like:
  320. >>> s = Bits(hex='0xabcde', offset=4, length=13)
  321. This has now been disallowed, and the offset and length parameters may only
  322. be used when initialising with bytes or a file. To replace the old behaviour
  323. you could instead use
  324. >>> s = Bits(hex='0xabcde')[4:17]
  325. * Renamed 'format' parameter 'fmt'.
  326. Methods with a 'format' parameter have had it renamed to 'fmt', to prevent
  327. hiding the built-in 'format'. Affects methods unpack, read, peek, readlist,
  328. peeklist and byteswap and the pack function.
  329. * Iterables instead of *format accepted for some methods.
  330. This means that for the affected methods (unpack, readlist and peeklist) you
  331. will need to use an iterable to specify multiple items. This is easier to
  332. show than to describe, so instead of
  333. >>> a, b, c, d = s.readlist('uint:12', 'hex:4', 'bin:7')
  334. you would instead write
  335. >>> a, b, c, d = s.readlist(['uint:12', 'hex:4', 'bin:7'])
  336. Note that you could still use the single string 'uint:12, hex:4, bin:7' if
  337. you preferred.
  338. * Bool auto-initialisation removed.
  339. You can no longer use True and False to initialise single bit bitstrings.
  340. The reasoning behind this is that as bool is a subclass of int, it really is
  341. bad practice to have Bits(False) be different to Bits(0) and to have Bits(True)
  342. different to Bits(1).
  343. If you have used bool auto-initialisation then you will have to be careful to
  344. replace it as the bools will now be interpreted as ints, so Bits(False) will
  345. be empty (a bitstring of length 0), and Bits(True) will be a single zero bit
  346. (a bitstring of length 1). Sorry for the confusion, but I think this will
  347. prevent bigger problems in the future.
  348. There are a few alternatives for creating a single bit bitstring. My favourite
  349. it to use a list with a single item:
  350. Bits(False) -> Bits([0])
  351. Bits(True) -> Bits([1])
  352. * New creation from file strategy
  353. Previously if you created a bitstring from a file, either by auto-initialising
  354. with a file object or using the filename parameter, the file would not be read
  355. into memory unless you tried to modify it, at which point the whole file would
  356. be read.
  357. The new behaviour depends on whether you create a Bits or a BitString from the
  358. file. If you create a Bits (which is immutable) then the file will never be
  359. read into memory. This allows very large files to be opened for examination
  360. even if they could never fit in memory.
  361. If however you create a BitString, the whole of the referenced file will be read
  362. to store in memory. If the file is very big this could take a long time, or fail,
  363. but the idea is that in saying you want the mutable BitString you are implicitly
  364. saying that you want to make changes and so (for now) we need to load it into
  365. memory.
  366. The new strategy is a bit more predictable in terms of performance than the old.
  367. The main point to remember is that if you want to open a file and don't plan to
  368. alter the bitstring then use the Bits class rather than BitString.
  369. Just to be clear, in neither case will the contents of the file ever be changed -
  370. if you want to output the modified BitString then use the tofile method, for
  371. example.
  372. * find and rfind return a tuple instead of a bool.
  373. If a find is unsuccessful then an empty tuple is returned (which is False in a
  374. boolean sense) otherwise a single item tuple with the bit position is returned
  375. (which is True in a boolean sense). You shouldn't need to recode unless you
  376. explicitly compared the result of a find to True or False, for example this
  377. snippet doesn't need to be altered:
  378. >>> if s.find('0x23'):
  379. ... print(s.bitpos)
  380. but you could now instead use
  381. >>> found = s.find('0x23')
  382. >>> if found:
  383. ... print(found[0])
  384. The reason for returning the bit position in a tuple is so that finding at
  385. position zero can still be True - it's the tuple (0,) - whereas not found can
  386. be False - the empty tuple ().
  387. The new features in this release are:
  388. -------------------------------------
  389. * New count method.
  390. This method just counts the number of 1 or 0 bits in the bitstring.
  391. >>> s = Bits('0x31fff4')
  392. >>> s.count(1)
  393. 16
  394. * read and peek methods accept integers.
  395. The read, readlist, peek and peeklist methods now accept integers as parameters
  396. to mean "read this many bits and return a bitstring". This has allowed a number
  397. of methods to be removed from this release, so for example instead of:
  398. >>> a, b, c = s.readbits(5, 6, 7)
  399. >>> if s.peekbit():
  400. ... do_something()
  401. you should write:
  402. >>> a, b, c = s.readlist([5, 6, 7])
  403. >>> if s.peek(1):
  404. ... do_something()
  405. * byteswap used to reverse all bytes.
  406. The byteswap method now allows a format specifier of 0 (the default) to signify
  407. that all of the whole bytes should be reversed. This means that calling just
  408. byteswap() is almost equivalent to the now removed bytereverse() method (a small
  409. difference is that byteswap won't raise an exception if the bitstring isn't a
  410. whole number of bytes long).
  411. * Auto initialise with bytearray or (for Python 3 only) bytes.
  412. So rather than writing:
  413. >>> a = Bits(bytes=some_bytearray)
  414. you can just write
  415. >>> a = Bits(some_bytearray)
  416. This also works for the bytes type, but only if you're using Python 3.
  417. For Python 2 it's not possible to distinguish between a bytes object and a
  418. str. For this reason this method should be used with some caution as it will
  419. make you code behave differently with the different major Python versions.
  420. >>> b = Bits(b'abcd\x23\x00') # Only Python 3!
  421. * set, invert, all and any default to whole bitstring.
  422. This means that you can for example write:
  423. >>> a = BitString(100) # 100 zero bits
  424. >>> a.set(1) # set all bits to 1
  425. >>> a.all(1) # are all bits set to 1?
  426. True
  427. >>> a.any(0) # are any set to 0?
  428. False
  429. >>> a.invert() # invert every bit
  430. * New exception types.
  431. As well as renaming BitStringError to just Error
  432. there are also new exceptions which use Error as a base class.
  433. These can be caught in preference to Error if you need finer control.
  434. The new exceptions sometimes also derive from built-in exceptions:
  435. ByteAlignError(Error) - whole byte position or length needed.
  436. ReadError(Error, IndexError) - reading or peeking off the end of
  437. the bitstring.
  438. CreationError(Error, ValueError) - inappropriate argument during
  439. bitstring creation.
  440. InterpretError(Error, ValueError) - inappropriate interpretation of
  441. binary data.
  442. --------------------------------------------------------------
  443. March 18th 2010: version 1.3.0 for Python 2.6 and 3.x released
  444. --------------------------------------------------------------
  445. New features:
  446. * byteswap method for changing endianness.
  447. Changes the endianness in-place according to a format string or
  448. integer(s) giving the byte pattern. See the manual for details.
  449. >>> s = BitString('0x00112233445566')
  450. >>> s.byteswap(2)
  451. 3
  452. >>> s
  453. BitString('0x11003322554466')
  454. >>> s.byteswap('h')
  455. 3
  456. >>> s
  457. BitString('0x00112233445566')
  458. >>> s.byteswap([2, 5])
  459. 1
  460. >>> s
  461. BitString('0x11006655443322')
  462. * Multiplicative factors in bitstring creation and reading.
  463. For example:
  464. >>> s = Bits('100*0x123')
  465. * Token grouping using parenthesis.
  466. For example:
  467. >>> s = Bits('3*(uint:6=3, 0b1)')
  468. * Negative slice indices allowed.
  469. The start and end parameters of many methods may now be negative, with the
  470. same meaning as for negative slice indices. Affects all methods with these
  471. parameters.
  472. * Sequence ABCs used.
  473. The Bits class now derives from collections.Sequence, while the BitString
  474. class derives from collections.MutableSequence.
  475. * Keywords allowed in readlist, peeklist and unpack.
  476. Keywords for token lengths are now permitted when reading. So for example,
  477. you can write
  478. >>> s = bitstring.pack('4*(uint:n)', 2, 3, 4, 5, n=7)
  479. >>> s.unpack('4*(uint:n)', n=7)
  480. [2, 3, 4, 5]
  481. * start and end parameters added to rol and ror.
  482. * join function accepts other iterables.
  483. Also its parameter has changed from 'bitstringlist' to 'sequence'. This is
  484. technically a backward incompatibility in the unlikely event that you are
  485. referring to the parameter by name.
  486. * __init__ method accepts keywords.
  487. Rather than a long list of initialisers the __init__ methods now use a
  488. **kwargs dictionary for all initialisers except 'auto'. This should have no
  489. effect, except that this is a small backward incompatibility if you use
  490. positional arguments when initialising with anything other than auto
  491. (which would be rather unusual).
  492. * More optimisations.
  493. * Bug fixed in replace method (it could fail if start != 0).
  494. ----------------------------------------------------------------
  495. January 19th 2010: version 1.2.0 for Python 2.6 and 3.x released
  496. ----------------------------------------------------------------
  497. * New 'Bits' class.
  498. Introducing a brand new class, Bits, representing an immutable sequence of
  499. bits.
  500. The Bits class is the base class for the mutable BitString. The differences
  501. between Bits and BitStrings are:
  502. 1) Bits are immutable, so once they have been created their value cannot change.
  503. This of course means that mutating methods (append, replace, del etc.) are not
  504. available for Bits.
  505. 2) Bits are hashable, so they can be used in sets and as keys in dictionaries.
  506. 3) Bits are potentially more efficient than BitStrings, both in terms of
  507. computation and memory. The current implementation is only marginally
  508. more efficient though - this should improve in future versions.
  509. You can switch from Bits to a BitString or vice versa by constructing a new
  510. object from the old.
  511. >>> s = Bits('0xabcd')
  512. >>> t = BitString(s)
  513. >>> t.append('0xe')
  514. >>> u = Bits(t)
  515. The relationship between Bits and BitString is supposed to loosely mirror that
  516. between bytes and bytearray in Python 3.
  517. * Deprecation messages turned on.
  518. A number of methods have been flagged for removal in version 2. Deprecation
  519. warnings will now be given, which include an alternative way to do the same
  520. thing. All of the deprecated methods have simpler equivalent alternatives.
  521. >>> t = s.slice(0, 2)
  522. __main__:1: DeprecationWarning: Call to deprecated function slice.
  523. Instead of 's.slice(a, b, c)' use 's[a:b:c]'.
  524. The deprecated methods are: advancebit, advancebits, advancebyte, advancebytes,
  525. retreatbit, retreatbits, retreatbyte, retreatbytes, tell, seek, slice, delete,
  526. tellbyte, seekbyte, truncatestart and truncateend.
  527. * Initialise from bool.
  528. Booleans have been added to the list of types that can 'auto'
  529. initialise a bitstring.
  530. >>> zerobit = BitString(False)
  531. >>> onebit = BitString(True)
  532. * Improved efficiency.
  533. More methods have been speeded up, in particular some deletions and insertions.
  534. * Bug fixes.
  535. A rare problem with truncating the start of bitstrings was fixed.
  536. A possible problem outputting the final byte in tofile() was fixed.
  537. -----------------------------------------------------------------
  538. December 22nd 2009: version 1.1.3 for Python 2.6 and 3.x released
  539. -----------------------------------------------------------------
  540. This version hopefully fixes an installation problem for platforms with
  541. case-sensitive file systems. There are no new features or other bug fixes.
  542. -----------------------------------------------------------------
  543. December 18th 2009: version 1.1.2 for Python 2.6 and 3.x released
  544. -----------------------------------------------------------------
  545. This is a minor update with (almost) no new features.
  546. * Improved efficiency.
  547. The speed of many typical operations has been increased, some substantially.
  548. * Initialise from integer.
  549. A BitString of '0' bits can be created using just an integer to give the length
  550. in bits. So instead of
  551. >>> s = BitString(length=100)
  552. you can write just
  553. >>> s = BitString(100)
  554. This matches the behaviour of bytearrays and (in Python 3) bytes.
  555. * A defect related to using the set / unset functions on BitStrings initialised
  556. from a file has been fixed.
  557. -----------------------------------------------------------------
  558. November 24th 2009: version 1.1.0 for Python 2.6 and 3.x released
  559. -----------------------------------------------------------------
  560. Note that this version will not work for Python 2.4 or 2.5. There may be an
  561. update for these Python versions some time next year, but it's not a priorty
  562. quite yet. Also note that only one version is now provided, which works for
  563. Python 2.6 and 3.x (done with the minimum of hackery!)
  564. * Improved efficiency.
  565. A fair number of functions have improved efficiency, some quite dramatically.
  566. * New bit setting and checking functions.
  567. Although these functions don't do anything that couldn't be done before, they
  568. do make some common use cases much more efficient. If you need to set or check
  569. single bits then these are the functions you need.
  570. set / unset : Set bit(s) to 1 or 0 respectively.
  571. allset / allunset : Check if all bits are 1 or all 0.
  572. anyset / anyunset : Check if any bits are 1 or any 0.
  573. >>> s = BitString(length=1000)
  574. >>> s.set((10, 100, 44, 12, 1))
  575. >>> s.allunset((2, 22, 222))
  576. True
  577. >>> s.anyset(range(7, 77))
  578. True
  579. * New rotate functions.
  580. ror / rol : Rotate bits to the right or left respectively.
  581. >>> s = BitString('0b100000000')
  582. >>> s.ror(2)
  583. >>> s.bin
  584. '0b001000000'
  585. >>> s.rol(5)
  586. >>> s.bin
  587. '0b000000100'
  588. * Floating point interpretations.
  589. New float initialisations and interpretations are available. These only work
  590. for BitStrings of length 32 or 64 bits.
  591. >>> s = BitString(float=0.2, length=64)
  592. >>> s.float
  593. 0.200000000000000001
  594. >>> t = bitstring.pack('<3f', -0.4, 1e34, 17.0)
  595. >>> t.hex
  596. '0xcdccccbedf84f67700008841'
  597. * 'bytes' token reintroduced.
  598. This token returns a bytes object (equivalent to a str in Python 2.6).
  599. >>> s = BitString('0x010203')
  600. >>> s.unpack('bytes:2, bytes:1')
  601. ['\x01\x02', '\x03']
  602. * 'uint' is now the default token type.
  603. So for example these are equivalent:
  604. a, b = s.readlist('uint:12, uint:12')
  605. a, b = s.readlist('12, 12')
  606. --------------------------------------------------------
  607. October 10th 2009: version 1.0.1 for Python 3.x released
  608. --------------------------------------------------------
  609. This is a straight port of version 1.0.0 to Python 3.
  610. For changes since the last Python 3 release read all the way down in this
  611. document to version 0.4.3.
  612. This version will also work for Python 2.6, but there's no advantage to using
  613. it over the 1.0.0 release. It won't work for anything before 2.6.
  614. -------------------------------------------------------
  615. October 9th 2009: version 1.0.0 for Python 2.x released
  616. -------------------------------------------------------
  617. Version 1 is here!
  618. This is the first release not to carry the 'beta' tag. It contains a couple of
  619. minor new features but is principally a release to fix the API. If you've been
  620. using an older version then you almost certainly will have to recode a bit. If
  621. you're not ready to do that then you may wish to delay updating.
  622. So the bad news is that there are lots of small changes to the API. The good
  623. news is that all the changes are pretty trivial, the new API is cleaner and
  624. more 'Pythonic', and that by making it version 1.0 I'm promising not to
  625. tweak it again for some time.
  626. ** API Changes **
  627. * New read / peek functions for returning multiple items.
  628. The functions read, readbits, readbytes, peek, peekbits and peekbytes now only
  629. ever return a single item, never a list.
  630. The new functions readlist, readbitlist, readbytelist, peeklist, peekbitlist
  631. and peekbytelist can be used to read multiple items and will always return a
  632. list.
  633. So a line like:
  634. >>> a, b = s.read('uint:12, hex:32')
  635. becomes
  636. >>> a, b = s.readlist('uint:12, hex:32')
  637. * Renaming / removing functions.
  638. Functions have been renamed as follows:
  639. seekbit -> seek
  640. tellbit -> tell
  641. reversebits -> reverse
  642. deletebits -> delete
  643. tostring -> tobytes
  644. and a couple have been removed altogether:
  645. deletebytes - use delete instead.
  646. empty - use 'not s' rather than 's.empty()'.
  647. * Renaming parameters.
  648. The parameters 'startbit' and 'endbit' have been renamed 'start' and 'end'.
  649. This affects the functions slice, find, findall, rfind, reverse, cut and split.
  650. The parameter 'bitpos' has been renamed to 'pos'. The affects the functions
  651. seek, tell, insert, overwrite and delete.
  652. * Mutating methods return None rather than self.
  653. This means that you can't chain functions together so
  654. >>> s.append('0x00').prepend('0xff')
  655. >>> t = s.reverse()
  656. Needs to be rewritten
  657. >>> s.append('0x00')
  658. >>> s.prepend('0xff)
  659. >>> s.reverse()
  660. >>> t = s
  661. Affects truncatestart, truncateend, insert, overwrite, delete, append,
  662. prepend, reverse and reversebytes.
  663. * Properties renamed.
  664. The 'data' property has been renamed to 'bytes'. Also if the BitString is not a
  665. whole number of bytes then a ValueError exception will be raised when using
  666. 'bytes' as a 'getter'.
  667. Properties 'len' and 'pos' have been added to replace 'length' and 'bitpos',
  668. although the longer names have not been removed so you can continue to use them
  669. if you prefer.
  670. * Other changes.
  671. The unpack function now always returns a list, never a single item.
  672. BitStrings are now 'unhashable', so calling hash on one or making a set will
  673. fail.
  674. The colon separating the token name from its length is now mandatory. So for
  675. example BitString('uint12=100') becomes BitString('uint:12=100').
  676. Removed support for the 'bytes' token in format strings. Instead of
  677. s.read('bytes:4') use s.read('bits:32').
  678. ** New features **
  679. * Added endswith and startswith functions.
  680. These do much as you'd expect; they return True or False depending on whether
  681. the BitString starts or ends with the parameter.
  682. >>> BitString('0xef342').startswith('0b11101')
  683. True
  684. ----------------------------------------------------------
  685. September 11th 2009: version 0.5.2 for Python 2.x released
  686. ----------------------------------------------------------
  687. Finally some tools for dealing with endianness!
  688. * New interpretations are now available for whole-byte BitStrings that treat
  689. them as big, little, or native-endian.
  690. >>> big = BitString(intbe=1, length=16) # or BitString('intbe:16=1') if you prefer.
  691. >>> little = BitString(intle=1, length=16)
  692. >>> print big.hex, little.hex
  693. 0x0001 0x0100
  694. >>> print big.intbe, little.intle
  695. 1 1
  696. * 'Struct'-like compact format codes
  697. To save some typing when using pack, unpack, read and peek, compact format
  698. codes based on those used in the struct and array modules have been added.
  699. These must start with a character indicating the endianness (>, < or @ for
  700. big, little and native-endian), followed by characters giving the format:
  701. b 1-byte signed int
  702. B 1-byte unsigned int
  703. h 2-byte signed int
  704. H 2-byte unsigned int
  705. l 4-byte signed int
  706. L 4-byte unsigned int
  707. q 8-byte signed int
  708. Q 8-byte unsigned int
  709. For example:
  710. >>> s = bitstring.pack('<4h', 0, 1, 2, 3)
  711. creates a BitString with four little-endian 2-byte integers. While
  712. >>> x, y, z = s.read('>hhl')
  713. reads them back as two big-endian two-byte integers and one four-byte big
  714. endian integer.
  715. Of course you can combine this new format with the old ones however you like:
  716. >>> s.unpack('<h, intle:24, uint:5, bin')
  717. [0, 131073, 0, '0b0000000001100000000']
  718. -------------------------------------------------------
  719. August 26th 2009: version 0.5.1 for Python 2.x released
  720. -------------------------------------------------------
  721. This update introduces pack and unpack functions for creating and dissembling
  722. BitStrings.
  723. * New pack() and unpack() functions.
  724. The module level pack function provides a flexible new method for creating
  725. BitStrings. Tokens for BitString 'literals' can be used in the same way as in
  726. the constructor.
  727. >>> from bitstring import BitString, pack
  728. >>> a = pack('0b11, 0xff, 0o77, int:5=-1, se=33')
  729. You can also leave placeholders in the format, which will be filled in by
  730. the values provided.
  731. >>> b = pack('uint:10, hex:4', 33, 'f')
  732. Finally you can use a dictionary or keywords.
  733. >>> c = pack('bin=a, hex=b, bin=a', a='010', b='ef')
  734. The unpack function is similar to the read function except that it always
  735. unpacks from the start of the BitString.
  736. >>> x, y = b.unpack('uint:10, hex')
  737. If a token is given without a length (as above) then it will expand to fill the
  738. remaining bits in the BitString. This also now works with read() and peek().
  739. * New tostring() and tofile() functions.
  740. The tostring() function just returns the data as a string, with up to seven
  741. zero bits appended to byte align. The tofile() function does the same except
  742. writes to a file object.
  743. >>> f = open('myfile', 'wb')
  744. >>> BitString('0x1234ff').tofile(f)
  745. * Other changes.
  746. The use of '=' is now mandatory in 'auto' initialisers. Tokens like 'uint12 100' will
  747. no longer work. Also the use of a ':' before the length is encouraged, but not yet
  748. mandated. So the previous example should be written as 'uint:12=100'.
  749. The 'auto' initialiser will now take a file object.
  750. >>> f = open('myfile', 'rb')
  751. >>> s = BitString(f)
  752. -----------------------------------------------------
  753. July 19th 2009: version 0.5.0 for Python 2.x released
  754. -----------------------------------------------------
  755. This update breaks backward compatibility in a couple of areas. The only one
  756. you probably need to be concerned about is the change to the default for
  757. bytealigned in find, replace, split, etc.
  758. See the user manual for more details on each of these items.
  759. * Expanded abilities of 'auto' initialiser.
  760. More types can be initialised through the 'auto' initialiser. For example
  761. instead of
  762. >>> a = BitString(uint=44, length=16)
  763. you can write
  764. >>> a = BitString('uint16=44')
  765. Also, different comma-separated tokens will be joined together, e.g.
  766. >>> b = BitString('0xff') + 'int8=-5'
  767. can be written
  768. >>> b = BitString('0xff, int8=-5')
  769. * New formatted read() and peek() functions.
  770. These takes a format string similar to that used in the auto initialiser.
  771. If only one token is provided then a single value is returned, otherwise a
  772. list of values is returned.
  773. >>> start_code, width, height = s.read('hex32, uint12, uint12')
  774. is equivalent to
  775. >>> start_code = s.readbits(32).hex
  776. >>> width = s.readbits(12).uint
  777. >>> height = s.readbits(12).uint
  778. The tokens are:
  779. int n : n bits as an unsigned integer.
  780. uint n : n bits as a signed integer.
  781. hex n : n bits as a hexadecimal string.
  782. oct n : n bits as an octal string.
  783. bin n : n bits as a binary string.
  784. ue : next bits as an unsigned exp-Golomb.
  785. se : next bits as a signed exp-Golomb.
  786. bits n : n bits as a new BitString.
  787. bytes n : n bytes as a new BitString.
  788. See the user manual for more details.
  789. * hex() and oct() functions removed.
  790. The special functions for hex() and oct() have been removed. Please use the
  791. hex and oct properties instead.
  792. >>> hex(s)
  793. becomes
  794. >>> s.hex
  795. * join made a member function.
  796. The join function must now be called on a BitString object, which will be
  797. used to join the list together. You may need to recode slightly:
  798. >>> s = bitstring.join('0x34', '0b1001', '0b1')
  799. becomes
  800. >>> s = BitString().join('0x34', '0b1001', '0b1')
  801. * More than one value allowed in readbits, readbytes, peekbits and peekbytes
  802. If you specify more than one bit or byte length then a list of BitStrings will
  803. be returned.
  804. >>> a, b, c = s.readbits(10, 5, 5)
  805. is equivalent to
  806. >>> a = readbits(10)
  807. >>> b = readbits(5)
  808. >>> c = readbits(5)
  809. * bytealigned defaults to False, and is at the end of the parameter list
  810. Functions that have a bytealigned paramater have changed so that it now
  811. defaults to False rather than True. Also its position in the parameter list
  812. has changed to be at the end. You may need to recode slightly (sorry!)
  813. * readue and readse functions have been removed
  814. Instead you should use the new read function with a 'ue' or 'se' token:
  815. >>> i = s.readue()
  816. becomes
  817. >>> i = s.read('ue')
  818. This is more flexible as you can read multiple items in one go, plus you can
  819. now also use the peek function with ue and se.
  820. * Minor bugs fixed.
  821. See the issue tracker for more details.
  822. -----------------------------------------------------
  823. June 15th 2009: version 0.4.3 for Python 2.x released
  824. -----------------------------------------------------
  825. This is a minor update. This release is the first to bundle the bitstring
  826. manual. This is a PDF and you can find it in the docs directory.
  827. Changes in version 0.4.3
  828. * New 'cut' function
  829. This function returns a generator for constant sized chunks of a BitString.
  830. >>> for byte in s.cut(8):
  831. ... do_something_with(byte)
  832. You can also specify a startbit and endbit, as well as a count, which limits
  833. the number of items generated:
  834. >>> first100TSPackets = list(s.cut(188*8, count=100))
  835. * 'slice' function now equivalent to __getitem__.
  836. This means that a step can also be given to the slice function so that the
  837. following are now the same thing, and it's just a personal preference which
  838. to use:
  839. >>> s1 = s[a:b:c]
  840. >>> s2 = s.slice(a, b, c)
  841. * findall gets a 'count' parameter.
  842. So now
  843. >>> list(a.findall(s, count=n))
  844. is equivalent to
  845. >>> list(a.findall(s))[:n]
  846. except that it won't need to generate the whole list and so is much more
  847. efficient.
  848. * Changes to 'split'.
  849. The split function now has a 'count' parameter rather than 'maxsplit'. This
  850. makes the interface closer to that for cut, replace and findall. The final item
  851. generated is now no longer the whole of the rest of the BitString.
  852. * A couple of minor bugs were fixed. See the issue tracker for details.
  853. ----------------------------------------------------
  854. May 25th 2009: version 0.4.2 for Python 2.x released
  855. ----------------------------------------------------
  856. This is a minor update, and almost doesn't break compatibility with version
  857. 0.4.0, but with the slight exception of findall() returning a generator,
  858. detailed below.
  859. Changes in version 0.4.2
  860. * Stepping in slices
  861. The use of the step parameter (also known as the stride) in slices has been
  862. added. Its use is a little non-standard as it effectively gives a multiplicative
  863. factor to apply to the start and stop parameters, rather than skipping over
  864. bits.
  865. For example this makes it much more convenient if you want to give slices in
  866. terms of bytes instead of bits. Instead of writing s[a*8:b*8] you can use
  867. s[a:b:8].
  868. When using a step the BitString is effectively truncated to a multiple of the
  869. step, so s[::8] is equal to s if s is an integer number of bytes, otherwise it
  870. is truncated by up to 7 bits. So the final seven complete 16-bit words could be
  871. written as s[-7::16]
  872. Negative slices are also allowed, and should do what you'd expect. So for
  873. example s[::-1] returns a bit-reversed copy of s (which is similar to
  874. s.reversebits(), which does the same operation on s in-place). As another
  875. example, to get the first 10 bytes in reverse byte order you could use
  876. s_bytereversed = s[0:10:-8].
  877. * Removed restrictions on offset
  878. You can now specify an offset of greater than 7 bits when creating a BitString,
  879. and the use of offset is also now permitted when using the filename initialiser.
  880. This is useful when you want to create a BitString from the middle of a file
  881. without having to read the file into memory.
  882. >>> f = BitString(filename='reallybigfile', offset=8000000, length=32)
  883. * Integers can be assigned to slices
  884. You can now assign an integer to a slice of a BitString. If the integer doesn't
  885. fit in the size of slice given then a ValueError exception is raised. So this
  886. is now allowed and works as expected:
  887. >>> s[8:16] = 106
  888. and is equivalent to
  889. >>> s[8:16] = BitString(uint=106, length=8)
  890. * Less exceptions raised
  891. Some changes have been made to slicing so that less exceptions are raised,
  892. bringing the interface closer to that for lists. So for example trying to delete
  893. past the end of the BitString will now just delete to the end, rather than
  894. raising a ValueError.
  895. * Initialisation from lists and tuples
  896. A new option for the auto initialiser is to pass it a list or tuple. The items
  897. in the list or tuple are evaluated as booleans and the bits in the BitString are
  898. set to 1 for True items and 0 for False items. This can be used anywhere the
  899. auto initialiser can currently be used. For example:
  900. >>> a = BitString([True, 7, False, 0, ()]) # 0b11000
  901. >>> b = a + ['Yes', ''] # Adds '0b10'
  902. >>> (True, True, False) in a
  903. True
  904. * Miscellany
  905. reversebits() now has optional startbit and endbit parameters.
  906. As an optimisation findall() will return a generator, rather than a list. If you
  907. still want the whole list then of course you can just call list() on the
  908. generator.
  909. Improved efficiency of rfind().
  910. A couple of minor bugs were fixed. See the issue tracker for details.
  911. -----------------------------------------------------
  912. April 23rd 2009: Python 3 only version 0.4.1 released
  913. -----------------------------------------------------
  914. This version is just a port of version 0.4.0 to Python 3. All the unit tests
  915. pass, but beyond that only limited ad hoc testing has been done and so it
  916. should be considered an experimental release. That said, the unit test
  917. coverage is very good - I'm just not sure if anyone even wants a Python 3
  918. version!
  919. ---------------------------------------
  920. April 11th 2009: version 0.4.0 released
  921. ---------------------------------------
  922. Changes in version 0.4.0
  923. * New functions
  924. Added rfind(), findall(), replace(). These do pretty much what you'd expect -
  925. see the docstrings or the wiki for more information.
  926. * More special functions
  927. Some missing functions were added: __repr__, __contains__, __rand__,
  928. __ror__, _rxor__ and __delitem__.
  929. * Miscellany
  930. A couple of small bugs were fixed (see the issue tracker).
  931. ----
  932. There are some small backward incompatibilities relative to version 0.3.2:
  933. * Combined find() and findbytealigned()
  934. findbytealigned() has been removed, and becomes part of find(). The default
  935. start position has changed on both find() and split() to be the start of the
  936. BitString. You may need to recode:
  937. >>> s1.find(bs)
  938. >>> s2.findbytealigned(bs)
  939. >>> s2.split(bs)
  940. becomes
  941. >>> s1.find(bs, bytealigned=False, startbit=s1.bitpos)
  942. >>> s2.find(bs, startbit=s1.bitpos) # bytealigned defaults to True
  943. >>> s2.split(bs, startbit=s2.bitpos)
  944. * Reading off end of BitString no longer raises exception.
  945. Previously a read or peek function that encountered the end of the BitString
  946. would raise a ValueError. It will now instead return the remainder of the
  947. BitString, which could be an empty BitString. This is closer to the file
  948. object interface.
  949. * Removed visibility of offset.
  950. The offset property was previously read-only, and has now been removed from
  951. public view altogether. As it is used internally for efficiency reasons you
  952. shouldn't really have needed to use it. If you do then use the _offset parameter
  953. instead (with caution).
  954. ---------------------------------------
  955. March 11th 2009: version 0.3.2 released
  956. ---------------------------------------
  957. Changes in version 0.3.2
  958. * Better performance
  959. A number of functions (especially find() and findbytealigned()) have been sped
  960. up considerably.
  961. * Bit-wise operations
  962. Added support for bit-wise AND (&), OR (|) and XOR (^). For example:
  963. >>> a = BitString('0b00111')
  964. >>> print a & '0b10101'
  965. 0b00101
  966. * Miscellany
  967. Added seekbit() and seekbyte() functions. These complement the 'advance' and
  968. 'retreat' functions, although you can still just use bitpos and bytepos
  969. properties directly.
  970. >>> a.seekbit(100) # Equivalent to a.bitpos = 100
  971. Allowed comparisons between BitString objects and strings. For example this
  972. will now work:
  973. >>> a = BitString('0b00001111')
  974. >>> a == '0x0f'
  975. True
  976. ------------------------------------------
  977. February 26th 2009: version 0.3.1 released
  978. ------------------------------------------
  979. Changes in version 0.3.1
  980. This version only adds features and fixes bugs relative to 0.3.0, and doesn't
  981. break backwards compatibility.
  982. * Octal interpretation and initialisation
  983. The oct property now joins bin and hex. Just prefix octal numbers with '0o'.
  984. >>> a = BitString('0o755')
  985. >>> print a.bin
  986. 0b111101101
  987. * Simpler copying
  988. Rather than using b = copy.copy(a) to create a copy of a BitString, now you
  989. can just use b = BitString(a).
  990. * More special methods
  991. Lots of new special methods added, for example bit-shifting via << and >>,
  992. equality testing via == and !=, bit inversion (~) and concatenation using *.
  993. Also __setitem__ is now supported so BitString objects can be modified using
  994. standard index notation.
  995. * Proper installer
  996. Finally got round to writing the distutils script. To install just
  997. python setup.py install.
  998. ------------------------------------------
  999. February 15th 2009: version 0.3.0 released
  1000. ------------------------------------------
  1001. Changes in version 0.3.0
  1002. * Simpler initialisation from binary and hexadecimal
  1003. The first argument in the BitString constructor is now called auto and will
  1004. attempt to interpret the type of a string. Prefix binary numbers with '0b'
  1005. and hexadecimals with '0x'.
  1006. >>> a = BitString('0b0') # single zero bit
  1007. >>> b = BitString('0xffff') # two bytes
  1008. Previously the first argument was data, so if you relied on this then you
  1009. will need to recode:
  1010. >>> a = BitString('\x00\x00\x01\xb3') # Don't do this any more!
  1011. becomes
  1012. >>> a = BitString(data='\x00\x00\x01\xb3')
  1013. or just
  1014. >>> a = BitString('0x000001b3')
  1015. This new notation can also be used in functions that take a BitString as an
  1016. argument. For example:
  1017. >>> a = BitString('0x0011') + '0xff'
  1018. >>> a.insert('0b001', 6)
  1019. >>> a.find('0b1111')
  1020. * BitString made more mutable
  1021. The functions append, deletebits, insert, overwrite, truncatestart and
  1022. truncateend now modify the BitString that they act upon. This allows for
  1023. cleaner and more efficient code, but you may need to rewrite slightly if you
  1024. depended upon the old behaviour:
  1025. >>> a = BitString(hex='0xffff')
  1026. >>> a = a.append(BitString(hex='0x00'))
  1027. >>> b = a.deletebits(10, 10)
  1028. becomes:
  1029. >>> a = BitString('0xffff')
  1030. >>> a.append('0x00')
  1031. >>> b = copy.copy(a)
  1032. >>> b.deletebits(10, 10)
  1033. Thanks to Frank Aune for suggestions in this and other areas.
  1034. * Changes to printing
  1035. The binary interpretation of a BitString is now prepended with '0b'. This is
  1036. in keeping with the Python 2.6 (and 3.0) bin function. The prefix is optional
  1037. when initialising using 'bin='.
  1038. Also, if you just print a BitString with no interpretation it will pick
  1039. something appropriate - hex if it is an integer number of bytes, otherwise
  1040. binary. If the BitString representation is very long it will be truncated
  1041. by '...' so it is only an approximate interpretation.
  1042. >>> a = BitString('0b0011111')
  1043. >>> print a
  1044. 0b0011111
  1045. >>> a += '0b0'
  1046. >>> print a
  1047. 0x3e
  1048. * More convenience functions
  1049. Some missing functions such as advancebit and deletebytes have been added. Also
  1050. a number of peek functions make an appearance as have prepend and reversebits.
  1051. See the Tutorial for more details.
  1052. -----------------------------------------
  1053. January 13th 2009: version 0.2.0 released
  1054. -----------------------------------------
  1055. Some fairly minor updates, not really deserving of a whole version point update.
  1056. ------------------------------------------
  1057. December 29th 2008: version 0.1.0 released
  1058. ------------------------------------------
  1059. First release!