Back to index

python3.2  3.2.2
Classes | Functions | Variables
encodings.idna Namespace Reference

Classes

class  Codec
 Codec APIs. More...
class  IncrementalEncoder
class  IncrementalDecoder
class  StreamWriter
class  StreamReader

Functions

def nameprep
def ToASCII
def ToUnicode
def getregentry

Variables

tuple dots = re.compile("[\u002E\u3002\uFF0E\uFF61]")
string ace_prefix = "xn--"
string sace_prefix = "xn--"

Function Documentation

Definition at line 278 of file idna.py.

00278 
00279 def getregentry():
00280     return codecs.CodecInfo(
00281         name='idna',
00282         encode=Codec().encode,
00283         decode=Codec().decode,
00284         incrementalencoder=IncrementalEncoder,
00285         incrementaldecoder=IncrementalDecoder,
00286         streamwriter=StreamWriter,
00287         streamreader=StreamReader,
00288     )
def encodings.idna.nameprep (   label)

Definition at line 14 of file idna.py.

00014 
00015 def nameprep(label):
00016     # Map
00017     newlabel = []
00018     for c in label:
00019         if stringprep.in_table_b1(c):
00020             # Map to nothing
00021             continue
00022         newlabel.append(stringprep.map_table_b2(c))
00023     label = "".join(newlabel)
00024 
00025     # Normalize
00026     label = unicodedata.normalize("NFKC", label)
00027 
00028     # Prohibit
00029     for c in label:
00030         if stringprep.in_table_c12(c) or \
00031            stringprep.in_table_c22(c) or \
00032            stringprep.in_table_c3(c) or \
00033            stringprep.in_table_c4(c) or \
00034            stringprep.in_table_c5(c) or \
00035            stringprep.in_table_c6(c) or \
00036            stringprep.in_table_c7(c) or \
00037            stringprep.in_table_c8(c) or \
00038            stringprep.in_table_c9(c):
00039             raise UnicodeError("Invalid character %r" % c)
00040 
00041     # Check bidi
00042     RandAL = [stringprep.in_table_d1(x) for x in label]
00043     for c in RandAL:
00044         if c:
00045             # There is a RandAL char in the string. Must perform further
00046             # tests:
00047             # 1) The characters in section 5.8 MUST be prohibited.
00048             # This is table C.8, which was already checked
00049             # 2) If a string contains any RandALCat character, the string
00050             # MUST NOT contain any LCat character.
00051             if any(stringprep.in_table_d2(x) for x in label):
00052                 raise UnicodeError("Violation of BIDI requirement 2")
00053 
00054             # 3) If a string contains any RandALCat character, a
00055             # RandALCat character MUST be the first character of the
00056             # string, and a RandALCat character MUST be the last
00057             # character of the string.
00058             if not RandAL[0] or not RandAL[-1]:
00059                 raise UnicodeError("Violation of BIDI requirement 3")
00060 
00061     return label

Here is the call graph for this function:

Here is the caller graph for this function:

def encodings.idna.ToASCII (   label)

Definition at line 62 of file idna.py.

00062 
00063 def ToASCII(label):
00064     try:
00065         # Step 1: try ASCII
00066         label = label.encode("ascii")
00067     except UnicodeError:
00068         pass
00069     else:
00070         # Skip to step 3: UseSTD3ASCIIRules is false, so
00071         # Skip to step 8.
00072         if 0 < len(label) < 64:
00073             return label
00074         raise UnicodeError("label empty or too long")
00075 
00076     # Step 2: nameprep
00077     label = nameprep(label)
00078 
00079     # Step 3: UseSTD3ASCIIRules is false
00080     # Step 4: try ASCII
00081     try:
00082         label = label.encode("ascii")
00083     except UnicodeError:
00084         pass
00085     else:
00086         # Skip to step 8.
00087         if 0 < len(label) < 64:
00088             return label
00089         raise UnicodeError("label empty or too long")
00090 
00091     # Step 5: Check ACE prefix
00092     if label.startswith(sace_prefix):
00093         raise UnicodeError("Label starts with ACE prefix")
00094 
00095     # Step 6: Encode with PUNYCODE
00096     label = label.encode("punycode")
00097 
00098     # Step 7: Prepend ACE prefix
00099     label = ace_prefix + label
00100 
00101     # Step 8: Check size
00102     if 0 < len(label) < 64:
00103         return label
00104     raise UnicodeError("label empty or too long")

Here is the call graph for this function:

Here is the caller graph for this function:

def encodings.idna.ToUnicode (   label)

Definition at line 105 of file idna.py.

00105 
00106 def ToUnicode(label):
00107     # Step 1: Check for ASCII
00108     if isinstance(label, bytes):
00109         pure_ascii = True
00110     else:
00111         try:
00112             label = label.encode("ascii")
00113             pure_ascii = True
00114         except UnicodeError:
00115             pure_ascii = False
00116     if not pure_ascii:
00117         # Step 2: Perform nameprep
00118         label = nameprep(label)
00119         # It doesn't say this, but apparently, it should be ASCII now
00120         try:
00121             label = label.encode("ascii")
00122         except UnicodeError:
00123             raise UnicodeError("Invalid character in IDN label")
00124     # Step 3: Check for ACE prefix
00125     if not label.startswith(ace_prefix):
00126         return str(label, "ascii")
00127 
00128     # Step 4: Remove ACE prefix
00129     label1 = label[len(ace_prefix):]
00130 
00131     # Step 5: Decode using PUNYCODE
00132     result = label1.decode("punycode")
00133 
00134     # Step 6: Apply ToASCII
00135     label2 = ToASCII(result)
00136 
00137     # Step 7: Compare the result of step 6 with the one of step 3
00138     # label2 will already be in lower case.
00139     if str(label, "ascii").lower() != str(label2, "ascii"):
00140         raise UnicodeError("IDNA does not round-trip", label, label2)
00141 
00142     # Step 8: return the result of step 5
00143     return result

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

Definition at line 10 of file idna.py.

tuple encodings.idna.dots = re.compile("[\u002E\u3002\uFF0E\uFF61]")

Definition at line 7 of file idna.py.

Definition at line 11 of file idna.py.