Skip to content

Commit

Permalink
Handle lack of support for os.environb on some OSes (such as Windows)
Browse files Browse the repository at this point in the history
  • Loading branch information
ronf committed Sep 15, 2024
1 parent d6a65a1 commit 581d561
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
13 changes: 10 additions & 3 deletions asyncssh/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,16 @@ def lookup_env(patterns: EnvList) -> Iterator[Tuple[bytes, bytes]]:
if isinstance(pattern, str):
pattern = pattern.encode('utf-8')

for key_bytes, value_bytes in os.environb.items():
if fnmatch.fnmatch(key_bytes, pattern):
yield key_bytes, value_bytes
if os.supports_bytes_environ:
for key_bytes, value_bytes in os.environb.items():
if fnmatch.fnmatch(key_bytes, pattern):
yield key_bytes, value_bytes
else: # pragma: no cover
for key, value in os.environ.items():
key_bytes = key.encode('utf-8')
value_bytes = value.encode('utf-8')
if fnmatch.fnmatch(key_bytes, pattern):
yield key_bytes, value_bytes


def decode_env(env: Dict[bytes, bytes]) -> Iterator[Tuple[str, str]]:
Expand Down
3 changes: 3 additions & 0 deletions tests/test_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import asyncio
import os
import tempfile
import unittest
from signal import SIGINT

from unittest.mock import patch
Expand Down Expand Up @@ -1315,6 +1316,8 @@ async def test_send_env(self):
result = ''.join(session.recv_buf[None])
self.assertEqual(result, 'test\n')

@unittest.skipUnless(os.supports_bytes_environ,
'skip binary send env if not supported by OS')
@asynctest
async def test_send_env_binary(self):
"""Test sending local environment using a byte string"""
Expand Down

0 comments on commit 581d561

Please sign in to comment.