Skip to content

Commit

Permalink
fix: Support stdin on Windows when possible
Browse files Browse the repository at this point in the history
This add support for stdin input from Windows from Python 3.12 onwards.
  • Loading branch information
kesara committed Oct 10, 2024
1 parent 4749e12 commit 289fea6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
3 changes: 3 additions & 0 deletions svgcheck/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,14 @@ def main():
if len(args) < 1:
# delete_on_close was introduced in Python 3.12
delete_on_close = {"delete_on_close": False} if sys.version_info >= (3, 12) else {}

with tempfile.NamedTemporaryFile(mode="w+b", **delete_on_close) as tmp_file:
data = sys.stdin.buffer.read()
tmp_file.write(data)
if sys.version_info >= (3, 12):
tmp_file.close()
elif sys.platform.lower().startswith("win"):
sys.exit("Stdin is only supported on Windows with Python 3.12 and above.")
else:
# Prior to Python 3.12 file is deleted as soon as it's closed
tmp_file.seek(0)
Expand Down
4 changes: 4 additions & 0 deletions svgcheck/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ def test_clear_cache(self):
None, None)
self.assertFalse(os.path.exists('Temp/cache/reference.RFC.1847.xml'))

@unittest.skipIf(
sys.platform.lower().startswith("win") and sys.version_info < (3, 12),
"Skip on Windows with Python version less than 3.12."
)
def test_stdin(self):
process = subprocess.Popen([sys.executable, test_program],
stdin=subprocess.PIPE,
Expand Down

0 comments on commit 289fea6

Please sign in to comment.