Python sys.maxsize() Method

  1. Syntax of Python sys.maxsize Method
  2. Example Codes: Use of the sys.maxsize() Method
  3. Example Codes: Check the Maximum Size of a List Using the sys.maxsize() Method
  4. Example Codes: The sys.maxsize() vs. sys.maxint Methods
  5. Example Codes: Use csv.field_size_limit(sys.maxsize) in Python

In Python, the sys module has a method known as maxsize(). This method returns the largest value a variable of Py_ssize_t can hold.

How To Find Length Of Python List |...
How To Find Length Of Python List | 2 Methods | Python Tutorial 2022 | Python Len Function

The Py_ssize_t is an integer that gives the maximum value a variable can take. The sizes are different depending on the bits of your operating system.

The 32-bit has a size of (2 power 31)-1, and 64-bit has a (2 power 63)-1 size.

Syntax of Python sys.maxsize Method

sys.maxsize()

Parameter

This method does not take any parameters.

Return

This method returns the maximum size value of Py_ssize_t depending on the platform type.

Example Codes: Use of the sys.maxsize() Method

To implement the method sys.maxsize() and check the maximum size value, we can import the sys module and use the method maxsize(). Depending on the platform architecture type, the sys.maxsize() method returns its maximum value size on the console.

Below is the implementation of the 32-bit and 64-bit OS and running the same sys.maxsize() method.

32-Bit Platform

# import the sys module to use the maxsize() method
import sys
# returns the maximum size
size = sys.maxsize
print("The maximum size of a 32-bit platform is:" , size)

Output:

The maximum size of a 32-bit platform is: 2147483647

64-Bit Platform

import sys
# returns the maximum size
size = sys.maxsize
print("The maximum size of a 32-bit platform is:" , size)

Output:

The maximum size of a 64-bit platform is: 9223372036854775807

Example Codes: Check the Maximum Size of a List Using the sys.maxsize() Method

To check the maximum size of our system, we can use the range() method to pass the maximum size of a list and then check its length. Similarly, in the second example, we exceeded the maximum size, and the Python interpreter catches the exception and returns the int too large to convert to C ssize_t error.

In the below examples, we can observe the effect of having a limit on Py_ssize_t. It is impossible to index a list with a greater element than its size as it won’t accept a non-Py_ssize_t.

Regarding the dictionary data structure, Py_ssize_t uses the hash because Python does not use a LinkedList for its implementation. Similarly, we cannot have more size in the dictionary than the size of Py_ssize_t.

With Maximum Size

import sys

size = sys.maxsize
# creates the max length
list = range(size)
# returns the length of a list
print("The maximum length of a list:" , len(list))
print("List is created successfully")

Output:

# maximum size limit on a 64-bit platform
The maximum length of a list: 9223372036854775807
List is created successfully

With Greater Than the Maximum Size

import sys

size = sys.maxsize
# handles the exception
try:
    # creates a list with maximum size + 1
    list = range(size + 1)
    # check the maximum size
    print(len(list))
    print("List is created successfully")
# exception if the size goes beyond the maximum size
except Exception as exception:
    print("Exception caught: ", exception)
    print("List is not created due to above exception")

Output:

# output shows exception occurs
Exception caught:  Python int too large to convert to C ssize_t
List is not created due to above exception

Example Codes: The sys.maxsize() vs. sys.maxint Methods

The sys.maxint() method no longer supports Python 3 as integers. If we use this method or constant, we will get the following AttributeError: module 'sys' has no attribute 'maxint'.

To overcome this in Python 3.0, another constant, sys.maxsize, was introduced, which we know returns the maximum value of Py_ssize_t. In Python 3, int and long int are merged.

The first implementation shows the example of AttributeError, and the second source code reveals a better understanding of the maxint.

Attribute Error

import sys

li = [20, 2, 23, 88, 3, 63, 12]
# sys.maxint is not supported in python 3. We need to use python version < 3.0
min_value = sys.maxint

for i in range(0, len(li)):
    if li[i] < min_value:
        min_value = li[i]

print("Min value : " + str(min_value))

Output:

AttributeError: module 'sys' has no attribute 'maxint'

maxint Implementation

import sys

max_int = sys.maxsize
min_int = sys.maxsize - 1
long_int = sys.maxsize + 1

print("Maximum integer size is : " + str(max_int)+" , "+str(type(max_int)))
print("Maximum integer size-1 is :" + str(max_int)+" , "+str(type(min_int)))
print("Maximum integer size+1 is :" + str(max_int)+" , "+str(type(long_int)))

Output:

Maximum integer size is : 9223372036854775807 , <class 'int'>
Maximum integer size-1 is :9223372036854775807 , <class 'int'>
Maximum integer size+1 is :9223372036854775807 , <class 'int'>

Example Codes: Use csv.field_size_limit(sys.maxsize) in Python

In Python, when we read the CSV file containing huge fields, it may throw an exception saying _csv.Error: field larger than field limit. The appropriate solution would be not to skip a few fields and their lines.

To analyze the CSV, we need to increase the field_size_limit. For this, we need to implement the following source code.

import sys
# to use the field_size_limit method
import csv
maximum_Integer = sys.maxsize

while True:
    # read the csv with huge fields
    with open('myfile.csv', newline='') as f:
    reader = csv.reader(f)
    for row in reader:
        print(row)
    # Here, we reduce the size if there is an overflow error
    try:
        csv.field_size_limit(maximum_Integer)
        break
    except OverflowError:
        maximum_Integer = int(maximum_Integer/10)

As the output, the fields of myfile.csv will be printed.

Related Article - Python Sys

  • Python sys.argv List
  • Python sys.stdout Method
  • Python sys.exit() Method