Skip to content

Commit 0c8ae3a

Browse files
authored
Update nix to 0.30 (#6120)
1 parent 056795e commit 0c8ae3a

File tree

10 files changed

+247
-211
lines changed

10 files changed

+247
-211
lines changed

Cargo.lock

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ junction = "1.2.0"
184184
libc = "0.2.169"
185185
libffi = "4.1"
186186
log = "0.4.27"
187-
nix = { version = "0.29", features = ["fs", "user", "process", "term", "time", "signal", "ioctl", "socket", "sched", "zerocopy", "dir", "hostname", "net", "poll"] }
187+
nix = { version = "0.30", features = ["fs", "user", "process", "term", "time", "signal", "ioctl", "socket", "sched", "zerocopy", "dir", "hostname", "net", "poll"] }
188188
malachite-bigint = "0.6"
189189
malachite-q = "0.6"
190190
malachite-base = "0.6"

Lib/test/test_posix.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1993,7 +1993,9 @@ def test_open_file(self):
19931993
with open(outfile, encoding="utf-8") as f:
19941994
self.assertEqual(f.read(), 'hello')
19951995

1996-
# TODO: RUSTPYTHON: FileNotFoundError: [Errno 2] No such file or directory (os error 2): '@test_55144_tmp' -> 'None'
1996+
# TODO: RUSTPYTHON: the rust runtime reopens closed stdio fds at startup,
1997+
# so this test fails, even though POSIX_SPAWN_CLOSE does
1998+
# actually have an effect
19971999
@unittest.expectedFailure
19982000
def test_close_file(self):
19992001
closefile = os_helper.TESTFN

common/src/crt_fd.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,10 @@ impl Owned {
189189
pub fn into_raw(self) -> Raw {
190190
self.inner.into_raw_fd()
191191
}
192+
193+
pub fn leak<'fd>(self) -> Borrowed<'fd> {
194+
unsafe { Borrowed::borrow_raw(self.into_raw()) }
195+
}
192196
}
193197

194198
#[cfg(unix)]

stdlib/src/mmap.rs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,9 @@ mod mmap {
2727
use nix::sys::stat::fstat;
2828
use nix::unistd;
2929
use num_traits::Signed;
30-
use std::fs::File;
30+
use rustpython_common::crt_fd;
3131
use std::io::{self, Write};
3232
use std::ops::{Deref, DerefMut};
33-
#[cfg(unix)]
34-
use std::os::unix::io::{FromRawFd, RawFd};
3533

3634
fn advice_try_from_i32(vm: &VirtualMachine, i: i32) -> PyResult<Advice> {
3735
Ok(match i {
@@ -179,7 +177,7 @@ mod mmap {
179177
struct PyMmap {
180178
closed: AtomicCell<bool>,
181179
mmap: PyMutex<Option<MmapObj>>,
182-
fd: RawFd,
180+
fd: AtomicCell<i32>,
183181
offset: libc::off_t,
184182
size: AtomicCell<usize>,
185183
pos: AtomicCell<usize>, // relative to offset
@@ -190,7 +188,7 @@ mod mmap {
190188
#[derive(FromArgs)]
191189
struct MmapNewArgs {
192190
#[pyarg(any)]
193-
fileno: RawFd,
191+
fileno: i32,
194192
#[pyarg(any)]
195193
length: isize,
196194
#[pyarg(any, default = MAP_SHARED)]
@@ -340,7 +338,8 @@ mod mmap {
340338
}
341339
};
342340

343-
if fd != -1 {
341+
let fd = unsafe { crt_fd::Borrowed::try_borrow_raw(fd) };
342+
if let Ok(fd) = fd {
344343
let metadata = fstat(fd)
345344
.map_err(|err| io::Error::from_raw_os_error(err as i32).to_pyexception(vm))?;
346345
let file_len = metadata.st_size;
@@ -366,27 +365,27 @@ mod mmap {
366365
let mmap_opt = mmap_opt.offset(offset.try_into().unwrap()).len(map_size);
367366

368367
let (fd, mmap) = || -> std::io::Result<_> {
369-
if fd == -1 {
370-
let mmap = MmapObj::Write(mmap_opt.map_anon()?);
371-
Ok((fd, mmap))
372-
} else {
373-
let new_fd = unistd::dup(fd)?;
368+
if let Ok(fd) = fd {
369+
let new_fd: crt_fd::Owned = unistd::dup(fd)?.into();
374370
let mmap = match access {
375371
AccessMode::Default | AccessMode::Write => {
376-
MmapObj::Write(unsafe { mmap_opt.map_mut(fd) }?)
372+
MmapObj::Write(unsafe { mmap_opt.map_mut(&new_fd) }?)
377373
}
378-
AccessMode::Read => MmapObj::Read(unsafe { mmap_opt.map(fd) }?),
379-
AccessMode::Copy => MmapObj::Write(unsafe { mmap_opt.map_copy(fd) }?),
374+
AccessMode::Read => MmapObj::Read(unsafe { mmap_opt.map(&new_fd) }?),
375+
AccessMode::Copy => MmapObj::Write(unsafe { mmap_opt.map_copy(&new_fd) }?),
380376
};
381-
Ok((new_fd, mmap))
377+
Ok((Some(new_fd), mmap))
378+
} else {
379+
let mmap = MmapObj::Write(mmap_opt.map_anon()?);
380+
Ok((None, mmap))
382381
}
383382
}()
384383
.map_err(|e| e.to_pyexception(vm))?;
385384

386385
let m_obj = Self {
387386
closed: AtomicCell::new(false),
388387
mmap: PyMutex::new(Some(mmap)),
389-
fd,
388+
fd: AtomicCell::new(fd.map_or(-1, |fd| fd.into_raw())),
390389
offset,
391390
size: AtomicCell::new(map_size),
392391
pos: AtomicCell::new(0),
@@ -841,9 +840,8 @@ mod mmap {
841840

842841
#[pymethod]
843842
fn size(&self, vm: &VirtualMachine) -> std::io::Result<PyIntRef> {
844-
let new_fd = unistd::dup(self.fd)?;
845-
let file = unsafe { File::from_raw_fd(new_fd) };
846-
let file_len = file.metadata()?.len();
843+
let fd = unsafe { crt_fd::Borrowed::try_borrow_raw(self.fd.load())? };
844+
let file_len = fstat(fd)?.st_size;
847845
Ok(PyInt::from(file_len).into_ref(&vm.ctx))
848846
}
849847

0 commit comments

Comments
 (0)