Why is python pickle failing to dump dictionaries in 3.7

落爺英雄遲暮 提交于 2020-12-30 03:12:53

问题


I recently switched to version 3.7.3 for python and I've been attempting to update the code to fit the new version. Before switching, pickle would have no problem dumping and loading the dictionaries I've sent it but now it keeps giving me a TypeError: can't pickle TaskStepMethWrapper objects error Searching for TaskStepMethWrapper shows that it may be related to asyncio, but this error did not show up when I was using python 3.6.

Heres my code

def load_guildlist(self):
    with open("guildlist.dadbot","rb") as sl:
        return pickle.loads(sl.read())

#edits the guildlist into an external file
def edit_guildlist(self,dictionary):
    with open("guildlist.dadbot","wb") as sl:
        #attempt to force the passing of a dictionary
        pickle.dump(dict(dictionary),sl)


#Registers the guild into the guildlist
async def on_guild_join(self,guild):
    #Setup an initial prefix for flexible changing later
    #Check to see if guildlist is empty or not(may only ever run once)
    try:
        self.guildlist = self.load_guildlist()
    except EOFError:
        self.guildlist = {}
        self.edit_guildlist(self.guildlist)
    #Stores a random prefix for the guild so it can be changed later
    #Should allow for guilds to have prefix preferences
    #Also sets guild preferences and statuses 
    if guild not in self.guildlist:
        #New guild
        self.guildlist[guild]={}
        self.guildlist[guild]["prefix"] = genPrefix()
        self.guildlist[guild]["voice_channel"] = "all"
        self.edit_guildlist(self.guildlist)

Heres the traceback:

Ignoring exception in on_guild_join
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/discord/client.py", line 251, in _run_event
    await coro(*args, **kwargs)
  File "main.py", line 68, in on_guild_join
    self.edit_guildlist(self.guildlist)
  File "main.py", line 48, in edit_guildlist
    pickle.dump(dict(dictionary),sl)
TypeError: can't pickle TaskStepMethWrapper objects
Ignoring exception in on_message
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/discord/client.py", line 251, in _run_event
    await coro(*args, **kwargs)
  File "main.py", line 80, in on_message
    self.guildlist = self.load_guildlist()
  File "main.py", line 42, in load_guildlist
    return pickle.loads(sl.read())
EOFError: Ran out of input

来源:https://stackoverflow.com/questions/56213159/why-is-python-pickle-failing-to-dump-dictionaries-in-3-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!