Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't deserialize something from an anonymous module #48872

Open
quinnj opened this issue Mar 2, 2023 · 5 comments
Open

Can't deserialize something from an anonymous module #48872

quinnj opened this issue Mar 2, 2023 · 5 comments
Labels

Comments

@quinnj
Copy link
Member

quinnj commented Mar 2, 2023

It seems like this should be able to work?

julia> using Serialization

julia> module Foo; foo() = "hey"; end
Main.Foo

julia> io = IOBuffer()
IOBuffer(data=UInt8[...], readable=true, writable=true, seekable=true, append=false, size=0, maxsize=Inf, ptr=1, mark=-1)

julia> serialize(io, Foo.foo)

julia> seekstart(io)
IOBuffer(data=UInt8[...], readable=true, writable=true, seekable=true, append=false, size=30, maxsize=Inf, ptr=1, mark=-1)

julia> deserialize(io)
foo (generic function with 1 method)

julia> m = Module()
Main.anonymous

julia> Core.eval(m, :(foo() = "hey"))
foo (generic function with 1 method)

julia> io = IOBuffer()
IOBuffer(data=UInt8[...], readable=true, writable=true, seekable=true, append=false, size=0, maxsize=Inf, ptr=1, mark=-1)

julia> serialize(io, m.foo)

julia> seekstart(io)
IOBuffer(data=UInt8[...], readable=true, writable=true, seekable=true, append=false, size=36, maxsize=Inf, ptr=1, mark=-1)

julia> deserialize(io)
ERROR: UndefVarError: `anonymous` not defined
Stacktrace:
  [1] deserialize_module(s::Serializer{IOBuffer})
    @ Serialization ~/juliax/usr/share/julia/stdlib/v1.10/Serialization/src/Serialization.jl:996
  [2] handle_deserialize(s::Serializer{IOBuffer}, b::Int32)
    @ Serialization ~/juliax/usr/share/julia/stdlib/v1.10/Serialization/src/Serialization.jl:895
  [3] deserialize(s::Serializer{IOBuffer})
    @ Serialization ~/juliax/usr/share/julia/stdlib/v1.10/Serialization/src/Serialization.jl:813
  [4] deserialize_datatype(s::Serializer{IOBuffer}, full::Bool)
    @ Serialization ~/juliax/usr/share/julia/stdlib/v1.10/Serialization/src/Serialization.jl:1389
  [5] handle_deserialize(s::Serializer{IOBuffer}, b::Int32)
    @ Serialization ~/juliax/usr/share/julia/stdlib/v1.10/Serialization/src/Serialization.jl:866
  [6] deserialize(s::Serializer{IOBuffer})
    @ Serialization ~/juliax/usr/share/julia/stdlib/v1.10/Serialization/src/Serialization.jl:813
  [7] handle_deserialize(s::Serializer{IOBuffer}, b::Int32)
    @ Serialization ~/juliax/usr/share/julia/stdlib/v1.10/Serialization/src/Serialization.jl:873
  [8] deserialize(s::Serializer{IOBuffer})
    @ Serialization ~/juliax/usr/share/julia/stdlib/v1.10/Serialization/src/Serialization.jl:813
  [9] handle_deserialize(s::Serializer{IOBuffer}, b::Int32)
    @ Serialization ~/juliax/usr/share/julia/stdlib/v1.10/Serialization/src/Serialization.jl:919
 [10] deserialize
    @ ~/juliax/usr/share/julia/stdlib/v1.10/Serialization/src/Serialization.jl:813 [inlined]
 [11] deserialize(s::IOBuffer)
    @ Serialization ~/juliax/usr/share/julia/stdlib/v1.10/Serialization/src/Serialization.jl:800
 [12] top-level scope
    @ REPL[12]:1
@quinnj
Copy link
Member Author

quinnj commented Mar 2, 2023

Or perhaps this is expected since Main.anonymous isn't really referring to a specific module, and how can serialize know which anonymous module you're referring to exactly?

@Seelengrab
Copy link
Contributor

If you do julia> const anonymous = 1, you also hit a type assert (rightly so):

julia> deserialize(io)
ERROR: TypeError: in typeassert, expected Module, got a value of type Int64
Stacktrace:

So either serialize needs to preserve the information that the parent module was anonymous and recreate it somehow, or serialization of functions in an anonymous module drops that information (or we disallow it entirely, seeing as we can't correctly deserialize this now, I don't think anyone is relying on this working right now without a bug coming for them later on).

@JeffBezanson
Copy link
Member

If we can detect an anonymous module (which we probably can by seeing that its binding doesn't exist or doesn't point to it), we could handle its functions like anonymous functions in Main, i.e. serialize them by contents instead of name. Would that be useful in this situation?

@quinnj
Copy link
Member Author

quinnj commented Mar 5, 2023

Ah yeah, that would be helpful.

@ablaom
Copy link

ablaom commented Jun 17, 2024

Yeah, I think this would also resolve the Documenter.jl error cross-referenced above, thanks.

@nsajko nsajko added the modules label Oct 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants