cython

Does a pointer get freed when called indirectly in a function from a struct?

Does a pointer get freed when called indirectly in a function from a struct? Question: Say we have the following in Cython: ctypedef struct Structure: int* array int size cdef print_fx(Structure obj): print(obj.array[0]) cdef create_object(int size): cdef Structure result result.size = size result.array = <int*>malloc(size*sizeof(int)) for i in range(size): result.array[i] = 1 return result And …

Total answers: 1

Cython: Passing pointer created by Python wrapper class to C function

Cython: Passing pointer created by Python wrapper class to C function Question: I am following the Cython Documentation to define a class that is created from an existing C pointer: # Example C struct ctypedef struct my_c_struct: int a int b cdef class WrapperClass: """A wrapper class for a C/C++ data structure""" cdef my_c_struct *_ptr …

Total answers: 1

Cython: Aliasing function argument name

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: …

Total answers: 1

Assigning ndarray in cython to new variable very slow? Or what is going on here?

Assigning ndarray in cython to new variable very slow? Or what is going on here? Question: I am fairly new to cython and I am wondering why the following takes very long: cpdef test(a): cdef np.ndarray[dtype=int] b for i in range(10): b=a a=np.array([1,2,3],dtype=int) t = timeit.Timer(functools.partial(test.test, a)) print(t.timeit(1000000)) -> 0.5446977 Seconds If i comment out …

Total answers: 1

As of 2023, is there any way to line profile Cython at all?

As of 2023, is there any way to line profile Cython at all? Question: Something has substantially changed with the way that line profiling Cython works, such that previous answers no longer work. I am not sure if something subtle has changed, or if it is simply totally broken. For instance, here is a very …

Total answers: 1

Poetry add dependency that uses cython

Poetry add dependency that uses cython Question: I have a project which needs to depend on the latest commit of pysam, because I’m working in python 3.11. This means building the package from source, so I do the following: poetry add git+https://github.com/pysam-developers/pysam However, I get an error which I think boils down to poetry not …

Total answers: 1

Spacy add_alias, TypeError

Spacy add_alias, TypeError Question: MWE from spacy.kb import KnowledgeBase import spacy #kb.add_entity already called. nlp = spacy.blank("en") kb = KnowledgeBase(vocab=nlp.vocab, entity_vector_length=96) name = "test" qid = 1 # type(qid) => int kb.add_alias(alias=name.lower(), entities=[qid], probabilities=[1]) produces the error at the last line: TypeError: an integer is required A previous SO post suggested that the same error …

Total answers: 1

Cython numpy array view off by one when wraparound is False

Cython numpy array view off by one when wraparound is False Question: I have some Cython code where I fill in the last value in each row of a memory view of a NumPy array with a number. If I compile the code with wraparound = False, the last value in the final row of …

Total answers: 1

How can I extend a built-in type in cython?

How can I extend a built-in type in cython? Question: I’m trying to extend the basic float in python with cython additional methods. I have a python implementation and I know I could create my own extended type by keeping an internal float value. But I’m trying to keep the same code base for interpreted …

Total answers: 1