FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[Original HTTPS Page]
gpython/py/util.go at main · go-python/gpython · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
go-python
/
gpython
Public
Notifications
You must be signed in to change notification settings
Fork
105
Star
1k
Code
Issues
49
Pull requests
5
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
gpython
/
py
/
util.go
Copy path
More file actions
More file actions
Latest commit
History
History
History
236 lines (208 loc) · 4.73 KB
Breadcrumbs
gpython
/
py
/
util.go
Copy path
File metadata and controls
236 lines (208 loc) · 4.73 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Copyright 2022 The go-python Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package
py
import
(
"errors"
"strconv"
"strings"
)
var
(
ErrUnsupportedObjType
=
errors
.
New
(
"unsupported obj type"
)
)
// GetLen is a high-level convenience function that returns the length of the given Object.
func
GetLen
(
obj
Object
) (
Int
,
error
) {
getlen
,
ok
:=
obj
.(
I__len__
)
if
!
ok
{
return
0
,
nil
}
lenObj
,
err
:=
getlen
.
M__len__
()
if
err
!=
nil
{
return
0
,
err
}
return
GetInt
(
lenObj
)
}
// GetInt is a high-level convenience function that converts the given value to an int.
func
GetInt
(
obj
Object
) (
Int
,
error
) {
toIdx
,
ok
:=
obj
.(
I__index__
)
if
!
ok
{
_
,
err
:=
cantConvert
(
obj
,
"int"
)
return
0
,
err
}
return
toIdx
.
M__index__
()
}
// LoadTuple attempts to convert each element of the given list and store into each destination value (based on its type).
func
LoadTuple
(
args
Tuple
,
vars
[]
interface
{})
error
{
if
len
(
args
)
>
len
(
vars
) {
return
ExceptionNewf
(
RuntimeError
,
"%d args given, expected %d"
,
len
(
args
),
len
(
vars
))
}
if
len
(
vars
)
>
len
(
args
) {
vars
=
vars
[:
len
(
args
)]
}
for
i
,
rval
:=
range
vars
{
err
:=
loadValue
(
args
[
i
],
rval
)
if
err
==
ErrUnsupportedObjType
{
return
ExceptionNewf
(
TypeError
,
"arg %d has unsupported object type: %s"
,
i
,
args
[
i
].
Type
().
Name
)
}
}
return
nil
}
// LoadAttr gets the named attribute and attempts to store it into the given destination value (based on its type).
func
LoadAttr
(
obj
Object
,
attrName
string
,
dst
interface
{})
error
{
attr
,
err
:=
GetAttrString
(
obj
,
attrName
)
if
err
!=
nil
{
return
err
}
err
=
loadValue
(
attr
,
dst
)
if
err
==
ErrUnsupportedObjType
{
return
ExceptionNewf
(
TypeError
,
"attribute
\"
%s
\"
has unsupported object type: %s"
,
attrName
,
attr
.
Type
().
Name
)
}
return
nil
}
// LoadIntsFromList extracts a list of ints contained given a py.List or py.Tuple
func
LoadIntsFromList
(
list
Object
) ([]
int64
,
error
) {
N
,
err
:=
GetLen
(
list
)
if
err
!=
nil
{
return
nil
,
err
}
getter
,
ok
:=
list
.(
I__getitem__
)
if
!
ok
{
return
nil
,
nil
}
if
N
<=
0
{
return
nil
,
nil
}
intList
:=
make
([]
int64
,
N
)
for
i
:=
Int
(
0
);
i
<
N
;
i
++
{
item
,
err
:=
getter
.
M__getitem__
(
i
)
if
err
!=
nil
{
return
nil
,
err
}
var
intVal
Int
intVal
,
err
=
GetInt
(
item
)
if
err
!=
nil
{
return
nil
,
err
}
intList
[
i
]
=
int64
(
intVal
)
}
return
intList
,
nil
}
func
loadValue
(
src
Object
,
data
interface
{})
error
{
var
(
v_str
string
v_float
float64
v_int
int64
)
haveStr
:=
false
switch
v
:=
src
.(
type
) {
case
Bool
:
if
v
{
v_int
=
1
v_float
=
1
v_str
=
"True"
}
else
{
v_str
=
"False"
}
haveStr
=
true
case
Int
:
v_int
=
int64
(
v
)
v_float
=
float64
(
v
)
case
Float
:
v_int
=
int64
(
v
)
v_float
=
float64
(
v
)
case
String
:
v_str
=
string
(
v
)
haveStr
=
true
case
NoneType
:
// No-op
default
:
return
ErrUnsupportedObjType
}
switch
dst
:=
data
.(
type
) {
case
*
Int
:
*
dst
=
Int
(
v_int
)
case
*
bool
:
*
dst
=
v_int
!=
0
case
*
int8
:
*
dst
=
int8
(
v_int
)
case
*
uint8
:
*
dst
=
uint8
(
v_int
)
case
*
int16
:
*
dst
=
int16
(
v_int
)
case
*
uint16
:
*
dst
=
uint16
(
v_int
)
case
*
int32
:
*
dst
=
int32
(
v_int
)
case
*
uint32
:
*
dst
=
uint32
(
v_int
)
case
*
int
:
*
dst
=
int
(
v_int
)
case
*
uint
:
*
dst
=
uint
(
v_int
)
case
*
int64
:
*
dst
=
v_int
case
*
uint64
:
*
dst
=
uint64
(
v_int
)
case
*
float32
:
if
haveStr
{
v_float
,
_
=
strconv
.
ParseFloat
(
v_str
,
32
)
}
*
dst
=
float32
(
v_float
)
case
*
float64
:
if
haveStr
{
v_float
,
_
=
strconv
.
ParseFloat
(
v_str
,
64
)
}
*
dst
=
v_float
case
*
Float
:
if
haveStr
{
v_float
,
_
=
strconv
.
ParseFloat
(
v_str
,
64
)
}
*
dst
=
Float
(
v_float
)
case
*
string
:
*
dst
=
v_str
case
*
String
:
*
dst
=
String
(
v_str
)
// case []uint64:
// for i := range data {
// dst[i] = order.Uint64(bs[8*i:])
// }
// case []float32:
// for i := range data {
// dst[i] = math.Float32frombits(order.Uint32(bs[4*i:]))
// }
// case []float64:
// for i := range data {
// dst[i] = math.Float64frombits(order.Uint64(bs[8*i:]))
// }
default
:
return
ExceptionNewf
(
NotImplementedError
,
"%s"
,
"unsupported Go data type"
)
}
return
nil
}
// Println prints the provided strings to gpython's stdout.
func
Println
(
self
Object
,
args
...
string
)
bool
{
sysModule
,
err
:=
self
.(
*
Module
).
Context
.
GetModule
(
"sys"
)
if
err
!=
nil
{
return
false
}
stdout
:=
sysModule
.
Globals
[
"stdout"
]
write
,
err
:=
GetAttrString
(
stdout
,
"write"
)
if
err
!=
nil
{
return
false
}
call
,
ok
:=
write
.(
I__call__
)
if
!
ok
{
return
false
}
for
_
,
v
:=
range
args
{
if
!
strings
.
Contains
(
v
,
"
\n
"
) {
v
+=
" "
}
_
,
err
:=
call
.
M__call__
(
Tuple
{
String
(
v
)},
nil
)
if
err
!=
nil
{
return
false
}
}
_
,
err
=
call
.
M__call__
(
Tuple
{
String
(
"
\n
"
)},
nil
)
// newline
return
err
==
nil
}
Back
|
FazBrowse Home
|
New Git URL