Skip to content

Commit

Permalink
Merge pull request #1064 from gboeing/pytest
Browse files Browse the repository at this point in the history
workaround for pytest on windows printing to __stdout__
  • Loading branch information
gboeing authored Oct 10, 2023
2 parents bbfe488 + be32a50 commit dd7fe41
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions osmnx/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,19 +297,21 @@ def log(message, level=None, name=None, filename=None):

# if logging to console (terminal window) is turned on
if settings.log_console:
# prepend timestamp
# prepend timestamp then convert to ASCII for Windows command prompts
message = f"{ts()} {message}"

# convert to ascii so it works in windows command prompts
message = ud.normalize("NFKD", message).encode("ascii", errors="replace").decode()

# print explicitly to terminal in case jupyter notebook is the stdout
if getattr(sys.stdout, "_original_stdstream_copy", None) is not None:
# redirect captured pipe back to original
os.dup2(sys.stdout._original_stdstream_copy, sys.__stdout__.fileno())
sys.stdout._original_stdstream_copy = None
with redirect_stdout(sys.__stdout__):
print(message, file=sys.__stdout__, flush=True)
try:
# print explicitly to terminal in case Jupyter has captured stdout
if getattr(sys.stdout, "_original_stdstream_copy", None) is not None:
# redirect the Jupyter-captured pipe back to original
os.dup2(sys.stdout._original_stdstream_copy, sys.__stdout__.fileno())
sys.stdout._original_stdstream_copy = None
with redirect_stdout(sys.__stdout__):
print(message, file=sys.__stdout__, flush=True)
except OSError:
# handle pytest on Windows raising OSError from sys.__stdout__
print(message, flush=True) # noqa: T201


def _get_logger(level, name, filename):
Expand Down

0 comments on commit dd7fe41

Please sign in to comment.