Skip to content

Commit

Permalink
Partial fix for 32862
Browse files Browse the repository at this point in the history
- added overloads to certain methods to allow null as a default parameter which more closely mimics CPython's behavior
- added return of 'default' values to some functions that matches CPython behavior.
- this fix improves the test_unicodedata.py passing rate as well.

Fix whitespace changes
  • Loading branch information
slide committed Jun 22, 2012
1 parent 8582506 commit 33518c5
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 36 deletions.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class UnicodeFunctionsTest(UnicodeDatabaseTest):
# update this, if the database changes # update this, if the database changes
expectedchecksum = '6ccf1b1a36460d2694f9b0b0f0324942fe70ede6' expectedchecksum = '6ccf1b1a36460d2694f9b0b0f0324942fe70ede6'


@unittest.skipIf(sys.platform == 'cli', 'Too slow')
def test_function_checksum(self): def test_function_checksum(self):
data = [] data = []
h = hashlib.sha1() h = hashlib.sha1()
Expand Down
188 changes: 152 additions & 36 deletions Languages/IronPython/IronPython/Modules/unicodedata.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
using System.Text; using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using IronPython.Runtime; using IronPython.Runtime;
using IronPython.Runtime.Operations;


[assembly: PythonModule("unicodedata", typeof(IronPython.Modules.unicodedata))] [assembly: PythonModule("unicodedata", typeof(IronPython.Modules.unicodedata))]


Expand All @@ -33,6 +34,7 @@ namespace IronPython.Modules
public static class unicodedata public static class unicodedata
{ {
private const string UnicodedataResourceName = "IronPython.Modules.unicodedata.IPyUnicodeData.txt.gz"; private const string UnicodedataResourceName = "IronPython.Modules.unicodedata.IPyUnicodeData.txt.gz";
private const string OtherNotAssigned = "Cn";


private static Dictionary<int, CharInfo> database; private static Dictionary<int, CharInfo> database;
private static List<RangeInfo> ranges; private static List<RangeInfo> ranges;
Expand Down Expand Up @@ -66,33 +68,67 @@ public static string name(char unichr, string @default = null)
} }
} }


public static int @decimal(char unichr, int? @default = null) public static int @decimal(char unichr, int @default)
{ {
try try
{ {
int? d = GetInfo(unichr).Numeric_Value_Decimal; int? d = GetInfo(unichr).Numeric_Value_Decimal;
if(d.HasValue) if (d.HasValue)
{ {
return d.Value; return d.Value;
}
else
{
return @default;
} }
else }
catch (KeyNotFoundException)
{
return @default;
}
}

public static int @decimal(char unichr)
{
try
{
int? d = GetInfo(unichr).Numeric_Value_Decimal;
if (d.HasValue)
{ {
if(@default.HasValue) return d.Value;
return @default.Value;
else
throw new Exception();
} }
else
{
throw PythonOps.ValueError("not a decimal");
}
}
catch (KeyNotFoundException)
{
throw PythonOps.ValueError("not a decimal");
} }
catch(KeyNotFoundException) }

public static object @decimal(char unichr, object @default)
{
try
{ {
if(@default.HasValue) int? d = GetInfo(unichr).Numeric_Value_Decimal;
return @default.Value; if (d.HasValue)
else {
throw new Exception(); return d.Value;
}
else
{
return @default;
}
}
catch (KeyNotFoundException)
{
return @default;
} }
} }


public static int digit(char unichr, int? @default = null) public static int digit(char unichr, int @default)
{ {
try try
{ {
Expand All @@ -103,74 +139,154 @@ public static int digit(char unichr, int? @default = null)
} }
else else
{ {
if(@default.HasValue) return @default;
return @default.Value;
else
throw new Exception();
} }
} }
catch(KeyNotFoundException) catch(KeyNotFoundException)
{ {
if(@default.HasValue) return @default;
return @default.Value;
else
throw new Exception();
} }
} }


public static double numeric(char unichr, double? @default = null) public static object digit(char unichr, object @default)
{ {
try try
{
int? d = GetInfo(unichr).Numeric_Value_Digit;
if (d.HasValue)
{
return d.Value;
}
else
{
return @default;
}
}
catch (KeyNotFoundException)
{
return @default;
}
}

public static int digit(char unichr)
{
try
{
int? d = GetInfo(unichr).Numeric_Value_Digit;
if (d.HasValue)
{
return d.Value;
}
else
{
throw PythonOps.ValueError("not a digit");
}
}
catch (KeyNotFoundException)
{
throw PythonOps.ValueError("not a digit");
}
}

public static double numeric(char unichr, double @default)
{
try
{ {
double? d = GetInfo(unichr).Numeric_Value_Numeric; double? d = GetInfo(unichr).Numeric_Value_Numeric;
if(d.HasValue) if (d.HasValue)
{ {
return d.Value; return d.Value;
}
else
{
return @default;
} }
else }
catch (KeyNotFoundException)
{
return @default;
}
}

public static double numeric(char unichr)
{
try
{
double? d = GetInfo(unichr).Numeric_Value_Numeric;
if (d.HasValue)
{
return d.Value;
}
else
{ {
if(@default.HasValue) throw PythonOps.ValueError("not a numeric character");
return @default.Value;
else
throw new Exception();
} }
}
catch (KeyNotFoundException)
{
throw PythonOps.ValueError("not a numeric character");
} }
catch(KeyNotFoundException) }

public static object numeric(char unichr, object @default)
{
try
{ {
if(@default.HasValue) double? d = GetInfo(unichr).Numeric_Value_Numeric;
return @default.Value; if (d.HasValue)
else {
throw new Exception(); return d.Value;
}
else
{
return @default;
}
}
catch (KeyNotFoundException)
{
return @default;
} }
} }


public static string category(char unichr) public static string category(char unichr)
{ {
if (!database.ContainsKey(unichr))
return OtherNotAssigned;
return GetInfo(unichr).General_Category; return GetInfo(unichr).General_Category;
} }


public static string bidirectional(char unichr) public static string bidirectional(char unichr)
{ {
if (!database.ContainsKey(unichr))
return string.Empty;
return GetInfo(unichr).Bidi_Class; return GetInfo(unichr).Bidi_Class;
} }


public static int combining(char unichr) public static int combining(char unichr)
{ {
if (!database.ContainsKey(unichr))
return 0;
return GetInfo(unichr).Canonical_Combining_Class; return GetInfo(unichr).Canonical_Combining_Class;
} }


public static string east_asian_width(char unichr) public static string east_asian_width(char unichr)
{ {
if (!database.ContainsKey(unichr))
return string.Empty;
return GetInfo(unichr).East_Asian_Width; return GetInfo(unichr).East_Asian_Width;
} }


public static int mirrored(char unichr) public static int mirrored(char unichr)
{ {
if (!database.ContainsKey(unichr))
return 0;
return GetInfo(unichr).Bidi_Mirrored; return GetInfo(unichr).Bidi_Mirrored;
} }


public static string decomposition(char unichr) public static string decomposition(char unichr)
{ {
if (!database.ContainsKey(unichr))
return string.Empty;
return GetInfo(unichr).Decomposition_Type; return GetInfo(unichr).Decomposition_Type;
} }


Expand Down Expand Up @@ -243,7 +359,7 @@ static void BuildNameLookup()
private static CharInfo GetInfo(char unichr) private static CharInfo GetInfo(char unichr)
{ {
EnsureLoaded(); EnsureLoaded();

return database[(int)unichr]; return database[(int)unichr];
} }


Expand Down

0 comments on commit 33518c5

Please sign in to comment.