Skip to content

Commit 435074a

Browse files
authored
Merge pull request #34 from geopozo/andrew/pipedreams
Andrew/pipedreams
2 parents 976b499 + 5414846 commit 435074a

2 files changed

Lines changed: 33 additions & 28 deletions

File tree

CHANGELOG.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
v2.0.1
2+
- Close read pipe when pipeLogger is closed
13
v2.0.0
24
- Remove all import-time side effects: Users must now
35
call `logistro.betterConfig()` manually.

logistro/_api.py

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,14 @@ def betterConfig(**kwargs: Any) -> None: # noqa: N802 camel-case like logging
128128
else:
129129
kwargs["level"] = cli_args.parsed.log.upper()
130130

131-
if "format" not in kwargs:
132-
kwargs["format"] = (
133-
human_formatter if cli_args.parsed.human else structured_formatter
134-
)
135-
136131
if "datefmt" not in kwargs:
137132
kwargs["datefmt"] = _date_string
138133

139134
logging.basicConfig(**kwargs)
135+
if "format" not in kwargs:
136+
logging.getLogger().handlers[0].setFormatter(
137+
human_formatter if cli_args.parsed.human else structured_formatter,
138+
)
140139

141140
betterConfig.__code__ = (lambda **_kwargs: None).__code__
142141
# function won't run after this
@@ -197,7 +196,8 @@ def getPipeLogger( # noqa: N802 camel-case like logging
197196
ifs: The character used as delimiter for pipe reads, defaults:"\n".
198197
199198
Returns:
200-
A tuple: (pipe, logger)
199+
A tuple: (pipe, logger). The pipe can be closed immediately after
200+
passing it to the writing process.
201201
202202
"""
203203
ifs = ifs.encode("utf-8") if isinstance(ifs, str) else ifs
@@ -214,29 +214,32 @@ def read_pipe(
214214
if bool(sys.version_info[:3] >= (3, 12) or platform.system() != "Windows"):
215215
os.set_blocking(r, True)
216216
raw_buffer = b""
217-
while True:
218-
try:
219-
last_size = len(raw_buffer)
220-
raw_buffer += os.read(
221-
r,
222-
1000,
223-
)
224-
if last_size == len(raw_buffer):
225-
# Just move us to exception mode
226-
raise Exception("") # noqa: TRY002,TRY301
227-
# Catch any exception, its all weird OS stuff, the game is up
228-
except Exception: # noqa: BLE001
217+
try:
218+
while True:
219+
try:
220+
last_size = len(raw_buffer)
221+
raw_buffer += os.read(
222+
r,
223+
1000,
224+
)
225+
if last_size == len(raw_buffer):
226+
# Just move us to exception mode
227+
raise Exception("") # noqa: TRY002,TRY301
228+
# Catch any exception, its all weird OS stuff, the game is up
229+
except Exception: # noqa: BLE001
230+
while raw_buffer:
231+
line, _, raw_buffer = raw_buffer.partition(ifs)
232+
if line:
233+
logger.log(default_level, line.decode())
234+
return
229235
while raw_buffer:
230-
line, _, raw_buffer = raw_buffer.partition(ifs)
231-
if line:
232-
logger.log(default_level, line.decode())
233-
return
234-
while raw_buffer:
235-
line, m, raw_buffer = raw_buffer.partition(ifs)
236-
if not m:
237-
raw_buffer = line
238-
break
239-
logger.log(default_level, line.decode())
236+
line, m, raw_buffer = raw_buffer.partition(ifs)
237+
if not m:
238+
raw_buffer = line
239+
break
240+
logger.log(default_level, line.decode())
241+
finally:
242+
os.close(r)
240243

241244
pipe_reader = Thread(
242245
target=read_pipe,

0 commit comments

Comments
 (0)