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

(mostly) Fix configobj copying for py3.6+ #189

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/configobj/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ def __setstate__(self, state):
self.__dict__.update(state[1])

def __reduce__(self):
state = (dict(self), self.__dict__)
state = (self.copy(), self.__dict__)
return (__newobj__, (self.__class__,), state)


Expand Down Expand Up @@ -462,6 +462,21 @@ def __init__(self, parent, depth, main, indict=None, name=None):
for entry, value in indict.items():
self[entry] = value

def copy(self):
"""
This will return a dictionary representation without
interpolating strings.
"""
# In py36, dict.copy() was changed
# so that it used the subclass's __getitem__,
# which causes interpolation to occur.
# See github issue #188.
orig_interp = self.main.interpolation
self.main.interpolation = False
try:
return dict.copy(self)
finally:
self.main.interpolation = orig_interp

def _initialise(self):
# the sequence of scalar values in this Section
Expand Down
88 changes: 88 additions & 0 deletions src/tests/test_configobj.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
# pylint: disable=wildcard-import, missing-docstring, no-self-use, bad-continuation
# pylint: disable=invalid-name, redefined-outer-name, too-few-public-methods
from __future__ import unicode_literals
import copy
import os
import sys
import re
import warnings

Expand Down Expand Up @@ -176,6 +178,92 @@ def test_interoplation_repr():
repr(c)


class TestCopies(object):
@pytest.fixture
def simple_conf(self):
return ConfigObj(['foo = bar', 'baz = $foo'], interpolation='Template')

@pytest.fixture
def nested_conf(self):
return ConfigObj(['[a]', 'foo = bar', 'baz = $foo'],
interpolation='Template')

def test_simple_copy_method(self, simple_conf):
conf_dict = simple_conf.copy()
assert conf_dict == {'foo': 'bar', 'baz': '$foo'}

def test_nested_copy_method(self, nested_conf):
conf_dict = nested_conf.copy()
assert conf_dict == {'a': {'foo': 'bar', 'baz': '$foo'}}

@pytest.mark.xfail(sys.version_info >= (3, 6, 7),
reason="dict() now uses self.items()"
" instead of dict.items()",
strict=True)
def test_simple_dict(self, simple_conf):
conf_dict = dict(simple_conf)
assert conf_dict == {'foo': 'bar', 'baz': '$foo'}

# Not exactly sure why this one does not fail in the same
# case as test_simple_dict, but at least this is a step in
# the right direction.
def test_nested_dict(self, nested_conf):
# NOTE: this is a subtle behavior change for v5.1.0.
# previously, it would interpolate 'baz'.
conf_dict = dict(nested_conf)
assert conf_dict == {'a': {'foo': 'bar', 'baz': '$foo'}}

def test_simple_copy(self, simple_conf):
conf_new = copy.copy(simple_conf)
assert conf_new is not simple_conf
assert isinstance(conf_new, ConfigObj)
assert conf_new.copy() == simple_conf.copy()
conf_new['foo'] = 'abc'
# Ensure the original configobj remains unchanged
assert simple_conf['foo'] == 'bar'
assert simple_conf['baz'] == 'bar'
assert conf_new['baz'] == 'abc'
assert conf_new.copy() == {'foo': 'abc', 'baz': '$foo'}

def test_nested_copy(self, nested_conf):
conf_new = copy.copy(nested_conf)
assert conf_new is not nested_conf
assert isinstance(conf_new, ConfigObj)
assert conf_new['a'] is nested_conf['a']
assert conf_new.copy() == nested_conf.copy()
conf_new['a']['foo'] = 'abc'
# Ensure the original configobj was changed
assert nested_conf['a']['foo'] == 'abc'
assert nested_conf['a']['baz'] == 'abc'
assert conf_new['a']['baz'] == 'abc'
assert conf_new.copy() == {'a': {'foo': 'abc', 'baz': '$foo'}}

def test_simple_deepcopy(self, simple_conf):
conf_new = copy.deepcopy(simple_conf)
assert conf_new is not simple_conf
assert isinstance(conf_new, ConfigObj)
assert conf_new.copy() == simple_conf.copy()
conf_new['foo'] = 'abc'
# Ensure the original configobj remains unchanged
assert simple_conf['foo'] == 'bar'
assert simple_conf['baz'] == 'bar'
assert conf_new['baz'] == 'abc'
assert conf_new.copy() == {'foo': 'abc', 'baz': '$foo'}

def test_nested_copy(self, nested_conf):
conf_new = copy.deepcopy(nested_conf)
assert conf_new is not nested_conf
assert isinstance(conf_new, ConfigObj)
assert conf_new['a'] is not nested_conf['a']
assert conf_new.copy() == nested_conf.copy()
conf_new['a']['foo'] = 'abc'
# Ensure the original configobj remains unchanged
assert nested_conf['a']['foo'] == 'bar'
assert nested_conf['a']['baz'] == 'bar'
assert conf_new['a']['baz'] == 'abc'
assert conf_new.copy() == {'a': {'foo': 'abc', 'baz': '$foo'}}


class TestEncoding(object):
@pytest.fixture
def ant_cfg(self):
Expand Down