0
# Version Detection and Type Constants
1
2
Core constants and type definitions for Python version detection and cross-version type checking. These utilities enable writing code that works consistently across Python 2 and 3 by providing unified type constants and version detection flags.
3
4
## Capabilities
5
6
### Version Detection Constants
7
8
Boolean constants that identify the Python version at runtime, enabling version-specific code paths.
9
10
```python { .api }
11
PY2: bool # True if running on Python 2, False otherwise
12
PY3: bool # True if running on Python 3, False otherwise
13
PY34: bool # True if running on Python 3.4 or higher, False otherwise
14
```
15
16
**Usage Examples:**
17
18
```python
19
import six
20
21
if six.PY2:
22
# Python 2 specific code
23
import cPickle as pickle
24
elif six.PY3:
25
# Python 3 specific code
26
import pickle
27
28
# Feature detection for Python 3.4+ features
29
if six.PY34:
30
from importlib.util import spec_from_loader
31
else:
32
spec_from_loader = None
33
```
34
35
### Type Constants for isinstance() Checks
36
37
Tuple constants containing the appropriate types for cross-version isinstance() checks.
38
39
```python { .api }
40
string_types: tuple # String types tuple for isinstance() checks
41
integer_types: tuple # Integer types tuple for isinstance() checks
42
class_types: tuple # Class types tuple for isinstance() checks
43
```
44
45
**Type Details:**
46
- `string_types`: `(str,)` in Python 3, `(basestring,)` in Python 2
47
- `integer_types`: `(int,)` in Python 3, `(int, long)` in Python 2
48
- `class_types`: `(type,)` in Python 3, `(type, types.ClassType)` in Python 2
49
50
**Usage Examples:**
51
52
```python
53
import six
54
55
def process_string(value):
56
if isinstance(value, six.string_types):
57
return value.upper()
58
raise TypeError("Expected string type")
59
60
def process_number(value):
61
if isinstance(value, six.integer_types):
62
return value * 2
63
raise TypeError("Expected integer type")
64
```
65
66
### Individual Type Constants
67
68
Direct references to the appropriate type for each Python version.
69
70
```python { .api }
71
text_type: type # Text string type (str in PY3, unicode in PY2)
72
binary_type: type # Binary string type (bytes in PY3, str in PY2)
73
```
74
75
**Usage Examples:**
76
77
```python
78
import six
79
80
def ensure_unicode(value):
81
if isinstance(value, six.text_type):
82
return value
83
return value.decode('utf-8')
84
85
def ensure_bytes(value):
86
if isinstance(value, six.binary_type):
87
return value
88
return value.encode('utf-8')
89
```
90
91
### Maximum Integer Size
92
93
Cross-version constant for the maximum value an integer can hold.
94
95
```python { .api }
96
MAXSIZE: int # Maximum value a variable of integer type can take
97
```
98
99
This constant provides `sys.maxsize` equivalent across Python versions, handling platform-specific differences including Jython compatibility.
100
101
**Usage Example:**
102
103
```python
104
import six
105
106
def check_large_number(value):
107
if value > six.MAXSIZE:
108
raise OverflowError("Number too large")
109
return value
110
```
111
112
### Import Specification Utility
113
114
Import specification utility available in Python 3.4+.
115
116
```python { .api }
117
spec_from_loader: Callable | None # importlib.util.spec_from_loader in PY34+, None otherwise
118
```
119
120
This provides access to `importlib.util.spec_from_loader` when available, or `None` for older Python versions.
121
122
**Usage Example:**
123
124
```python
125
import six
126
127
if six.spec_from_loader is not None:
128
# Use modern import specification
129
spec = six.spec_from_loader("mymodule", loader)
130
else:
131
# Fallback for older Python versions
132
spec = None
133
```
134
135
## Module Metadata
136
137
```python { .api }
138
__author__: str # "Benjamin Peterson <benjamin@python.org>"
139
__version__: str # "1.17.0"
140
```
141
142
These constants provide access to the six library's metadata for version checking and attribution purposes.