Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

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

Sign up
Appearance settings

Commit60a719c

Browse files
authored
Implement non-simple String constructors explicitly (#1862)
1 parent332f8e7 commit60a719c

File tree

2 files changed

+62
-6
lines changed

2 files changed

+62
-6
lines changed

‎src/runtime/Types/ClassObject.cs

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,24 +106,63 @@ static NewReference tp_new_impl(BorrowedReference tp, BorrowedReference args, Bo
106106

107107
/// <summary>
108108
/// Construct a new .NET String object from Python args
109+
///
110+
/// This manual implementation of all individual relevant constructors
111+
/// is required because System.String can't be allocated uninitialized.
112+
///
113+
/// Additionally, it implements `String(pythonStr)`
109114
/// </summary>
110115
privatestaticNewReferenceNewString(BorrowedReferenceargs,BorrowedReferencetp)
111116
{
112-
if(Runtime.PyTuple_Size(args)==1)
117+
varargCount=Runtime.PyTuple_Size(args);
118+
119+
string?result=null;
120+
if(argCount==1)
113121
{
114122
BorrowedReferenceob=Runtime.PyTuple_GetItem(args,0);
115123
if(Runtime.PyString_Check(ob))
116124
{
117125
if(Runtime.GetManagedString(ob)isstringval)
118-
returnCLRObject.GetReference(val,tp);
126+
result=val;
119127
}
128+
elseif(Converter.ToManagedValue(ob,typeof(char[]),outobject?arr,false))
129+
{
130+
result=newString((char[])arr!);
131+
}
132+
}
133+
elseif(argCount==2)
134+
{
135+
BorrowedReferencep1=Runtime.PyTuple_GetItem(args,0);
136+
BorrowedReferencep2=Runtime.PyTuple_GetItem(args,1);
120137

121-
// TODO: Initialise using constructors instead
122-
123-
Exceptions.SetError(Exceptions.TypeError,"no constructors match given arguments");
124-
returndefault;
138+
if(
139+
Converter.ToManagedValue(p1,typeof(char),outobject?chr,false)&&
140+
Converter.ToManagedValue(p2,typeof(int),outobject?count,false)
141+
)
142+
{
143+
result=newString((char)chr!,(int)count!);
144+
}
145+
}
146+
elseif(argCount==3)
147+
{
148+
BorrowedReferencep1=Runtime.PyTuple_GetItem(args,0);
149+
BorrowedReferencep2=Runtime.PyTuple_GetItem(args,1);
150+
BorrowedReferencep3=Runtime.PyTuple_GetItem(args,2);
151+
152+
if(
153+
Converter.ToManagedValue(p1,typeof(char[]),outobject?arr,false)&&
154+
Converter.ToManagedValue(p2,typeof(int),outobject?offset,false)&&
155+
Converter.ToManagedValue(p3,typeof(int),outobject?length,false)
156+
)
157+
{
158+
result=newString((char[])arr!,(int)offset!,(int)length!);
159+
}
125160
}
126161

162+
if(result!=null)
163+
returnCLRObject.GetReference(result!,tp);
164+
165+
Exceptions.SetError(Exceptions.TypeError,"no constructors match given arguments");
127166
returndefault;
128167
}
129168

‎tests/test_constructors.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,20 @@ def test_default_constructor_fallback():
6969

7070
withpytest.raises(TypeError):
7171
ob=DefaultConstructorMatching("2")
72+
73+
74+
deftest_string_constructor():
75+
fromSystemimportString,Char,Array
76+
77+
ob=String('A',10)
78+
assertob=='A'*10
79+
80+
arr=Array[Char](10)
81+
foriinrange(10):
82+
arr[i]=Char(str(i))
83+
84+
ob=String(arr)
85+
assertob=="0123456789"
86+
87+
ob=String(arr,5,4)
88+
assertob=="5678"

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp