(original) (raw)
On Thu, 7 Jan 2016 at 14:29 Blake Griffith <blake.a.griffith@gmail.com> wrote:
Hi!
I'm interested in adding the functionality to do something like:>>> b'a' ^ b'b'b'\\x03'Instead of the good ol' TypeError.
I think both bytes and bytearray should support all the bitwise operations.
I've never hacked on cpython before. I'm starting by just trying to add xor to bytearray. I have a ByteArray\_Xor function that I think should do this here https://github.com/cowlicks/cpython/commit/d6dddb11cdb33032b39dcb9dfdaa7b10d4377b5fBut I'm not sure how to hook this in to the rest of cypython. I tried adding it where bytearray\_as\_sequence is declared in this bytearrayobject.c file. But that gave me compiler warnings and broke things.So now that I have this ByteArray\_Xor function, how do I make it be bytearray.\_\_xor\_\_\_?
You need to set the PyNumberMethods struct with the appropriate function and then set that on the PyTypeObject. Look at https://hg.python.org/cpython/file/tip/Include/object.h#l237 and https://hg.python.org/cpython/file/tip/Objects/longobject.c#l5238 for an idea of what it takes.