Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Proposal: Implement binding manager to hold binding overrides #2543

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
65 changes: 65 additions & 0 deletions src/runtime/BindingOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
using System.Reflection;

namespace Python.Runtime
{
public class BindingOptions
{
private bool _SuppressDocs = false;
private bool _SuppressOverloads = false;

//[ModuleProperty]
public bool SuppressDocs
{
get { return _SuppressDocs; }
set { _SuppressDocs = value; }
}

//[ModuleProperty]
public bool SuppressOverloads
{
get { return _SuppressOverloads; }
set { _SuppressOverloads = value; }
}
}

public class BindingManager
{
static IDictionary<Type, BindingOptions> _typeOverrides = new Dictionary<Type, BindingOptions>();
static IDictionary<Assembly, BindingOptions> _assemblyOverrides = new Dictionary<Assembly, BindingOptions>();
static BindingOptions _defaultBindingOptions = new BindingOptions();

public static BindingOptions GetBindingOptions(Type type)
{
if (_typeOverrides.ContainsKey(type))
{
return _typeOverrides[type];
}

if (_assemblyOverrides.ContainsKey(type.Assembly))
{
return _assemblyOverrides[type.Assembly];
}
return _defaultBindingOptions;
}

public static BindingOptions DefaultBindingOptions => _defaultBindingOptions;

public static void SetBindingOptions(Type type, BindingOptions options)
{
_typeOverrides[type] = options;
}

public static void SetBindingOptions(Assembly assembly, BindingOptions options)
{
_assemblyOverrides[assembly] = options;
}

public static void Clear()
{
_typeOverrides.Clear();
_assemblyOverrides.Clear();
}
}
}
5 changes: 3 additions & 2 deletions src/runtime/ClassManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ internal static void InitClassBase(Type type, ClassBase impl, ReflectedClrType p
// information, including generating the member descriptors
// that we'll be putting in the Python class __dict__.

var bindingOptions = BindingManager.GetBindingOptions(type);
ClassInfo info = GetClassInfo(type, impl);

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

// don't generate the docstring if one was already set from a DocStringAttribute.
if (!CLRModule._SuppressDocs && doc.IsNull())
if (!bindingOptions.SuppressDocs && doc.IsNull())
{
doc = co.GetDocString();
Runtime.PyDict_SetItem(dict, PyIdentifier.__doc__, doc.Borrow());
Expand Down
13 changes: 4 additions & 9 deletions src/runtime/Types/ClrModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ internal class CLRModule : ModuleObject
protected static bool interactive_preload = true;
internal static bool preload;
// XXX Test performance of new features //
internal static bool _SuppressDocs = false;
internal static bool _SuppressOverloads = false;

static CLRModule()
Expand All @@ -39,10 +38,6 @@ public static void Reset()
{
interactive_preload = true;
preload = false;

// XXX Test performance of new features //
_SuppressDocs = false;
_SuppressOverloads = false;
}

/// <summary>
Expand Down Expand Up @@ -82,15 +77,15 @@ public static void setPreload(bool preloadFlag)
//[ModuleProperty]
public static bool SuppressDocs
{
get { return _SuppressDocs; }
set { _SuppressDocs = value; }
get { return BindingManager.DefaultBindingOptions.SuppressDocs; }
set { BindingManager.DefaultBindingOptions.SuppressDocs = value; }
}

//[ModuleProperty]
public static bool SuppressOverloads
{
get { return _SuppressOverloads; }
set { _SuppressOverloads = value; }
get { return BindingManager.DefaultBindingOptions.SuppressOverloads; }
set { BindingManager.DefaultBindingOptions.SuppressOverloads = value; }
}

[ModuleFunction]
Expand Down
Loading