Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
O
og-rek
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
og-rek
Commits
06476df7
Commit
06476df7
authored
Oct 15, 2014
by
Larz Conwell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Return errors rather than doing panics.
This makes it simpler to handle errors in code importing this package.
parent
b555ed17
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
44 additions
and
11 deletions
+44
-11
encode.go
encode.go
+9
-1
ogórek.go
ogórek.go
+29
-8
ogórek_test.go
ogórek_test.go
+6
-2
No files found.
encode.go
View file @
06476df7
...
...
@@ -8,6 +8,14 @@ import (
"reflect"
)
type
TypeError
struct
{
typ
string
}
func
(
te
*
TypeError
)
Error
()
string
{
return
fmt
.
Sprintf
(
"no support for type '%s'"
,
te
.
typ
)
}
// An Encoder encodes Go data structures into pickle byte stream
type
Encoder
struct
{
w
io
.
Writer
...
...
@@ -79,7 +87,7 @@ func (e *Encoder) encode(rv reflect.Value) error {
e
.
w
.
Write
([]
byte
{
opNone
})
default
:
panic
(
fmt
.
Sprintf
(
"no support for type '%s'"
,
rk
.
String
()))
return
&
TypeError
{
typ
:
rk
.
String
()}
}
return
nil
...
...
ogórek.go
View file @
06476df7
...
...
@@ -80,6 +80,7 @@ const (
var
errNotImplemented
=
errors
.
New
(
"unimplemented opcode"
)
var
ErrInvalidPickleVersion
=
errors
.
New
(
"invalid pickle version"
)
var
ErrNoMarker
=
errors
.
New
(
"no marker in stack"
)
type
OpcodeError
struct
{
Key
byte
...
...
@@ -243,15 +244,15 @@ func (d *Decoder) mark() {
}
// Return the position of the topmost marker
func
(
d
*
Decoder
)
marker
()
int
{
func
(
d
*
Decoder
)
marker
()
(
int
,
error
)
{
m
:=
mark
{}
var
k
int
for
k
=
len
(
d
.
stack
)
-
1
;
d
.
stack
[
k
]
!=
m
&&
k
>
0
;
k
--
{
}
if
k
>=
0
{
return
k
return
k
,
nil
}
panic
(
"no marker in stack"
)
return
0
,
ErrNoMarker
}
// Append a new value
...
...
@@ -553,7 +554,11 @@ func (d *Decoder) global() error {
}
func
(
d
*
Decoder
)
loadDict
()
error
{
k
:=
d
.
marker
()
k
,
err
:=
d
.
marker
()
if
err
!=
nil
{
return
err
}
m
:=
make
(
map
[
interface
{}]
interface
{},
0
)
items
:=
d
.
stack
[
k
+
1
:
]
for
i
:=
0
;
i
<
len
(
items
);
i
+=
2
{
...
...
@@ -570,7 +575,11 @@ func (d *Decoder) loadEmptyDict() error {
}
func
(
d
*
Decoder
)
loadAppends
()
error
{
k
:=
d
.
marker
()
k
,
err
:=
d
.
marker
()
if
err
!=
nil
{
return
err
}
l
:=
d
.
stack
[
k
-
1
]
switch
l
.
(
type
)
{
case
[]
interface
{}
:
...
...
@@ -621,14 +630,22 @@ func (d *Decoder) loadBool(b bool) error {
}
func
(
d
*
Decoder
)
loadList
()
error
{
k
:=
d
.
marker
()
k
,
err
:=
d
.
marker
()
if
err
!=
nil
{
return
err
}
v
:=
append
([]
interface
{}{},
d
.
stack
[
k
+
1
:
]
...
)
d
.
stack
=
append
(
d
.
stack
[
:
k
],
v
)
return
nil
}
func
(
d
*
Decoder
)
loadTuple
()
error
{
k
:=
d
.
marker
()
k
,
err
:=
d
.
marker
()
if
err
!=
nil
{
return
err
}
v
:=
append
([]
interface
{}{},
d
.
stack
[
k
+
1
:
]
...
)
d
.
stack
=
append
(
d
.
stack
[
:
k
],
v
)
return
nil
...
...
@@ -700,7 +717,11 @@ func (d *Decoder) loadSetItem() error {
}
func
(
d
*
Decoder
)
loadSetItems
()
error
{
k
:=
d
.
marker
()
k
,
err
:=
d
.
marker
()
if
err
!=
nil
{
return
err
}
l
:=
d
.
stack
[
k
-
1
]
switch
m
:=
l
.
(
type
)
{
case
map
[
interface
{}]
interface
{}
:
...
...
ogórek_test.go
View file @
06476df7
...
...
@@ -18,7 +18,11 @@ func TestMarker(t *testing.T) {
buf
:=
bytes
.
Buffer
{}
dec
:=
NewDecoder
(
&
buf
)
dec
.
mark
()
if
dec
.
marker
()
!=
0
{
k
,
err
:=
dec
.
marker
()
if
err
!=
nil
{
t
.
Error
(
err
)
}
if
k
!=
0
{
t
.
Error
(
"no marker found"
)
}
}
...
...
@@ -50,7 +54,7 @@ func TestDecode(t *testing.T) {
{
"unicode"
,
"V
\\
u65e5
\\
u672c
\\
u8a9e
\n
p0
\n
."
,
string
(
"日本語"
)},
{
"empty dict"
,
"(dp0
\n
."
,
make
(
map
[
interface
{}]
interface
{})},
{
"dict with strings"
,
"(dp0
\n
S'a'
\n
p1
\n
S'1'
\n
p2
\n
sS'b'
\n
p3
\n
S'2'
\n
p4
\n
s."
,
map
[
interface
{}]
interface
{}{
"a"
:
"1"
,
"b"
:
"2"
}},
{
"GLOBAL and REDUCE opcodes"
,
"cfoo
\n
bar
\n
S'bing'
\n\x85
R."
,
Call
{
Callable
:
Class
{
Module
:
"foo"
,
Name
:
"bar"
},
Args
:
[]
interface
{}
{
"bing"
}}},
{
"GLOBAL and REDUCE opcodes"
,
"cfoo
\n
bar
\n
S'bing'
\n\x85
R."
,
Call
{
Callable
:
Class
{
Module
:
"foo"
,
Name
:
"bar"
},
Args
:
[]
interface
{}{
"bing"
}}},
{
"LONG_BINPUT opcode"
,
"(lr0000I17
\n
a."
,
[]
interface
{}{
int64
(
17
)}},
{
"graphite message1"
,
string
(
graphitePickle1
),
[]
interface
{}{
map
[
interface
{}]
interface
{}{
"values"
:
[]
interface
{}{
float64
(
473
),
float64
(
497
),
float64
(
540
),
float64
(
1497
),
float64
(
1808
),
float64
(
1890
),
float64
(
2013
),
float64
(
1821
),
float64
(
1847
),
float64
(
2176
),
float64
(
2156
),
float64
(
1250
),
float64
(
2055
),
float64
(
1570
),
None
{},
None
{}},
"start"
:
int64
(
1383782400
),
"step"
:
int64
(
86400
),
"end"
:
int64
(
1385164800
),
"name"
:
"ZZZZ.UUUUUUUU.CCCCCCCC.MMMMMMMM.XXXXXXXXX.TTT"
}}},
{
"graphite message2"
,
string
(
graphitePickle2
),
[]
interface
{}{
map
[
interface
{}]
interface
{}{
"values"
:
[]
interface
{}{
float64
(
473
),
float64
(
497
),
float64
(
540
),
float64
(
1497
),
float64
(
1808
),
float64
(
1890
),
float64
(
2013
),
float64
(
1821
),
float64
(
1847
),
float64
(
2176
),
float64
(
2156
),
float64
(
1250
),
float64
(
2055
),
float64
(
1570
),
None
{},
None
{}},
"start"
:
int64
(
1383782400
),
"step"
:
int64
(
86400
),
"end"
:
int64
(
1385164800
),
"name"
:
"user.login.area.machine.metric.minute"
}}},
...
...
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