Skip to content

Allow fcntl functions to accept objects with fileno() function #4144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions Lib/test/test_fcntl.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,6 @@ def test_fcntl_64_bit(self):
finally:
os.close(fd)

# TODO: RUSTPYTHON, TypeError: 'BufferedRandom' object cannot be interpreted as an integer
@unittest.expectedFailure
def test_flock(self):
# Solaris needs readable file for shared lock
self.f = open(TESTFN, 'wb+')
Expand All @@ -157,7 +155,7 @@ def test_flock(self):
self.assertRaises(ValueError, fcntl.flock, -1, fcntl.LOCK_SH)
self.assertRaises(TypeError, fcntl.flock, 'spam', fcntl.LOCK_SH)

# TODO: RUSTPYTHON, TypeError: 'BufferedRandom' object cannot be interpreted as an integer
# TODO: RUSTPYTHON, AttributeError: module 'os' has no attribute 'fork'
@unittest.expectedFailure
@unittest.skipIf(platform.system() == "AIX", "AIX returns PermissionError")
def test_lockf_exclusive(self):
Expand All @@ -170,7 +168,7 @@ def test_lockf_exclusive(self):
fcntl.lockf(self.f, fcntl.LOCK_UN)
self.assertEqual(p.exitcode, 0)

# TODO: RUSTPYTHON, TypeError: 'BufferedRandom' object cannot be interpreted as an integer
# TODO: RUSTPYTHON, AttributeError: module 'os' has no attribute 'fork'
@unittest.expectedFailure
@unittest.skipIf(platform.system() == "AIX", "AIX returns PermissionError")
def test_lockf_share(self):
Expand Down
16 changes: 3 additions & 13 deletions stdlib/src/fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ mod fcntl {
#[cfg(any(target_os = "dragonfly", target_os = "netbsd", target_vendor = "apple"))]
#[pyattr]
use libc::F_GETPATH;
use rustpython_vm::PyObjectRef;

#[pyfunction]
fn fcntl(
Expand Down Expand Up @@ -90,21 +89,12 @@ mod fcntl {

#[pyfunction]
fn ioctl(
obj: PyObjectRef,
io::Fildes(fd): io::Fildes,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch, thank you!
cc @tgsong827

request: u32,
arg: OptionalArg<Either<Either<ArgMemoryBuffer, ArgStrOrBytesLike>, i32>>,
mutate_flag: OptionalArg<bool>,
vm: &VirtualMachine,
) -> PyResult {
let fd = obj.try_to_value(vm).or_else(|_| {
let meth = vm.get_method_or_type_error(
obj.clone(),
vm.ctx.interned_str("fileno").unwrap(),
|| "ioctl first arg must be an int or object with a fileno() method".to_owned(),
)?;
vm.invoke(&meth, ())?.try_into_value(vm)
})?;

let arg = arg.unwrap_or_else(|| Either::B(0));
match arg {
Either::A(buf_kind) => {
Expand Down Expand Up @@ -153,7 +143,7 @@ mod fcntl {
// XXX: at the time of writing, wasi and redox don't have the necessary constants/function
#[cfg(not(any(target_os = "wasi", target_os = "redox")))]
#[pyfunction]
fn flock(fd: i32, operation: i32, vm: &VirtualMachine) -> PyResult {
fn flock(io::Fildes(fd): io::Fildes, operation: i32, vm: &VirtualMachine) -> PyResult {
let ret = unsafe { libc::flock(fd, operation) };
// TODO: add support for platforms that don't have a builtin `flock` syscall
if ret < 0 {
Expand All @@ -166,7 +156,7 @@ mod fcntl {
#[cfg(not(any(target_os = "wasi", target_os = "redox")))]
#[pyfunction]
fn lockf(
fd: i32,
io::Fildes(fd): io::Fildes,
cmd: i32,
len: OptionalArg<PyIntRef>,
start: OptionalArg<PyIntRef>,
Expand Down