title: Pass python 3 open file object to C function as FILE *
body: <p>I want to pass my file to C function.
  I have problem with this. I saw &quot;Segmentation fault&quot;
  My C code:</p>
  <pre><code>// cc -fPIC -shared -o w.so w.c
  
  #include &lt;stdio.h&gt;
  
  void my_writer(FILE *fp, int num) {
      fprintf(fp, &quot;%d\n&quot;, num);
  }
  </code></pre>
  <p>My python code:</p>
  <pre><code>#!/usr/bin/python3
  #
  
  from ctypes import *
  
  so_file = &quot;./w.so&quot;
  my_c_writer = CDLL(so_file)
  
  my_num = 2022
  
  try:
      convert_file = pythonapi.PyObject_AsFileDescriptor
      convert_file.argtypes = [py_object]
      convert_file.restype = c_int
  
      # fp = open('path').fp     there is no .fp attribute!
      fp = open('num.txt', 'w')
      c_file = convert_file(fp)
      my_c_writer.my_writer(c_file, c_int(my_num))
      fp.close()
  except Exception as e:
      print(e)
  
  </code></pre>
  <p>I searched a lot but I found nothing.
  The C code file is unchangeable.
  I would very appreciate if you help me.</p>