Singleton Classes Aren't round-trippable
还没有人认领这个 Issue。
评估
调研方向
使用 YAML.unsafe_load 重现 Singleton 示例,然后阅读 psych/visitors/to_ruby.rb 中 revive 和报告的 klass.allocate 调用附近的代码。将其行为与 Singleton 对 Marshal 的处理进行比较,并确定分配自定义应如何工作。完成标准是 Singleton 实例在不序列化实例变量的情况下完成往返,并且反序列化会重新使用现有实例。
由索引模型根据 Issue 内容生成。
描述
require "psych"
require "singleton"
class A
include Singleton
end
YAML.unsafe_load(A.instance.to_yaml
result:
/Users/cody/.gem/ruby/3.1.3/gems/psych-5.1.1/lib/psych/visitors/to_ruby.rb:408:in `revive': private method `allocate' called for A:Class (NoMethodError)
s = register(node, klass.allocate)
^^^^^^^^^
from /Users/cody/.gem/ruby/3.1.3/gems/psych-5.1.1/lib/psych/visitors/to_ruby.rb:215:in `visit_Psych_Nodes_Mapping'
from /Users/cody/.gem/ruby/3.1.3/gems/psych-5.1.1/lib/psych/visitors/visitor.rb:30:in `visit'
from /Users/cody/.gem/ruby/3.1.3/gems/psych-5.1.1/lib/psych/visitors/visitor.rb:6:in `accept'
from /Users/cody/.gem/ruby/3.1.3/gems/psych-5.1.1/lib/psych/visitors/to_ruby.rb:35:in `accept'
from /Users/cody/.gem/ruby/3.1.3/gems/psych-5.1.1/lib/psych/visitors/to_ruby.rb:320:in `visit_Psych_Nodes_Document'
from /Users/cody/.gem/ruby/3.1.3/gems/psych-5.1.1/lib/psych/visitors/visitor.rb:30:in `visit'
from /Users/cody/.gem/ruby/3.1.3/gems/psych-5.1.1/lib/psych/visitors/visitor.rb:6:in `accept'
from /Users/cody/.gem/ruby/3.1.3/gems/psych-5.1.1/lib/psych/visitors/to_ruby.rb:35:in `accept'
from /Users/cody/.gem/ruby/3.1.3/gems/psych-5.1.1/lib/psych/nodes/node.rb:50:in `to_ruby'
from /Users/cody/.gem/ruby/3.1.3/gems/psych-5.1.1/lib/psych.rb:274:in `unsafe_load'
I would expect a Singleton class to both a) not actually serialize any instance variables, and b) upon deserialization, just use the existing instance. Singleton does this when using Marshal, but Marshal is part of Ruby core. And Psych does not provide any way for a class to control allocation when coming from YAML the way that Marshal can, so even if the argument is that Singleton should control its own destiny (by implementing encode_with), Psych needs to provide a way to customize object allocation (without having to override allocate, which might make things complicated for allocating objects "normally").
- 主要语言
- Ruby
- 星标
- 597
- 派生
- 223
- 平均合并
- 11 小时 23 分钟
- 30 天内合并 PR
- 3
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
ruby/psych 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 78/100
-
Documentation
难度 1/5 1 小时以内 新手友好度 72/100
-
难度 2/5 1-3 小时 新手友好度 55/100
-
难度 4/5 3-5 天 新手友好度 68/100
-
难度 4/5 3-5 天 新手友好度 52/100
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 75/100
simp/pupmod-simp-simp#395 ·
-
难度 2/5 1-3 小时 新手友好度 80/100
simp/pupmod-simp-rsyslog#219 ·
-
难度 1/5 1 小时以内 新手友好度 90/100
simp/pupmod-simp-pupmod#256 ·
-
难度 1/5 1 小时以内 新手友好度 90/100
simp/pupmod-simp-sudo#150 ·
-
难度 1/5 1 小时以内 新手友好度 90/100