Skip to content

Commit 2e5bb60

Browse files
authored
Resolve failure with jedi module (#1496)
* Resolve failure with jedi module * Fix CA1853 * Fix test failure * Add test
1 parent 99e337f commit 2e5bb60

File tree

7 files changed

+35
-21
lines changed

7 files changed

+35
-21
lines changed

Src/IronPython.Modules/_csv.cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,23 +119,18 @@ private static int GetFieldSizeLimit(CodeContext/*!*/ context) {
119119

120120
[Documentation(@"Delete the name/dialect mapping associated with a string name.\n
121121
csv.unregister_dialect(name)")]
122-
public static void unregister_dialect(CodeContext/*!*/ context,
123-
string name) {
122+
public static void unregister_dialect(CodeContext/*!*/ context, string name) {
124123
DialectRegistry dialects = GetDialects(context);
125-
if (name == null || !dialects.ContainsKey(name))
126-
throw MakeError("unknown dialect");
127-
128-
if (dialects.ContainsKey(name))
129-
dialects.Remove(name);
124+
if (name is not null && dialects.Remove(name)) return;
125+
throw MakeError("unknown dialect");
130126
}
131127

132128
[Documentation(@"Return the dialect instance associated with name.
133129
dialect = csv.get_dialect(name)")]
134130
public static object get_dialect(CodeContext/*!*/ context, string name) {
135131
DialectRegistry dialects = GetDialects(context);
136-
if (name == null || !dialects.ContainsKey(name))
137-
throw MakeError("unknown dialect");
138-
return dialects[name];
132+
if (name is not null && dialects.TryGetValue(name, out var value)) return value;
133+
throw MakeError("unknown dialect");
139134
}
140135

141136
[Documentation(@"Return a list of all know dialect names

Src/IronPython.Modules/_functools.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Collections;
99
using System.Collections.Generic;
1010
using System.Runtime.CompilerServices;
11+
using System.Text;
1112
using System.Threading;
1213

1314
using IronPython.Runtime;
@@ -191,6 +192,24 @@ public void __setstate__(CodeContext context, [NotNone] PythonTuple state) {
191192
}
192193
}
193194

195+
public string __repr__(CodeContext context) {
196+
var builder = new StringBuilder();
197+
builder.Append("functools.partial(");
198+
builder.Append(PythonOps.Repr(context, func));
199+
foreach (var x in _args) {
200+
builder.Append(", ");
201+
builder.Append(PythonOps.Repr(context, x));
202+
}
203+
foreach (var p in _keywordArgs) {
204+
builder.Append(", ");
205+
builder.Append(p.Key);
206+
builder.Append('=');
207+
builder.Append(PythonOps.Repr(context, p.Value));
208+
}
209+
builder.Append(')');
210+
return builder.ToString();
211+
}
212+
194213
#endregion
195214

196215
#region Operator methods

Src/IronPython/Runtime/Operations/PythonOps.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,9 @@ internal static bool Length(object? o, out int res, out BigInteger bigRes) {
888888
return true;
889889
}
890890

891-
object len = PythonContext.InvokeUnaryOperator(DefaultContext.Default, UnaryOperators.Length, o, $"object of type '{GetPythonTypeName(o)}' has no len()");
891+
if (!PythonContext.TryInvokeUnaryOperator(DefaultContext.Default, UnaryOperators.Length, o, out object len)) {
892+
throw TypeError("object of type '{0}' has no len()", GetPythonTypeName(o));
893+
}
892894

893895
var indexObj = Index(len);
894896

Src/IronPython/Runtime/PythonContext.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2137,14 +2137,8 @@ private static string GetTernarySymbol(TernaryOperators oper) {
21372137
return symbol;
21382138
}
21392139

2140-
internal static object InvokeUnaryOperator(CodeContext/*!*/ context, UnaryOperators oper, object target, string errorMsg) {
2141-
object res;
2142-
if (context.LanguageContext.InvokeOperatorWorker(context, oper, target, out res)) {
2143-
return res;
2144-
}
2145-
2146-
throw PythonOps.TypeError(errorMsg);
2147-
}
2140+
internal static bool TryInvokeUnaryOperator(CodeContext/*!*/ context, UnaryOperators oper, object target, out object res)
2141+
=> context.LanguageContext.InvokeOperatorWorker(context, oper, target, out res);
21482142

21492143
internal static object InvokeUnaryOperator(CodeContext/*!*/ context, UnaryOperators oper, object target) {
21502144
object res;

Src/StdLib/Lib/types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def _m(self): pass
3838

3939
# For Jython, the following two types are identical
4040
GetSetDescriptorType = type(FunctionType.__code__)
41-
MemberDescriptorType = type(FunctionType.__globals__)
41+
MemberDescriptorType = type(ModuleType.__dict__["__dict__"]) # ironpython: type(FunctionType.__globals__) is getset_descriptor
4242

4343
del sys, _f, _g, _C, # Not for export
4444

Tests/modules/type_related/test_types.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,8 @@ def test_cp24741(self):
1616
m = types.ModuleType('m')
1717
self.assertEqual(m.__doc__, None)
1818

19+
def test_ipy3_gh1496(self):
20+
self.assertEqual(repr(types.MemberDescriptorType), "<class 'member_descriptor'>")
21+
self.assertEqual(repr(types.GetSetDescriptorType), "<class 'getset_descriptor'>")
22+
1923
run_test(__name__)

Tests/test_functools_stdlib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def load_tests(loader, standard_tests, pattern):
5050
suite.addTest(test.test_functools.TestPartialC('test_pickle'))
5151
suite.addTest(test.test_functools.TestPartialC('test_positional'))
5252
suite.addTest(test.test_functools.TestPartialC('test_protection_of_callers_dict_argument'))
53-
suite.addTest(unittest.expectedFailure(test.test_functools.TestPartialC('test_repr'))) # AssertionError
53+
suite.addTest(test.test_functools.TestPartialC('test_repr'))
5454
suite.addTest(unittest.expectedFailure(test.test_functools.TestPartialC('test_setstate_refcount'))) # AttributeError: 'partial' object has no attribute '__setstate__'
5555
suite.addTest(test.test_functools.TestPartialC('test_weakref'))
5656
suite.addTest(test.test_functools.TestPartialC('test_with_bound_and_unbound_methods'))

0 commit comments

Comments
 (0)