Skip to content

translate-c can't handle C ternary operator with int condition #44

@cmpute

Description

@cmpute

Zig Version

0.14.1

Steps to Reproduce and Observed Behavior

Any call to CPython API PySequence_Fast_GET_SIZE will result in this error:

...\.zig-cache\o\6bacacdc44df520a1df153414437fc32\cimport.zig:8950:76: error: expected type 'bool', found 'c_int'
pub inline fn PySequence_Fast_GET_SIZE(o: anytype) @TypeOf(if (PyList_Check(o)) PyList_GET_SIZE(o) else PyTuple_GET_SIZE(o)) {
                                                               ~~~~~~~~~~~~^~~
pack_py.zig:62:44: note: called from here
    const len = py.PySequence_Fast_GET_SIZE(fastseq);
                ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~

py is defined as the following:

pub const py = @cImport({
    @cDefine("PY_SSIZE_T_CLEAN", {});
    @cInclude("Python.h");
});

Expected Behavior

Checking the translated code, I found that the translate-c module convert this C definition:

#define PySequence_Fast_GET_SIZE(o) \
    (PyList_Check(o) ? PyList_GET_SIZE(o) : PyTuple_GET_SIZE(o))

to

pub inline fn PySequence_Fast_GET_SIZE(o: anytype) @TypeOf(if (PyList_Check(o)) PyList_GET_SIZE(o) else PyTuple_GET_SIZE(o)) {
    _ = &o;
    return if (PyList_Check(o)) PyList_GET_SIZE(o) else PyTuple_GET_SIZE(o);
}

However, the return type of PyList_Check is int rather than bool, which caused this problem.

Maybe when translating this, the ternary condition should be wrapped with an helper function converting int to bool.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions