[issue39805] Copying functions doesn't actually copy them

2022-03-28 Thread STINNER Victor


STINNER Victor  added the comment:

See also bpo-47143 "Add functools.copy_class() which updates closures".

--
nosy: +vstinner

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue39805] Copying functions doesn't actually copy them

2022-02-13 Thread Numerlor


Numerlor  added the comment:

Having copy be an identity function for anything mutable seems very bug prone, 
even if it's functions. If not changed in the copy interface I would say that 
at least a warning would be helpful.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue39805] Copying functions doesn't actually copy them

2022-02-13 Thread Steven D'Aprano


Steven D'Aprano  added the comment:

If we can't change the default behaviour of copying functions, can we 
add a specialised copy_function() function, for when we really need to 
make a genuine copy?

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue39805] Copying functions doesn't actually copy them

2022-02-13 Thread Serhiy Storchaka


Serhiy Storchaka  added the comment:

It is by design. Functions and types are pickled by name. The copy module uses 
the pickling protocol and only use different methods for performance or for 
non-pickleable objects. Changing this will break existing code.

--
nosy: +serhiy.storchaka

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue39805] Copying functions doesn't actually copy them

2022-02-12 Thread Numerlor


Numerlor  added the comment:

Ran into this myself today; do you have any plans on implementing the proposed 
change?

--
nosy: +Numerlor

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue39805] Copying functions doesn't actually copy them

2020-02-29 Thread Steven D'Aprano


Steven D'Aprano  added the comment:

I think this is sufficient for a shallow copy.

import copy
import types

def copyfunction(func):
new = types.FunctionType(
func.__code__,
func.__globals__,
func.__name__,
func.__defaults__,
func.__closure__
)
vars(new).update(vars(func))
new.__annotations__.update(func.__annotations__)
if func.__kwdefaults__ is not None:
new.__kwdefaults__ = func.__kwdefaults__.copy()
return new

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue39805] Copying functions doesn't actually copy them

2020-02-29 Thread Steven D'Aprano


New submission from Steven D'Aprano :

Function objects are mutable, so I expected that a copy of a function should be 
an actual independent copy. But it isn't.

py> from copy import copy
py> a = lambda: 1
py> b = copy(a)
py> a is b
True

This burned me when I modified the copy and the original changed too:

py> a.attr = 27  # add extra data
py> b.attr = 42
py> a.attr
42


`deepcopy` doesn't copy the function either.

--
components: Library (Lib)
messages: 363039
nosy: steven.daprano
priority: normal
severity: normal
status: open
title: Copying functions doesn't actually copy them
type: behavior
versions: Python 3.9

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com