Skip to content

TypeError when patching a dictionary with a Unicode string literal on Python 2 #458

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
micbou opened this issue May 2, 2019 · 6 comments

Comments

@micbou
Copy link

micbou commented May 2, 2019

Patching a dictionary with a Unicode string literal on Python 2 raises the exception TypeError: string indices must be integers. Running a test.py file containing the following code

from mock import patch

some_dict = {}

with patch.dict(u'test.some_dict'):
    pass

results in

Traceback (most recent call last):
  File "test.py", line 5, in <module>
    with patch.dict(u'test.some_dict'):
  File "C:\Python27\lib\site-packages\mock\mock.py", line 1766, in __enter__
    self._patch_dict()
  File "C:\Python27\lib\site-packages\mock\mock.py", line 1783, in _patch_dict
    original[key] = in_dict[key]
TypeError: string indices must be integers

This is a regression introduced by commit 5268eda.

@cjw296
Copy link
Collaborator

cjw296 commented May 2, 2019

@tirkarthi - any ideas on a fix?

@cjw296 cjw296 closed this as completed in b66b5b9 May 2, 2019
@micbou
Copy link
Author

micbou commented May 2, 2019

Thanks for the quick resolution. Will there be a new release containing the fix?

@cjw296
Copy link
Collaborator

cjw296 commented May 2, 2019

3.0.3 should be out shortly.

@micbou
Copy link
Author

micbou commented May 2, 2019

Nice. We won't even have to freeze the version of mock to 2.0.0 then.

@tirkarthi
Copy link
Contributor

@cjw296 Good catch! I think backports should be little more careful since Python 3 has no basestring and the upstream would be using str all along :( Upstream doesn't check for unicode values in most cases. Backports would also be a problem when 3.x only features like f-strings are used.

There is also a large PR to add asyncio support for mock : python/cpython#9296

@cjw296
Copy link
Collaborator

cjw296 commented May 2, 2019

We just have to hope for test coverage to catch things, or add test coverage when bug reports come in.

Backporting is only automated in so much as the path renaming and iteration over patches since the last sync. There's no code re-writing and I don't think there'd be much of a win with this.

Still, the new CI for the backport will exercise all our supported versions before a merge, and will fail if an incoming patch drops coverage below 100%.

That async patch will be fun to backport :-/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants