python - "the choice must be consistent for all consumers"是什么意思?

标签 python c python-3.x

我正在实现一个 Python C 扩展,我希望我的自定义对象支持 buffer protocol .缓冲协议(protocol)本质上允许容器对象以受控和定义明确的方式公开指向其内存的原始指针。消费者传递一些标志,指示它准备处理哪种内存,导出者返回一个描述内存的结构。

我对 PyBUF_WRITABLE 特别感兴趣标志:

PyBUF_WRITABLE

Controls the readonly field. If set, the exporter MUST provide a writable buffer or else report failure. Otherwise, the exporter MAY provide either a read-only or writable buffer, but the choice MUST be consistent for all consumers.

我的对象是 observable , 但这自然会与将可写指针分发到原始内存中发生冲突,因此如果我有任何活跃的观察者,我只能分发只读缓冲区,如果我有任何事件的可写缓冲区,我不能注册任何观察者。

我想默认分发只读缓冲区,并且只在被要求时提供可写缓冲区,但我不确定这是合法的。我可以看到这句话的两种可能的解释:

  1. 所有未通过标志的消费者都应该收到同样的东西。为这些消费者提供只读缓冲区并为确实通过标志的消费者提供可写缓冲区是合法的。
  2. 所有消费者都应该收到同样的东西无论他们是否通过标志。如果可以返回一个可写缓冲区,那么在每种情况下都必须返回一个可写缓冲区。该标志的唯一目的是在无法提供可写缓冲区时抛出错误。

哪种解释是正确的?

最佳答案

重要的部分是:

Otherwise, the exporter may provide either a read-only or writable buffer, but the choice must be consistent

选择..这个选择,因为第一种情况是别无选择...必须与您为每个消费者做出的一致,而不是事情。

如果设置了该字段,则必须提供一个可写缓冲区。别无选择,因为无论出于何种原因,技术上无法做到这一点都是别无选择。 如果未设置,它仍然是可写的,但对于所有未设置标志的人来说,它必须是可写的。

关于python - "the choice must be consistent for all consumers"是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32534534/

相关文章:

c - c编程中fflush(stdin)有什么用?

c - 想要根据 C 程序中的数据长度动态分配内存

python - 快速 Python IPv6 压缩

python - 加载表单后如何运行函数 Kivy

python - 更改方法定义

python - 无法对 librosa Melspectrogram 使用多线程

c - SIGABRT 在 fclose 上吗?

python-3.x - 连接 numpy 行元素

python - 执行函数 : import unknown in class definition

python - Instant search with Whoosh - 使用 Woosh 输出 JSON 结果索引和搜索 MySQL 表