| 130 | |
| 131 | #[pymethod] |
| 132 | fn readline(&self, size: OptionalArg<isize>, vm: &VirtualMachine) -> PyResult<String> { |
| 133 | if self.fd != 0 { |
| 134 | return Err(vm.new_os_error("not readable".to_owned())); |
| 135 | } |
| 136 | let size = size.unwrap_or(-1); |
| 137 | if size == 0 { |
| 138 | return Ok(String::new()); |
| 139 | } |
| 140 | let mut line = String::new(); |
| 141 | std::io::stdin() |
| 142 | .read_line(&mut line) |
| 143 | .map_err(|e| vm.new_os_error(e.to_string()))?; |
| 144 | if size > 0 { |
| 145 | line.truncate(size as usize); |
| 146 | } |
| 147 | Ok(line) |
| 148 | } |
| 149 | |
| 150 | #[pymethod] |
| 151 | fn flush(&self, vm: &VirtualMachine) -> PyResult<()> { |