go-python / go-python/gopy

Imported types not working properly

未关闭
#303 0 条评论 3 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
Go
星标
2.3k
派生
136
平均合并
14 分钟
30 天内合并 PR
2

描述

I'm trying to build a function which uses imported types as argument and as return type:

```go
package request

import "github.com/Danny-Dasilva/CycleTLS/cycletls"

func Request(method string, url string, options cycletls.Options) (cycletls.Response, error) {
client := cycletls.Init()

response, err := client.Do(url, options, method)

return Response(response), err
}
```

However, the python types are not being generated properly, so I get the following error when trying to pass a `cycletls.Options` to the `Request` function in python:

```
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x7f5523f2a520]

goroutine 17 [running, locked to thread]:
main.request_Request(0xc00003e060?, 0x0?, 0x1?)
/home/matheus/tests/go-request/request/request.go:1881 +0xe0
Aborted
```

Here are the go types:

```go
type Options struct {
URL string `json:"url"`
Method string `json:"method"`
Headers map[string]string `json:"headers"`
Body string `json:"body"`
Ja3 string `json:"ja3"`
UserAgent string `json:"userAgent"`
Proxy string `json:"proxy"`
Cookies []Cookie `json:"cookies"`
Timeout int `json:"timeout"`
DisableRedirect bool `json:"disableRedirect"`
HeaderOrder []string `json:"headerOrder"`
OrderAsProvided bool `json:"orderAsProvided"`
}

type Response struct {
RequestID string
Status int
Body string
Headers map[string]string
}
```

And here are the generated python types:

```python
# Python type for cycletls.Options
class cycletls_Options(GoClass):
""""""
def __init__(self, *args, **kwargs):
"""
handle=A Go-side object is always initialized with an explicit handle=arg
"""
if len(kwargs) == 1 and 'handle' in kwargs:
self.handle = kwargs['handle']
_request.IncRef(self.handle)
elif len(args) == 1 and isinstance(args[0], GoClass):
self.handle = args[0].handle
_request.IncRef(self.handle)
elif len(args) == 1 and isinstance(args[0], int):
self.handle = args[0]
_request.IncRef(self.handle)
else:
self.handle = 0
def __del__(self):
_request.DecRef(self.handle)

# Python type for cycletls.Response
class cycletls_Response(GoClass):
""""""
def __init__(self, *args, **kwargs):
"""
handle=A Go-side object is always initialized with an explicit handle=arg
"""
if len(kwargs) == 1 and 'handle' in kwargs:
self.handle = kwargs['handle']
_request.IncRef(self.handle)
elif len(args) == 1 and isinstance(args[0], GoClass):
self.handle = args[0].handle
_request.IncRef(self.handle)
elif len(args) == 1 and isinstance(args[0], int):
self.handle = args[0]
_request.IncRef(self.handle)
else:
self.handle = 0
def __del__(self):
_request.DecRef(self.handle)
```

As a workaround, I was able to make it work by redefining the types in my go package, but it is a bit annoying, since I have to redefine the subtypes in `cycletls.Options` and rebuild the struct inside the function.

贡献指南

这个仓库没有索引到贡献指南

调研方向

从 request/request.go 中约第 1881 行的 Go Request 入口开始,将生成的 cycletls_Options 和 cycletls_Response 包装器与 issue 中展示的导入 Go 结构体进行比较。使用导入的 cycletls.Options 重现调用,并确定它为什么会到达 nil pointer。完成标准是:无需在本地 Go 包中重新定义导入的类型,即可将其传递给 Request。

由索引模型根据 Issue 内容生成。

评估

技术栈
go, python
领域
tooling
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。