Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
N
neo
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Labels
Merge Requests
2
Merge Requests
2
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Jobs
Commits
Open sidebar
Kirill Smelkov
neo
Commits
26f53db1
Commit
26f53db1
authored
Jul 01, 2018
by
Kirill Smelkov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
.
parent
9ce137f2
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
73 additions
and
13 deletions
+73
-13
go/zodb/py/pydata-gen-testdata
go/zodb/py/pydata-gen-testdata
+3
-3
go/zodb/pydata.go
go/zodb/pydata.go
+70
-10
No files found.
go/zodb/py/pydata-gen-testdata
View file @
26f53db1
...
...
@@ -22,7 +22,7 @@
from
ZODB.tests
import
testSerialize
from
ZODB
import
serialize
from
zodbtools.util
import
escapeqq
from
zodbtools.util
import
escapeqq
as
qq
def
main
():
# dump to go what to expect
...
...
@@ -46,8 +46,8 @@ def main():
emit
(
"
\
n
var _PyData_ClassName_Testv = [...]_PyDataClassName_TestEntry{"
)
for
test
in
testv
:
emit
(
"
\
t
{"
)
emit
(
"
\
t
\
t
%s,"
%
escape
qq
(
test
))
emit
(
"
\
t
\
t
%s,"
%
escape
qq
(
r
.
getClassName
(
test
)))
emit
(
"
\
t
\
t
%s,"
%
qq
(
test
))
emit
(
"
\
t
\
t
%s,"
%
qq
(
r
.
getClassName
(
test
)))
emit
(
"
\
t
},"
)
emit
(
'
\
t
{"aaa", "?.?"},'
)
# invalid
emit
(
"}"
)
...
...
go/zodb/pydata.go
View file @
26f53db1
// Copyright (C) 2016-201
7
Nexedi SA and Contributors.
// Copyright (C) 2016-201
8
Nexedi SA and Contributors.
// Kirill Smelkov <kirr@nexedi.com>
//
// This program is free software: you can Use, Study, Modify and Redistribute
...
...
@@ -22,21 +22,55 @@ package zodb
import
(
"bytes"
"errors"
"fmt"
pickle
"github.com/kisielk/og-rek"
)
// PyData represents data stored into ZODB by Python applications.
// PyData represents
raw
data stored into ZODB by Python applications.
//
// The format is based on python pickles. Basically every serialized object has
// two parts:
class description and
object state. See
// two parts:
pickle with class description and pickle with
object state. See
//
// https://github.com/zopefoundation/ZODB/blob/a89485c1/src/ZODB/serialize.py
//
// for format description.
//
// PyData can be decoded into PyObject.
type
PyData
[]
byte
// PyObject represents persistent Python object.
//
// PyObject can be decoded from PyData.
type
PyObject
struct
{
PyClass
pickle
.
Class
// python class of this object
State
interface
{}
// object state. python passes this to pyclass.__new__().__setstate__()
}
// Decode decodes raw ZODB python data into PyObject.
func
(
d
PyData
)
Decode
()
(
*
PyObject
,
error
)
{
p
:=
pickle
.
NewDecoder
(
bytes
.
NewReader
([]
byte
(
d
)))
xklass
,
err
:=
p
.
Decode
()
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"pydata: decode: class description: %s"
,
err
)
}
klass
,
err
:=
normPyClass
(
xklass
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"pydata: decode: class description: %s"
,
err
)
}
state
,
err
:=
p
.
Decode
()
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"pydata: decode: object state: %s"
,
err
)
}
return
&
PyObject
{
PyClass
:
klass
,
State
:
state
},
nil
}
// ClassName returns fully-qualified python class name used for object type.
//
// The format is "module.class".
...
...
@@ -49,23 +83,49 @@ func (d PyData) ClassName() string {
return
"?.?"
}
klass
,
err
:=
normPyClass
(
xklass
)
if
err
!=
nil
{
return
"?.?"
}
return
klass
.
Module
+
"."
+
klass
.
Name
}
var
errInvalidPyClass
=
errors
.
New
(
"invalid py class description"
)
// normPyClass normalizes py class that has just been decoded from a serialized
// ZODB object or reference.
func
normPyClass
(
xklass
interface
{})
(
pickle
.
Class
,
error
)
{
// class description:
//
// - type(obj), or
// - (xklass, newargs|None) ; xklass = type(obj) | (modname, classname)
if
t
,
ok
:=
xklass
.
(
pickle
.
Tuple
);
ok
{
if
len
(
t
)
!=
2
{
// (klass, args)
return
"?.?"
// t = (xklass, newargs|None)
if
len
(
t
)
!=
2
{
return
pickle
.
Class
{},
errInvalidPyClass
}
xklass
=
t
[
0
]
if
t
,
ok
:=
xklass
.
(
pickle
.
Tuple
);
ok
{
//
py: "old style reference"
//
t = (modname, classname)
if
len
(
t
)
!=
2
{
return
"?.?"
// (modname, classname)
return
pickle
.
Class
{},
errInvalidPyClass
}
modname
,
ok1
:=
t
[
0
]
.
(
string
)
classname
,
ok2
:=
t
[
1
]
.
(
string
)
if
!
(
ok1
&&
ok2
)
{
return
pickle
.
Class
{},
errInvalidPyClass
}
return
fmt
.
Sprintf
(
"%s.%s"
,
t
...
)
return
pickle
.
Class
{
Module
:
modname
,
Name
:
classname
},
nil
}
}
if
klass
,
ok
:=
xklass
.
(
pickle
.
Class
);
ok
{
return
klass
.
Module
+
"."
+
klass
.
Name
// klass = type(obj)
return
klass
,
nil
}
return
"?.?"
return
pickle
.
Class
{},
errInvalidPyClass
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment