1 """Utilities for writing code that runs on Python 2 and 3"""
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import operator
24 import sys
25 import types
26
27 __author__ = "Benjamin Peterson <benjamin@python.org>"
28 __version__ = "1.5.2"
29
30
31
32 PY2 = sys.version_info[0] == 2
33 PY3 = sys.version_info[0] == 3
34
35 if PY3:
36 string_types = str,
37 integer_types = int,
38 class_types = type,
39 text_type = str
40 binary_type = bytes
41
42 none_type = type(None)
43 boolean_type = bool
44 float_type = float
45 int_type = int
46 long_type = int
47 list_type = list
48 tuple_type = tuple
49 dictionary_type = dict
50
51 MAXSIZE = sys.maxsize
52 else:
53 string_types = basestring,
54 integer_types = (int, long)
55 class_types = (type, types.ClassType)
56 text_type = unicode
57 binary_type = str
58
59 import types
60 none_type = types.NoneType
61 boolean_type = types.BooleanType
62 int_type = types.IntType
63 long_type = types.LongType
64 float_type = types.FloatType
65 list_type = types.ListType
66 tuple_type = types.TupleType
67 dictionary_type = types.DictionaryType
68
69 if sys.platform.startswith("java"):
70
71 MAXSIZE = int((1 << 31) - 1)
72 else:
73
77 try:
78 len(X())
79 except OverflowError:
80
81 MAXSIZE = int((1 << 31) - 1)
82 else:
83
84 MAXSIZE = int((1 << 63) - 1)
85 del X
86
87
89 """Add documentation to a function."""
90 func.__doc__ = doc
91
92
94 """Import module, returning the module after the last dot."""
95 __import__(name)
96 return sys.modules[name]
97
98
100
103
105 result = self._resolve()
106 setattr(obj, self.name, result)
107
108 delattr(obj.__class__, self.name)
109 return result
110
111
113
114 - def __init__(self, name, old, new=None):
122
124 return _import_module(self.mod)
125
127
128
129
130
131
132
133 if attr in ("__file__", "__name__") and self.mod not in sys.modules:
134 raise AttributeError
135 _module = self._resolve()
136 value = getattr(_module, attr)
137 setattr(self, attr, value)
138 return value
139
140
154
155
157
158 - def __init__(self, name, old_mod, new_mod, old_attr=None, new_attr=None):
159 super(MovedAttribute, self).__init__(name)
160 if PY3:
161 if new_mod is None:
162 new_mod = name
163 self.mod = new_mod
164 if new_attr is None:
165 if old_attr is None:
166 new_attr = name
167 else:
168 new_attr = old_attr
169 self.attr = new_attr
170 else:
171 self.mod = old_mod
172 if old_attr is None:
173 old_attr = name
174 self.attr = old_attr
175
177 module = _import_module(self.mod)
178 return getattr(module, self.attr)
179
180
181
183 """Lazy loading of moved objects"""
184
185
186 _moved_attributes = [
187 MovedAttribute("cStringIO", "cStringIO", "io", "StringIO"),
188 MovedAttribute("filter", "itertools", "builtins", "ifilter", "filter"),
189 MovedAttribute("filterfalse", "itertools", "itertools", "ifilterfalse", "filterfalse"),
190 MovedAttribute("input", "__builtin__", "builtins", "raw_input", "input"),
191 MovedAttribute("map", "itertools", "builtins", "imap", "map"),
192 MovedAttribute("range", "__builtin__", "builtins", "xrange", "range"),
193 MovedAttribute("reload_module", "__builtin__", "imp", "reload"),
194 MovedAttribute("reduce", "__builtin__", "functools"),
195 MovedAttribute("StringIO", "StringIO", "io"),
196 MovedAttribute("UserString", "UserString", "collections"),
197 MovedAttribute("xrange", "__builtin__", "builtins", "xrange", "range"),
198 MovedAttribute("zip", "itertools", "builtins", "izip", "zip"),
199 MovedAttribute("zip_longest", "itertools", "itertools", "izip_longest", "zip_longest"),
200
201 MovedModule("builtins", "__builtin__"),
202 MovedModule("configparser", "ConfigParser"),
203 MovedModule("copyreg", "copy_reg"),
204 MovedModule("dbm_gnu", "gdbm", "dbm.gnu"),
205 MovedModule("http_cookiejar", "cookielib", "http.cookiejar"),
206 MovedModule("http_cookies", "Cookie", "http.cookies"),
207 MovedModule("html_entities", "htmlentitydefs", "html.entities"),
208 MovedModule("html_parser", "HTMLParser", "html.parser"),
209 MovedModule("http_client", "httplib", "http.client"),
210 MovedModule("email_mime_multipart", "email.MIMEMultipart", "email.mime.multipart"),
211 MovedModule("email_mime_text", "email.MIMEText", "email.mime.text"),
212 MovedModule("email_mime_base", "email.MIMEBase", "email.mime.base"),
213 MovedModule("BaseHTTPServer", "BaseHTTPServer", "http.server"),
214 MovedModule("CGIHTTPServer", "CGIHTTPServer", "http.server"),
215 MovedModule("SimpleHTTPServer", "SimpleHTTPServer", "http.server"),
216 MovedModule("cPickle", "cPickle", "pickle"),
217 MovedModule("queue", "Queue"),
218 MovedModule("reprlib", "repr"),
219 MovedModule("socketserver", "SocketServer"),
220 MovedModule("_thread", "thread", "_thread"),
221 MovedModule("tkinter", "Tkinter"),
222 MovedModule("tkinter_dialog", "Dialog", "tkinter.dialog"),
223 MovedModule("tkinter_filedialog", "FileDialog", "tkinter.filedialog"),
224 MovedModule("tkinter_scrolledtext", "ScrolledText", "tkinter.scrolledtext"),
225 MovedModule("tkinter_simpledialog", "SimpleDialog", "tkinter.simpledialog"),
226 MovedModule("tkinter_tix", "Tix", "tkinter.tix"),
227 MovedModule("tkinter_ttk", "ttk", "tkinter.ttk"),
228 MovedModule("tkinter_constants", "Tkconstants", "tkinter.constants"),
229 MovedModule("tkinter_dnd", "Tkdnd", "tkinter.dnd"),
230 MovedModule("tkinter_colorchooser", "tkColorChooser",
231 "tkinter.colorchooser"),
232 MovedModule("tkinter_commondialog", "tkCommonDialog",
233 "tkinter.commondialog"),
234 MovedModule("tkinter_tkfiledialog", "tkFileDialog", "tkinter.filedialog"),
235 MovedModule("tkinter_font", "tkFont", "tkinter.font"),
236 MovedModule("tkinter_messagebox", "tkMessageBox", "tkinter.messagebox"),
237 MovedModule("tkinter_tksimpledialog", "tkSimpleDialog",
238 "tkinter.simpledialog"),
239 MovedModule("urllib_parse", __name__ + ".moves.urllib_parse", "urllib.parse"),
240 MovedModule("urllib_error", __name__ + ".moves.urllib_error", "urllib.error"),
241 MovedModule("urllib", __name__ + ".moves.urllib", __name__ + ".moves.urllib"),
242 MovedModule("urllib_robotparser", "robotparser", "urllib.robotparser"),
243 MovedModule("xmlrpc_client", "xmlrpclib", "xmlrpc.client"),
244 MovedModule("winreg", "_winreg"),
245 ]
246 for attr in _moved_attributes:
247 setattr(_MovedItems, attr.name, attr)
248 if isinstance(attr, MovedModule):
249 sys.modules[__name__ + ".moves." + attr.name] = attr
250 del attr
251
252 _MovedItems._moved_attributes = _moved_attributes
253
254 moves = sys.modules[__name__ + ".moves"] = _MovedItems(__name__ + ".moves")
255
256
258 """Lazy loading of moved objects in six.moves.urllib_parse"""
259
260
261 _urllib_parse_moved_attributes = [
262 MovedAttribute("ParseResult", "urlparse", "urllib.parse"),
263 MovedAttribute("parse_qs", "urlparse", "urllib.parse"),
264 MovedAttribute("parse_qsl", "urlparse", "urllib.parse"),
265 MovedAttribute("urldefrag", "urlparse", "urllib.parse"),
266 MovedAttribute("urljoin", "urlparse", "urllib.parse"),
267 MovedAttribute("urlparse", "urlparse", "urllib.parse"),
268 MovedAttribute("urlsplit", "urlparse", "urllib.parse"),
269 MovedAttribute("urlunparse", "urlparse", "urllib.parse"),
270 MovedAttribute("urlunsplit", "urlparse", "urllib.parse"),
271 MovedAttribute("quote", "urllib", "urllib.parse"),
272 MovedAttribute("quote_plus", "urllib", "urllib.parse"),
273 MovedAttribute("unquote", "urllib", "urllib.parse"),
274 MovedAttribute("unquote_plus", "urllib", "urllib.parse"),
275 MovedAttribute("urlencode", "urllib", "urllib.parse"),
276 ]
277 for attr in _urllib_parse_moved_attributes:
278 setattr(Module_six_moves_urllib_parse, attr.name, attr)
279 del attr
280
281 Module_six_moves_urllib_parse._moved_attributes = _urllib_parse_moved_attributes
282
283 sys.modules[__name__ + ".moves.urllib_parse"] = sys.modules[__name__ + ".moves.urllib.parse"] = Module_six_moves_urllib_parse(__name__ + ".moves.urllib_parse")
284
285
287 """Lazy loading of moved objects in six.moves.urllib_error"""
288
289
290 _urllib_error_moved_attributes = [
291 MovedAttribute("URLError", "urllib2", "urllib.error"),
292 MovedAttribute("HTTPError", "urllib2", "urllib.error"),
293 MovedAttribute("ContentTooShortError", "urllib", "urllib.error"),
294 ]
295 for attr in _urllib_error_moved_attributes:
296 setattr(Module_six_moves_urllib_error, attr.name, attr)
297 del attr
298
299 Module_six_moves_urllib_error._moved_attributes = _urllib_error_moved_attributes
300
301 sys.modules[__name__ + ".moves.urllib_error"] = sys.modules[__name__ + ".moves.urllib.error"] = Module_six_moves_urllib_error(__name__ + ".moves.urllib.error")
302
303
305 """Lazy loading of moved objects in six.moves.urllib_request"""
306
307
308 _urllib_request_moved_attributes = [
309 MovedAttribute("urlopen", "urllib2", "urllib.request"),
310 MovedAttribute("install_opener", "urllib2", "urllib.request"),
311 MovedAttribute("build_opener", "urllib2", "urllib.request"),
312 MovedAttribute("pathname2url", "urllib", "urllib.request"),
313 MovedAttribute("url2pathname", "urllib", "urllib.request"),
314 MovedAttribute("getproxies", "urllib", "urllib.request"),
315 MovedAttribute("Request", "urllib2", "urllib.request"),
316 MovedAttribute("OpenerDirector", "urllib2", "urllib.request"),
317 MovedAttribute("HTTPDefaultErrorHandler", "urllib2", "urllib.request"),
318 MovedAttribute("HTTPRedirectHandler", "urllib2", "urllib.request"),
319 MovedAttribute("HTTPCookieProcessor", "urllib2", "urllib.request"),
320 MovedAttribute("ProxyHandler", "urllib2", "urllib.request"),
321 MovedAttribute("BaseHandler", "urllib2", "urllib.request"),
322 MovedAttribute("HTTPPasswordMgr", "urllib2", "urllib.request"),
323 MovedAttribute("HTTPPasswordMgrWithDefaultRealm", "urllib2", "urllib.request"),
324 MovedAttribute("AbstractBasicAuthHandler", "urllib2", "urllib.request"),
325 MovedAttribute("HTTPBasicAuthHandler", "urllib2", "urllib.request"),
326 MovedAttribute("ProxyBasicAuthHandler", "urllib2", "urllib.request"),
327 MovedAttribute("AbstractDigestAuthHandler", "urllib2", "urllib.request"),
328 MovedAttribute("HTTPDigestAuthHandler", "urllib2", "urllib.request"),
329 MovedAttribute("ProxyDigestAuthHandler", "urllib2", "urllib.request"),
330 MovedAttribute("HTTPHandler", "urllib2", "urllib.request"),
331 MovedAttribute("HTTPSHandler", "urllib2", "urllib.request"),
332 MovedAttribute("FileHandler", "urllib2", "urllib.request"),
333 MovedAttribute("FTPHandler", "urllib2", "urllib.request"),
334 MovedAttribute("CacheFTPHandler", "urllib2", "urllib.request"),
335 MovedAttribute("UnknownHandler", "urllib2", "urllib.request"),
336 MovedAttribute("HTTPErrorProcessor", "urllib2", "urllib.request"),
337 MovedAttribute("urlretrieve", "urllib", "urllib.request"),
338 MovedAttribute("urlcleanup", "urllib", "urllib.request"),
339 MovedAttribute("URLopener", "urllib", "urllib.request"),
340 MovedAttribute("FancyURLopener", "urllib", "urllib.request"),
341 MovedAttribute("proxy_bypass", "urllib", "urllib.request"),
342 ]
343 for attr in _urllib_request_moved_attributes:
344 setattr(Module_six_moves_urllib_request, attr.name, attr)
345 del attr
346
347 Module_six_moves_urllib_request._moved_attributes = _urllib_request_moved_attributes
348
349 sys.modules[__name__ + ".moves.urllib_request"] = sys.modules[__name__ + ".moves.urllib.request"] = Module_six_moves_urllib_request(__name__ + ".moves.urllib.request")
350
351
353 """Lazy loading of moved objects in six.moves.urllib_response"""
354
355
356 _urllib_response_moved_attributes = [
357 MovedAttribute("addbase", "urllib", "urllib.response"),
358 MovedAttribute("addclosehook", "urllib", "urllib.response"),
359 MovedAttribute("addinfo", "urllib", "urllib.response"),
360 MovedAttribute("addinfourl", "urllib", "urllib.response"),
361 ]
362 for attr in _urllib_response_moved_attributes:
363 setattr(Module_six_moves_urllib_response, attr.name, attr)
364 del attr
365
366 Module_six_moves_urllib_response._moved_attributes = _urllib_response_moved_attributes
367
368 sys.modules[__name__ + ".moves.urllib_response"] = sys.modules[__name__ + ".moves.urllib.response"] = Module_six_moves_urllib_response(__name__ + ".moves.urllib.response")
369
370
372 """Lazy loading of moved objects in six.moves.urllib_robotparser"""
373
374
375 _urllib_robotparser_moved_attributes = [
376 MovedAttribute("RobotFileParser", "robotparser", "urllib.robotparser"),
377 ]
378 for attr in _urllib_robotparser_moved_attributes:
379 setattr(Module_six_moves_urllib_robotparser, attr.name, attr)
380 del attr
381
382 Module_six_moves_urllib_robotparser._moved_attributes = _urllib_robotparser_moved_attributes
383
384 sys.modules[__name__ + ".moves.urllib_robotparser"] = sys.modules[__name__ + ".moves.urllib.robotparser"] = Module_six_moves_urllib_robotparser(__name__ + ".moves.urllib.robotparser")
385
386
388 """Create a six.moves.urllib namespace that resembles the Python 3 namespace"""
389 parse = sys.modules[__name__ + ".moves.urllib_parse"]
390 error = sys.modules[__name__ + ".moves.urllib_error"]
391 request = sys.modules[__name__ + ".moves.urllib_request"]
392 response = sys.modules[__name__ + ".moves.urllib_response"]
393 robotparser = sys.modules[__name__ + ".moves.urllib_robotparser"]
394
396 return ['parse', 'error', 'request', 'response', 'robotparser']
397
398
399 sys.modules[__name__ + ".moves.urllib"] = Module_six_moves_urllib(__name__ + ".moves.urllib")
400
401
403 """Add an item to six.moves."""
404 setattr(_MovedItems, move.name, move)
405
406
408 """Remove item from six.moves."""
409 try:
410 delattr(_MovedItems, name)
411 except AttributeError:
412 try:
413 del moves.__dict__[name]
414 except KeyError:
415 raise AttributeError("no such move, %r" % (name,))
416
417
418 if PY3:
419 _meth_func = "__func__"
420 _meth_self = "__self__"
421
422 _func_closure = "__closure__"
423 _func_code = "__code__"
424 _func_defaults = "__defaults__"
425 _func_globals = "__globals__"
426
427 _iterkeys = "keys"
428 _itervalues = "values"
429 _iteritems = "items"
430 _iterlists = "lists"
431 else:
432 _meth_func = "im_func"
433 _meth_self = "im_self"
434
435 _func_closure = "func_closure"
436 _func_code = "func_code"
437 _func_defaults = "func_defaults"
438 _func_globals = "func_globals"
439
440 _iterkeys = "iterkeys"
441 _itervalues = "itervalues"
442 _iteritems = "iteritems"
443 _iterlists = "iterlists"
444
445
446 try:
447 advance_iterator = next
448 except NameError:
451 next = advance_iterator
452
453
454 try:
455 callable = callable
456 except NameError:
458 return any("__call__" in klass.__dict__ for klass in type(obj).__mro__)
459
460
461 if PY3:
464
465 create_bound_method = types.MethodType
466
467 Iterator = object
468 else:
470 return unbound.im_func
471
473 return types.MethodType(func, obj, obj.__class__)
474
476
478 return type(self).__next__(self)
479
480 callable = callable
481 _add_doc(get_unbound_function,
482 """Get the function out of a possibly unbound function""")
483
484
485 get_method_function = operator.attrgetter(_meth_func)
486 get_method_self = operator.attrgetter(_meth_self)
487 get_function_closure = operator.attrgetter(_func_closure)
488 get_function_code = operator.attrgetter(_func_code)
489 get_function_defaults = operator.attrgetter(_func_defaults)
490 get_function_globals = operator.attrgetter(_func_globals)
491
492
494 """Return an iterator over the keys of a dictionary."""
495 return iter(getattr(d, _iterkeys)(**kw))
496
498 """Return an iterator over the values of a dictionary."""
499 return iter(getattr(d, _itervalues)(**kw))
500
502 """Return an iterator over the (key, value) pairs of a dictionary."""
503 return iter(getattr(d, _iteritems)(**kw))
504
506 """Return an iterator over the (key, [values]) pairs of a dictionary."""
507 return iter(getattr(d, _iterlists)(**kw))
508
509
510 if PY3:
512 return s.encode("latin-1")
515 unichr = chr
516 if sys.version_info[1] <= 1:
519 else:
520
521 int2byte = operator.methodcaller("to_bytes", 1, "big")
522 byte2int = operator.itemgetter(0)
523 indexbytes = operator.getitem
524 iterbytes = iter
525 import io
526 StringIO = io.StringIO
527 BytesIO = io.BytesIO
528 intern = sys.intern
529 file = io.IOBase
530 else:
533
536 unichr = unichr
537 int2byte = chr
544 import StringIO
545 StringIO = BytesIO = StringIO.StringIO
546 import __builtin__
547 intern = __builtin__.intern
548 file = __builtin__.file
549 _add_doc(b, """Byte literal""")
550 _add_doc(u, """Text literal""")
551
552
553 if PY3:
554 exec_ = getattr(moves.builtins, "exec")
555
556
558 if value.__traceback__ is not tb:
559 raise value.with_traceback(tb)
560 raise value
561
562 else:
563 - def exec_(_code_, _globs_=None, _locs_=None):
564 """Execute code in a namespace."""
565 if _globs_ is None:
566 frame = sys._getframe(1)
567 _globs_ = frame.f_globals
568 if _locs_ is None:
569 _locs_ = frame.f_locals
570 del frame
571 elif _locs_ is None:
572 _locs_ = _globs_
573 exec("""exec _code_ in _globs_, _locs_""")
574
575
576 exec_("""def reraise(tp, value, tb=None):
577 raise tp, value, tb
578 """)
579
580
581 print_ = getattr(moves.builtins, "print", None)
582 if print_ is None:
584 """The new-style print function for Python 2.4 and 2.5."""
585 fp = kwargs.pop("file", sys.stdout)
586 if fp is None:
587 return
588 def write(data):
589 if not isinstance(data, basestring):
590 data = str(data)
591
592 if (isinstance(fp, file) and
593 isinstance(data, unicode) and
594 fp.encoding is not None):
595 errors = getattr(fp, "errors", None)
596 if errors is None:
597 errors = "strict"
598 data = data.encode(fp.encoding, errors)
599 fp.write(data)
600 want_unicode = False
601 sep = kwargs.pop("sep", None)
602 if sep is not None:
603 if isinstance(sep, unicode):
604 want_unicode = True
605 elif not isinstance(sep, str):
606 raise TypeError("sep must be None or a string")
607 end = kwargs.pop("end", None)
608 if end is not None:
609 if isinstance(end, unicode):
610 want_unicode = True
611 elif not isinstance(end, str):
612 raise TypeError("end must be None or a string")
613 if kwargs:
614 raise TypeError("invalid keyword arguments to print()")
615 if not want_unicode:
616 for arg in args:
617 if isinstance(arg, unicode):
618 want_unicode = True
619 break
620 if want_unicode:
621 newline = unicode("\n")
622 space = unicode(" ")
623 else:
624 newline = "\n"
625 space = " "
626 if sep is None:
627 sep = space
628 if end is None:
629 end = newline
630 for i, arg in enumerate(args):
631 if i:
632 write(sep)
633 write(arg)
634 write(end)
635
636 _add_doc(reraise, """Reraise an exception.""")
637
638
642
656 return wrapper
657
664