Thanks to visit codestin.com
Credit goes to github.com

Skip to content

bpo-40058: Fix failed test_divide_and_round of test_datetime when run more than … #19213

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

Closed
wants to merge 1 commit into from

Conversation

shihai1991
Copy link
Member

@shihai1991 shihai1991 commented Mar 29, 2020

Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

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

LGTM. I tested manually that this change fix https://bugs.python.org/issue40058

@pganssle: Do you want to double check? Or can I merge the change?

@pganssle
Copy link
Member

pganssle commented Mar 29, 2020

@vstinner @shihai1991 I don't think we can rely on "the tests pass" or "the problem in bpo-40058 is fixed" to determine whether or not this is a good change, because you're removing code that is designed to ensure that datetime.datetime is in a clean state after the import trickery happens.

The other XXX comment hints at a potential problem:

# XXX(gb) First run all the _Pure tests, then all the _Fast tests.  You might
# not believe this, but in spite of all the sys.modules trickery running a _Pure
# test last will leave a mix of pure and native datetime stuff lying around.

I think the questions to answer before we can remove this (per Chesterton's Fence) are:

  1. Why was it deemed necessary to do this in the first place?
  2. Has anything changed that would invalidate the reasoning for #⁠1?
  3. Why was it having any effect at all (considering it's intended to keep the sys.modules cache clean, in which case subsequent import statements should just pull in these modules again)?

@shihai1991
Copy link
Member Author

shihai1991 commented Mar 29, 2020

because you're removing code that is designed to ensure that datetime.datetime is in a clean state after the import trickery happens.

To be honest, I don't know the purpose of clean state of sys.modules :(

@vstinner
Copy link
Member

vstinner commented Mar 30, 2020

@vstinner @shihai1991 I don't think we can rely on "the tests pass" or "the problem in bpo-40058 is fixed" to determine whether or not this is a good change, because you're removing code that is designed to ensure that datetime.datetime is in a clean state after the import trickery happens.

import_fresh_module() saves/restores datetime, _datetime and _strptime modules of sys.modules. I don't understand what do you mean by "clean state"? The PR removes:

    # XXX: import_fresh_module() is supposed to leave sys.module cache untouched,
    # XXX: but it does not, so we have to cleanup ourselves.

I expect that import_fresh_module() works as expected. If it's not the case, it should be fixed. But test_datetime must not workaround bugs, if there are bugs.

@pganssle
Copy link
Member

@vstinner I agree that the final solution to this problem should be to fix the underlying problem and remove the workarounds, but we can't remove the workarounds until we know what they are working around - that is essentially tantamount to breaking the tests - and what's worse, we don't even know how we're breaking them - the tests still pass, but they may be testing something other than they are expected to test.

Again this is a perfect Chesterton's Fence situation: I am eager to remove the "fence", but only once I know why it's here.

@shihai1991
Copy link
Member Author

Hm, I should know the Historical reason:
the original patch is: https://bugs.python.org/file18155/issue7989e.diff
in cf86e36(after this patch merged in):

>>> import sys
>>> from test.support import import_fresh_module, run_unittest
>>> saved_sys_modules = sys.modules.copy()
>>> len(sys.modules)
97
>>> pure_tests = import_fresh_module(TESTS, fresh=['datetime', '_strptime'], blocked=['_datetime'])
>>> fast_tests = import_fresh_module(TESTS, fresh=['datetime', '_datetime', '_strptime'])
>>> len(sys.modules)
103
>>> for module in sys.modules:
...     if module not in saved_sys_modules:
...         print(module)
...
datetime
test.datetimetester
calendar
math
_datetime
_strptime

so it increased 6 modules in sys.modules(in master, it would be more).
so there is another question is: datetime、_datetime、striptime should be removed after calling
import_fresh_module()?

@shihai1991
Copy link
Member Author

victor's #20472 will fix this bug, so I close this PR.
Thanks, folks.

@shihai1991 shihai1991 closed this Jun 26, 2020
@pganssle
Copy link
Member

pganssle commented Jun 26, 2020

I think #20472 incidentally fixes this issue. If anyone can figure out why this stuff was added in the first place, there still might be good reason to remove it. TBH, there might be even more reason to remove it after #20472, because the sys.modules modifications are almost certainly related to the fact that import datetime creates a bunch of stuff and then deletes it later, which is no longer happening AFAICT.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting merge skip news tests Tests in the Lib/test dir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants