pathlib.py:在 Windows 上实例化“PosixPath”

2023-11-22

我克隆了thefuck源代码来自它的存储库(这是真正的项目名称,是的,我知道......)。尝试通过以下命令安装它以进行开发:

pip install -r requirements.txt
python setup.py develop

效果很好。后来尝试通过以下方式运行测试:

py.test

出现以下错误:

================================== FAILURES ===================================
_______________________________ test_load_rule ________________________________

mocker = <pytest_mock.MockFixture object at 0x03275A10>

    def test_load_rule(mocker):
        match = object()
        get_new_command = object()
        load_source = mocker.patch(
            'thefuck.main.load_source',
            return_value=Mock(match=match,
                              get_new_command=get_new_command,
                              enabled_by_default=True,
                              priority=900))
        assert main.load_rule(Path('/rules/bash.py')) \
               == Rule('bash', match, get_new_command, priority=900)
>       load_source.assert_called_once_with('bash', '/rules/bash.py')

tests\test_main.py:20:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
C:\Python34\lib\unittest\mock.py:782: in assert_called_once_with
    return self.assert_called_with(*args, **kwargs)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

_mock_self = <MagicMock name='load_source' id='52977776'>
args = ('bash', '/rules/bash.py'), kwargs = {}
expected = (('bash', '/rules/bash.py'), {})
_error_message = <function NonCallableMock.assert_called_with.<locals>._error_me
ssage at 0x032A1030>
actual = call('bash', '\\rules\\bash.py'), cause = None

    def assert_called_with(_mock_self, *args, **kwargs):
        """assert that the mock was called with the specified arguments.

            Raises an AssertionError if the args and keyword args passed in are
            different to the last call to the mock."""
        self = _mock_self
        if self.call_args is None:
            expected = self._format_mock_call_signature(args, kwargs)
            raise AssertionError('Expected call: %s\nNot called' % (expected,))

        def _error_message():
            msg = self._format_mock_failure_message(args, kwargs)
            return msg
        expected = self._call_matcher((args, kwargs))
        actual = self._call_matcher(self.call_args)
        if expected != actual:
            cause = expected if isinstance(expected, Exception) else None
>           raise AssertionError(_error_message()) from cause
E           AssertionError: Expected call: load_source('bash', '/rules/bash.py')

E           Actual call: load_source('bash', '\\rules\\bash.py')

C:\Python34\lib\unittest\mock.py:771: AssertionError
__________________ TestGetRules.test_get[conf_rules0-rules0] __________________

self = <tests.test_main.TestGetRules object at 0x03286F50>
monkeypatch = <_pytest.monkeypatch.monkeypatch object at 0x03286CD0>
glob = <MagicMock name='glob' id='52981200'>, conf_rules = []
rules = ['bash', 'lisp', 'bash', 'lisp']

    @pytest.mark.parametrize('conf_rules, rules', [
        (conf.DEFAULT_RULES, ['bash', 'lisp', 'bash', 'lisp']),
        (types.RulesNamesList(['bash']), ['bash', 'bash'])])
    def test_get(self, monkeypatch, glob, conf_rules, rules):
>       glob.return_value = [PosixPath('bash.py'), PosixPath('lisp.py')]

tests\test_main.py:35:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

cls = <class 'pathlib.PosixPath'>, args = ('bash.py',), kwargs = {}
self = PosixPath('bash.py')

    def __new__(cls, *args, **kwargs):
        if cls is Path:
            cls = WindowsPath if os.name == 'nt' else PosixPath
        self = cls._from_parts(args, init=False)
        if not self._flavour.is_supported:
            raise NotImplementedError("cannot instantiate %r on your system"
>                                     % (cls.__name__,))
E           NotImplementedError: cannot instantiate 'PosixPath' on your system

C:\Python34\lib\site-packages\pathlib-1.0.1-py3.4.egg\pathlib.py:939: NotImpleme
ntedError
__________________ TestGetRules.test_get[conf_rules1-rules1] __________________

self = <tests.test_main.TestGetRules object at 0x032BAB50>
monkeypatch = <_pytest.monkeypatch.monkeypatch object at 0x03275A30>
glob = <MagicMock name='glob' id='53194576'>, conf_rules = ['bash']
rules = ['bash', 'bash']

    @pytest.mark.parametrize('conf_rules, rules', [
        (conf.DEFAULT_RULES, ['bash', 'lisp', 'bash', 'lisp']),
        (types.RulesNamesList(['bash']), ['bash', 'bash'])])
    def test_get(self, monkeypatch, glob, conf_rules, rules):
>       glob.return_value = [PosixPath('bash.py'), PosixPath('lisp.py')]

tests\test_main.py:35:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

cls = <class 'pathlib.PosixPath'>, args = ('bash.py',), kwargs = {}
self = PosixPath('bash.py')

    def __new__(cls, *args, **kwargs):
        if cls is Path:
            cls = WindowsPath if os.name == 'nt' else PosixPath
        self = cls._from_parts(args, init=False)
        if not self._flavour.is_supported:
            raise NotImplementedError("cannot instantiate %r on your system"
>                                     % (cls.__name__,))
E           NotImplementedError: cannot instantiate 'PosixPath' on your system

C:\Python34\lib\site-packages\pathlib-1.0.1-py3.4.egg\pathlib.py:939: NotImpleme
ntedError
______________________________ test_side_effect _______________________________

ssh_error = ('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n@
WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!  ...own_hosts', <function ssh_e
rror.<locals>.reset at 0x033F16F0>, <function ssh_error.<locals>.known_hosts at
0x033F1198>)

    def test_side_effect(ssh_error):
        errormsg, path, reset, known_hosts = ssh_error
        command = Command('ssh user@host', stderr=errormsg)
        side_effect(command, None)
        expected = ['123.234.567.890 asdjkasjdakjsd\n', '111.222.333.444 qwepoiw
qepoiss\n']
>       assert known_hosts(path) == expected
E       assert ['123.234.567...oiwqepoiss\n'] == ['123.234.567....oiwqepoiss\n']

E         At index 1 diff: '98.765.432.321 ejioweojwejrosj\n' != '111.222.333.44
4 qwepoiwqepoiss\n'
E         Left contains more items, first extra item: '111.222.333.444 qwepoiwqe
poiss\n'
E         Use -v to get the full diff

tests\rules\test_ssh_known_host.py:61: AssertionError
=============== 4 failed, 122 passed, 2 skipped in 0.56 seconds ===============

C:\code\thefuck>

我怀疑这个工具被编写为只能在 Linux 和类 UNIX 命令行上运行。

  • 出现这个错误的原因是什么?
  • 如果它是未针对 Windows 实现的 python 库的一部分,是否有任何快速修复方法?

我想开始从事开源项目,成为一名更好的开发人员。使用 Linux 来完成我的开发任务不是问题。如果我了解更有经验的开发人员对此的看法,这会对我有所帮助。


出现此错误的原因是,在 Windows 上,PosixPath没有实施。但是还有PurePosixPath,您可以在任何您想使用的地方使用PosixPath用于独立于平台的 POSIX 路径处理。或者,您可能实际上想要拥有依赖于平台的路径(即/在 Linux 和\在 Windows 上),在这种情况下你应该使用Path.

来自文档(继承图下方):

纯路径在某些特殊情况下很有用;例如:

  1. 如果您想在 Unix 机器上操作 Windows 路径(反之亦然)。运行时无法实例化 WindowsPath Unix,但您可以实例化 PureWindowsPath。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

pathlib.py:在 Windows 上实例化“PosixPath” 的相关文章

  • 如何使用 django (python) 和 s3 上传文件?

    我正在寻找一种将文件上传到 s3 的方法 我正在使用 django 我目前正在使用亚马逊的 python 库进行上传以及以下代码 View def submitpicture request fuser request session lo
  • 重新索引错误没有意义

    I have DataFrames大小在 100k 到 2m 之间 我正在处理这个问题的框架是如此之大 但请注意 我必须对其他框架执行相同的操作 gt gt gt len data 357451 现在这个文件是通过编译许多文件创建的 所以它
  • 在 Python 中绘制分类数据的三个维度

    我的数据包含三个我试图可视化的分类变量 城市 五个之一 职业 四种之一 血型 四种之一 到目前为止 我已经成功地以一种我认为易于使用的方式对数据进行了分组 import numpy as np pandas as pd Make data
  • 了解 asyncio 已经运行的永久循环和挂起的任务

    我在理解如何将新任务挂起到已经运行的事件循环中时遇到问题 这段代码 import asyncio import logging asyncio coroutine def blocking cmd while True logging in
  • 如何在Python中求和

    我想知道如何在 python 中表示总和而不需要像这样的循环here http docs scipy org doc scipy reference tutorial optimize html 我们有 def rosen x The Ro
  • 在新的浏览器进程中打开 URL

    我需要在新的浏览器进程中打开 URL 当浏览器进程退出时我需要收到通知 我当前使用的代码如下 Process browser new Process browser EnableRaisingEvents true browser Star
  • 如何使用 xlrd 将新列和行添加到 .xls 文件

    如何向 xlrd 中的工作表添加新列和 或行 我有一个使用 open workbook 读取的 xls 文件 我需要在第一张表中添加一个新列 bouncebacks 然后在该表中添加新行 但我在 xlrd 文档中找不到任何显示如何添加新行和
  • python-polars 通过分隔符将字符串列拆分为许多列

    在 pandas 中 以下代码会将 col1 中的字符串拆分为许多列 有没有办法在极地做到这一点 d col1 a b c d a b c d df pd DataFrame data d df a b c d df col1 str sp
  • python:是否有用于对输入流进行分块的库函数?

    我想对输入流进行分块以进行批处理 给定一个输入列表或生成器 x in 1 2 3 4 5 6 我想要一个能够返回该输入块的函数 说 如果chunk size 4 then x chunked 1 2 3 4 5 6 这是我一遍又一遍地做的事
  • Python,多线程,获取网页,下载网页

    我想在一个站点批量下载网页 我的 urls txt 文件中有 5000000 个 url 链接 大约有300M 如何让多线程链接这些网址并下载这些网页 或者如何批量下载这些网页 我的想法 with open urls txt r as f
  • PyCharm - 如何挂起所有线程

    我们使用 PyCharm 5 0 1 进行多线程调试 当它在断点处停止时 只有特定线程停止 而所有其他线程继续 这使得 冻结时刻 和检查参数值以及其他线程的当前状态变得困难 当其中一个线程在断点处停止时 是否可以挂起所有线程 这在最新的 P
  • Celery:每个工作人员的 task_acks_late 的不同设置/向 celery 添加自定义选项

    这个问题是后续问题django celery 禁用一个工作者的预取 有错误吗 https stackoverflow com questions 58290045 django celery disable prefetch for one
  • 在python中安装scipy模块时出错

    我正在尝试使用 pip 在 python 中安装 scipy 模块 它显示以下错误 Command c users sony appdata local programs python python35 32 python exe u c
  • 在 Windows 上部署 Meteor

    我觉得很奇怪的是 没有关于如何将自己的 Meteor Web 应用程序部署到自己的 Windows 服务器上的详细分步说明 或者也许我只是无法使用谷歌找到这样的解释 在很多页面上 甚至在关于 SO 的一些问题上 我发现人们只是简单地说 bu
  • Python 队列 get()/task_done() 问题

    我的消费者端队列 m queue get queue task done
  • 如何将 fields 参数传递到 Google Drive Python API 调用中

    I have results drive service files list body execute where body q query string maxResults 1 为了提高性能 我想限制返回的字段 如下所述 https
  • 有适用于 Windows 的 Bonjour SDK 吗?

    我想知道如何在 Windows 上实现 bonjour 以便我可以在 Windows 上设置服务器并使用 iphone 客户端 中的 bonjour 服务访问 Windows 服务器 中的文件 谁能告诉我我们是否有适用于 Windows 的
  • 如何保持 python 3 脚本 (Bot) 运行

    不是母语英语 抱歉 英语可能很蹩脚 我也是编程新手 您好 我正在尝试使用 QueryServer 连接到 TeamSpeak 服务器来创建机器人 经过几天的努力 它有效 只有 1 个问题 而我却被这个问题困扰了 如果您需要检查 这是我正在使
  • Flask 扩展未在 app.extensions 中注册

    我想访问在我的 Flask 应用程序上注册的一些扩展 我尝试使用app extensions 但我初始化的一些扩展不在字典中 from flask import current app current app extensions get
  • 在多个图表上绘制一条线

    I don t know how this thing is called or even how to describe it so the title may be a little bit misleading The first a

随机推荐