@@ -758,6 +758,126 @@ def test_other_objects(self):
758758 ])
759759
760760
761+ class PureShareableScriptTests (_GetXIDataTests ):
762+
763+ MODE = 'script-pure'
764+
765+ VALID_SCRIPTS = [
766+ '' ,
767+ 'spam' ,
768+ '# a comment' ,
769+ 'print("spam")' ,
770+ 'raise Exception("spam")' ,
771+ """if True:
772+ do_something()
773+ """ ,
774+ """if True:
775+ def spam(x):
776+ return x
777+ class Spam:
778+ def eggs(self):
779+ return 42
780+ x = Spam().eggs()
781+ raise ValueError(spam(x))
782+ """ ,
783+ ]
784+ INVALID_SCRIPTS = [
785+ ' pass' , # IndentationError
786+ '----' , # SyntaxError
787+ """if True:
788+ def spam():
789+ # no body
790+ spam()
791+ """ , # IndentationError
792+ ]
793+
794+ def test_valid_str (self ):
795+ self .assert_roundtrip_not_equal ([
796+ * self .VALID_SCRIPTS ,
797+ ], expecttype = types .CodeType )
798+
799+ def test_invalid_str (self ):
800+ self .assert_not_shareable ([
801+ * self .INVALID_SCRIPTS ,
802+ ])
803+
804+ def test_valid_bytes (self ):
805+ self .assert_roundtrip_not_equal ([
806+ * (s .encode ('utf8' ) for s in self .VALID_SCRIPTS ),
807+ ], expecttype = types .CodeType )
808+
809+ def test_invalid_bytes (self ):
810+ self .assert_not_shareable ([
811+ * (s .encode ('utf8' ) for s in self .INVALID_SCRIPTS ),
812+ ])
813+
814+ def test_pure_script_code (self ):
815+ self .assert_roundtrip_equal_not_identical ([
816+ * (f .__code__ for f in defs .PURE_SCRIPT_FUNCTIONS ),
817+ ])
818+
819+ def test_impure_script_code (self ):
820+ self .assert_not_shareable ([
821+ * (f .__code__ for f in defs .SCRIPT_FUNCTIONS
822+ if f not in defs .PURE_SCRIPT_FUNCTIONS ),
823+ ])
824+
825+ def test_other_code (self ):
826+ self .assert_not_shareable ([
827+ * (f .__code__ for f in defs .FUNCTIONS
828+ if f not in defs .SCRIPT_FUNCTIONS ),
829+ * (f .__code__ for f in defs .FUNCTION_LIKE ),
830+ ])
831+
832+ def test_pure_script_function (self ):
833+ self .assert_roundtrip_not_equal ([
834+ * defs .PURE_SCRIPT_FUNCTIONS ,
835+ ], expecttype = types .CodeType )
836+
837+ def test_impure_script_function (self ):
838+ self .assert_not_shareable ([
839+ * (f for f in defs .SCRIPT_FUNCTIONS
840+ if f not in defs .PURE_SCRIPT_FUNCTIONS ),
841+ ])
842+
843+ def test_other_function (self ):
844+ self .assert_not_shareable ([
845+ * (f for f in defs .FUNCTIONS
846+ if f not in defs .SCRIPT_FUNCTIONS ),
847+ * defs .FUNCTION_LIKE ,
848+ ])
849+
850+ def test_other_objects (self ):
851+ self .assert_not_shareable ([
852+ None ,
853+ True ,
854+ False ,
855+ Ellipsis ,
856+ NotImplemented ,
857+ (),
858+ [],
859+ {},
860+ object (),
861+ ])
862+
863+
864+ class ShareableScriptTests (PureShareableScriptTests ):
865+
866+ MODE = 'script'
867+
868+ def test_impure_script_code (self ):
869+ self .assert_roundtrip_equal_not_identical ([
870+ * (f .__code__ for f in defs .SCRIPT_FUNCTIONS
871+ if f not in defs .PURE_SCRIPT_FUNCTIONS ),
872+ ])
873+
874+ def test_impure_script_function (self ):
875+ self .assert_roundtrip_not_equal ([
876+ * (f for f in defs .SCRIPT_FUNCTIONS
877+ if f not in defs .PURE_SCRIPT_FUNCTIONS ),
878+ ], expecttype = types .CodeType )
879+
880+
761881class ShareableTypeTests (_GetXIDataTests ):
762882
763883 MODE = 'xidata'
0 commit comments