Cython: Aliasing function argument name

Question:

I have a C library that I am writing a Python extension for using Cython that includes this function (declared in the library’s header file):

#include <stdio.h>
#include "zlib.h"

int deflate_index_build(FILE *in, off_t span, struct deflate_index **built);

I am attempting to create a Cython extension for this function using:

from posix.types cimport off_t
from libc.stdio cimport FILE

cdef extern from "header.h":
        int deflate_index_build(FILE *in, off_t span, deflate_index **built)

However, the use of in as the name for the first argument of the function causes a syntax error on compilation because in is a Python keyword. I don’t want to change the name of this argument because it would have a large impact on the C library. Is there a way to alias the argument name in Cython to avoid this error?

Asked By: Forrest Williams

||

Answers:

From @user2357112 in the comment above:

C argument names aren’t part of the function signature. I don’t think you actually need to put the same argument names in your Cython code as you declared in your .h file. (You don’t even need to match argument names between your .h file and the actual function definition – in fact, you don’t even need argument names in your .h file at all.)

Answered By: Forrest Williams
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.