Skip to content
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

Dictionary unpacking doesn't create a copy #3227

Closed
malcolmvr opened this issue Nov 8, 2019 · 3 comments
Closed

Dictionary unpacking doesn't create a copy #3227

malcolmvr opened this issue Nov 8, 2019 · 3 comments

Comments

@malcolmvr
Copy link

With Python, executing the following code:

o = {'a': 1, 'b': 2}
o2 = {**o}
o2['a'] = 3
print(o['a'], o2['a'])

results in (1, 3) being printed.

When compiled with Cython (3,3) is printed.

If the code is changed to:

o = {'a': 1, 'b': 2}
o2 = {**o, 'c': 4} # changed
o2['a'] = 3
print(o['a'], o2['a'])

Then the Cython compiled version behaves the same as the Python-interpreted version. Cython seems to be taking a shortcut and not creating a copy of the object.

@pengwk
Copy link
Contributor

pengwk commented Apr 2, 2021

@da-woods Are there any plans to fix it?

@da-woods
Copy link
Contributor

da-woods commented Apr 2, 2021

I don't think anyone's currently working on it. A PR would be welcome. It probably isn't too difficult to fix - I suspect it is just a case of removing an optimization that shouldn't really be there.

In the short-term you could probably work around it by explicitly calling dict(d) rather than {**d}

@pengwk
Copy link
Contributor

pengwk commented Apr 5, 2021

I have checked that there is no problem with the above code(OS: [macOS], Python version [3.6.8], Cython version [ 0.29.21]), and also initiated a pull request to fix the problem I mentioned before. @da-woods

@scoder scoder closed this as completed in c25c3cc Apr 13, 2021
@scoder scoder added this to the 3.0 milestone Apr 13, 2021
@scoder scoder modified the milestones: 3.0, 0.29.23 Apr 14, 2021
scoder pushed a commit that referenced this issue Apr 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants