Skip to content

test: Do not write Python bytecode to source directory - #30533

Closed
hebasto wants to merge 1 commit into
bitcoin:masterfrom
hebasto:240726-rpcauth-ro
Closed

hebasto wants to merge 1 commit into
bitcoin:masterfrom
hebasto:240726-rpcauth-ro

Conversation

@hebasto

@hebasto hebasto commented Jul 26, 2024 •

Copy link
Copy Markdown
Member

This change prevents writing share/rpcauth/__pycache__/*.pyc, which is especially useful for out-of-source builds when the source directory is supposed to be read-only.

Also see #19385 (comment).

I believe that performance concerns can be disregarded in this case.

@DrahtBot

DrahtBot commented Jul 26, 2024 •

Copy link
Copy Markdown
Contributor

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Code Coverage

For detailed information about the code coverage, see the test coverage report.

Reviews

See the guideline for information on the review process.

Type Reviewers
Concept ACK stickies-v
Stale ACK tdb3, BrandonOdiwuor

If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

@DrahtBot DrahtBot added the Tests label Jul 26, 2024

@tdb3 tdb3 left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ACK bce6387
Tested on master (share/rpcauth/__pycache__ was generated when running rpcauth-test.py) and on the PR branch (share/rpcauth/__pycache__ was not generated).

Maybe it's doing this for contrib/devtools/test-security-check.py and test-symbol-check.py as well?

@BrandonOdiwuor BrandonOdiwuor left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Tested ACK bce6387

Screenshot from 2024-07-30 18-23-24

@hebasto

hebasto commented Aug 7, 2024

Copy link
Copy Markdown
Member Author

Friendly ping @stickies-v @maflcko :)

@maflcko

maflcko commented Aug 7, 2024

Copy link
Copy Markdown
Member

CI is green and the diff looks reasonable, so I guess it is harmless and fine.

Did you try setting the source dir read-only, and it passes with this change?

@hebasto

hebasto commented Aug 7, 2024 •

Copy link
Copy Markdown
Member Author

Did you try setting the source dir read-only, and it passes with this change?

Yes, it does. However, such a test is not very useful because Python just silently skips writing the bytecode cache if there are no write permissions.

A more useful test can be conducted on a branch, which performs a full migration to CMake and cleans up all .gitignore files from Autotools remnants. Running ctest keeps the source tree pristine.

@stickies-v stickies-v left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Concept ACK, the performance benefits of caching bytecode are irrelevant here so keeping a clean source dir seems preferable.

As an alternative approach, might it make more sense to use pycache_prefix so we can address this concern globally instead of just for this file, and in the build system code instead of leaking this into non-build system code? And that way we can keep the (in this case negligible) performance benefits of caching bytecode?

Comment thread test/util/rpcauth-test.py
with open(config_path, encoding="utf8") as config_file:
config.read_file(config_file)
sys.path.insert(0, os.path.dirname(config['environment']['RPCAUTH']))
sys.dont_write_bytecode = True

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should also update

rm -rf test/functional/__pycache__ test/functional/test_framework/__pycache__ test/cache share/rpcauth/__pycache__

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks! Updated.

@hebasto

hebasto commented Aug 7, 2024

Copy link
Copy Markdown
Member Author

As an alternative approach, might it make more sense to use pycache_prefix so we can address this concern globally instead of just for this file, and in the build system code instead of leaking this into non-build system code? And that way we can keep the (in this case negligible) performance benefits of caching bytecode?

I did consider it. I have to admit that I failed to find a solution that touches only the build system code and works in all scenarios, including running functional tests individually.

This change prevents writing `share/rpcauth/__pycache__/*.pyc`, which is
especially useful for out-of-source builds when the source directory is
supposed to be read-only.
@hebasto
hebasto force-pushed the 240726-rpcauth-ro branch from bce6387 to c91a10b Compare August 7, 2024 10:43
@hebasto

hebasto commented Aug 7, 2024

Copy link
Copy Markdown
Member Author

Addressed @stickies-v's #30533 (comment).

@stickies-v

stickies-v commented Aug 7, 2024 •

Copy link
Copy Markdown
Contributor

I failed to find a solution that touches only the build system code and works in all scenarios, including running functional tests individually.

I'm also not sure that's something we should be doing? Python programs creating pycache files is expected Python behaviour, and we can't control if/how/when users run Python programs. Users who wish to do so can easily avoid this caching behaviour in a multitude of ways, the shortest of which is running python -B rpcauth-test.py or setting env vars. I think we should strive to make our build system behave as well as we can, and e.g. make check cluttering up the source dir seems like undesirable and not something the user can control, and thus we should address.

In that, vein, I think my preferred approach for rpcauth-test.py would be to just update Makefile.test.include to something like this (but using some build dir instead of tmp):

git diff on c91a10b
diff --git a/src/Makefile.test.include b/src/Makefile.test.include
index 3d04498369..43c4d25ed7 100644
--- a/src/Makefile.test.include
+++ b/src/Makefile.test.include
@@ -438,7 +438,7 @@ if BUILD_BITCOIN_TX
 	$(PYTHON) $(top_builddir)/test/util/test_runner.py
 endif
 	@echo "Running test/util/rpcauth-test.py..."
-	$(PYTHON) $(top_builddir)/test/util/rpcauth-test.py
+	$(PYTHON) -X pycache_prefix=/tmp/pycache/ $(top_builddir)/test/util/rpcauth-test.py
 if TARGET_WINDOWS
 else
 if ENABLE_BENCH
diff --git a/test/util/rpcauth-test.py b/test/util/rpcauth-test.py
index 931eb80960..8a7ff26dcb 100755
--- a/test/util/rpcauth-test.py
+++ b/test/util/rpcauth-test.py
@@ -21,7 +21,6 @@ class TestRPCAuth(unittest.TestCase):
         with open(config_path, encoding="utf8") as config_file:
             config.read_file(config_file)
         sys.path.insert(0, os.path.dirname(config['environment']['RPCAUTH']))
-        sys.dont_write_bytecode = True
         self.rpcauth = importlib.import_module('rpcauth')
 
     def test_generate_salt(self):

All that said, this is a small change and I don't want to block progress. It's a small and reasonable diff, and easily reverted/changed in the future. I suppose it's more of a philosophical concern.

@hebasto

hebasto commented Aug 15, 2024

Copy link
Copy Markdown
Member Author

Closing until the migration to CMake is complete.

@vasild vasild left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ACK c91a10b in case this is reconsidered.

IMO this approach is better than leaving it as it is.

@bitcoin bitcoin locked and limited conversation to collaborators Aug 16, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

No open projects
Status: CMake follow-ups

Development

Successfully merging this pull request may close these issues.

7 participants