Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Begin development for Python 3 #143

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion darwin_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def _get_proc_info_by_pid(pid):

if status == 0:
# This means to data was written, this is an error
raise Exception,"Errno:"+str(get_ctypes_errno())+", Error: "+get_ctypes_error_str()
raise Exception("Errno:"+str(get_ctypes_errno())+", Error: "+get_ctypes_error_str())


def get_process_cpu_time(pid):
Expand Down
38 changes: 19 additions & 19 deletions emulcomm.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ def _is_already_connected_exception(exceptionobj):
# Convert the errno to and error string name
try:
errname = errno.errorcode[errnum]
except Exception,e:
except Exception as e:
# The error is unknown for some reason...
errname = None

Expand Down Expand Up @@ -267,7 +267,7 @@ def _is_addr_in_use_exception(exceptionobj):
# Convert the errno to and error string name
try:
errname = errno.errorcode[errnum]
except Exception,e:
except Exception as e:
# The error is unknown for some reason...
errname = None

Expand Down Expand Up @@ -304,7 +304,7 @@ def _is_addr_unavailable_exception(exceptionobj):
# Convert the errno to and error string name
try:
errname = errno.errorcode[errnum]
except Exception,e:
except Exception as e:
# The error is unknown for some reason...
errname = None

Expand Down Expand Up @@ -341,7 +341,7 @@ def _is_conn_refused_exception(exceptionobj):
# Convert the errno to and error string name
try:
errname = errno.errorcode[errnum]
except Exception,e:
except Exception as e:
# The error is unknown for some reason...
errname = None

Expand Down Expand Up @@ -379,7 +379,7 @@ def _is_conn_aborted_exception(exceptionobj):
# Convert the errno to and error string name
try:
errname = errno.errorcode[errnum]
except Exception,e:
except Exception as e:
# The error is unknown for some reason...
errname = None

Expand Down Expand Up @@ -419,7 +419,7 @@ def _is_network_down_exception(exceptionobj):
# Convert the errno to and error string name
try:
errname = errno.errorcode[errnum]
except Exception,e:
except Exception as e:
# The error is unknown for some reason...
errname = None

Expand Down Expand Up @@ -460,7 +460,7 @@ def _is_recoverable_network_exception(exceptionobj):
# Convert the errno to and error string name
try:
errname = errno.errorcode[errnum]
except Exception,e:
except Exception as e:
# The error is unknown for some reason...
errname = None

Expand Down Expand Up @@ -960,7 +960,7 @@ def sendmessage(destip, destport, message, localip, localport):

return bytessent

except Exception, e:
except Exception as e:

try:
# If we're borrowing the socket, closing is not appropriate.
Expand Down Expand Up @@ -1064,7 +1064,7 @@ def listenformessage(localip, localport):
# preserve send functionality on this port.
_BOUND_SOCKETS[("UDP", localip, localport)] = sock

except Exception, e:
except Exception as e:

# Check if this an already in use error
if _is_addr_in_use_exception(e):
Expand Down Expand Up @@ -1218,7 +1218,7 @@ def _timed_conn_initialize(localip,localport,destip,destport, timeout):
sock.connect((destip, destport))
connected = True
break
except Exception, e:
except Exception as e:
# Check if we are already connected
if _is_already_connected_exception(e):
connected = True
Expand Down Expand Up @@ -1385,7 +1385,7 @@ def openconnection(destip, destport,localip, localport, timeout):

# Register this socket as an outsocket
nanny.tattle_add_item('outsockets',id(sock))
except Exception, e:
except Exception as e:

# Check if this an already in use error
if _is_addr_in_use_exception(e):
Expand Down Expand Up @@ -1498,7 +1498,7 @@ def listenforconnection(localip, localport):
else:
sock.listen(5)

except Exception, e:
except Exception as e:

# Check if this an already in use error
if _is_addr_in_use_exception(e):
Expand Down Expand Up @@ -1593,7 +1593,7 @@ def _check_socket_state(realsock, waitfor="rw", timeout=0.0):
"""
# Check that waitfor is valid
if waitfor not in ["rw","r","w"]:
raise Exception, "Illegal waitfor argument!"
raise Exception("Illegal waitfor argument!")

# Array to hold the socket
sock_array = [realsock]
Expand Down Expand Up @@ -1755,7 +1755,7 @@ def close(self):



def recv(self,bytes):
def recv(self,some_bytes):
"""
<Purpose>
Receives data from a socket. It may receive fewer bytes than
Expand Down Expand Up @@ -1801,7 +1801,7 @@ def recv(self,bytes):
raise KeyError # Socket is closed locally

# Try to recieve the data
data_recieved = sock.recv(bytes)
data_recieved = sock.recv(some_bytes)

# Calculate the length of the data
data_length = len(data_recieved)
Expand All @@ -1825,7 +1825,7 @@ def recv(self,bytes):
except RepyException:
raise # Pass up from inner block

except Exception, e:
except Exception as e:
# Check if this a recoverable error
if _is_recoverable_network_exception(e):
# Operation would block
Expand Down Expand Up @@ -1924,7 +1924,7 @@ def send(self,message):
raise SocketClosedLocal("The socket is closed!")
except RepyException:
raise # pass up from inner block
except Exception, e:
except Exception as e:
# Check if this a recoverable error
if _is_recoverable_network_exception(e):
# Operation would block
Expand Down Expand Up @@ -2075,7 +2075,7 @@ def getmessage(self):
# Let these through from the inner block
raise

except Exception, e:
except Exception as e:
# Check if this is a would-block error
if _is_recoverable_network_exception(e):
raise SocketWouldBlockError("No messages currently available!")
Expand Down Expand Up @@ -2267,7 +2267,7 @@ def getconnection(self):
# Let these through from the inner block
raise

except Exception, e:
except Exception as e:
# Check if this is a would-block error
if _is_recoverable_network_exception(e):
raise SocketWouldBlockError("No connections currently available!")
Expand Down
37 changes: 19 additions & 18 deletions emulfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@
# Fix for SeattleTestbed/attic#983.
# By retaining a reference to unicode, we prevent os.path.abspath from
# failing in some versions of python when the unicode builtin is overwritten.
os.path.unicode = unicode
#os.path.unicode = unicode

# Store a reference to open, so that we retain access
# after the builtin's are disabled
safe_open = open
#safe_open = open

##### Constants

Expand Down Expand Up @@ -81,7 +81,7 @@ def listfiles():
A list of strings (file names)
"""
# We will consume 4K of fileread
nanny.tattle_quantity('fileread', 4096)
#nanny.tattle_quantity('fileread', 4096)

# Get the list of files from the current directory
files = os.listdir(repy_constants.REPY_CURRENT_DIR)
Expand Down Expand Up @@ -129,12 +129,12 @@ def removefile(filename):
absolute_filename = os.path.abspath(os.path.join(repy_constants.REPY_CURRENT_DIR, filename))

# Check if the file exists
nanny.tattle_quantity('fileread', 4096)
#nanny.tattle_quantity('fileread', 4096)
if not os.path.isfile(absolute_filename):
raise FileNotFoundError('Cannot remove non-existent file "'+filename+'".')

# Consume the filewrite resources
nanny.tattle_quantity('filewrite',4096)
#nanny.tattle_quantity('filewrite',4096)

# Remove the file (failure is an internal error)
os.remove(absolute_filename)
Expand Down Expand Up @@ -272,16 +272,16 @@ def __init__(self, filename, create):

# Get the absolute file name
self.abs_filename = os.path.abspath(os.path.join(repy_constants.REPY_CURRENT_DIR, filename))


# Here is where we try to allocate a "file" resource from the
# nanny system. We will restore this below if there is an exception
# This may raise a ResourceExhautedError
nanny.tattle_add_item('filesopened', self.abs_filename)
#nanny.tattle_add_item('filesopened', self.abs_filename)


# charge for checking if the file exists.
nanny.tattle_quantity('fileread', 4096)
#nanny.tattle_quantity('fileread', 4096)
exists = os.path.isfile(self.abs_filename)

# if there isn't a file already...
Expand All @@ -291,13 +291,13 @@ def __init__(self, filename, create):
raise FileNotFoundError('Cannot openfile non-existent file "'+filename+'" without creating it!')

# okay, we should create it...
nanny.tattle_quantity('filewrite', 4096)
safe_open(self.abs_filename, "w").close() # Forces file creation
#nanny.tattle_quantity('filewrite', 4096)
open(self.abs_filename, "w").close() # Forces file creation

# Store a file handle
# Always open in mode r+b, this avoids Windows text-mode
# quirks, and allows reading and writing
self.fobj = safe_open(self.abs_filename, "r+b")
self.fobj = open(self.abs_filename, "r+")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This almost certainly breaks the security of the system.


# Add the filename to the open files
OPEN_FILES.add(filename)
Expand All @@ -307,7 +307,7 @@ def __init__(self, filename, create):

except RepyException:
# Restore the file handle we tattled
nanny.tattle_remove_item('filesopened', self.abs_filename)
#nanny.tattle_remove_item('filesopened', self.abs_filename)
raise

finally:
Expand Down Expand Up @@ -336,7 +336,7 @@ def close(self):
OPEN_FILES_LOCK.acquire()

# Tell nanny we're gone.
nanny.tattle_remove_item('filesopened', self.abs_filename)
#nanny.tattle_remove_item('filesopened', self.abs_filename)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the nanny call removals are also likely a security concern.


# Acquire the seek lock
self.seek_lock.acquire()
Expand Down Expand Up @@ -386,7 +386,7 @@ def readat(self,sizelimit,offset):
end of the file, or if the sizelimit was 0.
"""
# Check the arguments
if sizelimit < 0 and sizelimit != None:
if sizelimit != None and sizelimit < 0:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this edit?

raise RepyArgumentError("Negative sizelimit specified!")
if offset < 0:
raise RepyArgumentError("Negative read offset speficied!")
Expand All @@ -408,7 +408,7 @@ def readat(self,sizelimit,offset):
fobj.seek(offset)

# Wait for available file read resources
nanny.tattle_quantity('fileread',0)
#nanny.tattle_quantity('fileread',0)

if sizelimit != None:
# Read the data
Expand All @@ -428,7 +428,7 @@ def readat(self,sizelimit,offset):
disk_blocks_read += 1

# Charge 4K per block
nanny.tattle_quantity('fileread', disk_blocks_read*4096)
#nanny.tattle_quantity('fileread', disk_blocks_read*4096)

# Return the data
return data
Expand Down Expand Up @@ -464,6 +464,7 @@ def writeat(self,data,offset):
raise RepyArgumentError("Negative read offset speficied!")
if type(data) is not str:
raise RepyArgumentError("Data must be specified as a string!")
#data = data.encode('utf-8')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should likely be removed.

Suggested change
#data = data.encode('utf-8')


# Get the seek lock
self.seek_lock.acquire()
Expand All @@ -482,7 +483,7 @@ def writeat(self,data,offset):
fobj.seek(offset)

# Wait for available file write resources
nanny.tattle_quantity('filewrite',0)
#nanny.tattle_quantity('filewrite',0)

# Write the data and flush to disk
fobj.write(data)
Expand All @@ -503,7 +504,7 @@ def writeat(self,data,offset):
disk_blocks_written += 1

# Charge 4K per block
nanny.tattle_quantity('filewrite', disk_blocks_written*4096)
#nanny.tattle_quantity('filewrite', disk_blocks_written*4096)


def __del__(self):
Expand Down
7 changes: 3 additions & 4 deletions emulmisc.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import nonportable # for getruntime
import harshexit # for harshexit()
import threading # for Lock()
import thread # to catch thread.error
import _thread as thread # to catch thread.error
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

from exception_hierarchy import *

##### Public Functions
Expand Down Expand Up @@ -63,7 +63,7 @@ def randombytes():
# unique from all other exit calls in repy.
try:
randomdata = os.urandom(1024)
except NotImplementedError, e:
except NotImplementedError as e:
tracebackrepy.handle_internalerror("os.urandom is not implemented " + \
"(Exception was: %s)" % e.message, 217)

Expand Down Expand Up @@ -198,8 +198,7 @@ def log(*args):
Nothing
"""
for arg in args:
print arg,

print(arg)

##### Class Declarations

Expand Down
11 changes: 6 additions & 5 deletions emultimer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"""

import threading
import thread # Armon: this is to catch thread.error
import _thread as thread # Armon: this is to catch thread.error
import nanny
import idhelper

Expand Down Expand Up @@ -61,7 +61,7 @@ def sleep(seconds):
"""

# Check seconds to ensure it is a valid type.
if type(seconds) not in [long, float, int]:
if type(seconds) not in [float, int]:
raise RepyArgumentError("Invalid type " + str(type(seconds)))

# Using getruntime() in lieu of time.time() because we want elapsed time
Expand Down Expand Up @@ -107,7 +107,7 @@ def createthread(function):

# Generate a unique handle and see if there are resources available
eventhandle = EVENT_PREFIX + idhelper.getuniqueid()
nanny.tattle_add_item('events', eventhandle)
#nanny.tattle_add_item('events', eventhandle)

# Wrap the provided function
def wrapped_func():
Expand All @@ -117,9 +117,10 @@ def wrapped_func():
# Exit if they throw an uncaught exception
tracebackrepy.handle_exception()
harshexit.harshexit(30)
finally:
#finally:
# Remove the event before I exit
nanny.tattle_remove_item('events',eventhandle)
#nanny.tattle_remove_item('events',eventhandle)


# Create a thread object
tobj = threading.Thread(target=wrapped_func, name=idhelper.get_new_thread_name(EVENT_PREFIX))
Expand Down
Loading