hpy icon indicating copy to clipboard operation
hpy copied to clipboard

HPyGlobal: debug mode should check registration in HPyModuleDef->globals

Open mattip opened this issue 1 year ago • 4 comments

I don't see tests for setting the globals field in HPyModuleDef, and the only reference I could find in the cpython backend is here for error checking.

The NumPy port does use this, but I think that is redundant, since there the objects are globally saved and retrieved but not from the module. Using .global would require being able to fish out the module handles, which is discussed elsewhere.

mattip avatar Dec 06 '23 11:12 mattip

You are right. The idea of HPyModuleDef.globals is that one needs to register all HPyGlobal variables such that the interpreter can initialize the C-level value of those and do any internal preparation to be able to use the globals.

For CPython, we don't actually need that because HPyGlobal variables are just C global variables of type PyObject * and those are guaranteed to be initialized to NULL.

But I agree with you: since the contract is that one must register the globals, we should enforce this in the debug mode. We could do that by writing some marker value to the HPyGlobal.

fangerer avatar Dec 06 '23 11:12 fangerer

I though I already had an issue opened for that. I took the liberty and renamed this issue. I think we are using that field in GraalPy (or at least planned to use it) to preallocate our internal array with the actual values for globals.

steve-s avatar Dec 06 '23 13:12 steve-s

yes, we already do that: https://github.com/oracle/graalpython/blob/7279f9c3ee0b9ea6a2bef578af88f575f9ed9f31/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/hpy/GraalHPyNodes.java#L490

fangerer avatar Dec 06 '23 13:12 fangerer

A test would be nice (and that test would fail on CPython since it does not register them).

mattip avatar Dec 06 '23 16:12 mattip