scott-griffiths / bitstring

A Python module to help you manage your bits
https://bitstring.readthedocs.io/en/stable/index.html
MIT License
404 stars 68 forks source link

module flags not always working #246

Closed scott-griffiths closed 5 months ago

scott-griffiths commented 1 year ago

If you import using

from bitstring import *

then you get both lsb0 and bytealigned imported, but changing them doesn't have any effect.

It also doesn't work if you do

from bitstring import lsb0

for example.

They do work the more usual way:

import bitstring
bitstring.lsb0 = True
...

Probably should remove them from the __all__ definition at least. Not sure if the underlying problem is fixable as the whole module property thing is a bit hacky.

scott-griffiths commented 1 year ago

Yeah, this is a pain. Maybe there should have been an intermediate class:

class Options:
    __slots__ = ('bytealigned', 'lsb0')

    def __init__(self):
        self.bytealigned = False
        self.lsb0 = False

    def __repr__(self):
        return f"bytealigned: {self.bytealigned}\n  lsb0: {self.lsb0}"

options = Options()

This could be added with some extra plumbing to link it to the current options, and use this method in the documentation?

scott-griffiths commented 5 months ago

The bitstring.options object is now the preferred method - I'm not sure it was possible to fix the underlying problem with the other methods.