Skip to content

TypeError with logging.StreamHandler.__repr__ and pytest's EncodedFile #2555

@blueyed

Description

@blueyed

Python's logging.StreamHandler has the following __repr__ (https://github.com/python/cpython/blob/0c3116309307ad2c7f8e2d2096612f4ab33cbb62/Lib/logging/__init__.py#L1000-L1005):

def __repr__(self):
    level = getLevelName(self.level)
    name = getattr(self.stream, 'name', '')
    if name:
        name += ' '
    return '<%s %s(%s)>' % (self.__class__.__name__, name, level)

Whereas pytest's EncodedFile.name is a integer (the fd), which results in a TypeError when it is used during tests:

> import logging
> print(logging.root.handlers)
*** TypeError: unsupported operand type(s) for +=: 'int' and 'str'

I think this should get fixed in Python itself, by using str() explicitly, but pytest should provide a proper name for the stream handler?!

The following patch works, but name might be expected to be the fd really in some places?!

diff --git i/_pytest/capture.py w/_pytest/capture.py
index 3661f269..aa7152a5 100644
--- i/_pytest/capture.py
+++ w/_pytest/capture.py
@@ -241,6 +241,7 @@ class EncodedFile(object):
     def __init__(self, buffer, encoding):
         self.buffer = buffer
         self.encoding = encoding
+        self.name = '_pytest.capture.EncodedFile {!r}'.format(buffer)
 
     def write(self, obj):
         if isinstance(obj, unicode):

Metadata

Metadata

Assignees

No one assigned

    Labels

    plugin: capturerelated to the capture builtin plugintype: bugproblem that needs to be addressed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions