Skip to content

Commit 0295dd2

Browse files
author
Mohamed Koubaa
committed
Implement binding manager to hold binding overrides
1 parent dfd746b commit 0295dd2

File tree

3 files changed

+72
-11
lines changed

3 files changed

+72
-11
lines changed

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
@@ -210,6 +210,7 @@ internal static void InitClassBase(Type type, ClassBase impl, ReflectedClrType p
210210
// information, including generating the member descriptors
211211
// that we'll be putting in the Python class __dict__.
212212

213+
var bindingOptions = BindingManager.GetBindingOptions(type);
213214
ClassInfo info = GetClassInfo(type, impl);
214215

215216
impl.indexer = info.indexer;
@@ -254,7 +255,7 @@ internal static void InitClassBase(Type type, ClassBase impl, ReflectedClrType p
254255
if (co.NumCtors > 0 && !co.HasCustomNew())
255256
{
256257
// Implement Overloads on the class object
257-
if (!CLRModule._SuppressOverloads)
258+
if (!bindingOptions.SuppressOverloads)
258259
{
259260
// HACK: __init__ points to instance constructors.
260261
// When unbound they fully instantiate object, so we get overloads for free from MethodBinding.
@@ -265,7 +266,7 @@ internal static void InitClassBase(Type type, ClassBase impl, ReflectedClrType p
265266
}
266267

267268
// don't generate the docstring if one was already set from a DocStringAttribute.
268-
if (!CLRModule._SuppressDocs && doc.IsNull())
269+
if (!bindingOptions.SuppressDocs && doc.IsNull())
269270
{
270271
doc = co.GetDocString();
271272
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)