python / python/cpython

`test_file` failed on Windows with a longer repo path in `test_urllib2`

未關閉
#133,448 2 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

OS-windows tests type-bug
主要語言
Python
星號
77.2k
分支
36k
PR 合併指標
PR 指標待擷取

描述

On Windows, when the CPython repository is cloned to a slightly longer path, for example, C:\Users\xxxxx\Source\cpython — this test case will fail:

ERROR: test_file (test.test_urllib2.HandlerTests.test_file)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\xxxxx\Source\cpython\Lib\encodings\idna.py", line 219, in encode
    result.extend(ToASCII(label))
                  ~~~~~~~^^^^^^^
  File "C:\Users\xxxxx\Source\cpython\Lib\encodings\idna.py", line 115, in ToASCII
    raise UnicodeEncodeError("idna", label, 0, len(label), "label too long")
UnicodeEncodeError: 'idna' codec can't encode characters in position 0-64: label too long

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\xxxxx\Source\cpython\Lib\test\test_urllib2.py", line 872, in test_file
    self.assertRaises(urllib.error.URLError,
    ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
                      h.file_open, Request(url))
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\xxxxx\Source\cpython\Lib\unittest\case.py", line 813, in assertRaises
    return context.handle('assertRaises', args, kwargs)
           ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\xxxxx\Source\cpython\Lib\unittest\case.py", line 247, in handle
    callable_obj(*args, **kwargs)
    ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
  File "C:\Users\xxxxx\Source\cpython\Lib\urllib\request.py", line 1469, in open_local_file
    localfile = url2pathname(req.full_url, require_scheme=True)
  File "C:\Users\xxxxx\Source\cpython\Lib\urllib\request.py", line 1658, in url2pathname
    if not _is_local_authority(authority):
           ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^
  File "C:\Users\xxxxx\Source\cpython\Lib\urllib\request.py", line 1498, in _is_local_authority
    address = socket.gethostbyname(authority)
  File "C:\Users\xxxxx\Source\cpython\Lib\encodings\idna.py", line 222, in encode
    raise UnicodeEncodeError(
    ...<5 lines>...
    )
UnicodeEncodeError: 'idna' codec can't encode characters in position 8-72: label too long
encoding with 'idna' codec failed

See:
https://github.com/python/cpython/blob/c336f1c3126203fd6c38050df94e9fe8c0d7f2e2/Lib/test/test_urllib2.py#L857-L860

These two URLs will become:

  • file://127.0.0.1:80C:\Users\xxxxx\Source\cpython\build\test_python_worker_17208æ/@test_17208_tmpæ
  • file://somerandomhost.ontheinternet.comC:\Users\xxxxx\Source\cpython\build\test_python_worker_4676æ/@test_4676_tmpæ

Here, 127.0.0.1:80C:\Users\xxxxx\Source\cpython\build\test_python_worker_17208æ is treated as the domain and passed to socket.gethostbyname. However, the part 1:80C:\Users\xxxxx\Source\cpython\build\test_python_worker_17208æ is too long to be a valid domain label. As a result, the idna codec raises an exception, indicating the label is too long.

A patch is on the way.

Linked PRs
  • gh-133449

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

從 Lib/test/test_urllib2.py 中 test_file 附近開始,重點查看 857-860 行附近的 URL 設定以及 872 行附近的斷言。在 Windows 上使用較長的 checkout 路徑執行 test.test_urllib2.HandlerTests.test_file,並比較所得的 URL 和例外。當測試在該環境中不再失敗,同時保留其預期的 URLError 斷言時,即表示完成。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
testing-qa
Issue 類型
缺陷
難度
2/5
預估耗時
1-3 小時
活躍度
停滯
描述清晰度
描述清楚
新手友好度
15/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。