Skip to content

Add XML docs to NuGet #1877

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

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Implement non-simple String constructors explicitly (#1862)
  • Loading branch information
filmor authored Jul 11, 2022
commit 60a719c1f5039b40e0780817f04fd08e2e811a6c
51 changes: 45 additions & 6 deletions src/runtime/Types/ClassObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,24 +106,63 @@ static NewReference tp_new_impl(BorrowedReference tp, BorrowedReference args, Bo

/// <summary>
/// Construct a new .NET String object from Python args
///
/// This manual implementation of all individual relevant constructors
/// is required because System.String can't be allocated uninitialized.
///
/// Additionally, it implements `String(pythonStr)`
/// </summary>
private static NewReference NewString(BorrowedReference args, BorrowedReference tp)
{
if (Runtime.PyTuple_Size(args) == 1)
var argCount = Runtime.PyTuple_Size(args);

string? result = null;
if (argCount == 1)
{
BorrowedReference ob = Runtime.PyTuple_GetItem(args, 0);
if (Runtime.PyString_Check(ob))
{
if (Runtime.GetManagedString(ob) is string val)
return CLRObject.GetReference(val, tp);
result = val;
}
else if (Converter.ToManagedValue(ob, typeof(char[]), out object? arr, false))
{
result = new String((char[])arr!);
}
}
else if (argCount == 2)
{
BorrowedReference p1 = Runtime.PyTuple_GetItem(args, 0);
BorrowedReference p2 = Runtime.PyTuple_GetItem(args, 1);

// TODO: Initialise using constructors instead

Exceptions.SetError(Exceptions.TypeError, "no constructors match given arguments");
return default;
if (
Converter.ToManagedValue(p1, typeof(char), out object? chr, false) &&
Converter.ToManagedValue(p2, typeof(int), out object? count, false)
)
{
result = new String((char)chr!, (int)count!);
}
}
else if (argCount == 3)
{
BorrowedReference p1 = Runtime.PyTuple_GetItem(args, 0);
BorrowedReference p2 = Runtime.PyTuple_GetItem(args, 1);
BorrowedReference p3 = Runtime.PyTuple_GetItem(args, 2);

if (
Converter.ToManagedValue(p1, typeof(char[]), out object? arr, false) &&
Converter.ToManagedValue(p2, typeof(int), out object? offset, false) &&
Converter.ToManagedValue(p3, typeof(int), out object? length, false)
)
{
result = new String((char[])arr!, (int)offset!, (int)length!);
}
}

if (result != null)
return CLRObject.GetReference(result!, tp);

Exceptions.SetError(Exceptions.TypeError, "no constructors match given arguments");
return default;
}

Expand Down
17 changes: 17 additions & 0 deletions tests/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,20 @@ def test_default_constructor_fallback():

with pytest.raises(TypeError):
ob = DefaultConstructorMatching("2")


def test_string_constructor():
from System import String, Char, Array

ob = String('A', 10)
assert ob == 'A' * 10

arr = Array[Char](10)
for i in range(10):
arr[i] = Char(str(i))

ob = String(arr)
assert ob == "0123456789"

ob = String(arr, 5, 4)
assert ob == "5678"