Mercurial > hg
annotate contrib/check-py3-compat.py @ 50810:5c3d07950bac
transaction: actually delete file created during the transaction on rollback
Transaction currently has two modes:
- one where file created during the transaction are deleted on rollback,
- one where file created during the transaction are truncated to 0 on rollback.
Before this change, `hg rollback` and `hg recover` are using the "delete" mode
and transaction abort is using the "truncate" option. This difference is never
really explained. A long time ago, there was two code paths, with this
divergence existing for unclear reasons. When the two code paths got merged into
a single one, a boolean argument have been added to preserve this divergence,
mostly probably as a cargo cult.
The divergence is weird and induce bad surprises, and the truncate behavior is a
bit odds, introducing other bad surprises (e.g. 08ecbdba186f)
So solve this, we stop using the "truncate" behavior and unify on the "delete"
behavior. Despite being currently more "common", the truncate behavior seems
less natural, resulting in the transaction leaving empty file around.
This is landed on default, early in the cycle, to help us catch problems that
could emerge.
author | Pierre-Yves David <pierre-yves.david@octobus.net> |
---|---|
date | Mon, 24 Jul 2023 05:13:52 +0200 |
parents | 13dfad0f9f7a |
children |
rev | line source |
---|---|
45830
c102b704edb5
global: use python3 in shebangs
Gregory Szorc <gregory.szorc@gmail.com>
parents:
45217
diff
changeset
|
1 #!/usr/bin/env python3 |
27279
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
2 # |
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
3 # check-py3-compat - check Python 3 compatibility of Mercurial files |
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
4 # |
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
5 # Copyright 2015 Gregory Szorc <gregory.szorc@gmail.com> |
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
6 # |
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
7 # This software may be used and distributed according to the terms of the |
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
8 # GNU General Public License version 2 or any later version. |
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
9 |
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
10 |
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
11 import ast |
32280
9b81fb217820
py3: remove delayed import of importlib
Gregory Szorc <gregory.szorc@gmail.com>
parents:
32212
diff
changeset
|
12 import importlib |
28584
d69172ddfdca
tests: try to import modules with Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28583
diff
changeset
|
13 import os |
27279
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
14 import sys |
28584
d69172ddfdca
tests: try to import modules with Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28583
diff
changeset
|
15 import traceback |
41555
ba7eaff26474
check-py3-compat: manually format and print warnings
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41554
diff
changeset
|
16 import warnings |
27279
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
17 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41555
diff
changeset
|
18 |
28583
260ce2eed951
tests: perform an ast parse with Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28475
diff
changeset
|
19 def check_compat_py3(f): |
260ce2eed951
tests: perform an ast parse with Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28475
diff
changeset
|
20 """Check Python 3 compatibility of a file with Python 3.""" |
260ce2eed951
tests: perform an ast parse with Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28475
diff
changeset
|
21 with open(f, 'rb') as fh: |
260ce2eed951
tests: perform an ast parse with Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28475
diff
changeset
|
22 content = fh.read() |
260ce2eed951
tests: perform an ast parse with Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28475
diff
changeset
|
23 |
260ce2eed951
tests: perform an ast parse with Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28475
diff
changeset
|
24 try: |
41554
01417ca7f2e2
check-py3-compat: provide filename to ast.parse()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
32421
diff
changeset
|
25 ast.parse(content, filename=f) |
28583
260ce2eed951
tests: perform an ast parse with Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28475
diff
changeset
|
26 except SyntaxError as e: |
260ce2eed951
tests: perform an ast parse with Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28475
diff
changeset
|
27 print('%s: invalid syntax: %s' % (f, e)) |
260ce2eed951
tests: perform an ast parse with Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28475
diff
changeset
|
28 return |
260ce2eed951
tests: perform an ast parse with Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28475
diff
changeset
|
29 |
28584
d69172ddfdca
tests: try to import modules with Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28583
diff
changeset
|
30 # Try to import the module. |
32421
778dc37ce683
check: check modules in hgdemandimport
Siddharth Agarwal <sid0@fb.com>
parents:
32374
diff
changeset
|
31 # For now we only support modules in packages because figuring out module |
778dc37ce683
check: check modules in hgdemandimport
Siddharth Agarwal <sid0@fb.com>
parents:
32374
diff
changeset
|
32 # paths for things not in a package can be confusing. |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41555
diff
changeset
|
33 if f.startswith( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41555
diff
changeset
|
34 ('hgdemandimport/', 'hgext/', 'mercurial/') |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41555
diff
changeset
|
35 ) and not f.endswith('__init__.py'): |
28584
d69172ddfdca
tests: try to import modules with Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28583
diff
changeset
|
36 assert f.endswith('.py') |
32212
65cd7e705ff6
policy: eliminate ".pure." from module name only if marked as dual
Yuya Nishihara <yuya@tcha.org>
parents:
30117
diff
changeset
|
37 name = f.replace('/', '.')[:-3] |
30095
e8aeeb28e35e
py3: remove superfluous indent from check-py3-compat.py
Yuya Nishihara <yuya@tcha.org>
parents:
30094
diff
changeset
|
38 try: |
e8aeeb28e35e
py3: remove superfluous indent from check-py3-compat.py
Yuya Nishihara <yuya@tcha.org>
parents:
30094
diff
changeset
|
39 importlib.import_module(name) |
e8aeeb28e35e
py3: remove superfluous indent from check-py3-compat.py
Yuya Nishihara <yuya@tcha.org>
parents:
30094
diff
changeset
|
40 except Exception as e: |
e8aeeb28e35e
py3: remove superfluous indent from check-py3-compat.py
Yuya Nishihara <yuya@tcha.org>
parents:
30094
diff
changeset
|
41 exc_type, exc_value, tb = sys.exc_info() |
e8aeeb28e35e
py3: remove superfluous indent from check-py3-compat.py
Yuya Nishihara <yuya@tcha.org>
parents:
30094
diff
changeset
|
42 # We walk the stack and ignore frames from our custom importer, |
e8aeeb28e35e
py3: remove superfluous indent from check-py3-compat.py
Yuya Nishihara <yuya@tcha.org>
parents:
30094
diff
changeset
|
43 # import mechanisms, and stdlib modules. This kinda/sorta |
e8aeeb28e35e
py3: remove superfluous indent from check-py3-compat.py
Yuya Nishihara <yuya@tcha.org>
parents:
30094
diff
changeset
|
44 # emulates CPython behavior in import.c while also attempting |
e8aeeb28e35e
py3: remove superfluous indent from check-py3-compat.py
Yuya Nishihara <yuya@tcha.org>
parents:
30094
diff
changeset
|
45 # to pin blame on a Mercurial file. |
e8aeeb28e35e
py3: remove superfluous indent from check-py3-compat.py
Yuya Nishihara <yuya@tcha.org>
parents:
30094
diff
changeset
|
46 for frame in reversed(traceback.extract_tb(tb)): |
e8aeeb28e35e
py3: remove superfluous indent from check-py3-compat.py
Yuya Nishihara <yuya@tcha.org>
parents:
30094
diff
changeset
|
47 if frame.name == '_call_with_frames_removed': |
e8aeeb28e35e
py3: remove superfluous indent from check-py3-compat.py
Yuya Nishihara <yuya@tcha.org>
parents:
30094
diff
changeset
|
48 continue |
e8aeeb28e35e
py3: remove superfluous indent from check-py3-compat.py
Yuya Nishihara <yuya@tcha.org>
parents:
30094
diff
changeset
|
49 if 'importlib' in frame.filename: |
e8aeeb28e35e
py3: remove superfluous indent from check-py3-compat.py
Yuya Nishihara <yuya@tcha.org>
parents:
30094
diff
changeset
|
50 continue |
e8aeeb28e35e
py3: remove superfluous indent from check-py3-compat.py
Yuya Nishihara <yuya@tcha.org>
parents:
30094
diff
changeset
|
51 if 'mercurial/__init__.py' in frame.filename: |
e8aeeb28e35e
py3: remove superfluous indent from check-py3-compat.py
Yuya Nishihara <yuya@tcha.org>
parents:
30094
diff
changeset
|
52 continue |
e8aeeb28e35e
py3: remove superfluous indent from check-py3-compat.py
Yuya Nishihara <yuya@tcha.org>
parents:
30094
diff
changeset
|
53 if frame.filename.startswith(sys.prefix): |
e8aeeb28e35e
py3: remove superfluous indent from check-py3-compat.py
Yuya Nishihara <yuya@tcha.org>
parents:
30094
diff
changeset
|
54 continue |
e8aeeb28e35e
py3: remove superfluous indent from check-py3-compat.py
Yuya Nishihara <yuya@tcha.org>
parents:
30094
diff
changeset
|
55 break |
28584
d69172ddfdca
tests: try to import modules with Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28583
diff
changeset
|
56 |
30095
e8aeeb28e35e
py3: remove superfluous indent from check-py3-compat.py
Yuya Nishihara <yuya@tcha.org>
parents:
30094
diff
changeset
|
57 if frame.filename: |
e8aeeb28e35e
py3: remove superfluous indent from check-py3-compat.py
Yuya Nishihara <yuya@tcha.org>
parents:
30094
diff
changeset
|
58 filename = os.path.basename(frame.filename) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41555
diff
changeset
|
59 print( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41555
diff
changeset
|
60 '%s: error importing: <%s> %s (error at %s:%d)' |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41555
diff
changeset
|
61 % (f, type(e).__name__, e, filename, frame.lineno) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41555
diff
changeset
|
62 ) |
30095
e8aeeb28e35e
py3: remove superfluous indent from check-py3-compat.py
Yuya Nishihara <yuya@tcha.org>
parents:
30094
diff
changeset
|
63 else: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41555
diff
changeset
|
64 print( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41555
diff
changeset
|
65 '%s: error importing module: <%s> %s (line %d)' |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41555
diff
changeset
|
66 % (f, type(e).__name__, e, frame.lineno) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41555
diff
changeset
|
67 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41555
diff
changeset
|
68 |
28584
d69172ddfdca
tests: try to import modules with Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28583
diff
changeset
|
69 |
27279
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
70 if __name__ == '__main__': |
48872
968b29a5a7fc
check-py3-compat: drop support for Python 2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
45830
diff
changeset
|
71 # check_compat_py3 will import every filename we specify as long as it |
968b29a5a7fc
check-py3-compat: drop support for Python 2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
45830
diff
changeset
|
72 # starts with one of a few prefixes. It does this by converting |
968b29a5a7fc
check-py3-compat: drop support for Python 2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
45830
diff
changeset
|
73 # specified filenames like 'mercurial/foo.py' to 'mercurial.foo' and |
968b29a5a7fc
check-py3-compat: drop support for Python 2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
45830
diff
changeset
|
74 # importing that. When running standalone (not as part of a test), this |
968b29a5a7fc
check-py3-compat: drop support for Python 2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
45830
diff
changeset
|
75 # means we actually import the installed versions, not the files we just |
968b29a5a7fc
check-py3-compat: drop support for Python 2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
45830
diff
changeset
|
76 # specified. When running as test-check-py3-compat.t, we technically |
968b29a5a7fc
check-py3-compat: drop support for Python 2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
45830
diff
changeset
|
77 # would import the correct paths, but it's cleaner to have both cases |
968b29a5a7fc
check-py3-compat: drop support for Python 2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
45830
diff
changeset
|
78 # use the same import logic. |
49217 | 79 sys.path.insert(0, os.getcwd()) |
28583
260ce2eed951
tests: perform an ast parse with Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28475
diff
changeset
|
80 |
27279
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
81 for f in sys.argv[1:]: |
41555
ba7eaff26474
check-py3-compat: manually format and print warnings
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41554
diff
changeset
|
82 with warnings.catch_warnings(record=True) as warns: |
48872
968b29a5a7fc
check-py3-compat: drop support for Python 2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
45830
diff
changeset
|
83 check_compat_py3(f) |
41555
ba7eaff26474
check-py3-compat: manually format and print warnings
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41554
diff
changeset
|
84 |
ba7eaff26474
check-py3-compat: manually format and print warnings
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41554
diff
changeset
|
85 for w in warns: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41555
diff
changeset
|
86 print( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41555
diff
changeset
|
87 warnings.formatwarning( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41555
diff
changeset
|
88 w.message, w.category, w.filename, w.lineno |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41555
diff
changeset
|
89 ).rstrip() |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41555
diff
changeset
|
90 ) |
27279
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
91 |
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
92 sys.exit(0) |