我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用__future__.unicode_literals()。
def setproctitle(title): assert isinstance(title, compat_str) # ctypes in Jython is not complete # http://bugs.jython.org/issue2148 if sys.platform.startswith('java'): return try: libc = ctypes.cdll.LoadLibrary('libc.so.6') except OSError: return except TypeError: # LoadLibrary in Windows Python 2.7.13 only expects # a bytestring, but since unicode_literals turns # every string into a unicode string, it fails. return title_bytes = title.encode('utf-8') buf = ctypes.create_string_buffer(len(title_bytes)) buf.value = title_bytes try: libc.prctl(15, buf, 0, 0, 0) except AttributeError: return # Strange libc, just skip this
def _imports_unicode_literals(contents_text): try: ast_obj = ast_parse(contents_text) except SyntaxError: return False for node in ast_obj.body: # Docstring if isinstance(node, ast.Expr) and isinstance(node.value, ast.Str): continue elif isinstance(node, ast.ImportFrom): if ( node.module == '__future__' and any(name.name == 'unicode_literals' for name in node.names) ): return True elif node.module == '__future__': continue else: return False else: return False
def testParseWithNulls(self): # This relies on the from __future__ import unicode_literals, because # explicitly specifying a unicode literal is a syntax error in Py 3.2 # May want to switch to u'...' if we ever drop Python 3.2 support. pstring = '\x00\x00August 29, 1924' self.assertEqual(parse(pstring), datetime(1924, 8, 29))
def transform(self, node, results): future_import(u"unicode_literals", node)
def strip_future_imports(self, code): """ Strips any of these import lines: from __future__ import <anything> from future <anything> from future.<anything> from builtins <anything> or any line containing: install_hooks() or: install_aliases() Limitation: doesn't handle imports split across multiple lines like this: from __future__ import (absolute_import, division, print_function, unicode_literals) """ output = [] # We need .splitlines(keepends=True), which doesn't exist on Py2, # so we use this instead: for line in code.split('\n'): if not (line.startswith('from __future__ import ') or line.startswith('from future ') or line.startswith('from builtins ') or 'install_hooks()' in line or 'install_aliases()' in line # but don't match "from future_builtins" :) or line.startswith('from future.')): output.append(line) return '\n'.join(output)
def test_all_present(self): import youtube_dl.compat all_names = youtube_dl.compat.__all__ present_names = set(filter( lambda c: '_' in c and not c.startswith('_'), dir(youtube_dl.compat))) - set(['unicode_literals']) self.assertEqual(all_names, sorted(present_names))
def test_all_files(self): for dirpath, dirnames, filenames in os.walk(rootDir): for ignore_dir in IGNORED_DIRS: if ignore_dir in dirnames: # If we remove the directory from dirnames os.walk won't # recurse into it dirnames.remove(ignore_dir) for basename in filenames: if not basename.endswith('.py'): continue if basename in IGNORED_FILES: continue fn = os.path.join(dirpath, basename) with io.open(fn, encoding='utf-8') as inf: code = inf.read() if "'" not in code and '"' not in code: continue assertRegexpMatches( self, code, r'(?:(?:#.*?|\s*)\n)*from __future__ import (?:[a-z_]+,\s*)*unicode_literals', 'unicode_literals import missing in %s' % fn) m = re.search(r'(?<=\s)u[\'"](?!\)|,|$)', code) if m is not None: self.assertTrue( m is None, 'u present in %s, around %s' % ( fn, code[m.start() - 10:m.end() + 10]))
def test_str_encode_decode_with_py2_str_arg(self): # Try passing a standard Py2 string (as if unicode_literals weren't imported) b = str(TEST_UNICODE_STR).encode(utils.bytes_to_native_str(b'utf-8')) self.assertTrue(isinstance(b, bytes)) self.assertFalse(isinstance(b, str)) s = b.decode(utils.bytes_to_native_str(b'utf-8')) self.assertTrue(isinstance(s, str)) self.assertEqual(s, TEST_UNICODE_STR)
def test_type(self): """ The following fails when passed a unicode string on Python (including when unicode_literals is in effect) and fails when passed a byte-string on Python 3. So type() always wants a native string as the first argument. TODO: maybe provide a replacement that works identically on Py2/3? """ mytype = type('blah', (dict,), {"old": 1, "new": 2}) d = mytype() self.assertTrue(isinstance(d, mytype)) self.assertTrue(isinstance(d, dict))
def test_Py2_StringIO_module(self): """ This requires that the argument to io.StringIO be made a unicode string explicitly if we're not using unicode_literals: Ideally, there would be a fixer for this. For now: TODO: add the Py3 equivalent for this to the docs. Also add back a test for the unicode_literals case. """ before = """ import cStringIO import StringIO s1 = cStringIO.StringIO('my string') s2 = StringIO.StringIO('my other string') assert isinstance(s1, cStringIO.InputType) """ # There is no io.InputType in Python 3. futurize should change this to # something like this. But note that the input to io.StringIO # must be a unicode string on both Py2 and Py3. after = """ import io import io s1 = io.StringIO(u'my string') s2 = io.StringIO(u'my other string') assert isinstance(s1, io.StringIO) """ self.convert_check(before, after)
def test_all_imports(self): before = """ import math import os l = range(10) assert isinstance(l, list) print 'Hello' for i in xrange(100): pass print('Hello') """ after = """ from __future__ import unicode_literals from __future__ import print_function from __future__ import division from __future__ import absolute_import from future import standard_library standard_library.install_aliases() from builtins import range from builtins import * import math import os l = list(range(10)) assert isinstance(l, list) print('Hello') for i in range(100): pass print('Hello') """ self.convert_check(before, after, all_imports=True)
def transform(self, node, results): future_import(u"unicode_literals", node) future_import(u"print_function", node) future_import(u"division", node) future_import(u"absolute_import", node)
def read(self, filenames, encoding=None): """Read and parse a filename or a list of filenames. Files that cannot be opened are silently ignored; this is designed so that you can specify a list of potential configuration file locations (e.g. current directory, user's home directory, systemwide directory), and all existing configuration files in the list will be read. A single filename may also be given. Return list of successfully read files. """ if PY2 and isinstance(filenames, bytes): # we allow for a little unholy magic for Python 2 so that # people not using unicode_literals can still use the library # conveniently warnings.warn( "You passed a bytestring as `filenames`. This will not work" " on Python 3. Use `cp.read_file()` or switch to using Unicode" " strings across the board.", DeprecationWarning, stacklevel=2, ) filenames = [filenames] elif isinstance(filenames, str): filenames = [filenames] read_ok = [] for filename in filenames: try: with open(filename, encoding=encoding) as fp: self._read(fp, filename) except IOError: continue read_ok.append(filename) return read_ok
def setUp(self): """ The outputs from the various futurize stages should have the following headers: """ # After stage1: # TODO: use this form after implementing a fixer to consolidate # __future__ imports into a single line: # self.headers1 = """ # from __future__ import absolute_import, division, print_function # """ self.headers1 = reformat_code(""" from __future__ import absolute_import from __future__ import division from __future__ import print_function """) # After stage2 --all-imports: # TODO: use this form after implementing a fixer to consolidate # __future__ imports into a single line: # self.headers2 = """ # from __future__ import (absolute_import, division, # print_function, unicode_literals) # from future import standard_library # from future.builtins import * # """ self.headers2 = reformat_code(""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from future import standard_library standard_library.install_aliases() from builtins import * """) self.interpreters = [sys.executable] self.tempdir = tempfile.mkdtemp() + os.path.sep pypath = os.getenv('PYTHONPATH') if pypath: self.env = {'PYTHONPATH': os.getcwd() + os.pathsep + pypath} else: self.env = {'PYTHONPATH': os.getcwd()}