Maximum Characters That Can Be Stuffed into Raw_Input() in Python

Maximum characters that can be stuffed into raw_input() in Python

Are you sure of the fact that your 10k long word doesn't contain newlines?


raw_input([prompt])

If the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that. When EOF is read, EOFError is raised.

...

If the readline module was loaded, then raw_input() will use it to provide elaborate line editing and history features.

There is no maximum limit (in python) of the buffer returned by raw_input, and as I tested some big length of input to stdin I could not reproduce your result. I tried to search the web for information regarding this but came up with nothing that would help me answer your question.

my tests

  :/tmp% python -c 'print "A"*1000000' | python -c 'print len (raw_input ())';
1000000
:/tmp% python -c 'print "A"*210012300' | python -c 'print len (raw_input ())';
210012300
:/tmp% python -c 'print "A"*100+"\n"+"B"*100' | python -c 'print len (raw_input ())';
100

Python raw_input() limit with Mac OS X Terminal?

I'd say it's a limitation/bug with the OSX Terminal - try running the script with input via IDLE and see whether you still hit the same problem.

As for better ways of dealing with large input - it totally depends on your requirements but some ways could be:

  • Import text from a file
  • Create some kind of GUI/frontend to handle text input via more user friendly controls

python input prompt string length

It's not python limitation, Turn off canonical input processing:

stty -icanon

Buffer Overflow Protective mechanisms in Python

The buffer overflow attack is a different topic and it doesn't apply here as long as the implementation of raw_input is correct (meaning it's not writing beyond the buffer that it has allocated for storing the input).
Let's assume the implementation of input_raw is safe.

Like many structures in python raw_input will store its input in a dynamically allocated and dynamically increasing buffer. The initially allocated buffer for storing the input is normally small (perhaps a few dozen elements) and as you keep filling up the buffer it keeps getting extended (reallocated with a larger size to accommodate even more elements).

There is for sure a hard limit due to the OS, hardware limitations and because of the implementation itself. For a 32bit platform running a 32bit python the limit is most likely 2**32-1 (4 Gibibytes or at least 2).

In worst case python could exhaust system memory if there are no per process limits enforced by the OS. But even so on Linux for example the oom handler will kill the process with the highest memory use, which could be exactly the python process which is misbehaving (but it could be also another legit process).

Why Does This Program Run on Linux Python Shell But Not on Windows?

I added a few modifications to your script.
It works fine on Python 2.7 for Windows.
Here's the code:

import csv

csvname = raw_input("Enter csv name: ")

sbuxfile = csv.reader(open(csvname), delimiter=',', quotechar='|')
# List of Data
row5, row8, row3, row7, avgequity, roe1, roe2 = ([] for i in range(7))

count = 0
# Grab data and numerical values from CSV.
for row in sbuxfile:
count += 1
if count == 8:
row8 = row
elif count == 5:
row5 = row
elif count == 3:
row3 = row
elif count == 7:
row7 = row


a = 1

# Perform calculations for average equity and ROE.
while a < 8 :
if a == 1:
avgequity.append(round(float(row8[a]),2))
roe1.append(float(row5[a]) / float(row8[a]))
roe2.append((float(row5[a]) / float(row3[a])) * (float(row3[a]) / float(row7[a])) * (float(row7[a]) / float(row8[a])))
a+=1 #added this line
else:
avgequity.append(round(float(row8[a]),2) + float(row8[a-1])/2) #rewrote this line as it had an error
roe1.append(float(row5[a]) / float(row8[a]))
roe2.append((float(row5[a]) / float(row3[a])) * (float(row3[a]) / float(row7[a])) * (float(row7[a]) / ((float(row8[a]) + float(row8[a-1]))/2)))
a+=1

print "\nAverage equity is " + str(avgequity) + "\n"
print "ROE method 1 is " + str(roe1) + "\n"
print "ROE method 2 is " + str(roe2)

The output was :
Average equity is [2071.11, 3505.7650000000003, 3325.3650000000002, 3273.6400000000003, 3398.375, 4187.76, 5197.549999999999]

ROE method 1 is [0.12812453225565035, 0.15742791098732495, 0.23651124740462906, 0.2532005689900426, 0.2944854035689894, 0.1283120464917753, 0.2573271287452037]

ROE method 2 is [0.12812453225565038, 0.17126298080734237, 0.21680660107401206, 0.2613058810726202, 0.29811440335236883, 0.1466466034500227, 0.2814118207249569]



Related Topics



Leave a reply



Submit