Skip to content

Commit

Permalink
Merge pull request #266 from dahlia/empty-image-repr
Browse files Browse the repository at this point in the history
Fix repr() for empty images
  • Loading branch information
dahlia committed Dec 1, 2015
2 parents 2f5244d + 2edc311 commit 53672a8
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
3 changes: 2 additions & 1 deletion docs/changes.rst
Expand Up @@ -7,7 +7,8 @@ Wand Changelog
Version 0.4.3
-------------

To be released.
- Fixed :func:`repr()` for empty :class:`~.wand.image.Image` objects.
[:issue:`265`]


Version 0.4.2
Expand Down
1 change: 1 addition & 0 deletions tests/image_test.py
Expand Up @@ -37,6 +37,7 @@
def test_empty_image():
with Image() as img:
assert img.size == (0, 0)
assert repr(img) == '<wand.image.Image: (empty)>'


def test_image_invalid_params():
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
@@ -1,5 +1,5 @@
[tox]
envlist = py26, py27, py32, py33, py34, py35, pypy
envlist = py26, py27, py32, py33, py34, py35, pypy, pypy3

[testenv]
deps =
Expand Down
24 changes: 13 additions & 11 deletions wand/image.py
Expand Up @@ -2362,13 +2362,19 @@ def transform_colorspace(self, colorspace_type):
if not r:
self.raise_exception()

def __repr__(self):
def __repr__(self, extra_format=' ({self.width}x{self.height})'):
cls = type(self)
typename = '{0}.{1}'.format(
cls.__module__,
getattr(cls, '__qualname__', cls.__name__)
)
if getattr(self, 'c_resource', None) is None:
return '<{0}.{1}: (closed)>'.format(cls.__module__, cls.__name__)
return '<{0}.{1}: {2} ({3}x{4})>'.format(
cls.__module__, cls.__name__,
self.signature[:7], self.width, self.height
return '<{0}: (closed)>'.format(typename)
sig = self.signature
if not sig:
return '<{0}: (empty)>'.format(typename)
return '<{0}: {1}{2}>'.format(
typename, sig[:7], extra_format.format(self=self)
)


Expand Down Expand Up @@ -3154,12 +3160,8 @@ def _repr_png_(self):
return cloned.make_blob()

def __repr__(self):
cls = type(self)
if getattr(self, 'c_resource', None) is None:
return '<{0}.{1}: (closed)>'.format(cls.__module__, cls.__name__)
return '<{0}.{1}: {2} {3!r} ({4}x{5})>'.format(
cls.__module__, cls.__name__,
self.signature[:7], self.format, self.width, self.height
return super(Image, self).__repr__(
extra_format=' {self.format!r} ({self.width}x{self.height})'
)


Expand Down

0 comments on commit 53672a8

Please sign in to comment.