The new-style print function for Python 2.4 and 2.5.
(*args, **kwargs)
| 762 | print_ = getattr(moves.builtins, "print", None) |
| 763 | if print_ is None: |
| 764 | def print_(*args, **kwargs): |
| 765 | """The new-style print function for Python 2.4 and 2.5.""" |
| 766 | fp = kwargs.pop("file", sys.stdout) |
| 767 | if fp is None: |
| 768 | return |
| 769 | |
| 770 | def write(data): |
| 771 | if not isinstance(data, basestring): |
| 772 | data = str(data) |
| 773 | # If the file has an encoding, encode unicode with it. |
| 774 | if (isinstance(fp, file) and |
| 775 | isinstance(data, unicode) and |
| 776 | fp.encoding is not None): |
| 777 | errors = getattr(fp, "errors", None) |
| 778 | if errors is None: |
| 779 | errors = "strict" |
| 780 | data = data.encode(fp.encoding, errors) |
| 781 | fp.write(data) |
| 782 | want_unicode = False |
| 783 | sep = kwargs.pop("sep", None) |
| 784 | if sep is not None: |
| 785 | if isinstance(sep, unicode): |
| 786 | want_unicode = True |
| 787 | elif not isinstance(sep, str): |
| 788 | raise TypeError("sep must be None or a string") |
| 789 | end = kwargs.pop("end", None) |
| 790 | if end is not None: |
| 791 | if isinstance(end, unicode): |
| 792 | want_unicode = True |
| 793 | elif not isinstance(end, str): |
| 794 | raise TypeError("end must be None or a string") |
| 795 | if kwargs: |
| 796 | raise TypeError("invalid keyword arguments to print()") |
| 797 | if not want_unicode: |
| 798 | for arg in args: |
| 799 | if isinstance(arg, unicode): |
| 800 | want_unicode = True |
| 801 | break |
| 802 | if want_unicode: |
| 803 | newline = unicode("\n") |
| 804 | space = unicode(" ") |
| 805 | else: |
| 806 | newline = "\n" |
| 807 | space = " " |
| 808 | if sep is None: |
| 809 | sep = space |
| 810 | if end is None: |
| 811 | end = newline |
| 812 | for i, arg in enumerate(args): |
| 813 | if i: |
| 814 | write(sep) |
| 815 | write(arg) |
| 816 | write(end) |
| 817 | if sys.version_info[:2] < (3, 3): |
| 818 | _print = print_ |
| 819 |