[pypy-svn] r54830 - in pypy/branch/gameboy-emulator/pypy/lang/gameboy: . test

cami at codespeak.net cami at codespeak.net
Sat May 17 15:43:35 CEST 2008


Author: cami
Date: Sat May 17 15:43:34 2008
New Revision: 54830

Modified:
   pypy/branch/gameboy-emulator/pypy/lang/gameboy/cartridge.py
   pypy/branch/gameboy-emulator/pypy/lang/gameboy/test/test_cartridge.py
Log:
change battery file write functions to use rlib streams


Modified: pypy/branch/gameboy-emulator/pypy/lang/gameboy/cartridge.py
==============================================================================
--- pypy/branch/gameboy-emulator/pypy/lang/gameboy/cartridge.py	(original)
+++ pypy/branch/gameboy-emulator/pypy/lang/gameboy/cartridge.py	Sat May 17 15:43:34 2008
@@ -3,6 +3,7 @@
 
 from pypy.lang.gameboy import constants
 from pypy.lang.gameboy.timer import *
+from pypy.rlib.streamio import open_file_as_stream
 
 import os
 
@@ -28,6 +29,9 @@
 
 
 
+def map_to_byte(value):
+    return ord(value) & 0xFF
+
 # ==============================================================================
 # CARTRIDGE
 
@@ -148,35 +152,44 @@
             self.load(file)
         
     def reset(self):
-        #self.cartridge_name = None
+        self.cartridge_name = None
         self.cartridge_file_path = None
-        self.cartridge_file = None    
+        self.cartridge_stream = None   
+        self.cartridge_file_contents = None 
         self.battery_name = None
         self.battery_file_path = None
-        self.battery_file = None
+        self.battery_stream = None
+        self.battery_file_contents = None
         
         
     def load(self, cartridge_path):
         cartridge_path = str(cartridge_path)
         self.cartridge_file_path = cartridge_path
-        #self.cartridge_name = os.path.basename(self.cartridge_file_path)
-        self.cartridge_file = file(cartridge_path, "r")
+        self.cartridge_name = os.path.basename(self.cartridge_file_path)
+        #FIXED open_file_as_stream
+        self.cartridge_stream = open_file_as_stream(cartridge_path)
+        self.cartridge_file_contents = map(map_to_byte, \
+                                           self.cartridge_stream.readall())
         self.load_battery(cartridge_path)
         
-        cartridge_path
     def load_battery(self, cartridge_file_path):
-        self.battery_file_path = self.create_battery_file_path(cartridge_path)
+        self.battery_file_path = self.create_battery_file_path(cartridge_file_path)
         self.battery_name = os.path.basename(self.battery_file_path)
         if self.has_battery():
-            self.battery_file = open(self.battery_file_path)
+            self.battery_stream = open_file_as_stream(self.battery_file_path)
+            self.battery_file_contents = map(map_to_byte, \
+                                             self.battery_stream.readall())
     
     def create_battery_file_path(self, cartridge_file_path):
         if cartridge_file_path.endswith(constants.CARTRIDGE_FILE_EXTENSION):
-            return cartridge_file_path.replace(constants.CARTRIDGE_FILE_EXTENSION,
-                    constants.BATTERY_FILE_EXTENSION)
-        elif cartridge_file_path.endswith(constants.CARTRIDGE_COLOR_FILE_EXTENSION):
-            return cartridge_file_path.replace(constants.CARTRIDGE_COLOR_FILE_EXTENSION,
-                    constants.BATTERY_FILE_EXTENSION)
+            return cartridge_file_path.replace( \
+                                        constants.CARTRIDGE_FILE_EXTENSION,
+                                        constants.BATTERY_FILE_EXTENSION)
+        elif cartridge_file_path.endswith(\
+                                constants.CARTRIDGE_COLOR_FILE_EXTENSION):
+            return cartridge_file_path.replace( \
+                                    constants.CARTRIDGE_COLOR_FILE_EXTENSION,
+                                    constants.BATTERY_FILE_EXTENSION)
         else:
             return cartridge_file_path + constants.BATTERY_FILE_EXTENSION
     
@@ -184,17 +197,16 @@
         return os.path.exists(self.battery_file_path)
     
     def read(self):
-        self.cartridge_file.seek(0)
-        return map(map_to_byte, self.cartridge_file.read())
+        return self.cartridge_file_contents
     
     def read_battery(self):
-        self.battery_file.seek(0)
-        return  map(map_to_byte, self.battery_file.read())
+        return  self.battery_file_contents
     
     def write_battery(self, ram):
-        self.battery_file = open(self.battery_file_path, "w")
-        self.battery_file.write(("").join(map(chr, ram)))
-        self.battery_file = open(self.battery_file_path, "r+")
+        output_stream = open_file_as_stream(self.battery_file_path, "w")
+        output_stream.write(("").join(map(chr, ram)))
+        output_stream.flush()
+        self.battery_file_contents = ram
         
     def remove_battery(self):
         if self.has_battery():
@@ -206,11 +218,6 @@
     def get_battery_size(self):
         return os.path.getsize(self.battery_file_path)
         
-     
-
-def map_to_byte(value):
-    return ord(value) & 0xFF
-   
 # ==============================================================================
 # CARTRIDGE TYPES
 

Modified: pypy/branch/gameboy-emulator/pypy/lang/gameboy/test/test_cartridge.py
==============================================================================
--- pypy/branch/gameboy-emulator/pypy/lang/gameboy/test/test_cartridge.py	(original)
+++ pypy/branch/gameboy-emulator/pypy/lang/gameboy/test/test_cartridge.py	Sat May 17 15:43:34 2008
@@ -10,7 +10,7 @@
 
 ROM_PATH = str(py.magic.autopath().dirpath().dirpath())+"/rom"
 CONTENT = "abcdefghijklmnopqrstuvwxyz1234567890"
-MAPPED_CONTENT = map(mapToByte, CONTENT)
+MAPPED_CONTENT = map(map_to_byte, CONTENT)
 
 # ------------------------------------------------------------------------------
 
@@ -43,13 +43,13 @@
 def test_cartridge_init(): 
     cartridge = get_cartridge()
     
-    #assert cartridge.cartridge_name is None
-    assert cartridge.cartridge_file_path is None
-    assert cartridge.cartridge_file is None
+    assert cartridge.cartridge_name is None
+    assert cartridge.cartridge_stream is None
+    assert cartridge.cartridge_file_contents is None
     
     assert cartridge.battery_name is None
-    assert cartridge.battery_file_path is None
-    assert cartridge.battery_file is None
+    assert cartridge.battery_stream is None
+    assert cartridge.battery_file_contents is None
     
 
 def rest_cartridge_load():
@@ -60,12 +60,10 @@
     cartridge.load(romFilePath)
     #assert cartridge.cartridge_name == romName
     assert cartridge.cartridge_file_path == romFilePath
-    assert cartridge.cartridge_file is not None
     
     assert cartridge.battery_name == romFile+constants.BATTERY_FILE_EXTENSION
     assert cartridge.battery_file_path ==  romFilePath+constants.BATTERY_FILE_EXTENSION
     assert cartridge.has_battery() == False
-    assert cartridge.battery_file is None
     
     
 def test_cartridge_hasBattery():
@@ -80,9 +78,7 @@
     
 def test_cartridge_read():
     cartridge = get_cartridge()
-    cartridge.cartridge_file = File(CONTENT)
-    
-    assert cartridge.read() == MAPPED_CONTENT
+    assert cartridge.read() == None
     
     
 def test_cartridge_remove_write_read_Battery():
@@ -97,8 +93,6 @@
     
     cartridge.write_battery(MAPPED_CONTENT)
     assert cartridge.has_battery() == True
-    print cartridge.battery_file
-    assert cartridge.battery_file.read() == CONTENT
     assert cartridge.read_battery() == MAPPED_CONTENT
     
     cartridge.remove_battery()



More information about the Pypy-commit mailing list