cloudtools / cloudtools/troposphere

Deepcopying resources causes infinite recursion.

Open
#648 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
4.9k
Forks
1.4k
PR merge metrics
No merged PRs in 30d

Description

Currently, deepcopying any troposphere resource object triggers an infinite recursion:

```
>>> from troposphere.ec2 import SecurityGroup
>>> from copy import deepcopy
>>> group = SecurityGroup(
... 'SomeSecurityGroup',
... VpcId = '',
... GroupDescription = "A security group."
... )
>>> deepcopy(group)
Traceback (most recent call last):
File "", line 1, in
File "/.../lib/python3.5/copy.py", line 182, in deepcopy
y = _reconstruct(x, rv, 1, memo)
File "/.../lib/python3.5/copy.py", line 298, in _reconstruct
if hasattr(y, '__setstate__'):
File "/.../lib/python3.5/site-packages/troposphere/__init__.py", line 86, in __getattr__
return self.properties.__getitem__(name)
File "/.../lib/python3.5/site-packages/troposphere/__init__.py", line 86, in __getattr__
return self.properties.__getitem__(name)
<...>
File "/.../lib/python3.5/site-packages/troposphere/__init__.py", line 86, in __getattr__
return self.properties.__getitem__(name)
RecursionError: maximum recursion depth exceeded while calling a Python object
```

I was able to work around this by assigning a deepcopy method to the BaseAWSObject:
```
def __deepcopy__(self, memo):
memo[id(self)] = self
return self
BaseAWSObject.__deepcopy__ = __deepcopy__
```

However this just disables deepcopying of any resources (which for my needs was enough). If I could be given some hint as to what exactly is causing the recursion, I'd be willing to write up a patch and pull request. At present I'm not familiar enough with Troposphere's internals to provide a more concrete fix.

If you're wondering why I'm deepcopying resources in the first place, I'm using troposphere in a tool that is modifying an already-existing cloudformation template. At various points in modification of the template I need to create 'checkpoints'.

Contributor guide

Open the contributing guide

Research direction

Start in troposphere/__init__.py, particularly BaseAWSObject.__getattr__, and reproduce the reported SecurityGroup deepcopy case. Trace why copy.py's lookup of __setstate__ reaches __getattr__; done means deepcopying the resource completes without infinite recursion.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
cloud, infrastructure
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.