| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -51,6 +51,7 @@ One must now either use enum members (e.g. `MyEnum.Option`), or use enum constru | |||
| 51 | 51 | - BREAKING: custom encoders are no longer called for instances of `System.Type` | |
| 52 | 52 | - `PythonException.Restore` no longer clears `PythonException` instance. | |
| 53 | 53 | - Replaced the old `__import__` hook hack with a PEP302-style Meta Path Loader | |
| 54 | + - Names of .NET types (e.g. `str(__class__)`) changed to better support generic types | ||
| 54 | 55 | ||
| 55 | 56 | ### Fixed | |
| 56 | 57 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -211,18 +211,7 @@ internal static unsafe PyType CreateType(Type impl) | |||
| 211 | 211 | ||
| 212 | 212 | static PyType CreateClass(Type clrType) | |
| 213 | 213 | { | |
| 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); | ||
| 226 | 215 | ||
| 227 | 216 | using var baseTuple = GetBaseTypeTuple(clrType); | |
| 228 | 217 | ||
@@ -251,6 +240,65 @@ static PyType CreateClass(Type clrType) | |||
| 251 | 240 | return pyType; | |
| 252 | 241 | } | |
| 253 | 242 | ||
| 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 | + | ||
| 254 | 302 | static BorrowedReference InitializeBases(PyType pyType, PyTuple baseTuple) | |
| 255 | 303 | { | |
| 256 | 304 | Debug.Assert(baseTuple.Length() > 0); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments