Skip to content

Commit 77b3037

Browse files
committed
Enable custom mappings support
1 parent d379c5f commit 77b3037

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/future/types/newbytes.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,17 +177,21 @@ def __rmul__(self, other):
177177
def __mod__(self, vals):
178178
if isinstance(vals, newbytes):
179179
vals = _builtin_bytes.__str__(vals)
180-
elif isinstance(vals, dict):
181-
for k, v in vals.items():
182-
if isinstance(v, newbytes):
183-
vals[k] = _builtin_bytes.__str__(v)
180+
184181
elif isinstance(vals, tuple):
185182
newvals = []
186183
for v in vals:
187184
if isinstance(v, newbytes):
188185
v = _builtin_bytes.__str__(v)
189186
newvals.append(v)
190187
vals = tuple(newvals)
188+
189+
elif (hasattr(vals.__class__, '__getitem__') and
190+
hasattr(vals.__class__, 'iteritems')):
191+
for k, v in vals.iteritems():
192+
if isinstance(v, newbytes):
193+
vals[k] = _builtin_bytes.__str__(v)
194+
191195
return _builtin_bytes.__mod__(self, vals)
192196

193197
def join(self, iterable_of_bytes):

tests/test_future/test_bytes.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,20 @@ def test_maketrans(self):
552552
self.assertRaises(ValueError, bytes.maketrans, b'abc', b'xyzq')
553553
self.assertRaises(TypeError, bytes.maketrans, 'abc', 'def')
554554

555+
@unittest.skipUnless(utils.PY2, 'test requires Python 2')
556+
def test_mod_custom_dict(self):
557+
import UserDict
558+
559+
class MyDict(UserDict.UserDict):
560+
pass
561+
562+
d = MyDict()
563+
d['foo'] = bytes(b'bar')
564+
self.assertFalse(isinstance(d, dict))
565+
self.assertTrue(isinstance(d, UserDict.UserDict))
566+
567+
self.assertEqual(bytes(b'%(foo)s') % d, b'bar')
568+
555569
@unittest.skipUnless(utils.PY35 or utils.PY2,
556570
'test requires Python 2 or 3.5+')
557571
def test_mod_more(self):

0 commit comments

Comments
 (0)