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,19 @@ 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+ def test_script_abspath (self ):
237+ # pass the script using the relative path, expect the absolute path
238+ # in __file__
239+ with os_helper .temp_cwd () as script_dir :
240+ self .assertTrue (os .path .isabs (script_dir ), script_dir )
241+
242+ script_name = _make_test_script (script_dir , 'script' )
243+ relative_name = os .path .basename (script_name )
244+ self ._check_script (relative_name , script_name , relative_name ,
231245 script_dir , None ,
232246 importlib .machinery .SourceFileLoader )
233247
@@ -399,14 +413,12 @@ def test_issue8202(self):
399413 script_name , script_name , script_dir , 'test_pkg' ,
400414 importlib .machinery .SourceFileLoader )
401415
402- # TODO: RUSTPYTHON
403- @unittest .expectedFailure
404416 def test_issue8202_dash_c_file_ignored (self ):
405417 # Make sure a "-c" file in the current directory
406418 # does not alter the value of sys.path[0]
407419 with os_helper .temp_dir () as script_dir :
408420 with os_helper .change_cwd (path = script_dir ):
409- with open ("-c" , "w" ) as f :
421+ with open ("-c" , "w" , encoding = "utf-8" ) as f :
410422 f .write ("data" )
411423 rc , out , err = assert_python_ok ('-c' ,
412424 'import sys; print("sys.path[0]==%r" % sys.path[0])' ,
@@ -422,7 +434,7 @@ def test_issue8202_dash_m_file_ignored(self):
422434 with os_helper .temp_dir () as script_dir :
423435 script_name = _make_test_script (script_dir , 'other' )
424436 with os_helper .change_cwd (path = script_dir ):
425- with open ("-m" , "w" ) as f :
437+ with open ("-m" , "w" , encoding = "utf-8" ) as f :
426438 f .write ("data" )
427439 rc , out , err = assert_python_ok ('-m' , 'other' , * example_args ,
428440 __isolated = False )
@@ -435,7 +447,7 @@ def test_issue20884(self):
435447 # will be failed.
436448 with os_helper .temp_dir () as script_dir :
437449 script_name = os .path .join (script_dir , "issue20884.py" )
438- with open (script_name , "w" , newline = '\n ' ) as f :
450+ with open (script_name , "w" , encoding = "latin1" , newline = '\n ' ) as f :
439451 f .write ("#coding: iso-8859-1\n " )
440452 f .write ('"""\n ' )
441453 for _ in range (30 ):
@@ -507,6 +519,16 @@ def test_dash_m_bad_pyc(self):
507519 self .assertNotIn (b'is a package' , err )
508520 self .assertNotIn (b'Traceback' , err )
509521
522+ def test_hint_when_triying_to_import_a_py_file (self ):
523+ with os_helper .temp_dir () as script_dir , \
524+ os_helper .change_cwd (path = script_dir ):
525+ # Create invalid *.pyc as empty file
526+ with open ('asyncio.py' , 'wb' ):
527+ pass
528+ err = self .check_dash_m_failure ('asyncio.py' )
529+ self .assertIn (b"Try using 'asyncio' instead "
530+ b"of 'asyncio.py' as the module name" , err )
531+
510532 def test_dash_m_init_traceback (self ):
511533 # These were wrapped in an ImportError and tracebacks were
512534 # suppressed; see Issue 14285
@@ -546,7 +568,7 @@ def test_pep_409_verbiage(self):
546568 script_name = _make_test_script (script_dir , 'script' , script )
547569 exitcode , stdout , stderr = assert_python_failure (script_name )
548570 text = stderr .decode ('ascii' ).split ('\n ' )
549- self .assertEqual (len (text ), 4 )
571+ self .assertEqual (len (text ), 5 )
550572 self .assertTrue (text [0 ].startswith ('Traceback' ))
551573 self .assertTrue (text [1 ].startswith (' File ' ))
552574 self .assertTrue (text [3 ].startswith ('NameError' ))
@@ -565,7 +587,7 @@ def test_non_ascii(self):
565587
566588 # Issue #16218
567589 source = 'print(ascii(__file__))\n '
568- script_name = _make_test_script (os .curdir , name , source )
590+ script_name = _make_test_script (os .getcwd () , name , source )
569591 self .addCleanup (os_helper .unlink , script_name )
570592 rc , stdout , stderr = assert_python_ok (script_name )
571593 self .assertEqual (
@@ -574,10 +596,6 @@ def test_non_ascii(self):
574596 'stdout=%r stderr=%r' % (stdout , stderr ))
575597 self .assertEqual (0 , rc )
576598
577- # TODO: RUSTPYTHON
578- if sys .platform == "linux" :
579- test_non_ascii = unittest .expectedFailure (test_non_ascii )
580-
581599 # TODO: RUSTPYTHON
582600 @unittest .expectedFailure
583601 def test_issue20500_exit_with_exception_value (self ):
@@ -596,7 +614,7 @@ def test_issue20500_exit_with_exception_value(self):
596614 script_name = _make_test_script (script_dir , 'script' , script )
597615 exitcode , stdout , stderr = assert_python_failure (script_name )
598616 text = stderr .decode ('ascii' )
599- self .assertEqual (text , "some text" )
617+ self .assertEqual (text . rstrip () , "some text" )
600618
601619 # TODO: RUSTPYTHON
602620 @unittest .expectedFailure
@@ -606,8 +624,8 @@ def test_syntaxerror_unindented_caret_position(self):
606624 script_name = _make_test_script (script_dir , 'script' , script )
607625 exitcode , stdout , stderr = assert_python_failure (script_name )
608626 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 )
627+ # Confirm that the caret is located under the '=' sign
628+ self .assertIn ("\n ^^^^^ \n " , text )
611629
612630 # TODO: RUSTPYTHON
613631 @unittest .expectedFailure
@@ -620,8 +638,8 @@ def test_syntaxerror_indented_caret_position(self):
620638 script_name = _make_test_script (script_dir , 'script' , script )
621639 exitcode , stdout , stderr = assert_python_failure (script_name )
622640 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 )
641+ # Confirm that the caret starts under the first 1 character
642+ self .assertIn ("\n 1 + 1 = 2\n ^^^^^ \n " , text )
625643
626644 # Try the same with a form feed at the start of the indented line
627645 script = (
@@ -632,7 +650,7 @@ def test_syntaxerror_indented_caret_position(self):
632650 exitcode , stdout , stderr = assert_python_failure (script_name )
633651 text = io .TextIOWrapper (io .BytesIO (stderr ), "ascii" ).read ()
634652 self .assertNotIn ("\f " , text )
635- self .assertIn ("\n 1 + 1 = 2\n ^" , text )
653+ self .assertIn ("\n 1 + 1 = 2\n ^^^^^ \n " , text )
636654
637655 # TODO: RUSTPYTHON
638656 @unittest .expectedFailure
@@ -644,7 +662,7 @@ def test_syntaxerror_multi_line_fstring(self):
644662 self .assertEqual (
645663 stderr .splitlines ()[- 3 :],
646664 [
647- b' foo = f """{} ' ,
665+ b' foo"""' ,
648666 b' ^' ,
649667 b'SyntaxError: f-string: empty expression not allowed' ,
650668 ],
@@ -653,18 +671,17 @@ def test_syntaxerror_multi_line_fstring(self):
653671 # TODO: RUSTPYTHON
654672 @unittest .expectedFailure
655673 def test_syntaxerror_invalid_escape_sequence_multi_line (self ):
656- script = 'foo = """\\ q\n """\n '
674+ script = 'foo = """\\ q"""\n '
657675 with os_helper .temp_dir () as script_dir :
658676 script_name = _make_test_script (script_dir , 'script' , script )
659677 exitcode , stdout , stderr = assert_python_failure (
660678 '-Werror' , script_name ,
661679 )
662680 self .assertEqual (
663681 stderr .splitlines ()[- 3 :],
664- [
665- b' foo = """\\ q' ,
666- b' ^' ,
667- b'SyntaxError: invalid escape sequence \\ q' ,
682+ [ b' foo = """\\ q"""' ,
683+ b' ^^^^^^^^' ,
684+ b'SyntaxError: invalid escape sequence \' \\ q\' '
668685 ],
669686 )
670687
@@ -743,7 +760,7 @@ def test_consistent_sys_path_for_module_execution(self):
743760 def test_nonexisting_script (self ):
744761 # bpo-34783: "./python script.py" must not crash
745762 # if the script file doesn't exist.
746- # (Skip test for macOS framework builds because sys.excutable name
763+ # (Skip test for macOS framework builds because sys.executable name
747764 # is not the actual Python executable file name.
748765 script = 'nonexistingscript.py'
749766 self .assertFalse (os .path .exists (script ))
@@ -755,10 +772,10 @@ def test_nonexisting_script(self):
755772 self .assertIn (": can't open file " , err )
756773 self .assertNotEqual (proc .returncode , 0 )
757774
758-
759- def test_main ():
760- support .run_unittest (CmdLineTest )
775+ def tearDownModule ():
761776 support .reap_children ()
762777
778+
763779if __name__ == '__main__' :
764- test_main ()
780+ unittest .main ()
781+
0 commit comments