Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6849475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:59:44+00:00 2026-05-27T00:59:44+00:00

I am trying to write a module with some c and some python parts.

  • 0

I am trying to write a module with some c and some python parts. I am using cython to bridge the gap.

I want to store my (very long) string constants in python, because of the much nicer syntax:

const char long_string = "\npart of string\n"
  "next part\n"
  "last part\n";

versus:

long_string = """
part of string
next part
last part
"""

(the strings are much longer than this, and more complicated – to the extent that I don’t want to have to add and remove the "s and \n"s every time I want to edit it with syntax highlighting. In fact, they are openCL kernels.)

I need to be able to turn these into c strings using cython, and according to the documentation I should just need this:

cdef bytes py_bytes = py_string.encode()
cdef char* c_string = py_bytes

and with no manual memory management, c_string will work as long as I keep a reference to py_bytes.

However, I can’t get this working with a simple printf test. Here is my cython file:

cdef extern from "stdio.h":
  printf(char* string)

def go():
  py_string = """
a complicated string
with a few
newlines.
"""

  cdef bytes py_bytes = py_string.encode()

  cdef char* c_string = py_bytes

  printf(c_string)

  print "we don't get this far :("

which, when compiled at runtime using pyximport gives the following output to terminal before segfaulting:

a complicated string
with a few
newlines.
Segmentation fault: 11

now, I’ve checked what cython actually puts in the c file, and tried it in a vanilla C file where it doesn’t segfault:

#include "stdio.h"

static char __pyx_k_1[] = "\na complicated string\nwith a few\nnewlines.\n";

int main(void) {
  void* output = printf(__pyx_k_1);
  if (!output) {
    printf("apparently, !output.");
  }
}

to be clear, cython generates code which catches the output of printf and tests for “not that”. the type of the variable is a PyObject*.

My only guess here was that the string was improperly terminated, so printf just carries on past the end of it and causes the segfault, but since that doesn’t happen in my pure c test, I’m completely stumped.

So, my actual question is how do I really pass a c-string to c code from cython? Answers pointing out an easier way to solve the actual problem I’m trying to solve at the top are also very welcome 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T00:59:45+00:00Added an answer on May 27, 2026 at 12:59 am

    Importing printf from libc.stdio fixes the problem for me:

    from libc.stdio cimport printf
    
    def go():
        py_string = """
    a complicated string
    with a few
    newlines.
    """
    
        cdef bytes py_bytes = py_string.encode()
        cdef char* c_string = py_bytes
        printf(c_string)
    
        print "we actually got this far! :)"
    

    The error is in the declaration of printf. That should have been, as stdio.pxd lists,

    cdef extern from *:
        ctypedef char const_char "const char"
    
    int printf(const_char *, ...)
    

    whereas your version is implicitly object printf(char *); the default return value type is Python object rather than int as in C. Getting the right declaration turns off Cython’s attempt to Py_XDECREF the return value from printf.

    (Btw, in your “vanilla” C problem, you shouldn’t be casting the return value from printf to void *.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

currently I am trying to write to an 20x4 LCD Dot-Matrix Module using Perle's
I am trying to write a command but I do not want one long
I'm trying to write some unittests using trial for my twisted application, I wrote
I'm in the early stages of trying to write some sensible Javascript. I want
I'm trying to write a kernel module, which prints some information about the objects
I am trying to write a generic module to extend the World class. I
I am trying to write a MSBuild logger module which logs information when receiving
I'm trying to write a unit test for a module that will give me
I am trying to write a small app that uses the subprocess module. My
I'm trying to write some Erlang that would filter an array in the form:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.