Skip to content

bpo-35802: Clean up code which checked presence of os.{stat,lstat,chmod} #11643

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

Merged
merged 1 commit into from
Feb 25, 2019

Conversation

asottile
Copy link
Contributor

@asottile asottile commented Jan 21, 2019

@pablogsal
Copy link
Member

pablogsal commented Jan 21, 2019

Hummmm, I do not feel very comfortable removing these checks. Autotools actually is checking at least for lstat:

https://github.com/python/cpython/blob/master/configure.ac#L3505

@asottile
Copy link
Contributor Author

Hummmm, I do not feel very comfortable removing these checks. Autotools actually is checking at least for lstat:

https://github.com/python/cpython/blob/master/configure.ac#L3505

Unless I'm reading incorrectly, it is unconditionally defined in the os module:

#define OS_LSTAT_METHODDEF \
{"lstat", (PyCFunction)(void(*)(void))os_lstat, METH_FASTCALL|METH_KEYWORDS, os_lstat__doc__},

The actual implementation chooses whether to use lstat or not and does the platform-specific dances:

cpython/Modules/posixmodule.c

Lines 2123 to 2170 in 7a23680

static PyObject *
posix_do_stat(const char *function_name, path_t *path,
int dir_fd, int follow_symlinks)
{
STRUCT_STAT st;
int result;
#if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT)
if (follow_symlinks_specified(function_name, follow_symlinks))
return NULL;
#endif
if (path_and_dir_fd_invalid("stat", path, dir_fd) ||
dir_fd_and_fd_invalid("stat", dir_fd, path->fd) ||
fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks))
return NULL;
Py_BEGIN_ALLOW_THREADS
if (path->fd != -1)
result = FSTAT(path->fd, &st);
#ifdef MS_WINDOWS
else if (follow_symlinks)
result = win32_stat(path->wide, &st);
else
result = win32_lstat(path->wide, &st);
#else
else
#if defined(HAVE_LSTAT)
if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
result = LSTAT(path->narrow, &st);
else
#endif /* HAVE_LSTAT */
#ifdef HAVE_FSTATAT
if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks)
result = fstatat(dir_fd, path->narrow, &st,
follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
else
#endif /* HAVE_FSTATAT */
result = STAT(path->narrow, &st);
#endif /* MS_WINDOWS */
Py_END_ALLOW_THREADS
if (result != 0) {
return path_error(path);
}
return _pystat_fromstructstat(&st);
}

@asottile
Copy link
Contributor Author

the ifdefs are a bit tricky but if lstat / fstatat are unavailable then os.lstat falls back to os.stat

@pablogsal
Copy link
Member

pablogsal commented Jan 21, 2019

Note that the os module falls back outside posix if the posixmodule is not compiled :

https://github.com/python/cpython/blob/master/Lib/os.py#L68

Windows systems (nt) usually will have chmod and lstat and friends, but I am not sure if they can be missing for some reason.

Nervermind, the nt module is actually posixmodule.

@asottile
Copy link
Contributor Author

On windows, the nt module is created from the very same posixmodule.c (yes it's confusing 😭):

/* This file is also used for Windows NT/MS-Win. In that case the
module actually calls itself 'nt', not 'posix', and a few

@pablogsal
Copy link
Member

pablogsal commented Jan 21, 2019

Apparently, there is nothing missing in the logic that leads to clean up the code as this PR is doing, but I would like Gregory to take a look in case I am missing something :)

@asottile
Copy link
Contributor Author

@giampaolo @gpshead friendly ping :)

Copy link
Contributor

@giampaolo giampaolo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM (good change).

@asottile
Copy link
Contributor Author

@gpshead @giampaolo anything left for me to do here?

@giampaolo
Copy link
Contributor

It was already approved. You can merge.

@asottile
Copy link
Contributor Author

I am a mere pleb without green-button access <3

@giampaolo giampaolo merged commit 8377cd4 into python:master Feb 25, 2019
@bedevere-bot
Copy link

@giampaolo: Please replace # with GH- in the commit message next time. Thanks!

@asottile asottile deleted the os_stat_lstat_bop_35802 branch February 25, 2019 22:33
@giampaolo
Copy link
Contributor

Sorry, my bad.

@bedevere-bot
Copy link

⚠️⚠️⚠️ Buildbot failure ⚠️⚠️⚠️

Hi! The buildbot x86 Gentoo Non-Debug with X 3.x has failed when building commit 8377cd4.

What do you need to do:

  1. Don't panic.
  2. Check the buildbot page in the devguide if you don't know what the buildbots are or how they work.
  3. Go to the page of the buildbot that failed (https://buildbot.python.org/all/#builders/99/builds/2198) and take a look at the build logs.
  4. Check if the failure is related to this commit (8377cd4) or if it is a false positive.
  5. If the failure is related to this commit, please, reflect that on the issue and make a new Pull Request with a fix.

You can take a look at the buildbot page here:

https://buildbot.python.org/all/#builders/99/builds/2198

Click to see traceback logs
Traceback (most recent call last):
  File "/buildbot/buildarea/cpython/3.x.ware-gentoo-x86.nondebug/build/Lib/test/test_descr.py", line 4341, in test_vicious_descriptor_nonsense
    self.assertEqual(c.attr, 1)
  File "/buildbot/buildarea/cpython/3.x.ware-gentoo-x86.nondebug/build/Lib/test/test_descr.py", line 4328, in __eq__
    del C.attr
AttributeError: attr

----------------------------------------------------------------------

Ran 140 tests in 2.449s

FAILED (errors=1, skipped=2, expected failures=2)


Traceback (most recent call last):
  File "/buildbot/buildarea/cpython/3.x.ware-gentoo-x86.nondebug/build/Lib/test/test_descr.py", line 4341, in test_vicious_descriptor_nonsense
    self.assertEqual(c.attr, 1)
  File "/buildbot/buildarea/cpython/3.x.ware-gentoo-x86.nondebug/build/Lib/test/test_descr.py", line 4328, in __eq__
    del C.attr
AttributeError: attr

----------------------------------------------------------------------

Ran 140 tests in 2.229s

FAILED (errors=1, skipped=2, expected failures=2)

vstinner added a commit that referenced this pull request Apr 26, 2019
* Clean up code which checked presence of os.{stat,lstat,chmod} (GH-11643)

(cherry picked from commit 8377cd4)

* bpo-36725: regrtest: add TestResult type (GH-12960)

* Add TestResult and MultiprocessResult types to ensure that results
  always have the same fields.
* runtest() now handles KeyboardInterrupt
* accumulate_result() and format_test_result() now takes a TestResult
* cleanup_test_droppings() is now called by runtest() and mark the
  test as ENV_CHANGED if the test leaks support.TESTFN file.
* runtest() now includes code "around" the test in the test timing
* Add print_warning() in test.libregrtest.utils to standardize how
  libregrtest logs warnings to ease parsing the test output.
* support.unload() is now called with abstest rather than test_name
* Rename 'test' variable/parameter to 'test_name'
* dash_R(): remove unused the_module parameter
* Remove unused imports

(cherry picked from commit 4d29983)

* bpo-36725: Refactor regrtest multiprocessing code (GH-12961)

Rewrite run_tests_multiprocess() function as a new MultiprocessRunner
class with multiple methods to better report errors and stop
immediately when needed.

Changes:

* Worker processes are now killed immediately if tests are
  interrupted or if a test does crash (CHILD_ERROR): worker
  processes are killed.
* Rewrite how errors in a worker thread are reported to
  the main thread. No longer ignore BaseException or parsing errors
  silently.
* Remove 'finished' variable: use worker.is_alive() instead
* Always compute omitted tests. Add Regrtest.get_executed() method.

(cherry picked from commit 3cde440)

* bpo-36719: regrtest always detect uncollectable objects (GH-12951)

regrtest now always detects uncollectable objects. Previously, the
check was only enabled by --findleaks. The check now also works with
-jN/--multiprocess N.

--findleaks becomes a deprecated alias to --fail-env-changed.

(cherry picked from commit 75120d2)

* bpo-34060: Report system load when running test suite for Windows (GH-8357)

While Windows exposes the system processor queue length, the raw value
used for load calculations on Unix systems, it does not provide an API
to access the averaged value. Hence to calculate the load we must track
and average it ourselves. We can't use multiprocessing or a thread to
read it in the background while the tests run since using those would
conflict with test_multiprocessing and test_xxsubprocess.

Thus, we use Window's asynchronous IO API to run the tracker in the
background with it sampling at the correct rate. When we wish to access
the load we check to see if there's new data on the stream, if there is,
we update our load values.


(cherry picked from commit e16467a)

* bpo-36719: Fix regrtest re-run (GH-12964)

Properly handle a test which fail but then pass.

Add test_rerun_success() unit test.

(cherry picked from commit 837acc1)

* bpo-36719: regrtest closes explicitly WindowsLoadTracker (GH-12965)

Regrtest.finalize() now closes explicitly the WindowsLoadTracker
instance.

(cherry picked from commit 00db7c7)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants