• Carlos Ramos Carreño's avatar
    Parse `bytes` as str in Golang. · 5a2b639c
    Carlos Ramos Carreño authored
    [In Go, strings can contain arbitrary bytes](https://go.dev/blog/strings)
    (similar to the ones in Python 2), and there is no explicit `bytes`
    class.
    Thus, all the code that deals with arbitrary data in Go is currently
    using the string type.
    
    However, the [ogórek package](https://github.com/kisielk/og-rek) used
    to load Python pickles in Go, decodes Python 3 `bytes` objects as a
    special string type `ogórek.Bytes`, incompatible with APIs expecting
    normal strings.
    This causes errors such as the following:
    ```
    --- FAIL: TestΔFtail (1.31s)
    panic: @03f97a3b982b7bcc: get blkdata from obj 0000000000000002: ZBlk0(0000000000000002): loadBlkData: wendelin.bigfile.file_zodb.ZBlk0(0000000000000002): activate: pysetstate: expect str; got ogórek.Bytes [recovered]
            panic: file:///tmp/TestΔFtail493253177/001/1.fs: @03f97a3b982b7bcc: get blktab: @03f97a3b982b7bcc: get blkdata from obj 0000000000000002: ZBlk0(0000000000000002): loadBlkData: wendelin.bigfile.file_zodb.ZBlk0(0000000000000002): activate: pysetstate: expect str; got ogórek.Bytes [recovered]
            panic: file:///tmp/TestΔFtail493253177/001/1.fs: @03f97a3b982b7bcc: get blktab: @03f97a3b982b7bcc: get blkdata from obj 0000000000000002: ZBlk0(0000000000000002): loadBlkData: wendelin.bigfile.file_zodb.ZBlk0(0000000000000002): activate: pysetstate: expect str; got ogórek.Bytes
    ```
    
    In order to fix that, we have copied the [`Xstrbytes` function from NEO](
    https://lab.nexedi.com/kirr/neo/-/blob/f7776fc1689b0d62e582b132ecc017ab72dc3b23/go/zodb/internal/pickletools/pickletools.go#L49-64)
    in the `pycompat.go` file, which accepts either a string or an
    `ogórek.Bytes` object and returns the corresponding string.
    
    We have used that function whenever a `bytes` object could be present.
    5a2b639c
zblk.go 13.4 KB