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

Deepcopying property objects results in unexpected TypeError #82474

Closed
GudniNatan mannequin opened this issue Sep 27, 2019 · 9 comments
Closed

Deepcopying property objects results in unexpected TypeError #82474

GudniNatan mannequin opened this issue Sep 27, 2019 · 9 comments
Labels
3.7 (EOL) end of life 3.8 (EOL) end of life 3.9 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@GudniNatan
Copy link
Mannequin

GudniNatan mannequin commented Sep 27, 2019

BPO 38293
Nosy @serhiy-storchaka, @eamanu, @miss-islington, @GudniNatan
PRs
  • bpo-38293: Allow shallow and deep copying of property objects #16438
  • [3.7] bpo-38293: Allow shallow and deep copying of property objects (GH-16438) #17968
  • [3.8] bpo-38293: Allow shallow and deep copying of property objects (GH-16438) #17969
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2020-01-12.17:43:50.420>
    created_at = <Date 2019-09-27.16:16:43.481>
    labels = ['3.7', '3.8', 'type-bug', 'library', '3.9']
    title = 'Deepcopying property objects results in unexpected TypeError'
    updated_at = <Date 2020-01-12.18:04:21.916>
    user = 'https://github.com/GudniNatan'

    bugs.python.org fields:

    activity = <Date 2020-01-12.18:04:21.916>
    actor = 'miss-islington'
    assignee = 'none'
    closed = True
    closed_date = <Date 2020-01-12.17:43:50.420>
    closer = 'cheryl.sabella'
    components = ['Library (Lib)']
    creation = <Date 2019-09-27.16:16:43.481>
    creator = 'GudniNatan'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 38293
    keywords = ['patch']
    message_count = 9.0
    messages = ['353375', '353376', '353377', '353383', '353388', '353428', '359857', '359860', '359861']
    nosy_count = 4.0
    nosy_names = ['serhiy.storchaka', 'eamanu', 'miss-islington', 'GudniNatan']
    pr_nums = ['16438', '17968', '17969']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue38293'
    versions = ['Python 3.7', 'Python 3.8', 'Python 3.9']

    @GudniNatan
    Copy link
    Mannequin Author

    GudniNatan mannequin commented Sep 27, 2019

    Currently, attempting to deepcopy a property object will result in an unexpected TypeError:

    >>> import copy
    >>> obj = property()
    >>> new_obj = copy.deepcopy(obj)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.1264.0_x64__qbz5n2kfra8p0\lib\copy.py", line 169, in deepcopy
        rv = reductor(4)
    TypeError: can't pickle property objects

    What I believe is happening here is that since property objects are not treated by the copy module as atomic, they are passed off to be pickled and so our error is raised.
    This can be fixed in a similar manner to how it works for type objects, function objects and more.
    Adding a single line of code to Lib/copy.py after line 208:

    d[property] = _deepcopy_atomic
    

    Means that property objects will be treated as atomic, and therefore returned as-is.

    @GudniNatan GudniNatan mannequin added 3.7 (EOL) end of life stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Sep 27, 2019
    @GudniNatan
    Copy link
    Mannequin Author

    GudniNatan mannequin commented Sep 27, 2019

    A small change:

    The fix should go to Lib/copy.py:198, not line 208.

    @serhiy-storchaka
    Copy link
    Member

    But the property object is not atomic. It's attribute __doc__ is writeable.

    @GudniNatan
    Copy link
    Mannequin Author

    GudniNatan mannequin commented Sep 27, 2019

    Function objects are considered "atomic" here and I believe you can also write to their __doc__ (among other attributes).

    @GudniNatan
    Copy link
    Mannequin Author

    GudniNatan mannequin commented Sep 27, 2019

    This bug appears to also affect shallow copies and can be reproduced with the following code:

    >>> import copy
    >>> obj = property()
    >>> copy.copy(obj)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.1264.0_x64__qbz5n2kfra8p0\lib\copy.py", line 96, in copy
        rv = reductor(4)
    TypeError: can't pickle property objects

    @eamanu
    Copy link
    Mannequin

    eamanu mannequin commented Sep 27, 2019

    I can confirm this behavior also on python 3.6 3.8 3.9

    @eamanu eamanu mannequin added 3.8 (EOL) end of life 3.9 only security fixes labels Sep 27, 2019
    @miss-islington
    Copy link
    Contributor

    New changeset 9f3fc6c by Miss Islington (bot) (Guðni Natan Gunnarsson) in branch 'master':
    bpo-38293: Allow shallow and deep copying of property objects (GH-16438)
    9f3fc6c

    @miss-islington
    Copy link
    Contributor

    New changeset 4be9726 by Miss Islington (bot) in branch '3.7':
    bpo-38293: Allow shallow and deep copying of property objects (GH-16438)
    4be9726

    @miss-islington
    Copy link
    Contributor

    New changeset 3043ec7 by Miss Islington (bot) in branch '3.8':
    bpo-38293: Allow shallow and deep copying of property objects (GH-16438)
    3043ec7

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life 3.8 (EOL) end of life 3.9 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants