Skip to content

Dealing with cyclic referencing #89

@thewhitegoatcb

Description

@thewhitegoatcb

Browsing the sources and testing around got me thinking that there's no trivial solution to a cyclic referencing problem on Lua side, admittedly I'm no expert in the field.

Currently if I do something like that

local py = require("python")

local weak_refs = setmetatable({}, { __mode = "v" })
do
	local pb = py.builtins()
	
	local control_table = {}
	
	-- reference itself in a table - control_table = {{control_table,},}
	table.insert(control_table, {control_table})
	weak_refs["control_table"] = control_table
	
	local mixed_table = {}
	
	list = pb.list()
	list.append(mixed_table)
	
	-- reference itself in a table - mixed_table = {py_list(mixed_table),}
	table.insert(mixed_table, list)
	weak_refs["mixed_table"] = mixed_table
end

collectgarbage('collect')

for k, v in pairs(weak_refs) do
	print(k, "is not collected! with value of:", v)
end

Output
mixed_table is not collected! with value of: table: 0x226797e0

Since I don't see a way to let the garbage collector to traverse a userdata I don't see a solution to this currently.

Untested but the issue would exist on the Python side too but it can be solved with: https://docs.python.org/3/c-api/gcsupport.html
You would have to build a traverser that would traverse Lua tables and other Python objects, but you would have to convert Lua tables into Python objects too and those have to be cached to avoid revisiting existing nodes, caching could be done with weak referenced table in lua, the key would be the cached lua object and the value is userdata with the cached PyObject.

Is there something obvious that I missed? Is there a way of solving this on the Lua side ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions