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

bools stored on 1.5.2 come out as strings that evaluate to True on 1.6.1 #265

Open
sabw8217 opened this issue Sep 29, 2020 · 1 comment
Open

Comments

@sabw8217
Copy link

I believe this commit results in bools written with 1.5.2 to strings in 1.6.1, which causes the value to actually switch if you stored False. This makes upgrading from 1.5.2 to 1.6.1 (and getting the pickle_protocol behavior, thank you for adding that!) difficult. Unfortunately tracking down all the places we might be setting False is also difficult (and tedious). Any ideas about how we can work around this? The only thing I've come up with is forking 1.6 to have the write path set an extra flag for anything with FLAG_TEXT, and then checking for that extra flag on read, monitoring what fraction of our reads come back without that flag, and hoping that eventually it goes to 0.

(pylibmctest) ✔ 12:16 ~ $ python
Python 3.7.3 | packaged by conda-forge | (default, Dec  6 2019, 08:36:57)
[Clang 9.0.0 (tags/RELEASE_900/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pylibmc
>>> cl = pylibmc.Client(['localhost:11211'])
>>> cl.set('foo', False)
True
>>> cl.get('foo')
False
>>>
(pylibmctest) ✔ 12:44 ~ $ pip freeze | grep pylibmc
pylibmc==1.5.2
(pylibmctest) ✔ 12:45 ~ $ pip install pylibmc==1.6.1
Collecting pylibmc==1.6.1
  Using cached https://files.pythonhosted.org/packages/a7/0c/f7a3af34b05c167a69ed1fc330b06b658dac4ab25b8632c52d1022dd5337/pylibmc-1.6.1.tar.gz
Building wheels for collected packages: pylibmc
  Building wheel for pylibmc (setup.py) ... done
  Created wheel for pylibmc: filename=pylibmc-1.6.1-cp37-cp37m-macosx_10_9_x86_64.whl size=32878 sha256=5bde92da27513e7024d29f73dd0a9837ebb4c7024d2b553eac62db835d504fdf
  Stored in directory: /Users/aaronwebber/Library/Caches/pip/wheels/71/5e/41/6796b369874b1b0345bccf31449e162eadcda85b104922f2de
Successfully built pylibmc
Installing collected packages: pylibmc
  Found existing installation: pylibmc 1.5.2
    Uninstalling pylibmc-1.5.2:
      Successfully uninstalled pylibmc-1.5.2
Successfully installed pylibmc-1.6.1
(pylibmctest) ✔ 12:45 ~ $ python
Python 3.7.3 | packaged by conda-forge | (default, Dec  6 2019, 08:36:57)
[Clang 9.0.0 (tags/RELEASE_900/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pylibmc
>>> cl = pylibmc.Client(['localhost:11211'])
>>> cl.get('foo')
'0'
>>> bool(cl.get('foo'))    # This is the real problem  :(
True
@ljluestc
Copy link

ljluestc commented Aug 5, 2023

One possible workaround you can try is to check for the string value '0' when reading the data and convert it back to False. This way, you can handle the previous bool values correctly even with the newer version of pylibmc.

Here's a possible implementation for your case:

import pylibmc

def convert_to_bool(value):
    if value == '0':
        return False
    return bool(value)

# Example usage
cl = pylibmc.Client(['localhost:11211'])
stored_value = cl.get('foo')
if stored_value is not None:
    foo_value = convert_to_bool(stored_value)
    print(foo_value)  # This should correctly print False

# Now you can use foo_value as a boolean, knowing it has been correctly converted.

By introducing the convert_to_bool function, you can handle the conversion from the string representation back to the correct boolean value. This way, you can maintain compatibility while upgrading to pylibmc 1.6.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants