Skip to content

Commit

Permalink
better error message when detecting nested uses of alive_progress
Browse files Browse the repository at this point in the history
  • Loading branch information
rsalmei committed May 26, 2023
1 parent 87fa20e commit 76e52ef
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions alive_progress/core/hook_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,12 @@ def write(stream, part):
buffer[:] = []

# better hook impl, which works even when nested, since __hash__ will be forwarded.
class Hook:
def __init__(self, stream):
self.__stream = stream

class Hook(BaseHook):
def write(self, part):
return write(self.__stream, part)
return write(self._stream, part)

def flush(self):
return flush(self.__stream)

def __getattr__(self, item):
return getattr(self.__stream, item)
return flush(self._stream)

def get_hook_for(handler):
if handler.stream: # supports FileHandlers with delay=true.
Expand Down Expand Up @@ -120,6 +114,9 @@ def uninstall():
# which causes a TypeError: unhashable type: 'types.SimpleNamespace'...
# or simply a logger **reuses** a handler...

if issubclass(sys.stdout.__class__, BaseHook):
raise UserWarning('Nested use of alive_progress is not yet supported.')

# internal data.
buffers = defaultdict(list)
get_header = gen_header(header_template, get_pos) if header_template else null_header
Expand All @@ -136,6 +133,14 @@ def uninstall():
return hook_manager


class BaseHook:
def __init__(self, stream):
self._stream = stream

def __getattr__(self, item):
return getattr(self._stream, item)


def passthrough_hook_manager(): # pragma: no cover
passthrough_hook_manager.flush_buffers = __noop
passthrough_hook_manager.install = __noop
Expand Down

0 comments on commit 76e52ef

Please sign in to comment.