FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Implement binding manager to hold binding overrides · pythonnet/pythonnet@fcf0bea · GitHub

Repository navigation

Commit fcf0bea

Browse files
authored andcommitted
Implement binding manager to hold binding overrides
1 parent deca529 commit fcf0bea

3 files changed

Lines changed: 72 additions & 11 deletions

File tree

‎src/runtime/BindingOptions.cs‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Reflection;
4+
5+
namespace Python.Runtime
6+
{
7+
public class BindingOptions
8+
{
9+
private bool _SuppressDocs = false;
10+
private bool _SuppressOverloads = false;
11+
12+
//[ModuleProperty]
13+
public bool SuppressDocs
14+
{
15+
get { return _SuppressDocs; }
16+
set { _SuppressDocs = value; }
17+
}
18+
19+
//[ModuleProperty]
20+
public bool SuppressOverloads
21+
{
22+
get { return _SuppressOverloads; }
23+
set { _SuppressOverloads = value; }
24+
}
25+
}
26+
27+
public class BindingManager
28+
{
29+
static IDictionary<Type, BindingOptions> _typeOverrides = new Dictionary<Type, BindingOptions>();
30+
static IDictionary<Assembly, BindingOptions> _assemblyOverrides = new Dictionary<Assembly, BindingOptions>();
31+
static BindingOptions _defaultBindingOptions = new BindingOptions();
32+
33+
public static BindingOptions GetBindingOptions(Type type)
34+
{
35+
if (_typeOverrides.ContainsKey(type))
36+
{
37+
return _typeOverrides[type];
38+
}
39+
40+
if (_assemblyOverrides.ContainsKey(type.Assembly))
41+
{
42+
return _assemblyOverrides[type.Assembly];
43+
}
44+
return _defaultBindingOptions;
45+
}
46+
47+
public static BindingOptions DefaultBindingOptions => _defaultBindingOptions;
48+
49+
public static void SetBindingOptions(Type type, BindingOptions options)
50+
{
51+
_typeOverrides[type] = options;
52+
}
53+
54+
public static void SetBindingOptions(Assembly assembly, BindingOptions options)
55+
{
56+
_assemblyOverrides[assembly] = options;
57+
}
58+
59+
public static void Clear()
60+
{
61+
_typeOverrides.Clear();
62+
_assemblyOverrides.Clear();
63+
}
64+
}
65+
}

‎src/runtime/ClassManager.cs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ internal static void InitClassBase(Type type, ClassBase impl, ReflectedClrType p
215215
// information, including generating the member descriptors
216216
// that we'll be putting in the Python class __dict__.
217217

218+
var bindingOptions = BindingManager.GetBindingOptions(type);
218219
ClassInfo info = GetClassInfo(type, impl);
219220

220221
impl.indexer = info.indexer;
@@ -260,7 +261,7 @@ internal static void InitClassBase(Type type, ClassBase impl, ReflectedClrType p
260261
if (co.NumCtors > 0 && !co.HasCustomNew())
261262
{
262263
// Implement Overloads on the class object
263-
if (!CLRModule._SuppressOverloads)
264+
if (!bindingOptions.SuppressOverloads)
264265
{
265266
// HACK: __init__ points to instance constructors.
266267
// When unbound they fully instantiate object, so we get overloads for free from MethodBinding.
@@ -271,7 +272,7 @@ internal static void InitClassBase(Type type, ClassBase impl, ReflectedClrType p
271272
}
272273

273274
// don't generate the docstring if one was already set from a DocStringAttribute.
274-
if (!CLRModule._SuppressDocs && doc.IsNull())
275+
if (!bindingOptions.SuppressDocs && doc.IsNull())
275276
{
276277
doc = co.GetDocString();
277278
Runtime.PyDict_SetItem(dict, PyIdentifier.__doc__, doc.Borrow());

‎src/runtime/Types/ClrModule.cs‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ internal class CLRModule : ModuleObject
1616
protected static bool interactive_preload = true;
1717
internal static bool preload;
1818
// XXX Test performance of new features //
19-
internal static bool _SuppressDocs = false;
2019
internal static bool _SuppressOverloads = false;
2120

2221
static CLRModule()
@@ -39,10 +38,6 @@ public static void Reset()
3938
{
4039
interactive_preload = true;
4140
preload = false;
42-
43-
// XXX Test performance of new features //
44-
_SuppressDocs = false;
45-
_SuppressOverloads = false;
4641
}
4742

4843
/// <summary>
@@ -82,15 +77,15 @@ public static void setPreload(bool preloadFlag)
8277
//[ModuleProperty]
8378
public static bool SuppressDocs
8479
{
85-
get { return _SuppressDocs; }
86-
set { _SuppressDocs = value; }
80+
get { return BindingManager.DefaultBindingOptions.SuppressDocs; }
81+
set { BindingManager.DefaultBindingOptions.SuppressDocs = value; }
8782
}
8883

8984
//[ModuleProperty]
9085
public static bool SuppressOverloads
9186
{
92-
get { return _SuppressOverloads; }
93-
set { _SuppressOverloads = value; }
87+
get { return BindingManager.DefaultBindingOptions.SuppressOverloads; }
88+
set { BindingManager.DefaultBindingOptions.SuppressOverloads = value; }
9489
}
9590

9691
[ModuleFunction]

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL