2015-08-21 56 views
0

改變大小有它發送崇高插件RuntimeError:字典中迭代

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,崇高的文本3

+0

不是錯誤,但是'requests'版本使用的是支持張貼'json'直接使用'請求.post(HackerRankConfig.compile_tests_url,json = postdata,headers = user_headers)'(你不必設置'Co在這種情況下是ntent-Type頭)。 –

+0

這是在最新版本的'requests'中引入的一個錯誤。 –

回答

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} 
+0

https://github.com/dineshkumar-cse/SublimePluginHackerRank是repo,它可以在相同的python版本下正常工作,但後來在本地我重構了它,並且失敗了。我錯過了什麼? – Dineshkumar