Sublime Plugin RuntimeError:字典在迭代期间改变了大小

时间:2015-08-21 06:59:50

标签: python-3.x dictionary sublime-text-plugin

有一个发送

的课程
postdata = {"some" : "value"}
user_headesr = Config.get_headers() //returning static class variable
requests.post(url, data=json.dumps(postdata), headers=user_headers)

但我得到了错误

Traceback (most recent call last):
  File "/Applications/Sublime Text.app/Contents/MacOS/sublime_plugin.py", line 535, in run_
    return self.run()
  File "~Library/Application Support/Sublime Text 3/Packages/MyPlugin/compile_code.py", line 22, in run
    compile_response = hr.send_code_to_server(code, HackerRankConfig.language)
  File "~Library/Application Support/Sublime Text 3/Packages/MyPlugin/plugin/hackerrank.py", line 15, in send_code_to_server
    resp = requests.post(HackerRankConfig.compile_tests_url, data=json.dumps(postdata), headers=user_headers)
  File "~Library/Application Support/Sublime Text 3/Packages/MyPlugin/requests/requests/api.py", line 109, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "~Library/Application Support/Sublime Text 3/Packages/MyPlugin/requests/requests/api.py", line 50, in request
    response = session.request(method=method, url=url, **kwargs)
  File "~Library/Application Support/Sublime Text 3/Packages/MyPlugin/requests/requests/sessions.py", line 455, in request
    prep = self.prepare_request(req)
  File "~Library/Application Support/Sublime Text 3/Packages/MyPlugin/requests/requests/sessions.py", line 382, in prepare_request
    headers=merge_setting(request.headers, self.headers, dict_class=CaseInsensitiveDict),
  File "~Library/Application Support/Sublime Text 3/Packages/MyPlugin/requests/requests/sessions.py", line 66, in merge_setting
    for (k, v) in merged_setting.items():
  File "./collections/abc.py", line 479, in __iter__
  File "~Library/Application Support/Sublime Text 3/Packages/MyPlugin/requests/requests/structures.py", line 60, in <genexpr>
    return (casedkey for casedkey, mappedvalue in self._store.values())
RuntimeError: dictionary changed size during iteration
  • 我的代码没有更改字典,get_headers()从package.sublime-settings文件中读取。

  • 我已尝试制作方法返回副本以确保不会导致问题,但问题仍然存在。

  • 我正在使用python 3.3.3,Sublime Text 3

1 个答案:

答案 0 :(得分:0)

您正在使用从GitHub获取的当前requests主分支。不要使用当前的开发版本很容易导致问题,因为它还没有准备就绪。您正在使用的修订包含仅在Python 3上出现的字典标题合并代码中的错误; this change没有考虑到dict.items()是Python 3上的实时字典视图:

>>> requests.get('http://httpbin.org/get', headers={'foo': None})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/mj/Development/venvs/stackoverflow-3.4/requests/requests/api.py", line 69, in get
    return request('get', url, params=params, **kwargs)
  File "/Users/mj/Development/venvs/stackoverflow-3.4/requests/requests/api.py", line 50, in request
    response = session.request(method=method, url=url, **kwargs)
  File "/Users/mj/Development/venvs/stackoverflow-3.4/requests/requests/sessions.py", line 455, in request
    prep = self.prepare_request(req)
  File "/Users/mj/Development/venvs/stackoverflow-3.4/requests/requests/sessions.py", line 382, in prepare_request
    headers=merge_setting(request.headers, self.headers, dict_class=CaseInsensitiveDict),
  File "/Users/mj/Development/venvs/stackoverflow-3.4/requests/requests/sessions.py", line 66, in merge_setting
    for (k, v) in merged_setting.items():
  File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python3.4/_collections_abc.py", line 503, in __iter__
    for key in self._mapping:
  File "/Users/mj/Development/venvs/stackoverflow-3.4/requests/requests/structures.py", line 60, in <genexpr>
    return (casedkey for casedkey, mappedvalue in self._store.values())
RuntimeError: dictionary changed size during iteration

我已提交pull request来正确解决问题(现已合并)。

最好的办法是坚持使用PyPI的最新版本(目前是version 2.7.0经过试用和测试。

如果您不能使用PyPI版本,也不能查看master分支的当前提示,那么解决方法之一是在使用前从字典中过滤掉None值标题:

user_headers = {k: v for k, v in Config.get_headers().items() if v is not None}