TypeError: cannot deepcopy this pattern object
Trying to understand this error in my "Variable" class.
I was hoping to store a sre.SRE_Pattern in my "Variable" class. I just started copying the Variable class and noticed that it was causing all my Variable class instances to change. I now understand that I need to deepcopy this class, but now I run into "TypeError: cannot deepcopy this pattern object". Sure, I can store the pattern as a text string but the rest of my code already expects a compiled pattern! What would be the best way to copy my Variable class with a pattern object?
import re from copy import deepcopy class VariableWithRE(object): "general variable class" def __init__(self,name,regexTarget,type): self.name = name self.regexTarget = re.compile(regexTarget, re.U|re.M) self.type = type class VariableWithoutRE(object): "general variable class" def __init__(self,name,regexTarget,type): self.name = name self.regexTarget = regexTarget self.type = type if __name__ == "__main__": myVariable = VariableWithoutRE("myName","myRegexSearch","myType") myVariableCopy = deepcopy(myVariable) myVariable = VariableWithRE("myName","myRegexSearch","myType") myVariableCopy = deepcopy(myVariable)
deepcopy doesn't know anything about your classes and doesn't know how to copy them.
You can tell
deepcopy how to copy your objects by implementing a
class VariableWithoutRE(object): # ... def __deepcopy__(self): return VariableWithoutRE(self.name, self.regexTarget, self.type)
"TypeError: cannot deepcopy this pattern object" when there is a , "TypeError: cannot deepcopy this pattern object" when there is a Widget with a compiled regular expression attribute #791. Closed. Airflow Python TypeError: cannot deepcopy this pattern object I found the issue is actually with one of the task passed complex object, TypeError: 'frozenset
This seems to be fixed in Python version 3.7+:
Compiled regular expression and match objects can now be copied using copy.copy() and copy.deepcopy(). (Contributed by Serhiy Storchaka in bpo-10076.)
import re,copy class C(): def __init__(self): self.regex=re.compile('\d+') myobj = C() foo = copy.deepcopy(myobj) foo.regex == myobj.regex # True
TypeError: cannot deepcopy this pattern object · Issue #8897 , TypeError: cannot deepcopy this pattern object #8897. Open. Bezier89 opened this issue on Jul 10, 2019 · 0 comments. Open I have a yas3fs mount as my data directory on a Owncloud installation, on EC2 t1.micro instances. Every now and then, I get this error, when I try to log in on the Owncloud running in this intances: 2014-06-02 18:38:00,043 DEBUG write '/
This can be worked around by patching
copy module in pre-3.7 python:
import copy import re copy._deepcopy_dispatch[type(re.compile(''))] = lambda r, _: r o = re.compile('foo') assert copy.deepcopy(o) == o
TypeError: cannot deepcopy this pattern object, About Scrapy: Running "scrapy crawl" occurs "TypeError: cannot deepcopy this pattern object". tags: scrapy deepcopy python settings int-ua changed the title "TypeError: cannot deepcopy this pattern object" after upgrading to 1.0.1 "TypeError: cannot deepcopy this pattern object" when there is a Widget with a compiled regular expression attribute Jun 19, 2018
The problem seems to be the compiled regex.
deepcopy cannot handle them.
A minimal example gives me the same error:
import re,copy class C(): def __init__(self): self.regex=re.compile('\d+') myobj = C() copy.deepcopy(myobj)
This throws the error:
TypeError: cannot deepcopy this pattern object. I'm in python3.5.
Developers, "TypeError: cannot deepcopy this pattern object" when there is a Widget with a compiled regular expression attribute. Why GitHub? Features →. Code review; Project management; Integrations; Actions; Packages; Security
IF your instances of this class (and its subclasses_) don't need deep-copy but are only causing problems because they are part of object graphs that do need deep-copy, then you could do this:
#might as well limit this hack to versions that need it... if sys.version_info <= (3, 7): def __deepcopy__(self, *args, **kwargs): """ cheat here because regex can't be deepcopied""" return self
Now, you need to be careful re. the assumption I am making. If you start altering any instances of this class, you are at risk of having side effects appear because the deepcopy did not in fact take place. It's only worthwhile if you need deepcopy elsewhere and you are pretty sure you don't care about this class and deepcopy.
Python cannot deepcopy this pattern object, py", line 173, in deepcopy y = copier(memo) TypeError: cannot deepcopy this pattern object I'll attach a patch against 2.7 to correct this. Using a regex causes TypeError: cannot deepcopy this pattern object. here is a sample code: from mongoengine.connection import register_connection, DEFAULT_CONNECTION_NAME from mongoengine import Document, StringField from mongoengine.qu
Issue 10076: Regex objects became uncopyable in 2.5, Ask questionsTypeError: cannot deepcopy this pattern object. Running into this error when testing one run configuration. I tried to run the same script (using API) It's not strictly necessary that re objects be copyable, but there's no reason to break it either. It's not strictly necessary that str or int be copyable either. This came up in code that had objects with a number of members, one of which was a regexp pattern object. deepcopy of this object broke in 2.5.
TypeError: cannot deepcopy this pattern object, line 173, in deepcopy y = copier(memo) TypeError: cannot deepcopy this pattern object Actual results: Unable to deepcopy compiled regular expression. Solutions / discussion. At its heart this is due to how we clone both configs and contexts in a few spots in Invoke's task execution stuff in order to prevent hard-to-debug unintentional state bleed between tasks, subroutines, etc. Cloning uses copy.deepcopy heavily since there's no other good way to copy arbitrary nested objects with unknown contents.
1075503 – Unable to deepcopy compiled regexp, Description. MoinMoin breaks with error: TypeError: cannot deepcopy this pattern object. Steps to reproduce. update from 1.3.5 to 1.5.0. How to fix (TypeError: cannot unpack non-iterable int object) ?? I'm working on a mesh grid assignment that has a cursor that moves when you enter the assigned number. I was able to get the cursor to move, the only problem I'm having is that I want it to print out the location of the updated coordinates as the cursor moves, (ex. if the cursor
- Given that compiler regexp is immutable, it's not necessary to deepcopy it. However I don't remember how tell
deepcopy()how to handle a particular type (but note, that you can add attributes to the build in type if needed).
- What line from your code throwing exception? I have copied it and compile then -> it works without any errors at my side. The only thing i found is that you try to use variable that rewrite python method 'type' and this is not very good style.
- The last line throws the error for me in Python 2.6.
- The problem is to copy
VariableWithRE, so your example doesn't really help, but it goes in the right direction.
- I tried this method in both of my classes, first I got "TypeError: __deepcopy__() takes exactly 1 argument (2 given)", then I looked at this question, and tried "def __deepcopy__(self,memo):". Syntax seems strange to me as someone still learning Python, but ok, memo, fine. This works in class VariableWithoutRE but still problem with class VariableWithRE! New error: "ValueError: Cannot process flags argument with a compiled pattern"
- @user789215 The problem is that you are invoking the
VariableWithoutREconstructor (init) with an already compiled regex (self.regexTarget), while the constructor expects a string.
- Does this create a new instance? From my testing it looks like it but the code suggests that its the same instance?
- @MarkusRessel: It does not -- as shown by
assert copy.deepcopy(o) is o. How did you test it?
- You are right, my testing was wrong. The
re.compile()method has a built in cache, so even using something like
re.compile(r.pattern)in the lambda will not create a new instance if the pattern (and flags) are the same. I wonder if this is how it is done in python 3.7 too.
- It's effectively the same (i.e.: copy is implemented as identity) but implemented in native code for re module: github.com/python/cpython/commit/…
- I'm having the same issue