Skip to content
This repository has been archived by the owner on Sep 14, 2018. It is now read-only.

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.
Expand Up @@ -82,6 +82,7 @@ class UnicodeFunctionsTest(UnicodeDatabaseTest):
# update this, if the database changes
expectedchecksum = '6ccf1b1a36460d2694f9b0b0f0324942fe70ede6'

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

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

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

private static Dictionary<int, CharInfo> database;
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;
if(d.HasValue)
if (d.HasValue)
{
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 @default.Value;
else
throw new Exception();
return d.Value;
}
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)
return @default.Value;
else
throw new Exception();
int? d = GetInfo(unichr).Numeric_Value_Decimal;
if (d.HasValue)
{
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
{
Expand All @@ -103,74 +139,154 @@ public static int digit(char unichr, int? @default = null)
}
else
{
if(@default.HasValue)
return @default.Value;
else
throw new Exception();
return @default;
}
}
catch(KeyNotFoundException)
{
if(@default.HasValue)
return @default.Value;
else
throw new Exception();
return @default;
}
}

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;
if(d.HasValue)
if (d.HasValue)
{
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)
return @default.Value;
else
throw new Exception();
throw PythonOps.ValueError("not a numeric character");
}
}
catch (KeyNotFoundException)
{
throw PythonOps.ValueError("not a numeric character");
}
catch(KeyNotFoundException)
}

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

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

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

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

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

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

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

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

return database[(int)unichr];
}

Expand Down

0 comments on commit 33518c5

Please sign in to comment.