Python encodings.idna() Examples
The following are 30
code examples of encodings.idna().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
encodings
, or try the search function
.
Example #1
Source File: test_codecs.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
def test_nameprep(self): from encodings.idna import nameprep for pos, (orig, prepped) in enumerate(nameprep_tests): if orig is None: # Skipped continue # The Unicode strings are given in UTF-8 orig = str(orig, "utf-8", "surrogatepass") if prepped is None: # Input contains prohibited characters self.assertRaises(UnicodeError, nameprep, orig) else: prepped = str(prepped, "utf-8", "surrogatepass") try: self.assertEqual(nameprep(orig), prepped) except Exception as e: raise support.TestFailed("Test 3.%d: %s" % (pos+1, str(e)))
Example #2
Source File: test_codecs.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
def test_basics_capi(self): from _testcapi import codec_incrementalencoder, codec_incrementaldecoder s = "abc123" # all codecs should be able to encode these for encoding in all_unicode_encodings: if encoding not in broken_unicode_with_stateful: # check incremental decoder/encoder (fetched via the C API) try: cencoder = codec_incrementalencoder(encoding) except LookupError: # no IncrementalEncoder pass else: # check C API encodedresult = b"" for c in s: encodedresult += cencoder.encode(c) encodedresult += cencoder.encode("", True) cdecoder = codec_incrementaldecoder(encoding) decodedresult = "" for c in encodedresult: decodedresult += cdecoder.decode(bytes([c])) decodedresult += cdecoder.decode(b"", True) self.assertEqual(decodedresult, s, "encoding=%r" % encoding) if encoding not in ("idna", "mbcs"): # check incremental decoder/encoder with errors argument try: cencoder = codec_incrementalencoder(encoding, "ignore") except LookupError: # no IncrementalEncoder pass else: encodedresult = b"".join(cencoder.encode(c) for c in s) cdecoder = codec_incrementaldecoder(encoding, "ignore") decodedresult = "".join(cdecoder.decode(bytes([c])) for c in encodedresult) self.assertEqual(decodedresult, s, "encoding=%r" % encoding)
Example #3
Source File: test_codecs.py From ironpython3 with Apache License 2.0 | 6 votes |
def test_nameprep(self): from encodings.idna import nameprep for pos, (orig, prepped) in enumerate(nameprep_tests): if orig is None: # Skipped continue # The Unicode strings are given in UTF-8 orig = str(orig, "utf-8", "surrogatepass") if prepped is None: # Input contains prohibited characters self.assertRaises(UnicodeError, nameprep, orig) else: prepped = str(prepped, "utf-8", "surrogatepass") try: self.assertEqual(nameprep(orig), prepped) except Exception as e: raise support.TestFailed("Test 3.%d: %s" % (pos+1, str(e)))
Example #4
Source File: test_codecs.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 6 votes |
def test_nameprep(self): from encodings.idna import nameprep for pos, (orig, prepped) in enumerate(nameprep_tests): if orig is None: # Skipped continue # The Unicode strings are given in UTF-8 orig = str(orig, "utf-8", "surrogatepass") if prepped is None: # Input contains prohibited characters self.assertRaises(UnicodeError, nameprep, orig) else: prepped = str(prepped, "utf-8", "surrogatepass") try: self.assertEqual(nameprep(orig), prepped) except Exception as e: raise support.TestFailed("Test 3.%d: %s" % (pos+1, str(e)))
Example #5
Source File: test_codecs.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_builtin_decode(self): self.assertEqual(str(b"python.org", "idna"), "python.org") self.assertEqual(str(b"python.org.", "idna"), "python.org.") self.assertEqual(str(b"xn--pythn-mua.org", "idna"), "pyth\xf6n.org") self.assertEqual(str(b"xn--pythn-mua.org.", "idna"), "pyth\xf6n.org.")
Example #6
Source File: test_codecs.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_bad_decode_args(self): for encoding in all_unicode_encodings: decoder = codecs.getdecoder(encoding) self.assertRaises(TypeError, decoder) if encoding not in ("idna", "punycode"): self.assertRaises(TypeError, decoder, 42)
Example #7
Source File: test_codecs.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_seek(self): # all codecs should be able to encode these s = "%s\n%s\n" % (100*"abc123", 100*"def456") for encoding in all_unicode_encodings: if encoding == "idna": # FIXME: See SF bug #1163178 continue if encoding in broken_unicode_with_stateful: continue reader = codecs.getreader(encoding)(io.BytesIO(s.encode(encoding))) for t in range(5): # Test that calling seek resets the internal codec state and buffers reader.seek(0, 0) data = reader.read() self.assertEqual(s, data)
Example #8
Source File: test_codecs.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_basics_capi(self): from _testcapi import codec_incrementalencoder, codec_incrementaldecoder s = "abc123" # all codecs should be able to encode these for encoding in all_unicode_encodings: if encoding not in broken_unicode_with_stateful: # check incremental decoder/encoder (fetched via the C API) try: cencoder = codec_incrementalencoder(encoding) except LookupError: # no IncrementalEncoder pass else: # check C API encodedresult = b"" for c in s: encodedresult += cencoder.encode(c) encodedresult += cencoder.encode("", True) cdecoder = codec_incrementaldecoder(encoding) decodedresult = "" for c in encodedresult: decodedresult += cdecoder.decode(bytes([c])) decodedresult += cdecoder.decode(b"", True) self.assertEqual(decodedresult, s, "encoding=%r" % encoding) if encoding not in ("idna", "mbcs"): # check incremental decoder/encoder with errors argument try: cencoder = codec_incrementalencoder(encoding, "ignore") except LookupError: # no IncrementalEncoder pass else: encodedresult = b"".join(cencoder.encode(c) for c in s) cdecoder = codec_incrementaldecoder(encoding, "ignore") decodedresult = "".join(cdecoder.decode(bytes([c])) for c in encodedresult) self.assertEqual(decodedresult, s, "encoding=%r" % encoding)
Example #9
Source File: test_codecs.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_errors(self): """Only supports "strict" error handler""" "python.org".encode("idna", "strict") b"python.org".decode("idna", "strict") for errors in ("ignore", "replace", "backslashreplace", "surrogateescape"): self.assertRaises(Exception, "python.org".encode, "idna", errors) self.assertRaises(Exception, b"python.org".decode, "idna", errors)
Example #10
Source File: test_codecs.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_incremental_decode(self): self.assertEqual( "".join(codecs.iterdecode((bytes([c]) for c in b"python.org"), "idna")), "python.org" ) self.assertEqual( "".join(codecs.iterdecode((bytes([c]) for c in b"python.org."), "idna")), "python.org." ) self.assertEqual( "".join(codecs.iterdecode((bytes([c]) for c in b"xn--pythn-mua.org."), "idna")), "pyth\xf6n.org." ) self.assertEqual( "".join(codecs.iterdecode((bytes([c]) for c in b"xn--pythn-mua.org."), "idna")), "pyth\xf6n.org." ) decoder = codecs.getincrementaldecoder("idna")() self.assertEqual(decoder.decode(b"xn--xam", ), "") self.assertEqual(decoder.decode(b"ple-9ta.o", ), "\xe4xample.") self.assertEqual(decoder.decode(b"rg"), "") self.assertEqual(decoder.decode(b"", True), "org") decoder.reset() self.assertEqual(decoder.decode(b"xn--xam", ), "") self.assertEqual(decoder.decode(b"ple-9ta.o", ), "\xe4xample.") self.assertEqual(decoder.decode(b"rg."), "org.") self.assertEqual(decoder.decode(b"", True), "")
Example #11
Source File: test_codecs.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_stream(self): r = codecs.getreader("idna")(io.BytesIO(b"abc")) r.read(3) self.assertEqual(r.read(), "")
Example #12
Source File: test_codecs.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_builtin_encode(self): self.assertEqual("python.org".encode("idna"), b"python.org") self.assertEqual("python.org.".encode("idna"), b"python.org.") self.assertEqual("pyth\xf6n.org".encode("idna"), b"xn--pythn-mua.org") self.assertEqual("pyth\xf6n.org.".encode("idna"), b"xn--pythn-mua.org.")
Example #13
Source File: test_codecs.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_builtin_decode(self): self.assertEqual(str(b"python.org", "idna"), "python.org") self.assertEqual(str(b"python.org.", "idna"), "python.org.") self.assertEqual(str(b"xn--pythn-mua.org", "idna"), "pyth\xf6n.org") self.assertEqual(str(b"xn--pythn-mua.org.", "idna"), "pyth\xf6n.org.")
Example #14
Source File: validators.py From pydal with BSD 3-Clause "New" or "Revised" License | 5 votes |
def validate(self, value, record_id=None): if ( not (isinstance(value, (basestring, unicodeT))) or not value or "@" not in value ): raise ValidationError(self.translator(self.error_message)) body, domain = value.rsplit("@", 1) try: regex_flags = re.VERBOSE | re.IGNORECASE match_body = re.match(self.REGEX_BODY, body, regex_flags) match_domain = re.match(self.REGEX_DOMAIN, domain, regex_flags) if not match_domain: # check for Internationalized Domain Names # see https://docs.python.org/2/library/codecs.html#module-encodings.idna domain_encoded = to_unicode(domain).encode("idna").decode("ascii") match_domain = re.match(self.REGEX_DOMAIN, domain_encoded, regex_flags) match = (match_body is not None) and (match_domain is not None) except (TypeError, UnicodeError): # Value may not be a string where we can look for matches. # Example: we're calling ANY_OF formatter and IS_EMAIL is asked to validate a date. match = None if match: if (not self.banned or not self.banned.match(domain)) and ( not self.forced or self.forced.match(domain) ): return value raise ValidationError(self.translator(self.error_message))
Example #15
Source File: test_codecs.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_bad_decode_args(self): for encoding in all_unicode_encodings: decoder = codecs.getdecoder(encoding) self.assertRaises(TypeError, decoder) if encoding not in ("idna", "punycode"): self.assertRaises(TypeError, decoder, 42)
Example #16
Source File: test_codecs.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_seek(self): # all codecs should be able to encode these s = "%s\n%s\n" % (100*"abc123", 100*"def456") for encoding in all_unicode_encodings: if encoding == "idna": # FIXME: See SF bug #1163178 continue if encoding in broken_unicode_with_stateful: continue reader = codecs.getreader(encoding)(io.BytesIO(s.encode(encoding))) for t in range(5): # Test that calling seek resets the internal codec state and buffers reader.seek(0, 0) data = reader.read() self.assertEqual(s, data)
Example #17
Source File: test_codecs.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_basics_capi(self): from _testcapi import codec_incrementalencoder, codec_incrementaldecoder s = "abc123" # all codecs should be able to encode these for encoding in all_unicode_encodings: if encoding not in broken_unicode_with_stateful: # check incremental decoder/encoder (fetched via the C API) try: cencoder = codec_incrementalencoder(encoding) except LookupError: # no IncrementalEncoder pass else: # check C API encodedresult = b"" for c in s: encodedresult += cencoder.encode(c) encodedresult += cencoder.encode("", True) cdecoder = codec_incrementaldecoder(encoding) decodedresult = "" for c in encodedresult: decodedresult += cdecoder.decode(bytes([c])) decodedresult += cdecoder.decode(b"", True) self.assertEqual(decodedresult, s, "encoding=%r" % encoding) if encoding not in ("idna", "mbcs"): # check incremental decoder/encoder with errors argument try: cencoder = codec_incrementalencoder(encoding, "ignore") except LookupError: # no IncrementalEncoder pass else: encodedresult = b"".join(cencoder.encode(c) for c in s) cdecoder = codec_incrementaldecoder(encoding, "ignore") decodedresult = "".join(cdecoder.decode(bytes([c])) for c in encodedresult) self.assertEqual(decodedresult, s, "encoding=%r" % encoding)
Example #18
Source File: test_codecs.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_errors(self): """Only supports "strict" error handler""" "python.org".encode("idna", "strict") b"python.org".decode("idna", "strict") for errors in ("ignore", "replace", "backslashreplace", "surrogateescape"): self.assertRaises(Exception, "python.org".encode, "idna", errors) self.assertRaises(Exception, b"python.org".decode, "idna", errors)
Example #19
Source File: test_codecs.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_incremental_decode(self): self.assertEqual( "".join(codecs.iterdecode((bytes([c]) for c in b"python.org"), "idna")), "python.org" ) self.assertEqual( "".join(codecs.iterdecode((bytes([c]) for c in b"python.org."), "idna")), "python.org." ) self.assertEqual( "".join(codecs.iterdecode((bytes([c]) for c in b"xn--pythn-mua.org."), "idna")), "pyth\xf6n.org." ) self.assertEqual( "".join(codecs.iterdecode((bytes([c]) for c in b"xn--pythn-mua.org."), "idna")), "pyth\xf6n.org." ) decoder = codecs.getincrementaldecoder("idna")() self.assertEqual(decoder.decode(b"xn--xam", ), "") self.assertEqual(decoder.decode(b"ple-9ta.o", ), "\xe4xample.") self.assertEqual(decoder.decode(b"rg"), "") self.assertEqual(decoder.decode(b"", True), "org") decoder.reset() self.assertEqual(decoder.decode(b"xn--xam", ), "") self.assertEqual(decoder.decode(b"ple-9ta.o", ), "\xe4xample.") self.assertEqual(decoder.decode(b"rg."), "org.") self.assertEqual(decoder.decode(b"", True), "")
Example #20
Source File: test_codecs.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_stream(self): r = codecs.getreader("idna")(io.BytesIO(b"abc")) r.read(3) self.assertEqual(r.read(), "")
Example #21
Source File: test_codecs.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_builtin_encode(self): self.assertEqual("python.org".encode("idna"), b"python.org") self.assertEqual("python.org.".encode("idna"), b"python.org.") self.assertEqual("pyth\xf6n.org".encode("idna"), b"xn--pythn-mua.org") self.assertEqual("pyth\xf6n.org.".encode("idna"), b"xn--pythn-mua.org.")
Example #22
Source File: test_codecs.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_builtin_decode(self): self.assertEqual(str(b"python.org", "idna"), "python.org") self.assertEqual(str(b"python.org.", "idna"), "python.org.") self.assertEqual(str(b"xn--pythn-mua.org", "idna"), "pyth\xf6n.org") self.assertEqual(str(b"xn--pythn-mua.org.", "idna"), "pyth\xf6n.org.")
Example #23
Source File: test_codecs.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_bad_decode_args(self): for encoding in all_unicode_encodings: decoder = codecs.getdecoder(encoding) self.assertRaises(TypeError, decoder) if encoding not in ("idna", "punycode"): self.assertRaises(TypeError, decoder, 42)
Example #24
Source File: test_codecs.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_seek(self): # all codecs should be able to encode these s = "%s\n%s\n" % (100*"abc123", 100*"def456") for encoding in all_unicode_encodings: if encoding == "idna": # FIXME: See SF bug #1163178 continue if encoding in broken_unicode_with_stateful: continue reader = codecs.getreader(encoding)(io.BytesIO(s.encode(encoding))) for t in range(5): # Test that calling seek resets the internal codec state and buffers reader.seek(0, 0) data = reader.read() self.assertEqual(s, data)
Example #25
Source File: test_codecs.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_errors(self): """Only supports "strict" error handler""" "python.org".encode("idna", "strict") b"python.org".decode("idna", "strict") for errors in ("ignore", "replace", "backslashreplace", "surrogateescape"): self.assertRaises(Exception, "python.org".encode, "idna", errors) self.assertRaises(Exception, b"python.org".decode, "idna", errors)
Example #26
Source File: test_codecs.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_incremental_decode(self): self.assertEqual( "".join(codecs.iterdecode((bytes([c]) for c in b"python.org"), "idna")), "python.org" ) self.assertEqual( "".join(codecs.iterdecode((bytes([c]) for c in b"python.org."), "idna")), "python.org." ) self.assertEqual( "".join(codecs.iterdecode((bytes([c]) for c in b"xn--pythn-mua.org."), "idna")), "pyth\xf6n.org." ) self.assertEqual( "".join(codecs.iterdecode((bytes([c]) for c in b"xn--pythn-mua.org."), "idna")), "pyth\xf6n.org." ) decoder = codecs.getincrementaldecoder("idna")() self.assertEqual(decoder.decode(b"xn--xam", ), "") self.assertEqual(decoder.decode(b"ple-9ta.o", ), "\xe4xample.") self.assertEqual(decoder.decode(b"rg"), "") self.assertEqual(decoder.decode(b"", True), "org") decoder.reset() self.assertEqual(decoder.decode(b"xn--xam", ), "") self.assertEqual(decoder.decode(b"ple-9ta.o", ), "\xe4xample.") self.assertEqual(decoder.decode(b"rg."), "org.") self.assertEqual(decoder.decode(b"", True), "")
Example #27
Source File: test_codecs.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_stream(self): r = codecs.getreader("idna")(io.BytesIO(b"abc")) r.read(3) self.assertEqual(r.read(), "")
Example #28
Source File: test_codecs.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_builtin_encode(self): self.assertEqual("python.org".encode("idna"), b"python.org") self.assertEqual("python.org.".encode("idna"), b"python.org.") self.assertEqual("pyth\xf6n.org".encode("idna"), b"xn--pythn-mua.org") self.assertEqual("pyth\xf6n.org.".encode("idna"), b"xn--pythn-mua.org.")
Example #29
Source File: validators.py From pydal with BSD 3-Clause "New" or "Revised" License | 4 votes |
def unicode_to_ascii_authority(authority): """ Follows the steps in RFC 3490, Section 4 to convert a unicode authority string into its ASCII equivalent. For example, u'www.Alliancefran\\xe7aise.nu' will be converted into 'www.xn--alliancefranaise-npb.nu' Args: authority: unicode string, the URL authority component to convert, e.g. u'www.Alliancefran\\xe7aise.nu' Returns: string: the US-ASCII character equivalent to the inputed authority, e.g. 'www.xn--alliancefranaise-npb.nu' Raises: Exception: if the function is not able to convert the inputed authority @author: Jonathan Benn """ # RFC 3490, Section 4, Step 1 # The encodings.idna Python module assumes that AllowUnassigned == True # RFC 3490, Section 4, Step 2 labels = re.split(REGEX_AUTHORITY_SPLITTER, authority) # RFC 3490, Section 4, Step 3 # The encodings.idna Python module assumes that UseSTD3ASCIIRules == False # RFC 3490, Section 4, Step 4 # We use the ToASCII operation because we are about to put the authority # into an IDN-unaware slot asciiLabels = [] for label in labels: if label: asciiLabels.append(to_native(encodings.idna.ToASCII(label))) else: # encodings.idna.ToASCII does not accept an empty string, but # it is necessary for us to allow for empty labels so that we # don't modify the URL asciiLabels.append("") # RFC 3490, Section 4, Step 5 return str(reduce(lambda x, y: x + unichr(0x002E) + y, asciiLabels))
Example #30
Source File: net.py From SA-ctf_scoreboard with Creative Commons Zero v1.0 Universal | 4 votes |
def valid_url(self, value): match = self.URL_REGEX.match(value) if not match: return False url = match.groupdict() if url['scheme'].lower() not in self.schemes: return False if url['host6']: if IPv6Type.valid_ip(url['host6']): return url else: return False if url['host4']: return url try: hostname = url['hostn'].encode('ascii').decode('ascii') except UnicodeError: try: hostname = url['hostn'].encode('idna').decode('ascii') except UnicodeError: return False if hostname[-1] == '.': hostname = hostname[:-1] if len(hostname) > 253: return False labels = hostname.split('.') for label in labels: if not 0 < len(label) < 64: return False if '-' in (label[0], label[-1]): return False if self.fqdn: if len(labels) == 1 \ or not self.TLD_REGEX.match(labels[-1]): return False url['hostn_enc'] = hostname return url