Deepcopy with session.query() does not work

Issue #2458 resolved
Former user created an issue

(original reporter: YaD) I have pushed to the limits with querying into unit tests. For now I am using "trick" with lambda function but I would be really grateful.

Version of sqlalchemy is 0.7.6. I have tried it for Python 2.7 and Python 3.2, both same.

(However, great project! :) )

>>> import sqlalchemy
>>> e = sqlalchemy.create_engine("sqlite:///:memory:")
>>> import sqlalchemy.orm
>>> s = sqlalchemy.orm.sessionmaker(bind=e)()
>>> q = s.query()
>>> from copy import deepcopy
>>> deepcopy(q)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/copy.py", line 190, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File "/usr/local/lib/python2.7/copy.py", line 334, in _reconstruct
    state = deepcopy(state, memo)
  File "/usr/local/lib/python2.7/copy.py", line 163, in deepcopy
    y = copier(x, memo)
  File "/usr/local/lib/python2.7/copy.py", line 257, in _deepcopy_dict
    y[memo)](deepcopy(key,) = deepcopy(value, memo)
  File "/usr/local/lib/python2.7/copy.py", line 190, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File "/usr/local/lib/python2.7/copy.py", line 334, in _reconstruct
    state = deepcopy(state, memo)
  File "/usr/local/lib/python2.7/copy.py", line 163, in deepcopy
    y = copier(x, memo)
  File "/usr/local/lib/python2.7/copy.py", line 257, in _deepcopy_dict
    y[memo)](deepcopy(key,) = deepcopy(value, memo)
  File "/usr/local/lib/python2.7/copy.py", line 190, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File "/usr/local/lib/python2.7/copy.py", line 334, in _reconstruct
    state = deepcopy(state, memo)
  File "/usr/local/lib/python2.7/copy.py", line 163, in deepcopy
    y = copier(x, memo)
  File "/usr/local/lib/python2.7/copy.py", line 257, in _deepcopy_dict
    y[memo)](deepcopy(key,) = deepcopy(value, memo)
  File "/usr/local/lib/python2.7/copy.py", line 190, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File "/usr/local/lib/python2.7/copy.py", line 334, in _reconstruct
    state = deepcopy(state, memo)
  File "/usr/local/lib/python2.7/copy.py", line 163, in deepcopy
    y = copier(x, memo)
  File "/usr/local/lib/python2.7/copy.py", line 257, in _deepcopy_dict
    y[memo)](deepcopy(key,) = deepcopy(value, memo)
  File "/usr/local/lib/python2.7/copy.py", line 190, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File "/usr/local/lib/python2.7/copy.py", line 334, in _reconstruct
    state = deepcopy(state, memo)
  File "/usr/local/lib/python2.7/copy.py", line 163, in deepcopy
    y = copier(x, memo)
  File "/usr/local/lib/python2.7/copy.py", line 257, in _deepcopy_dict
    y[memo)](deepcopy(key,) = deepcopy(value, memo)
  File "/usr/local/lib/python2.7/copy.py", line 190, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File "/usr/local/lib/python2.7/copy.py", line 334, in _reconstruct
    state = deepcopy(state, memo)
  File "/usr/local/lib/python2.7/copy.py", line 163, in deepcopy
    y = copier(x, memo)
  File "/usr/local/lib/python2.7/copy.py", line 257, in _deepcopy_dict
    y[memo)](deepcopy(key,) = deepcopy(value, memo)
  File "/usr/local/lib/python2.7/copy.py", line 189, in deepcopy
    "un(deep)copyable object of type %s" % cls)
copy.Error: un(deep)copyable object of type <type 'PyCapsule'>

Comments (3)

  1. Mike Bayer repo owner

    why do you need to "deepcopy" query() ? It doesn't really support that, it's true, and it would be exceedingly difficult to do so. The query refers to essentially your entire datamodel if you continue following references.

    query() has a "_clone()" method already for use in creating a (shallow) copy of Query that can be generated:

    newquery = query._clone()
    

    in general we point people to recipes using @_generative:

    from sqlalchemy.orm.query import Query, _generative
    
    class MyQuery(Query):
       @_generative
       def do_something_fun(self):
            self._some_attribute = True
    

    where above, self is a copy of the original Query.

    So there's no need for deepcopy() and I'd like to close this as "wontfix", are you OK with that ?

  2. Former user Account Deleted

    (original author: YaD) Yes, it is alright. I have tried copy too but it has not done a real copy, that's why I have tried deepcopy.

    I have not known about query._clone(), but it works fine for me. Thank you very much!

  3. Log in to comment