|
45 | 45 | Dict, Set, SerializationContext, EmbeddedMessage, |
46 | 46 | serializeStream, deserializeStream, decodeSerializedObject, |
47 | 47 | Forward, Final, Function, Entrypoint, TypeFunction, PointerTo, |
48 | | - SubclassOf |
| 48 | + SubclassOf, NotCompiled |
49 | 49 | ) |
50 | 50 |
|
51 | 51 | from typed_python._types import ( |
52 | 52 | refcount, isRecursive, identityHash, buildPyFunctionObject, |
53 | | - setFunctionClosure, typesAreEquivalent, recursiveTypeGroupDeepRepr |
| 53 | + setFunctionClosure, typesAreEquivalent, recursiveTypeGroupDeepRepr, |
54 | 54 | ) |
55 | 55 |
|
56 | 56 | module_level_testfun = dummy_test_module.testfunction |
@@ -2735,6 +2735,61 @@ def test_serialization_independent_of_whether_function_is_hashed(self): |
2735 | 2735 |
|
2736 | 2736 | assert s1 == s2 |
2737 | 2737 |
|
| 2738 | + def test_serialization_has_no_filename_reference(self): |
| 2739 | + def makeF(modulename): |
| 2740 | + with tempfile.TemporaryDirectory() as tempdir: |
| 2741 | + path = os.path.join(tempdir, modulename + ".py") |
| 2742 | + |
| 2743 | + CONTENTS = ( |
| 2744 | + "def f(x):\n" |
| 2745 | + " return x\n" |
| 2746 | + ) |
| 2747 | + |
| 2748 | + with open(path, "w") as f: |
| 2749 | + f.write(CONTENTS) |
| 2750 | + |
| 2751 | + globals = {'__file__': path} |
| 2752 | + |
| 2753 | + exec( |
| 2754 | + compile(CONTENTS, path, "exec"), |
| 2755 | + globals |
| 2756 | + ) |
| 2757 | + |
| 2758 | + s = SerializationContext() |
| 2759 | + return s.serialize(globals['f']) |
| 2760 | + |
| 2761 | + f1 = SerializationContext().deserialize(callFunctionInFreshProcess(makeF, ('asdf',))) |
| 2762 | + f2 = SerializationContext().deserialize(callFunctionInFreshProcess(makeF, ('asdf2',))) |
| 2763 | + |
| 2764 | + def checkSame(f1, f2): |
| 2765 | + s = SerializationContext().withoutCompression() |
| 2766 | + s2 = SerializationContext().withoutCompression().withoutLineInfoEncoded().withSerializeHashSequence() |
| 2767 | + |
| 2768 | + assert s.serialize(f1) != s.serialize(f2) |
| 2769 | + |
| 2770 | + if s2.serialize(f1) != s2.serialize(f2): |
| 2771 | + decoded1 = decodeSerializedObject(s2.serialize(f1)) |
| 2772 | + decoded2 = decodeSerializedObject(s2.serialize(f2)) |
| 2773 | + |
| 2774 | + decoded1Print = pprint.PrettyPrinter(indent=2).pformat(decoded1).split("\n") |
| 2775 | + decoded2Print = pprint.PrettyPrinter(indent=2).pformat(decoded2).split("\n") |
| 2776 | + |
| 2777 | + for i in range(len(decoded1Print)): |
| 2778 | + if decoded1Print[i] != decoded2Print[i]: |
| 2779 | + for j in range(max(0, i-5), i): |
| 2780 | + print(decoded1Print[j]) |
| 2781 | + print("******************************** DIFFERENCE *******************") |
| 2782 | + print(decoded1Print[i]) |
| 2783 | + print(decoded2Print[i]) |
| 2784 | + print("***************************************************************") |
| 2785 | + break |
| 2786 | + |
| 2787 | + assert s2.serialize(f1) == s2.serialize(f2) |
| 2788 | + |
| 2789 | + checkSame(f1, f2) |
| 2790 | + checkSame(Entrypoint(f1), Entrypoint(f2)) |
| 2791 | + checkSame(NotCompiled(f1), NotCompiled(f2)) |
| 2792 | + |
2738 | 2793 | def test_serialize_anonymous_class_with_defaults_and_nonempty(self): |
2739 | 2794 | class C1(Class): |
2740 | 2795 | x1 = Member(int, default_value=10, nonempty=True) |
@@ -2821,3 +2876,43 @@ def test_serialization_context_names_for_pmap_functions(self): |
2821 | 2876 | from typed_python.lib.pmap import ensureThreads |
2822 | 2877 | sc = SerializationContext() |
2823 | 2878 | assert sc.nameForObject(type(ensureThreads)) is not None |
| 2879 | + |
| 2880 | + def test_pmap_of_notcompiled_serialized_externally(self): |
| 2881 | + def makeC(): |
| 2882 | + with tempfile.TemporaryDirectory() as tempdir: |
| 2883 | + path = os.path.join(tempdir, "asdf.py") |
| 2884 | + |
| 2885 | + CONTENTS = ( |
| 2886 | + "from typed_python import NotCompiled\n" |
| 2887 | + "@NotCompiled\n" |
| 2888 | + "def f(x) -> str:\n" |
| 2889 | + " return str(x)\n" |
| 2890 | + ) |
| 2891 | + |
| 2892 | + with open(path, "w") as f: |
| 2893 | + f.write(CONTENTS) |
| 2894 | + |
| 2895 | + globals = {'__file__': path} |
| 2896 | + |
| 2897 | + exec( |
| 2898 | + compile(CONTENTS, path, "exec"), |
| 2899 | + globals |
| 2900 | + ) |
| 2901 | + |
| 2902 | + s = SerializationContext() |
| 2903 | + return s.serialize(globals['f']) |
| 2904 | + |
| 2905 | + serializedF = callFunctionInFreshProcess(makeC, ()) |
| 2906 | + |
| 2907 | + f = SerializationContext().deserialize(serializedF) |
| 2908 | + |
| 2909 | + assert f(2) == "2" |
| 2910 | + |
| 2911 | + from typed_python.lib.pmap import pmap |
| 2912 | + |
| 2913 | + args = ListOf(int)(range(1000000)) |
| 2914 | + |
| 2915 | + t0 = time.time() |
| 2916 | + while time.time() - t0 < 10.0: |
| 2917 | + print("DO!") |
| 2918 | + pmap(args, f, str, minGranularity=10000) |
0 commit comments