source: sandbox/compat_python2_vs_3.py @ 1198:155bd636297f

Last change on this file since 1198:155bd636297f was 1198:155bd636297f, checked in by Stefan Schwarzer <sschwarzer@…>, 6 years ago
Program to check input and result types in different Python version.
File size: 1.7 KB
Line 
1"""
2Determine various types used in Python 2 and 3.
3
4This program should be runnable in all Python versions, starting from
5version 2.6.
6"""
7
8from __future__ import print_function
9from __future__ import unicode_literals
10
11import ftplib
12import sys
13
14
15class TypesTester(object):
16
17    def __init__(self, callable):
18        self.callable = callable
19
20    def test_call(self, *args, **kwargs):
21        should_print_result = kwargs.pop("print_result", False)
22        print("Callable: {0}; Args: {1}; Kwargs: {2}".format(self.callable,
23                                                             args, kwargs))
24        try:
25            result = self.callable(*args, **kwargs)
26        except Exception as exc:
27            print("Exception in `test_call`: {0}".format(exc))
28        else:
29            print("Result type: {0}".format(type(result)))
30            if should_print_result:
31                print("Result: {0!r}".format(result))
32        print()
33        return result
34
35
36def test_types(callable, *args, **kwargs):
37    tester = TypesTester(callable)
38    return tester.test_call(*args, **kwargs)
39
40
41def printer(*args, **kwargs):
42    return TypesTester(print).test_call
43
44
45def main():
46    print("Running under Python {0.major}.{0.minor}.{0.micro} ...".
47          format(sys.version_info))
48    print()
49    test_types(open, b"/etc/passwd", print_result=True)
50    test_types(open, "/etc/passwd", print_result=True)
51    ftp = ftplib.FTP("localhost", 'ftptest',
52                     'd605581757de5eb56d568a4419f4126e')
53    try:
54        cwd = test_types(ftp.pwd, print_result=True)
55        test_types(ftp.cwd, cwd)
56        #ftp.retrlines("LIST", callback=printer())
57    finally:
58        ftp.close()
59
60
61if __name__ == "__main__":
62    main()
Note: See TracBrowser for help on using the repository browser.