This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Python/import.c still references fstat even with DONT_HAVE_FSTAT/!HAVE_FSTAT
Type: compile error Stage: resolved
Components: Build Versions: Python 3.2, Python 3.3, Python 3.4, Python 2.7
process
Status: closed Resolution: wont fix
Dependencies: Superseder: Drop HAVE_FSTAT: require fstat() to compile/use Python
View: 23753
Assigned To: Nosy List: brett.cannon, chromatix, eric.snow, giampaolo.rodola, joshtriplett, ncoghlan, petri.lehtinen, pitrou, vstinner
Priority: normal Keywords:

Created on 2011-05-15 23:45 by joshtriplett, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (16)
msg136055 - (view) Author: Josh Triplett (joshtriplett) Date: 2011-05-15 23:45
Even if pyconfig.h defines DONT_HAVE_STAT and DONT_HAVE_FSTAT (which prevents the definitions of HAVE_STAT and HAVE_FSTAT), Python still references fstat in Python/import.c, along with struct stat and constants like S_IXUSR.  I ran into this when attempting to compile Python for an embedded platform, which has some basic file operations but does not have stat.  (I will likely end up faking fstat for now, but I'd rather not have to do so.)
msg136065 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2011-05-16 03:28
Oh, so it's possible to not have the fstat() syscall. I asked me the question when I modified import.c (and other files related to importing a module).
msg136937 - (view) Author: Petri Lehtinen (petri.lehtinen) * (Python committer) Date: 2011-05-26 10:54
Should the .pyc/.pyo file writing be disabled altogether if stat() and/or fstat() is not available. In this case, there's no way to check mtimes, right?
msg136943 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2011-05-26 11:29
Does your filesystem store the modification time? Why don't you have/want fstat()? Do you have stat() or a similar function?
msg137059 - (view) Author: Josh Triplett (joshtriplett) Date: 2011-05-27 15:21
GRUB's filesystem drivers don't support reading mtime.  And no, no form of stat() function exists, f or otherwise.

On a related note, without HAVE_STAT, import.c can't import package modules at all, since it uses stat to check in advance for a directory.  In the spirit of Python's usual "try it and see if it works" approach, why not just try opening foo/__init__.py, and if that doesn't work try opening foo.py?
msg137296 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2011-05-30 15:38
> Should the .pyc/.pyo file writing be disabled altogether
> if stat() and/or fstat() is not available.

If we cannot get the file modification time, .pyc/.pyo writing must be disabled. If your OS/libc/filesystem doesn't have fstat(), you don't have file modification, so no .pyc/.pyo write.
msg137319 - (view) Author: Petri Lehtinen (petri.lehtinen) * (Python committer) Date: 2011-05-30 18:50
STINNER Victor wrote:
> If we cannot get the file modification time, .pyc/.pyo writing must be disabled. If your OS/libc/filesystem doesn't have fstat(), you don't have file modification, so no .pyc/.pyo write.

Should I go on and write a patch? Any hints on how this code path (of
not writing .pyc/.pyo because of missing stat() and fstat()) could be
tested?
msg137323 - (view) Author: Josh Triplett (joshtriplett) Date: 2011-05-30 20:09
Given that GRUB doesn't support writing to filesystems at all, I already have to set Py_DontWriteBytecodeFlag, so disabling .pyc/.pyo entirely would work fine for my use case.
msg137324 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2011-05-30 20:12
> Should I go on and write a patch?

Yes please, write a patch, I will review it.

To emulate a system without stat or fstat, you may use:
#define fstat dont_have_fstat
#define stat dont_have_stat

The link will fail if the code still refer to fstat() or stat().
msg137358 - (view) Author: Petri Lehtinen (petri.lehtinen) * (Python committer) Date: 2011-05-31 11:52
I tried to compile Python 3.3 (from default branch) with DONT_HAVE_FSTAT and DONT_HAVE_STAT. It seems to depend even more heavily on stat() being available, in other files than Python/import.c.

With 2.7, it was quite easy to disable the .pyc/.pyo writing in the absence of fstat(), but the NullImporter needs to be able to check for a directory. In addition, the build process fails when trying to run "setup.py build", because distutils cannot be imported. And it cannot be imported because it's a package, and packages (directories) cannot be detected without having stat().

So... Unless we have another way to check for a directory, I'm not sure whether it will be possible to compile Python at all without stat().
msg137364 - (view) Author: Josh Triplett (joshtriplett) Date: 2011-05-31 15:23
Rather than checking for a directory, how about just opening foo/__init__.py, and if that fails opening foo.py?
msg137365 - (view) Author: Petri Lehtinen (petri.lehtinen) * (Python committer) Date: 2011-05-31 15:30
The NullImporter is documented to raise ImportError on directories, not directories containing __init__.py:

    http://docs.python.org/library/imp.html#imp.NullImporter

Checking for __init__.py{,c,o} seems doable to me without having stat().
msg175269 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2012-11-10 09:43
My vote is for won't fix. Systems without stat() or fstat() are clearly broken from Python's point of view; at the very least, they aren't POSIX-compliant.
msg175418 - (view) Author: Petri Lehtinen (petri.lehtinen) * (Python committer) Date: 2012-11-12 06:42
Should the defines be removed then, because they're kind of false promises for the user.
msg175464 - (view) Author: Chromatix (chromatix) Date: 2012-11-12 18:47
Actually many people try to compile python on an environment with freestanding C library, so this can be a specific case of that. Is there any issue or thread where compiling python with freestanding headers is discussed? As this relates to that.

Shall you remove the defines, please put instructions in the source code to let user know how they can provide their self-invented fstat function to the code.
msg189582 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2013-05-19 12:21
For those of you who are concerned with this issue, could you explain your use case on the following discussion thread: http://mail.python.org/pipermail/python-dev/2013-May/126285.html ?

I would personally like to remove HAVE_FSTAT and make Python unconditionally use fstat(). It will make the code quite simpler in some places.
History
Date User Action Args
2022-04-11 14:57:17adminsetgithub: 56291
2015-04-09 13:20:48r.david.murraysetstatus: open -> closed
superseder: Drop HAVE_FSTAT: require fstat() to compile/use Python
stage: resolved
2013-05-19 12:21:34pitrousetmessages: + msg189582
2012-11-12 18:47:49chromatixsetmessages: + msg175464
2012-11-12 06:42:14petri.lehtinensetmessages: + msg175418
2012-11-10 09:43:22pitrousetresolution: wont fix

messages: + msg175269
nosy: + pitrou
2012-11-10 05:30:30eric.snowsetversions: + Python 3.4, - Python 3.1
2011-12-16 05:22:39eric.snowsetnosy: + eric.snow
2011-12-15 11:28:32chromatixsetnosy: + chromatix
2011-12-14 19:31:31giampaolo.rodolasetnosy: + giampaolo.rodola
2011-05-31 15:30:15petri.lehtinensetmessages: + msg137365
2011-05-31 15:23:11joshtriplettsetmessages: + msg137364
2011-05-31 11:52:24petri.lehtinensetmessages: + msg137358
2011-05-30 20:12:28vstinnersetmessages: + msg137324
2011-05-30 20:09:18joshtriplettsetmessages: + msg137323
2011-05-30 18:50:42petri.lehtinensetmessages: + msg137319
2011-05-30 15:38:43vstinnersetmessages: + msg137296
2011-05-27 15:21:03joshtriplettsetmessages: + msg137059
2011-05-26 11:29:46vstinnersetmessages: + msg136943
2011-05-26 10:54:35petri.lehtinensetnosy: + brett.cannon, ncoghlan, petri.lehtinen

messages: + msg136937
versions: + Python 3.1, Python 3.2, Python 3.3
2011-05-16 03:28:00vstinnersetnosy: + vstinner
messages: + msg136065
2011-05-15 23:45:18joshtriplettcreate