clarete / forbiddenfruit

Patch built-in python objects

Home Page:https://clarete.li/forbiddenfruit/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Modify/Change __add__ magic method for str type

ali-hallaji opened this issue · comments

I want to attach/concatenate a string with bytes of its to Python3 for solving TypeError exception.

I use this sample code:

from forbiddenfruit import curse

def __add__(self, a):
    """
        this is the test
    """
    if isinstance(a, bytes):
        self += a.decode('utf-8')
    else:
        self += a


curse(str, '__add__', __add__)

s = "sample string"a
print(s + "encode string".encode('utf-8'))

And returns the error below (exactly the normal state):

Traceback (most recent call last):
  File "test1.py", line 16, in <module>
    print(s + "encode string".encode('utf-8'))
TypeError: Can't convert 'bytes' object to str implicitly

How can I to fix this error?

Hi So that's a lil tricky, something like that was asked before but I still didn't change it to easily allow mocking these __*__ methods

#4

I guess #24 also closes this