Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit cccbd2e

Browse filesBrowse files
committed
Implement IncrementalNewlineDecoder in rust
1 parent 8588348 commit cccbd2e
Copy full SHA for cccbd2e

File tree

7 files changed

+314
-71
lines changed
Filter options

7 files changed

+314
-71
lines changed

‎Lib/io.py

Copy file name to clipboardExpand all lines: Lib/io.py
+1-10Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,7 @@
5757
from _io import (DEFAULT_BUFFER_SIZE, BlockingIOError, UnsupportedOperation,
5858
open, open_code, FileIO, BytesIO, StringIO, BufferedReader,
5959
BufferedWriter, BufferedRWPair, BufferedRandom,
60-
# XXX RUSTPYTHON TODO: IncrementalNewlineDecoder
61-
# IncrementalNewlineDecoder,
62-
text_encoding, TextIOWrapper)
60+
IncrementalNewlineDecoder, text_encoding, TextIOWrapper)
6361

6462
# Pretend this exception was created here.
6563
UnsupportedOperation.__module__ = "io"
@@ -100,10 +98,3 @@ class TextIOBase(_io._TextIOBase, IOBase):
10098
pass
10199
else:
102100
RawIOBase.register(_WindowsConsoleIO)
103-
104-
105-
# XXX: RUSTPYTHON; borrow IncrementalNewlineDecoder from _pyio
106-
try:
107-
from _pyio import IncrementalNewlineDecoder
108-
except ImportError:
109-
pass

‎Lib/test/test_io.py

Copy file name to clipboardExpand all lines: Lib/test/test_io.py
-40Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4121,41 +4121,6 @@ def test_newlines(self):
41214121
def test_newlines_input(self):
41224122
super().test_newlines_input()
41234123

4124-
# TODO: RUSTPYTHON
4125-
@unittest.expectedFailure
4126-
def test_read_one_by_one(self):
4127-
super().test_read_one_by_one()
4128-
4129-
# TODO: RUSTPYTHON
4130-
@unittest.expectedFailure
4131-
def test_read_by_chunk(self):
4132-
super().test_read_by_chunk()
4133-
4134-
# TODO: RUSTPYTHON
4135-
@unittest.expectedFailure
4136-
def test_issue1395_1(self):
4137-
super().test_issue1395_1()
4138-
4139-
# TODO: RUSTPYTHON
4140-
@unittest.expectedFailure
4141-
def test_issue1395_2(self):
4142-
super().test_issue1395_2()
4143-
4144-
# TODO: RUSTPYTHON
4145-
@unittest.expectedFailure
4146-
def test_issue1395_3(self):
4147-
super().test_issue1395_3()
4148-
4149-
# TODO: RUSTPYTHON
4150-
@unittest.expectedFailure
4151-
def test_issue1395_4(self):
4152-
super().test_issue1395_4()
4153-
4154-
# TODO: RUSTPYTHON
4155-
@unittest.expectedFailure
4156-
def test_issue1395_5(self):
4157-
super().test_issue1395_5()
4158-
41594124
# TODO: RUSTPYTHON
41604125
@unittest.expectedFailure
41614126
def test_reconfigure_write_through(self):
@@ -4812,11 +4777,6 @@ class CMiscIOTest(MiscIOTest):
48124777
name_of_module = "io", "_io"
48134778
extra_exported = "BlockingIOError",
48144779

4815-
# TODO: RUSTPYTHON
4816-
@unittest.expectedFailure
4817-
def test___all__(self):
4818-
super().test___all__()
4819-
48204780
def test_readinto_buffer_overflow(self):
48214781
# Issue #18025
48224782
class BadReader(self.io.BufferedIOBase):

‎common/src/lock/thread_mutex.rs

Copy file name to clipboardExpand all lines: common/src/lock/thread_mutex.rs
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ impl<R: RawMutex, G: GetThreadId, T: Default> Default for ThreadMutex<R, G, T> {
9292
Self::new(T::default())
9393
}
9494
}
95+
impl<R: RawMutex, G: GetThreadId, T> From<T> for ThreadMutex<R, G, T> {
96+
fn from(val: T) -> Self {
97+
Self::new(val)
98+
}
99+
}
95100
impl<R: RawMutex, G: GetThreadId, T: ?Sized> ThreadMutex<R, G, T> {
96101
pub fn lock(&self) -> Option<ThreadMutexGuard<R, G, T>> {
97102
if self.raw.lock() {

‎derive-impl/src/from_args.rs

Copy file name to clipboardExpand all lines: derive-impl/src/from_args.rs
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use proc_macro2::TokenStream;
22
use quote::{quote, ToTokens};
3+
use syn::ext::IdentExt;
34
use syn::{
45
parse_quote, Attribute, Data, DeriveInput, Expr, Field, Ident, Lit, Meta, NestedMeta, Result,
56
};
@@ -138,7 +139,7 @@ fn generate_field((i, field): (usize, &Field)) -> Result<TokenStream> {
138139
};
139140

140141
let name = field.ident.as_ref();
141-
let name_string = name.map(Ident::to_string);
142+
let name_string = name.map(|ident| ident.unraw().to_string());
142143
if matches!(&name_string, Some(s) if s.starts_with("_phantom")) {
143144
return Ok(quote! {
144145
#name: ::std::marker::PhantomData,

‎vm/src/builtins/tuple.rs

Copy file name to clipboardExpand all lines: vm/src/builtins/tuple.rs
+39-10Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,30 @@ impl IntoPyTuple for Vec<PyObjectRef> {
5656
}
5757
}
5858

59-
macro_rules! impl_into_pyobj_tuple {
60-
($(($T:ident, $idx:tt)),+) => {
59+
pub trait FromPyTuple<'a>: Sized {
60+
fn from_pytuple(tuple: &'a PyTuple, vm: &VirtualMachine) -> PyResult<Self>;
61+
}
62+
63+
macro_rules! impl_from_into_pytuple {
64+
($($T:ident),+) => {
6165
impl<$($T: ToPyObject),*> IntoPyTuple for ($($T,)*) {
6266
fn into_pytuple(self, vm: &VirtualMachine) -> PyTupleRef {
63-
PyTuple::new_ref(vec![$(self.$idx.to_pyobject(vm)),*], &vm.ctx)
67+
#[allow(non_snake_case)]
68+
let ($($T,)*) = self;
69+
PyTuple::new_ref(vec![$($T.to_pyobject(vm)),*], &vm.ctx)
70+
}
71+
}
72+
73+
// TODO: figure out a way to let PyObjectRef implement TryFromBorrowedObject, and
74+
// have this be a TryFromBorrowedObject bound
75+
impl<'a, $($T: TryFromObject),*> FromPyTuple<'a> for ($($T,)*) {
76+
fn from_pytuple(tuple: &'a PyTuple, vm: &VirtualMachine) -> PyResult<Self> {
77+
#[allow(non_snake_case)]
78+
let &[$(ref $T),+] = tuple.as_slice().try_into().map_err(|_| {
79+
vm.new_type_error(format!("expected tuple with {} elements", impl_from_into_pytuple!(@count $($T)+)))
80+
})?;
81+
Ok(($($T::try_from_object(vm, $T.clone())?,)+))
82+
6483
}
6584
}
6685

@@ -70,15 +89,21 @@ macro_rules! impl_into_pyobj_tuple {
7089
}
7190
}
7291
};
92+
(@count $($T:ident)+) => {
93+
0 $(+ impl_from_into_pytuple!(@discard $T))+
94+
};
95+
(@discard $T:ident) => {
96+
1
97+
};
7398
}
7499

75-
impl_into_pyobj_tuple!((A, 0));
76-
impl_into_pyobj_tuple!((A, 0), (B, 1));
77-
impl_into_pyobj_tuple!((A, 0), (B, 1), (C, 2));
78-
impl_into_pyobj_tuple!((A, 0), (B, 1), (C, 2), (D, 3));
79-
impl_into_pyobj_tuple!((A, 0), (B, 1), (C, 2), (D, 3), (E, 4));
80-
impl_into_pyobj_tuple!((A, 0), (B, 1), (C, 2), (D, 3), (E, 4), (F, 5));
81-
impl_into_pyobj_tuple!((A, 0), (B, 1), (C, 2), (D, 3), (E, 4), (F, 5), (G, 6));
100+
impl_from_into_pytuple!(A);
101+
impl_from_into_pytuple!(A, B);
102+
impl_from_into_pytuple!(A, B, C);
103+
impl_from_into_pytuple!(A, B, C, D);
104+
impl_from_into_pytuple!(A, B, C, D, E);
105+
impl_from_into_pytuple!(A, B, C, D, E, F);
106+
impl_from_into_pytuple!(A, B, C, D, E, F, G);
82107

83108
impl PyTuple {
84109
pub(crate) fn fast_getitem(&self, idx: usize) -> PyObjectRef {
@@ -185,6 +210,10 @@ impl PyTuple {
185210
Self { elements }.into_ref(&vm.ctx)
186211
})
187212
}
213+
214+
pub fn extract_tuple<'a, T: FromPyTuple<'a>>(&'a self, vm: &VirtualMachine) -> PyResult<T> {
215+
T::from_pytuple(self, vm)
216+
}
188217
}
189218

190219
#[pyclass(

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.