فهرست منبع

Use UTF-8 for transfer receipts and Windows adapter output

codex 2 ماه پیش
والد
کامیت
2ec8e2c4db
4فایلهای تغییر یافته به همراه20 افزوده شده و 0 حذف شده
  1. 13 0
      tests/unit/test_remote_transfer.py
  2. 3 0
      tools/remote_transfer.py
  3. 2 0
      tools/windows/sync-from-remote.ps1
  4. 2 0
      tools/windows/sync-to-remote.ps1

+ 13 - 0
tests/unit/test_remote_transfer.py

@@ -132,3 +132,16 @@ def test_terminated_ssh_is_reported_as_transport_interruption(monkeypatch,exit_c
     with pytest.raises(transfer.TransferError) as error:
         transfer.SSH('/data/prod2u/remote_transfer.py',0).request({'operation':'describe'})
     assert error.value.code==255
+
+def test_cli_reports_unicode_paths_even_when_started_in_cp1252(tmp_path,monkeypatch):
+    import json
+    monkeypatch.setattr(transfer,'REMOTE_ROOT',tmp_path)
+    source=tmp_path/'源.bin';source.write_bytes(b'bytes')
+    destination=tmp_path/'目标.bin'
+    remote=LocalRemote()
+    monkeypatch.setattr(transfer,'SSH',lambda *args:remote)
+    output=io.BytesIO();stream=io.TextIOWrapper(output,encoding='cp1252')
+    monkeypatch.setattr(transfer.sys,'stdout',stream)
+    assert transfer.main(['--remote-worker','fixture','upload',str(source),str(destination)])==0
+    stream.flush()
+    assert json.loads(output.getvalue().decode('utf-8'))['destination']==str(destination)

+ 3 - 0
tools/remote_transfer.py

@@ -381,6 +381,9 @@ def receipt(direction, source, destination, manifest, transferred, reused):
             "verified_bytes_transferred": sum(manifest["chunks"][i]["size"] for i in transferred)}
 
 def main(argv=None):
+    for stream in (sys.stdout, sys.stderr):
+        if hasattr(stream, "reconfigure"):
+            stream.reconfigure(encoding="utf-8")
     parser = argparse.ArgumentParser(description=__doc__)
     parser.add_argument("--serve", help=argparse.SUPPRESS)
     parser.add_argument("--remote-worker", help="exact remote tools/remote_transfer.py path")

+ 2 - 0
tools/windows/sync-from-remote.ps1

@@ -8,6 +8,8 @@ param(
   [switch]$Replace
 )
 $ErrorActionPreference='Stop'
+[Console]::OutputEncoding = New-Object System.Text.UTF8Encoding($false)
+$OutputEncoding = [Console]::OutputEncoding
 $arguments=@('--remote-worker',$RemoteWorker,'--chunk-bytes',$ChunkBytes,'--retries',$Retries)
 if($Replace){$arguments+='--replace'}
 $arguments+=@('download',$RemoteSource,$Destination,'--expected-sha256',$ExpectedSha256)

+ 2 - 0
tools/windows/sync-to-remote.ps1

@@ -7,6 +7,8 @@ param(
   [switch]$Replace
 )
 $ErrorActionPreference='Stop'
+[Console]::OutputEncoding = New-Object System.Text.UTF8Encoding($false)
+$OutputEncoding = [Console]::OutputEncoding
 if(-not $RemotePartial.EndsWith('.partial')){throw 'REMOTE_PARTIAL_SUFFIX_REQUIRED'}
 $destination=$RemotePartial.Substring(0,$RemotePartial.Length-8)
 $arguments=@('--remote-worker',$RemoteWorker,'--chunk-bytes',$ChunkBytes,'--retries',$Retries)