-
-
Notifications
You must be signed in to change notification settings - Fork 12.4k
Expand file tree
/
Copy pathverify_c_api_version.py
More file actions
66 lines (51 loc) · 2.03 KB
/
verify_c_api_version.py
File metadata and controls
66 lines (51 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3
import argparse
import os
import sys
class MismatchCAPIError(ValueError):
pass
def get_api_versions(apiversion):
"""
Return current C API checksum and the recorded checksum.
Return current C API checksum and the recorded checksum for the given
version of the C API version.
"""
# Compute the hash of the current API as defined in the .txt files in
# code_generators
sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))
try:
m = __import__('genapi')
numpy_api = __import__('numpy_api')
curapi_hash = m.fullapi_hash(numpy_api.full_api)
apis_hash = m.get_versions_hash()
finally:
del sys.path[0]
return curapi_hash, apis_hash[apiversion]
def check_api_version(apiversion):
"""Emits a MismatchCAPIWarning if the C API version needs updating."""
curapi_hash, api_hash = get_api_versions(apiversion)
# If different hash, it means that the api .txt files in
# codegen_dir have been updated without the API version being
# updated. Any modification in those .txt files should be reflected
# in the api and eventually abi versions.
# To compute the checksum of the current API, use numpy/_core/cversions.py
if not curapi_hash == api_hash:
msg = ("API mismatch detected, the C API version "
"numbers have to be updated. Current C api version is "
f"{apiversion}, with checksum {curapi_hash}, but recorded "
f"checksum in _core/codegen_dir/cversions.txt is {api_hash}. "
"If functions were added in the C API, you have to update "
f"C_API_VERSION in numpy/core/meson.build."
)
raise MismatchCAPIError(msg)
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"--api-version",
type=str,
help="C API version to verify (as a hex string)"
)
args = parser.parse_args()
check_api_version(int(args.api_version, base=16))
if __name__ == "__main__":
main()