|
14 | 14 |
|
15 | 15 | import threading |
16 | 16 | import pytest |
| 17 | +import tempfile |
| 18 | +import os |
17 | 19 |
|
18 | 20 | import typed_python |
19 | | -from typed_python.test_util import evaluateExprInFreshProcess |
| 21 | +from typed_python.test_util import evaluateExprInFreshProcess, callFunctionInFreshProcess |
20 | 22 | from typed_python import ( |
21 | 23 | UInt64, UInt32, |
22 | 24 | ListOf, TupleOf, Tuple, NamedTuple, Dict, OneOf, Forward, identityHash, |
@@ -699,3 +701,86 @@ def f(self): |
699 | 701 | return 0 |
700 | 702 |
|
701 | 703 | print(typeWalkRecord(N)) |
| 704 | + |
| 705 | + |
| 706 | +def test_module_hash_magic_value(): |
| 707 | + with tempfile.TemporaryDirectory() as tempDir: |
| 708 | + |
| 709 | + def makeFun(mh): |
| 710 | + """Produce a dummy function with __module_hash__ of 'mh' |
| 711 | +
|
| 712 | + Note that we need to produce this function with 'backing code' so that |
| 713 | + the AST system can find it and serialize it. |
| 714 | + """ |
| 715 | + globalsDict = {} |
| 716 | + fname = os.path.join(tempDir, "code_" + mh + ".py") |
| 717 | + |
| 718 | + pyCode = ( |
| 719 | + f"from typed_python import Function\n" |
| 720 | + f"__module_hash__ = '{mh}'\n" |
| 721 | + f"@Function\n" |
| 722 | + f"def f(x):\n" |
| 723 | + f" return x\n" |
| 724 | + ) |
| 725 | + |
| 726 | + with open(fname, "w") as f: |
| 727 | + f.write(pyCode) |
| 728 | + |
| 729 | + exec(compile(pyCode, fname, "exec"), globalsDict) |
| 730 | + return globalsDict['f'] |
| 731 | + |
| 732 | + def makeFunIH(mh): |
| 733 | + return identityHash(type(makeFun(mh))) |
| 734 | + |
| 735 | + # check that the identity hash depends on the module hash |
| 736 | + assert identityHash(type(makeFun('A'))) == identityHash(type(makeFun('A'))) |
| 737 | + assert identityHash(type(makeFun('A'))) != identityHash(type(makeFun('B'))) |
| 738 | + |
| 739 | + # functions should have this in their globals |
| 740 | + f = makeFun('A') |
| 741 | + assert '__module_hash__' in f.overloads[0].functionGlobals |
| 742 | + assert '__module_hash__' in f.overloads[0].realizedGlobals |
| 743 | + |
| 744 | + # even if we execute this in another process, we should get the same function back |
| 745 | + # and it should have a __module_hash__ in its globals. We have to be careful about |
| 746 | + # this because we need the serializer to understand that __module_hash__ is special |
| 747 | + # and that the function implicitly references it. |
| 748 | + fFromOtherProcess = callFunctionInFreshProcess(makeFun, ('C',)) |
| 749 | + assert fFromOtherProcess.overloads[0].functionGlobals['__module_hash__'] == 'C' |
| 750 | + assert fFromOtherProcess.overloads[0].realizedGlobals['__module_hash__'] == 'C' |
| 751 | + |
| 752 | + # check that the identity hash we loaded is the same one we would get from a |
| 753 | + # subprocess reading it. |
| 754 | + assert ( |
| 755 | + identityHash(type(fFromOtherProcess)) |
| 756 | + == callFunctionInFreshProcess(makeFunIH, ('C',)) |
| 757 | + ) |
| 758 | + |
| 759 | + |
| 760 | +def test_module_hash_magic_value_on_untyped_function_preserved_by_serialization(): |
| 761 | + with tempfile.TemporaryDirectory() as tempDir: |
| 762 | + |
| 763 | + def makeFun(mh): |
| 764 | + """Produce a dummy function with __module_hash__ of 'mh' |
| 765 | +
|
| 766 | + Note that we need to produce this function with 'backing code' so that |
| 767 | + the AST system can find it and serialize it. |
| 768 | + """ |
| 769 | + globalsDict = {} |
| 770 | + fname = os.path.join(tempDir, "code_" + mh + ".py") |
| 771 | + |
| 772 | + pyCode = ( |
| 773 | + f"from typed_python import Entrypoint\n" |
| 774 | + f"__module_hash__ = '{mh}'\n" |
| 775 | + f"def f(x):\n" |
| 776 | + f" return x\n" |
| 777 | + ) |
| 778 | + |
| 779 | + with open(fname, "w") as f: |
| 780 | + f.write(pyCode) |
| 781 | + |
| 782 | + exec(compile(pyCode, fname, "exec"), globalsDict) |
| 783 | + return globalsDict['f'] |
| 784 | + |
| 785 | + fFromOtherProcess = callFunctionInFreshProcess(makeFun, ('C',)) |
| 786 | + assert fFromOtherProcess.__globals__['__module_hash__'] == 'C' |
0 commit comments