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 8837925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:51:16+00:00 2026-06-14T09:51:16+00:00

I am currently attempting to port some code between my linux machine (Ubuntu 12.04.1

  • 0

I am currently attempting to port some code between my linux machine (Ubuntu 12.04.1 LTS) and my new Mac (OS X 10.7.4) and I have come across some confusing behavior when using python’s ctypes module to access the C standard library on the Mac.

To illustrate the problem, the following is a minimal example:

import ctypes as C
import numpy as np

libc = C.CDLL("/usr/lib/libc.dylib")   #/usr/lib/libc.so.6 on ubuntu

np.arange(10,dtype="ubyte").tofile("test.bin") # create some test data

buffer_array = np.empty(10,dtype="ubyte") # create a reading buffer

buffer_array_c = np.ctypeslib.as_ctypes(buffer_array) # get the ctypes version of the buffer 

c_file = libc.fopen("test.bin","r") # open the file through libc   

libc.fread(buffer_array_c, 1, 10, c_file) # read from the file

libc.fclose(c_file)

print "Desired output:"
print np.fromfile("test.bin",dtype="ubyte")
print
print "Actual output:"
print buffer_array

On Linux, this works as expected, producing the following:

Desired output:
[0 1 2 3 4 5 6 7 8 9]

Actual output:
[0 1 2 3 4 5 6 7 8 9]

On the Mac however, I just get `Segmentation fault: 11′.

I have experimented with this a bit, swapping out the fopen call with:

py_file = open("test.bin","r")

c_file = C.pythonapi.PyFile_AsFile(C.py_object(py_file))

Which also works on Linux but not on Mac.

I think the problem is coming from calling fread using c_file, as if I write a minimal C function to open the file then call fread using the previously allocated buffer, the code performs as expected.

I am not normally a Mac user, so the problem may be obvious, but any help would be very useful.

For reference, I am using:

Python 2.7.3, Numpy 1.4.0 and Ctypes 1.1.0

Edit:

To give this some context, I am experimenting with fast methods to read very large binary files (~40-200 GB) into python, piece by piece. As a commenter points out below, there is not really any performance increase to be had from directly accessing the standard library fread and fwrite functions. This is true, but I am confused as to why. If I were to use numpy.fromfile to read a large file in chunks, wouldn’t I be creating a new memory allocation with each read?

Solution:

The problem seems to stem from the 64bit/32bit difference in storage of file handles.
The solution is simply to explicitly set the restype and argtypes of each c function prior to use.

i.e on a 64-bit machine we put this after the C.CDLL call:

lib.fopen.restype = C.c_long
lib.fread.argtypes = [C.c_void_p, C.c_size_t, C.c_size_t, C.c_long]
lib.fclose.argtypes = [C.c_long]

While on a 32-bit machine:

lib.fopen.restype = C.c_int
lib.fread.argtypes = [C.c_void_p, C.c_size_t, C.c_size_t, C.c_int]
lib.fclose.argtypes = [C.c_int]
  • 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-06-14T09:51:17+00:00Added an answer on June 14, 2026 at 9:51 am

    Are you trying on a 32-bit Ubuntu versus a 64-bit OS/X? I think the issue is that your version of libc.fopen() returns a C “int”, which is almost always a 32-bit value — but the real fopen() returns a pointer. So on a 64-bit operating system, the c_file that you get is truncated to a 32-bit integer. On a 32-bit operating system, it works anyway because the 32-bit integer can be passed back to the fread() and fclose(), which will interpret it again as a pointer. To fix it, you need to declare the restype of libc.fopen().

    (I can only recommend CFFI as an alternative to ctypes with saner defaults, but of course I’m partial there, being one of the authors 🙂

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

Sidebar

Related Questions

Currently attempting to get some code to compile using Lejos for the Lego NXT
Currently I have some code deleting some temporary files created by my program: #
I am attempting a quick prototype-port of some old database code over to use
I'm currently attempting to code one for part of a college project - binary/hex
I currently have a deployed app (fortworth.herokuapp.com) that I am attempting to sort movies
I am currently attempting to remove a number of .Resolve(s) in our code. I
I have a web application that's currently running under Tomcat 5.5.25. I'm attempting to
I'm currently attempting to track down the source of heap corruption in our code
I have an existing website which I am attempting to port over to rails
I have some code that extracts a bunch of data and stores the result

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.