Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

posix.stat.struct_stat has st_birthtime which doesn't exist on Linux #3024

Open
JonathonReinhart opened this issue Jun 29, 2019 · 0 comments

Comments

Projects
None yet
1 participant
@JonathonReinhart
Copy link

commented Jun 29, 2019

Problem

posix.stat.struct_stat has an st_birthtime member which doesn't exist in struct stat from <sys/stat.h>:

        # st_birthtime exists on *BSD and OS X.
        # Under Linux, defining it here does not hurt. Compilation under Linux
        # will only (and rightfully) fail when attempting to use the field.
        time_t  st_birthtime

Even though I'm not (directly) using the field, it still causes problems:

poc.pyx

from posix.stat cimport struct_stat

cdef example():
    cdef struct_stat st
    # populate some members...
    return st

setup.py

from distutils.core import setup, Extension
from Cython.Build import cythonize

setup(
    ext_modules = cythonize([Extension("poc", ["poc.pyx"])])
)

Build output

$ python3 setup.py build_ext
Compiling poc.pyx because it changed.
[1/1] Cythonizing poc.pyx
running build_ext
building 'poc' extension
creating build
creating build/temp.linux-x86_64-3.6
gcc -pthread -Wno-unused-result -Wsign-compare -DDYNAMIC_ANNOTATIONS_ENABLED=1 -DNDEBUG -O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -D_GNU_SOURCE -fPIC -fwrapv -fPIC -I/usr/include/python3.6m -c poc.c -o build/temp.linux-x86_64-3.6/poc.o
poc.c: In function ‘__pyx_convert__to_py_struct__stat’:
poc.c:2036:38: error: ‘struct stat’ has no member named ‘st_birthtime’; did you mean ‘st_blksize’?
   member = __Pyx_PyInt_From_time_t(s.st_birthtime); if (unlikely(!member)) goto bad;
                                      ^~~~~~~~~~~~
                                      st_blksize
At top level:
poc.c:1039:18: warning: ‘__pyx_f_3poc_example’ defined but not used [-Wunused-function]
 static PyObject *__pyx_f_3poc_example(void) {
                  ^~~~~~~~~~~~~~~~~~~~
error: command 'gcc' failed with exit status 1

Generated code snippet

It looks the problem (for me) is that returning a cdef struct from a function with no declared return type wants to create a Python object and populate all members from the C structure (even if, in my case, they do not exist):

static PyObject* __pyx_convert__to_py_struct__stat(struct stat s) { 
  PyObject* res; 
  PyObject* member;
  res = __Pyx_PyDict_NewPresized(14); if (unlikely(!res)) return NULL;

  // ...

  member = __Pyx_PyInt_From_time_t(s.st_birthtime); if (unlikely(!member)) goto bad; 
  if (unlikely(PyDict_SetItem(res, __pyx_n_s_st_birthtime, member) < 0)) goto bad; 
  Py_DECREF(member);
  return res; 
  bad: 
  Py_XDECREF(member);
  Py_DECREF(res);
  return NULL;
}

Version Information

$ cython --version
Cython version 0.28.6

$ gcc --version
gcc (GCC) 8.3.1 20190223 (Red Hat 8.3.1-2)
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ /usr/lib64/libc.so.6
GNU C Library (GNU libc) stable release version 2.27.
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
Compiled by GNU CC version 8.3.1 20190223 (Red Hat 8.3.1-2).
libc ABIs: UNIQUE IFUNC
For bug reporting instructions, please see:
<http://www.gnu.org/software/libc/bugs.html>.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.