PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/DLR_Main/Languages/IronPython/IronPython/Runtime/BindingWarnings.cs

https://bitbucket.org/mdavid/dlr
C# | 95 lines | 65 code | 12 blank | 18 comment | 7 complexity | ddf2e7e13de851c89603d2fa0356554f MD5 | raw file
  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * This source code is subject to terms and conditions of the Apache License, Version 2.0. A
  6. * copy of the license can be found in the License.html file at the root of this distribution. If
  7. * you cannot locate the Apache License, Version 2.0, please send an email to
  8. * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. * by the terms of the Apache License, Version 2.0.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. *
  14. * ***************************************************************************/
  15. #if !CLR2
  16. using System.Linq.Expressions;
  17. #else
  18. using Microsoft.Scripting.Ast;
  19. #endif
  20. using System;
  21. using System.Reflection;
  22. using System.Threading;
  23. using IronPython.Runtime.Exceptions;
  24. using IronPython.Runtime.Types;
  25. using Microsoft.Scripting.Actions.Calls;
  26. using Microsoft.Scripting.Utils;
  27. using AstUtils = Microsoft.Scripting.Ast.Utils;
  28. namespace IronPython.Runtime.Binding {
  29. /// <summary>
  30. /// Provides support for emitting warnings when built in methods are invoked at runtime.
  31. /// </summary>
  32. internal static class BindingWarnings {
  33. public static bool ShouldWarn(PythonContext/*!*/ context, OverloadInfo/*!*/ method, out WarningInfo info) {
  34. Assert.NotNull(method);
  35. ObsoleteAttribute[] os = (ObsoleteAttribute[])method.ReflectionInfo.GetCustomAttributes(typeof(ObsoleteAttribute), true);
  36. if (os.Length > 0) {
  37. info = new WarningInfo(
  38. PythonExceptions.DeprecationWarning,
  39. String.Format("{0}.{1} has been obsoleted. {2}",
  40. NameConverter.GetTypeName(method.DeclaringType),
  41. method.Name,
  42. os[0].Message
  43. )
  44. );
  45. return true;
  46. }
  47. if (context.PythonOptions.WarnPython30) {
  48. Python3WarningAttribute[] py3kwarnings = (Python3WarningAttribute[])method.ReflectionInfo.GetCustomAttributes(typeof(Python3WarningAttribute), true);
  49. if (py3kwarnings.Length > 0) {
  50. info = new WarningInfo(
  51. PythonExceptions.DeprecationWarning,
  52. py3kwarnings[0].Message
  53. );
  54. return true;
  55. }
  56. }
  57. #if !SILVERLIGHT
  58. // no apartment states on Silverlight
  59. if (method.DeclaringType == typeof(Thread)) {
  60. if (method.Name == "Sleep") {
  61. info = new WarningInfo(
  62. PythonExceptions.RuntimeWarning,
  63. "Calling Thread.Sleep on an STA thread doesn't pump messages. Use Thread.CurrentThread.Join instead.",
  64. Expression.Equal(
  65. Expression.Call(
  66. Expression.Property(
  67. null,
  68. typeof(Thread).GetProperty("CurrentThread")
  69. ),
  70. typeof(Thread).GetMethod("GetApartmentState")
  71. ),
  72. AstUtils.Constant(ApartmentState.STA)
  73. )
  74. );
  75. return true;
  76. }
  77. }
  78. #endif
  79. info = null;
  80. return false;
  81. }
  82. }
  83. }