Package pyxb :: Package utils :: Module six
[hide private]
[frames] | no frames]

Source Code for Module pyxb.utils.six

  1  """Utilities for writing code that runs on Python 2 and 3""" 
  2   
  3  # Copyright (c) 2010-2014 Benjamin Peterson 
  4  # 
  5  # Permission is hereby granted, free of charge, to any person obtaining a copy 
  6  # of this software and associated documentation files (the "Software"), to deal 
  7  # in the Software without restriction, including without limitation the rights 
  8  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
  9  # copies of the Software, and to permit persons to whom the Software is 
 10  # furnished to do so, subject to the following conditions: 
 11  # 
 12  # The above copyright notice and this permission notice shall be included in all 
 13  # copies or substantial portions of the Software. 
 14  # 
 15  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 16  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 17  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 18  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 19  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 20  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
 21  # SOFTWARE. 
 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  # Useful for very coarse version differentiation. 
 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          # Jython always uses 32 bits. 
 71          MAXSIZE = int((1 << 31) - 1) 
 72      else: 
 73          # It's possible to have sizeof(long) != sizeof(Py_ssize_t). 
74 - class X(object):
75 - def __len__(self):
76 return 1 << 31
77 try: 78 len(X()) 79 except OverflowError: 80 # 32-bit 81 MAXSIZE = int((1 << 31) - 1) 82 else: 83 # 64-bit 84 MAXSIZE = int((1 << 63) - 1) 85 del X 86 87
88 -def _add_doc(func, doc):
89 """Add documentation to a function.""" 90 func.__doc__ = doc
91 92
93 -def _import_module(name):
94 """Import module, returning the module after the last dot.""" 95 __import__(name) 96 return sys.modules[name]
97 98
99 -class _LazyDescr(object):
100
101 - def __init__(self, name):
102 self.name = name
103
104 - def __get__(self, obj, tp):
105 result = self._resolve() 106 setattr(obj, self.name, result) # Invokes __set__. 107 # This is a bit ugly, but it avoids running this again. 108 delattr(obj.__class__, self.name) 109 return result
110 111
112 -class MovedModule(_LazyDescr):
113
114 - def __init__(self, name, old, new=None):
115 super(MovedModule, self).__init__(name) 116 if PY3: 117 if new is None: 118 new = name 119 self.mod = new 120 else: 121 self.mod = old
122
123 - def _resolve(self):
124 return _import_module(self.mod)
125
126 - def __getattr__(self, attr):
127 # Hack around the Django autoreloader. The reloader tries to get 128 # __file__ or __name__ of every module in sys.modules. This doesn't work 129 # well if this MovedModule is for an module that is unavailable on this 130 # machine (like winreg on Unix systems). Thus, we pretend __file__ and 131 # __name__ don't exist if the module hasn't been loaded yet. See issues 132 # #51 and #53. 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
141 -class _LazyModule(types.ModuleType):
142
143 - def __init__(self, name):
144 super(_LazyModule, self).__init__(name) 145 self.__doc__ = self.__class__.__doc__
146
147 - def __dir__(self):
148 attrs = ["__doc__", "__name__"] 149 attrs += [attr.name for attr in self._moved_attributes] 150 return attrs
151 152 # Subclasses should override this 153 _moved_attributes = []
154 155
156 -class MovedAttribute(_LazyDescr):
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
176 - def _resolve(self):
177 module = _import_module(self.mod) 178 return getattr(module, self.attr)
179 180 181
182 -class _MovedItems(_LazyModule):
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
257 -class Module_six_moves_urllib_parse(_LazyModule):
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
286 -class Module_six_moves_urllib_error(_LazyModule):
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
304 -class Module_six_moves_urllib_request(_LazyModule):
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
352 -class Module_six_moves_urllib_response(_LazyModule):
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
371 -class Module_six_moves_urllib_robotparser(_LazyModule):
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
387 -class Module_six_moves_urllib(types.ModuleType):
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
395 - def __dir__(self):
396 return ['parse', 'error', 'request', 'response', 'robotparser']
397 398 399 sys.modules[__name__ + ".moves.urllib"] = Module_six_moves_urllib(__name__ + ".moves.urllib") 400 401
402 -def add_move(move):
403 """Add an item to six.moves.""" 404 setattr(_MovedItems, move.name, move)
405 406
407 -def remove_move(name):
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:
449 - def advance_iterator(it):
450 return it.next()
451 next = advance_iterator 452 453 454 try: 455 callable = callable 456 except NameError:
457 - def callable(obj):
458 return any("__call__" in klass.__dict__ for klass in type(obj).__mro__)
459 460 461 if PY3:
462 - def get_unbound_function(unbound):
463 return unbound
464 465 create_bound_method = types.MethodType 466 467 Iterator = object 468 else:
469 - def get_unbound_function(unbound):
470 return unbound.im_func
471
472 - def create_bound_method(func, obj):
473 return types.MethodType(func, obj, obj.__class__)
474
475 - class Iterator(object):
476
477 - def next(self):
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
493 -def iterkeys(d, **kw):
494 """Return an iterator over the keys of a dictionary.""" 495 return iter(getattr(d, _iterkeys)(**kw))
496
497 -def itervalues(d, **kw):
498 """Return an iterator over the values of a dictionary.""" 499 return iter(getattr(d, _itervalues)(**kw))
500
501 -def iteritems(d, **kw):
502 """Return an iterator over the (key, value) pairs of a dictionary.""" 503 return iter(getattr(d, _iteritems)(**kw))
504
505 -def iterlists(d, **kw):
506 """Return an iterator over the (key, [values]) pairs of a dictionary.""" 507 return iter(getattr(d, _iterlists)(**kw))
508 509 510 if PY3:
511 - def b(s):
512 return s.encode("latin-1")
513 - def u(s):
514 return s
515 unichr = chr 516 if sys.version_info[1] <= 1:
517 - def int2byte(i):
518 return bytes((i,))
519 else: 520 # This is about 2x faster than the implementation above on 3.2+ 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:
531 - def b(s):
532 return s
533 # Workaround for standalone backslash
534 - def u(s):
535 return unicode(s.replace(r'\\', r'\\\\'), "unicode_escape")
536 unichr = unichr 537 int2byte = chr
538 - def byte2int(bs):
539 return ord(bs[0])
540 - def indexbytes(buf, i):
541 return ord(buf[i])
542 - def iterbytes(buf):
543 return (ord(byte) for byte in buf)
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
557 - def reraise(tp, value, tb=None):
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: 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
639 -def with_metaclass(meta, *bases):
640 """Create a base class with a metaclass.""" 641 return meta("NewBase", bases, {})
642
643 -def add_metaclass(metaclass):
644 """Class decorator for creating a class with a metaclass.""" 645 def wrapper(cls): 646 orig_vars = cls.__dict__.copy() 647 orig_vars.pop('__dict__', None) 648 orig_vars.pop('__weakref__', None) 649 slots = orig_vars.get('__slots__') 650 if slots is not None: 651 if isinstance(slots, str): 652 slots = [slots] 653 for slots_var in slots: 654 orig_vars.pop(slots_var) 655 return metaclass(cls.__name__, cls.__bases__, orig_vars)
656 return wrapper 657
658 -def unicode_convertible (clazz):
659 if not PY3: 660 import pyxb 661 clazz.__unicode__ = clazz.__str__ 662 clazz.__str__ = lambda _s: _s.__unicode__().encode(pyxb._OutputEncoding) 663 return clazz
664