Skip to content

Commit bcf2a3f

Browse files
author
hartsantler
committed
go lang: fixed passing normal and keyword args when making a new instance of a class.
1 parent b1f7bf0 commit bcf2a3f

2 files changed

Lines changed: 34 additions & 11 deletions

File tree

pythonjs/pythonjs_to_go.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,26 @@ def visit_ClassDef(self, node):
4444
out.append('%s %s' %(name, sdef[name]))
4545
out.append('}')
4646

47-
out.append( 'func __new__%s() *%s {' %(node.name, node.name))
48-
out.append( ' ob := %s{}' %node.name )
49-
out.append( ' ob.__init__()')
50-
out.append( ' return &ob')
51-
out.append('}')
47+
48+
init = None
5249

5350
for b in node.body:
5451
out.append( self.visit(b) )
52+
assert isinstance(b, ast.FunctionDef)
53+
if b.name == '__init__':
54+
init = b
55+
56+
if init:
57+
58+
out.append( 'func __new__%s( %s ) *%s {' %(node.name, init._args_signature, node.name))
59+
out.append( ' ob := %s{}' %node.name )
60+
out.append( ' ob.__init__(%s)' %','.join(init._arg_names))
61+
out.append( ' return &ob')
62+
out.append('}')
63+
64+
else:
65+
out.append( 'func __new__%s() *%s { return &%s{} }' %(node.name, node.name, node.name))
66+
5567

5668
self._class_stack.pop()
5769
return '\n'.join(out)
@@ -201,7 +213,7 @@ def _visit_function(self, node):
201213
return_type = decor.args[0].id
202214

203215

204-
#args = self.visit(node.args)
216+
node._arg_names = []
205217
args = []
206218
oargs = []
207219
offset = len(node.args.args) - len(node.args.defaults)
@@ -210,8 +222,10 @@ def _visit_function(self, node):
210222
is_method = False
211223
for i, arg in enumerate(node.args.args):
212224
arg_name = arg.id
225+
213226
if arg_name not in args_typedefs:
214227
if arg_name=='self':
228+
assert i==0
215229
is_method = True
216230
continue
217231
else:
@@ -235,16 +249,21 @@ def _visit_function(self, node):
235249
oargs.append( (arg_name, default_value) )
236250
else:
237251
args.append( a )
252+
node._arg_names.append( arg_name )
238253

239254
if oargs:
240255
#args.append( '[%s]' % ','.join(oargs) )
241256
#args.append( '{%s}' % ','.join(oargs) )
242257
args.append( '__kwargs _kwargs_type_')
258+
node._arg_names.append( '__kwargs' )
243259

244260
if node.args.vararg:
245261
starargs = node.args.vararg
246262
assert starargs in args_typedefs
247263
args.append( '%s ...%s' %(starargs, args_typedefs[starargs]))
264+
node._arg_names.append( starargs )
265+
266+
node._args_signature = ','.join(args)
248267

249268
####
250269
if is_method:

regtests/go/class.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@ class A:
55
{
66
x:int,
77
y:int,
8+
z:int,
89
}
9-
def __init__(self):
10-
self.x = 1
11-
self.y = 2
10+
def __init__(self, x:int, y:int, z:int=1):
11+
self.x = x
12+
self.y = y
13+
self.z = z
1214

1315
def main():
14-
a = A()
15-
print( a.x )
16+
a = A( 100, 200, z=9999 )
17+
print( a.x )
18+
print( a.y )
19+
print( a.z )

0 commit comments

Comments
 (0)