尝试为 Jira 问题对象调用add_attachment,但未使用字典时"RuntimeError: dictionary keys changed during iteration"?



我试图将 csv 文件添加到我的问题中作为测试,但我不断收到错误:

RuntimeError: dictionary keys changed during iteration

这是代码(我已经删除了服务器,用户名和密码的参数(:

from jira import JIRA
options = {"server": "serverlinkgoeshere"}
jira = JIRA(options, basic_auth=('username', 'password'))
issuesList = jira.search_issues(jql_str='', startAt=0, maxResults=100)
for issue in issuesList:
with open("./csv/Adobe.csv",'rb') as f:
jira.add_attachment(issue=issue, attachment=f)
f.close()

我很茫然,我没有更改代码中的任何字典键。以下是完整的错误消息:

Traceback (most recent call last):
File "C:/Users/USER/PycharmProjects/extractor/main/jiraCSVDupdate.py", line 8, in <module>
jira.add_attachment(issue=issue, attachment=f)
File "C:UsersUSERAppDataRoamingPythonPython38site-packagesjiraclient.py", line 126, in wrapper
result = func(*arg_list, **kwargs)
File "C:UsersUSERAppDataRoamingPythonPython38site-packagesjiraclient.py", line 787, in add_attachment
url, data=m, headers=CaseInsensitiveDict({'content-type': m.content_type, 'X-Atlassian-Token': 'nocheck'}), retry_data=file_stream)
File "C:UsersUSERAppDataRoamingPythonPython38site-packagesjirautils__init__.py", line 41, in __init__
for key, value in super(CaseInsensitiveDict, self).items():
RuntimeError: dictionary keys changed during iteration

引用: Jira add_attachment示例: https://jira.readthedocs.io/en/master/examples.html#attachments

add_attachment源代码: https://jira.readthedocs.io/en/master/_modules/jira/client.html#JIRA.add_attachment

问题的根源位于jira.utils.__init__py

for key, value in super(CaseInsensitiveDict, self).items():
if key != key.lower():
self[key.lower()] = value
self.pop(key, None)

这是一个编程错误:不应修改正在迭代的数据结构。因此,这需要一个补丁,并且必须是唯一接受的解决方案。

与此同时,我建议一个猴子补丁:

import jira.client
class CaseInsensitiveDict(dict):
def __init__(self, *args, **kw):
super(CaseInsensitiveDict, self).__init__(*args, **kw)
for key, value in self.copy().items():
if key != key.lower():
self[key.lower()] = value
self.pop(key, None)
jira.client.CaseInsensitiveDict = CaseInsensitiveDict

这里的诀窍是,你通过执行self.copy().items()来迭代字典结构的副本,而不是原始的 -self的。

作为参考,我的软件包版本:jira==2.0.0

应该从 jira lib 版本 3.1.1 (https://github.com/pycontribs/jira/commit/a83cc8f447fa4f9b6ce55beca8b4aee4a669c098( 修复

因此,假设您使用要求编辑您的要求.txt文件jira>=3.1.1并安装它们pip install -r requirements.txt

否则使用:pip install jira==3.1.1

这可以作为使用 Python 3.9 的修复程序。

Jira 站点包中以下行中的字典键client.py并不全是小写的。

headers=CaseInsensitiveDict({'content-type': None, 'X-Atlassian-Token': 'nocheck'}))

url, data=m, headers=CaseInsensitiveDict({'content-type': m.content_type, 'X-Atlassian-Token': 'nocheck'}), retry_data=file_stream)

这可以作为一种解决方案。

您可以将字典键更改为全部小写。然后,这将允许将附件添加到JIRA票证中。

headers = CaseInsensitiveDict({'content-type': None, 'x-atlassian-token': 'nocheck'}))

url, data = m, headers = CaseInsensitiveDict({'content-type': m.content_type, 'x-atlassian-token': 'nocheck'}), retry_data = file_stream)

最新更新