Skip to content

Commit 15cf488

Browse files
committed
Names of .NET types (e.g. str(__class__)) changed to better support generic types
1 parent d3c6942 commit 15cf488

File tree

2 files changed

+61
-12
lines changed

2 files changed

+61
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ One must now either use enum members (e.g. `MyEnum.Option`), or use enum constru
5151
- BREAKING: custom encoders are no longer called for instances of `System.Type`
5252
- `PythonException.Restore` no longer clears `PythonException` instance.
5353
- Replaced the old `__import__` hook hack with a PEP302-style Meta Path Loader
54+
- BREAKING: Names of .NET types (e.g. `str(__class__)`) changed to better support generic types
5455

5556
### Fixed
5657

src/runtime/typemanager.cs

Lines changed: 60 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -211,18 +211,7 @@ internal static unsafe PyType CreateType(Type impl)
211211

212212
static PyType CreateClass(Type clrType)
213213
{
214-
// Cleanup the type name to get rid of funny nested type names.
215-
string name = $"clr.{clrType.FullName}";
216-
int i = name.LastIndexOf('+');
217-
if (i > -1)
218-
{
219-
name = name.Substring(i + 1);
220-
}
221-
i = name.LastIndexOf('.');
222-
if (i > -1)
223-
{
224-
name = name.Substring(i + 1);
225-
}
214+
string name = GetPythonTypeName(clrType);
226215

227216
using var baseTuple = GetBaseTypeTuple(clrType);
228217

@@ -251,6 +240,65 @@ static PyType CreateClass(Type clrType)
251240
return pyType;
252241
}
253242

243+
static string GetPythonTypeName(Type clrType)
244+
{
245+
var result = new System.Text.StringBuilder();
246+
GetPythonTypeName(clrType, target: result);
247+
return result.ToString();
248+
}
249+
250+
static void GetPythonTypeName(Type clrType, System.Text.StringBuilder target)
251+
{
252+
if (clrType.IsGenericType)
253+
{
254+
string fullName = clrType.GetGenericTypeDefinition().FullName;
255+
int argCountIndex = fullName.LastIndexOf('`');
256+
if (argCountIndex >= 0)
257+
{
258+
string nonGenericFullName = fullName.Substring(0, argCountIndex);
259+
string nonGenericName = CleanupFullName(nonGenericFullName);
260+
target.Append(nonGenericName);
261+
262+
var arguments = clrType.GetGenericArguments();
263+
target.Append('[');
264+
for (int argIndex = 0; argIndex < arguments.Length; argIndex++)
265+
{
266+
if (argIndex != 0)
267+
{
268+
target.Append(',');
269+
}
270+
271+
GetPythonTypeName(arguments[argIndex], target);
272+
}
273+
274+
target.Append(']');
275+
return;
276+
}
277+
}
278+
279+
string name = CleanupFullName(clrType.FullName);
280+
target.Append(name);
281+
}
282+
283+
static string CleanupFullName(string fullTypeName)
284+
{
285+
// Cleanup the type name to get rid of funny nested type names.
286+
string name = "clr." + fullTypeName;
287+
int i = name.LastIndexOf('+');
288+
if (i > -1)
289+
{
290+
name = name.Substring(i + 1);
291+
}
292+
293+
i = name.LastIndexOf('.');
294+
if (i > -1)
295+
{
296+
name = name.Substring(i + 1);
297+
}
298+
299+
return name;
300+
}
301+
254302
static BorrowedReference InitializeBases(PyType pyType, PyTuple baseTuple)
255303
{
256304
Debug.Assert(baseTuple.Length() > 0);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy