1414
1515import textwrap
1616from test import support
17+ from test .support import import_helper
18+ from test .support import os_helper
1719from test .support .script_helper import (
1820 make_pkg , make_script , make_zip_pkg , make_zip_script ,
1921 assert_python_ok , assert_python_failure , spawn_python , kill_python )
20- from test .support import os_helper , import_helper
2122
2223verbose = support .verbose
2324
@@ -228,6 +229,21 @@ def test_basic_script(self):
228229 with os_helper .temp_dir () as script_dir :
229230 script_name = _make_test_script (script_dir , 'script' )
230231 self ._check_script (script_name , script_name , script_name ,
232+ script_dir , None ,
233+ importlib .machinery .SourceFileLoader ,
234+ expected_cwd = script_dir )
235+
236+ # TODO: RUSTPYTHON
237+ @unittest .expectedFailure
238+ def test_script_abspath (self ):
239+ # pass the script using the relative path, expect the absolute path
240+ # in __file__
241+ with os_helper .temp_cwd () as script_dir :
242+ self .assertTrue (os .path .isabs (script_dir ), script_dir )
243+
244+ script_name = _make_test_script (script_dir , 'script' )
245+ relative_name = os .path .basename (script_name )
246+ self ._check_script (relative_name , script_name , relative_name ,
231247 script_dir , None ,
232248 importlib .machinery .SourceFileLoader )
233249
@@ -406,7 +422,7 @@ def test_issue8202_dash_c_file_ignored(self):
406422 # does not alter the value of sys.path[0]
407423 with os_helper .temp_dir () as script_dir :
408424 with os_helper .change_cwd (path = script_dir ):
409- with open ("-c" , "w" ) as f :
425+ with open ("-c" , "w" , encoding = "utf-8" ) as f :
410426 f .write ("data" )
411427 rc , out , err = assert_python_ok ('-c' ,
412428 'import sys; print("sys.path[0]==%r" % sys.path[0])' ,
@@ -422,7 +438,7 @@ def test_issue8202_dash_m_file_ignored(self):
422438 with os_helper .temp_dir () as script_dir :
423439 script_name = _make_test_script (script_dir , 'other' )
424440 with os_helper .change_cwd (path = script_dir ):
425- with open ("-m" , "w" ) as f :
441+ with open ("-m" , "w" , encoding = "utf-8" ) as f :
426442 f .write ("data" )
427443 rc , out , err = assert_python_ok ('-m' , 'other' , * example_args ,
428444 __isolated = False )
@@ -435,7 +451,7 @@ def test_issue20884(self):
435451 # will be failed.
436452 with os_helper .temp_dir () as script_dir :
437453 script_name = os .path .join (script_dir , "issue20884.py" )
438- with open (script_name , "w" , newline = '\n ' ) as f :
454+ with open (script_name , "w" , encoding = "latin1" , newline = '\n ' ) as f :
439455 f .write ("#coding: iso-8859-1\n " )
440456 f .write ('"""\n ' )
441457 for _ in range (30 ):
@@ -507,6 +523,18 @@ def test_dash_m_bad_pyc(self):
507523 self .assertNotIn (b'is a package' , err )
508524 self .assertNotIn (b'Traceback' , err )
509525
526+ # TODO: RUSTPYTHON
527+ @unittest .expectedFailure
528+ def test_hint_when_triying_to_import_a_py_file (self ):
529+ with os_helper .temp_dir () as script_dir , \
530+ os_helper .change_cwd (path = script_dir ):
531+ # Create invalid *.pyc as empty file
532+ with open ('asyncio.py' , 'wb' ):
533+ pass
534+ err = self .check_dash_m_failure ('asyncio.py' )
535+ self .assertIn (b"Try using 'asyncio' instead "
536+ b"of 'asyncio.py' as the module name" , err )
537+
510538 def test_dash_m_init_traceback (self ):
511539 # These were wrapped in an ImportError and tracebacks were
512540 # suppressed; see Issue 14285
@@ -546,7 +574,7 @@ def test_pep_409_verbiage(self):
546574 script_name = _make_test_script (script_dir , 'script' , script )
547575 exitcode , stdout , stderr = assert_python_failure (script_name )
548576 text = stderr .decode ('ascii' ).split ('\n ' )
549- self .assertEqual (len (text ), 4 )
577+ self .assertEqual (len (text ), 5 )
550578 self .assertTrue (text [0 ].startswith ('Traceback' ))
551579 self .assertTrue (text [1 ].startswith (' File ' ))
552580 self .assertTrue (text [3 ].startswith ('NameError' ))
@@ -565,7 +593,7 @@ def test_non_ascii(self):
565593
566594 # Issue #16218
567595 source = 'print(ascii(__file__))\n '
568- script_name = _make_test_script (os .curdir , name , source )
596+ script_name = _make_test_script (os .getcwd () , name , source )
569597 self .addCleanup (os_helper .unlink , script_name )
570598 rc , stdout , stderr = assert_python_ok (script_name )
571599 self .assertEqual (
@@ -578,8 +606,6 @@ def test_non_ascii(self):
578606 if sys .platform == "linux" :
579607 test_non_ascii = unittest .expectedFailure (test_non_ascii )
580608
581- # TODO: RUSTPYTHON
582- @unittest .expectedFailure
583609 def test_issue20500_exit_with_exception_value (self ):
584610 script = textwrap .dedent ("""\
585611 import sys
@@ -596,7 +622,7 @@ def test_issue20500_exit_with_exception_value(self):
596622 script_name = _make_test_script (script_dir , 'script' , script )
597623 exitcode , stdout , stderr = assert_python_failure (script_name )
598624 text = stderr .decode ('ascii' )
599- self .assertEqual (text , "some text" )
625+ self .assertEqual (text . rstrip () , "some text" )
600626
601627 # TODO: RUSTPYTHON
602628 @unittest .expectedFailure
@@ -606,8 +632,8 @@ def test_syntaxerror_unindented_caret_position(self):
606632 script_name = _make_test_script (script_dir , 'script' , script )
607633 exitcode , stdout , stderr = assert_python_failure (script_name )
608634 text = io .TextIOWrapper (io .BytesIO (stderr ), 'ascii' ).read ()
609- # Confirm that the caret is located under the first 1 character
610- self .assertIn ("\n 1 + 1 = 2 \n ^ " , text )
635+ # Confirm that the caret is located under the '=' sign
636+ self .assertIn ("\n ^^^^^ \n " , text )
611637
612638 # TODO: RUSTPYTHON
613639 @unittest .expectedFailure
@@ -620,8 +646,8 @@ def test_syntaxerror_indented_caret_position(self):
620646 script_name = _make_test_script (script_dir , 'script' , script )
621647 exitcode , stdout , stderr = assert_python_failure (script_name )
622648 text = io .TextIOWrapper (io .BytesIO (stderr ), 'ascii' ).read ()
623- # Confirm that the caret is located under the first 1 character
624- self .assertIn ("\n 1 + 1 = 2\n ^" , text )
649+ # Confirm that the caret starts under the first 1 character
650+ self .assertIn ("\n 1 + 1 = 2\n ^^^^^ \n " , text )
625651
626652 # Try the same with a form feed at the start of the indented line
627653 script = (
@@ -632,7 +658,7 @@ def test_syntaxerror_indented_caret_position(self):
632658 exitcode , stdout , stderr = assert_python_failure (script_name )
633659 text = io .TextIOWrapper (io .BytesIO (stderr ), "ascii" ).read ()
634660 self .assertNotIn ("\f " , text )
635- self .assertIn ("\n 1 + 1 = 2\n ^" , text )
661+ self .assertIn ("\n 1 + 1 = 2\n ^^^^^ \n " , text )
636662
637663 # TODO: RUSTPYTHON
638664 @unittest .expectedFailure
@@ -644,7 +670,7 @@ def test_syntaxerror_multi_line_fstring(self):
644670 self .assertEqual (
645671 stderr .splitlines ()[- 3 :],
646672 [
647- b' foo = f """{} ' ,
673+ b' foo"""' ,
648674 b' ^' ,
649675 b'SyntaxError: f-string: empty expression not allowed' ,
650676 ],
@@ -653,18 +679,17 @@ def test_syntaxerror_multi_line_fstring(self):
653679 # TODO: RUSTPYTHON
654680 @unittest .expectedFailure
655681 def test_syntaxerror_invalid_escape_sequence_multi_line (self ):
656- script = 'foo = """\\ q\n """\n '
682+ script = 'foo = """\\ q"""\n '
657683 with os_helper .temp_dir () as script_dir :
658684 script_name = _make_test_script (script_dir , 'script' , script )
659685 exitcode , stdout , stderr = assert_python_failure (
660686 '-Werror' , script_name ,
661687 )
662688 self .assertEqual (
663689 stderr .splitlines ()[- 3 :],
664- [
665- b' foo = """\\ q' ,
666- b' ^' ,
667- b'SyntaxError: invalid escape sequence \\ q' ,
690+ [ b' foo = """\\ q"""' ,
691+ b' ^^^^^^^^' ,
692+ b'SyntaxError: invalid escape sequence \' \\ q\' '
668693 ],
669694 )
670695
@@ -743,7 +768,7 @@ def test_consistent_sys_path_for_module_execution(self):
743768 def test_nonexisting_script (self ):
744769 # bpo-34783: "./python script.py" must not crash
745770 # if the script file doesn't exist.
746- # (Skip test for macOS framework builds because sys.excutable name
771+ # (Skip test for macOS framework builds because sys.executable name
747772 # is not the actual Python executable file name.
748773 script = 'nonexistingscript.py'
749774 self .assertFalse (os .path .exists (script ))
@@ -755,10 +780,14 @@ def test_nonexisting_script(self):
755780 self .assertIn (": can't open file " , err )
756781 self .assertNotEqual (proc .returncode , 0 )
757782
758-
783+ # TODO: RUSTPYTHON
784+ # def tearDownModule():
759785def test_main ():
760786 support .run_unittest (CmdLineTest )
761787 support .reap_children ()
762788
789+
763790if __name__ == '__main__' :
791+ # TODO: RUSTPYTHON
792+ # unittest.main()
764793 test_main ()
0 commit comments