Can't pass a bytearray to SET
See original GitHub issueVersion: 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:
- Created 4 years ago
- Comments:5 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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
andint
.You can convert the
bytearray
tobytes
yourself with:redis.set('my-key', bytes(bytearray(10)))
Improved the error message to be more obvious.