question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't pass a bytearray to SET

See original GitHub issue

Version: redis-py 3.3.8 and redis 5.0.5

Platform: Python 3.6.8 on Ubuntu 18.04

Description: Redis strings are should be binary safe, but redis.set(key, value) doesn’t accept a bytearray as a valid type. Passing a bytearray in as a value, results in the following exception: Invalid input of type: 'bytearray'. Convert to a byte, string or number first.

I understand that this might be a limitation of Python or perhaps I’ve misunderstood the Redis documentation. Thanks in advance.

Example code:

#!/usr/bin/python3
import redis

redis = redis.Redis(host="localhost", port=6379, db=0)

# This will result in an exception
redis.set("my-key", bytearray(10))

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
andymccurdycommented, Sep 12, 2019

All keys and values need to be serialized to bytes before being transmitted to the Redis server. redis-py only knows how to implicitly convert the following types: bytes, str, float and int.

You can convert the bytearray to bytes yourself with: redis.set('my-key', bytes(bytearray(10)))

0reactions
andymccurdycommented, Dec 29, 2019

Improved the error message to be more obvious.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't pass a bytearray to SET · Issue #1214 · redis/redis-py
Passing a bytearray in as a value, results in the following exception: Invalid input of type: 'bytearray'. Convert to a byte, string or...
Read more >
Java: How to pass byte[] by reference? - Stack Overflow
As for the OP's question, just pass in the reference to the byte[] array to the method. The net result would be similar...
Read more >
How to convert byte[] array to String in Java - Mkyong.com
In Java, we can use new String(bytes, StandardCharsets.UTF_8) to convert a byte[] to a String . // string to byte[] byte[] bytes =...
Read more >
Bytes and Bytearray tutorial in Python 3 - YouTube
Copy link. Info. Shopping. Tap to unmute. If playback doesn't begin shortly, try restarting your device. Your browser can't play this video.
Read more >
Python Bytes, Bytearray - w3resource
To construct byte arrays, use the bytearray() function. Contents. Bytes literals; bytes() and bytearray() functions; Create a bytes object in ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found