Skip to content

Commit 5718b37

Browse files
authored
Use absolute file name for stat (#1713)
1 parent fbc37f4 commit 5718b37

2 files changed

Lines changed: 19 additions & 6 deletions

File tree

Src/IronPython.Modules/nt.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -439,20 +439,20 @@ public static object fstat(CodeContext/*!*/ context, int fd) {
439439
PythonContext pythonContext = context.LanguageContext;
440440
pythonContext.FileManager.TryGetObjectFromId(pythonContext, fd, out object obj);
441441
if (obj is PythonIOModule.FileIO file) {
442-
if (file.IsConsole) return new stat_result(8192);
442+
if (file.IsConsole) return new stat_result(0x2000);
443443
if (StatStream(file._readStream) is not null and var res) return res;
444-
if (file.name is string strName) return lstat(strName, new Dictionary<string, object>(1));
445444
} else if (obj is Stream stream && StatStream(stream) is not null and var res) {
446445
return res;
447446
}
448447
return LightExceptions.Throw(PythonOps.OSError(9, "Bad file descriptor"));
449448

450-
static stat_result? StatStream(Stream stream) {
451-
if (stream is PipeStream) return new stat_result(4096);
449+
static object? StatStream(Stream stream) {
450+
if (stream is FileStream fs) return lstat(fs.Name, new Dictionary<string, object>(1));
451+
if (stream is PipeStream) return new stat_result(0x1000);
452452
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
453-
if (ReferenceEquals(stream, Stream.Null)) return new stat_result(8192);
453+
if (ReferenceEquals(stream, Stream.Null)) return new stat_result(0x2000);
454454
} else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) {
455-
if (IsUnixStream(stream)) return new stat_result(4096);
455+
if (IsUnixStream(stream)) return new stat_result(0x1000);
456456
}
457457
return null;
458458
}

Tests/modules/io_related/test_fd.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,4 +216,17 @@ def test_pipe_fds(self):
216216
for i in range(1, 3):
217217
os.unlink(test_filename + str(i))
218218

219+
def test_stat_chdir(self):
220+
test_filename = "tmp_%d.stat.test" % os.getpid()
221+
fd = os.open(test_filename, os.O_CREAT | os.O_RDWR)
222+
self.assertIsNotNone(os.fstat(fd))
223+
cwd = os.getcwd()
224+
os.chdir(os.sep)
225+
try:
226+
self.assertIsNotNone(os.fstat(fd))
227+
finally:
228+
os.chdir(cwd)
229+
os.close(fd)
230+
os.unlink(test_filename)
231+
219232
run_test(__name__)

0 commit comments

Comments
 (0)