11import importlib
22import io
33import operator
4- import os
54import queue
65import sys
76import textwrap
1211from typing import TYPE_CHECKING
1312from typing import Union
1413
15- import py
16-
1714import _pytest
1815import pytest
1916from _pytest ._code .code import ExceptionChainRepr
2017from _pytest ._code .code import ExceptionInfo
2118from _pytest ._code .code import FormattedExcinfo
2219from _pytest ._io import TerminalWriter
20+ from _pytest .monkeypatch import MonkeyPatch
21+ from _pytest .pathlib import bestrelpath
2322from _pytest .pathlib import import_path
2423from _pytest .pytester import LineMatcher
2524from _pytest .pytester import Pytester
@@ -150,9 +149,10 @@ def xyz():
150149 " except somenoname: # type: ignore[name-defined] # noqa: F821" ,
151150 ]
152151
153- def test_traceback_cut (self ):
152+ def test_traceback_cut (self ) -> None :
154153 co = _pytest ._code .Code .from_function (f )
155154 path , firstlineno = co .path , co .firstlineno
155+ assert isinstance (path , Path )
156156 traceback = self .excinfo .traceback
157157 newtraceback = traceback .cut (path = path , firstlineno = firstlineno )
158158 assert len (newtraceback ) == 1
@@ -163,11 +163,11 @@ def test_traceback_cut_excludepath(self, pytester: Pytester) -> None:
163163 p = pytester .makepyfile ("def f(): raise ValueError" )
164164 with pytest .raises (ValueError ) as excinfo :
165165 import_path (p ).f () # type: ignore[attr-defined]
166- basedir = py . path . local (pytest .__file__ ).dirpath ()
166+ basedir = Path (pytest .__file__ ).parent
167167 newtraceback = excinfo .traceback .cut (excludepath = basedir )
168168 for x in newtraceback :
169- if hasattr ( x , "path" ):
170- assert not py . path . local ( x .path ). relto ( basedir )
169+ assert isinstance ( x . path , Path )
170+ assert basedir not in x .path . parents
171171 assert newtraceback [- 1 ].frame .code .path == p
172172
173173 def test_traceback_filter (self ):
@@ -376,7 +376,7 @@ def test_excinfo_no_python_sourcecode(tmpdir):
376376 for item in excinfo .traceback :
377377 print (item ) # XXX: for some reason jinja.Template.render is printed in full
378378 item .source # shouldn't fail
379- if isinstance (item .path , py . path . local ) and item .path .basename == "test.txt" :
379+ if isinstance (item .path , Path ) and item .path .name == "test.txt" :
380380 assert str (item .source ) == "{{ h()}}:"
381381
382382
@@ -392,16 +392,16 @@ def test_entrysource_Queue_example():
392392 assert s .startswith ("def get" )
393393
394394
395- def test_codepath_Queue_example ():
395+ def test_codepath_Queue_example () -> None :
396396 try :
397397 queue .Queue ().get (timeout = 0.001 )
398398 except queue .Empty :
399399 excinfo = _pytest ._code .ExceptionInfo .from_current ()
400400 entry = excinfo .traceback [- 1 ]
401401 path = entry .path
402- assert isinstance (path , py . path . local )
403- assert path .basename .lower () == "queue.py"
404- assert path .check ()
402+ assert isinstance (path , Path )
403+ assert path .name .lower () == "queue.py"
404+ assert path .exists ()
405405
406406
407407def test_match_succeeds ():
@@ -805,21 +805,21 @@ def entry():
805805
806806 raised = 0
807807
808- orig_getcwd = os . getcwd
808+ orig_path_cwd = Path . cwd
809809
810810 def raiseos ():
811811 nonlocal raised
812812 upframe = sys ._getframe ().f_back
813813 assert upframe is not None
814- if upframe .f_code .co_name == "checked_call " :
814+ if upframe .f_code .co_name == "_makepath " :
815815 # Only raise with expected calls, but not via e.g. inspect for
816816 # py38-windows.
817817 raised += 1
818818 raise OSError (2 , "custom_oserror" )
819- return orig_getcwd ()
819+ return orig_path_cwd ()
820820
821- monkeypatch .setattr (os , "getcwd " , raiseos )
822- assert p ._makepath (__file__ ) == __file__
821+ monkeypatch .setattr (Path , "cwd " , raiseos )
822+ assert p ._makepath (Path ( __file__ ) ) == __file__
823823 assert raised == 1
824824 repr_tb = p .repr_traceback (excinfo )
825825
@@ -1015,33 +1015,32 @@ def f():
10151015 assert line .endswith ("mod.py" )
10161016 assert tw_mock .lines [10 ] == ":3: ValueError"
10171017
1018- def test_toterminal_long_filenames (self , importasmod , tw_mock ):
1018+ def test_toterminal_long_filenames (
1019+ self , importasmod , tw_mock , monkeypatch : MonkeyPatch
1020+ ) -> None :
10191021 mod = importasmod (
10201022 """
10211023 def f():
10221024 raise ValueError()
10231025 """
10241026 )
10251027 excinfo = pytest .raises (ValueError , mod .f )
1026- path = py .path .local (mod .__file__ )
1027- old = path .dirpath ().chdir ()
1028- try :
1029- repr = excinfo .getrepr (abspath = False )
1030- repr .toterminal (tw_mock )
1031- x = py .path .local ().bestrelpath (path )
1032- if len (x ) < len (str (path )):
1033- msg = tw_mock .get_write_msg (- 2 )
1034- assert msg == "mod.py"
1035- assert tw_mock .lines [- 1 ] == ":3: ValueError"
1036-
1037- repr = excinfo .getrepr (abspath = True )
1038- repr .toterminal (tw_mock )
1028+ path = Path (mod .__file__ )
1029+ monkeypatch .chdir (path .parent )
1030+ repr = excinfo .getrepr (abspath = False )
1031+ repr .toterminal (tw_mock )
1032+ x = bestrelpath (Path .cwd (), path )
1033+ if len (x ) < len (str (path )):
10391034 msg = tw_mock .get_write_msg (- 2 )
1040- assert msg == path
1041- line = tw_mock .lines [- 1 ]
1042- assert line == ":3: ValueError"
1043- finally :
1044- old .chdir ()
1035+ assert msg == "mod.py"
1036+ assert tw_mock .lines [- 1 ] == ":3: ValueError"
1037+
1038+ repr = excinfo .getrepr (abspath = True )
1039+ repr .toterminal (tw_mock )
1040+ msg = tw_mock .get_write_msg (- 2 )
1041+ assert msg == str (path )
1042+ line = tw_mock .lines [- 1 ]
1043+ assert line == ":3: ValueError"
10451044
10461045 @pytest .mark .parametrize (
10471046 "reproptions" ,
0 commit comments