Jeff Hardy avatar Jeff Hardy committed 7c4fdc8

Match exceptions with Python 2.7.

Comments (0)

Files changed (3)

         {
             zst = new ZStream();
             int err = zst.deflateInit(level, wbits);
+            switch(err)
+            {
+                case ZlibModule.Z_OK:
+                    break;
 
-            if(err != Z_OK)
-            {
-                throw PythonOps.ValueError("Could not initialize deflate.");
+                case ZlibModule.Z_STREAM_ERROR:
+                    throw PythonOps.ValueError("Invalid initialization option");
+
+                default:
+                    throw ZlibModule.zlib_error(this.zst, err, "while creating compression object");
             }
         }
 
 
             if(err != Z_OK && err != Z_BUF_ERROR)
             {
-                throw PythonOps.ValueError("deflate error.");
+                throw ZlibModule.zlib_error(this.zst, err, "while compressing");
             }
 
             return ZlibModule.Latin1.GetString(output, 0, (int)(zst.total_out - start_total_out));
                 err = zst.deflateEnd();
                 if(err != Z_OK)
                 {
-                    throw PythonOps.ValueError("Error finishing deflate.");
+                    throw ZlibModule.zlib_error(this.zst, err, "from deflateEnd()");
                 }
             }
             else if(err != Z_OK && err != Z_BUF_ERROR)
             {
-                throw ZlibModule.MakeError("deflate error");
+                throw ZlibModule.zlib_error(this.zst, err, "while flushing");
             }
 
             return ZlibModule.Latin1.GetString(output, 0, (int)(zst.total_out - start_total_out));

src/Decompress.cs

         {
             zst = new ZStream();
             int err = zst.inflateInit(wbits);
+            switch(err)
+            {
+                case ZlibModule.Z_OK:
+                    break;
 
-            if(err != ZlibModule.Z_OK)
-            {
-                throw PythonOps.ValueError("Error initializing inflate.");
+                case ZlibModule.Z_STREAM_ERROR:
+                    throw PythonOps.ValueError("Invalid initialization option");
+
+                default:
+                    throw ZlibModule.zlib_error(this.zst, err, "while creating decompression object");
             }
 
             _unused_data = string.Empty;
             }
             else if(err != Z_OK && err != Z_BUF_ERROR)
             {
-                throw PythonOps.ValueError("inflate error");
+                throw ZlibModule.zlib_error(this.zst, err, "while decompressing");
             }
 
             return ZlibModule.Latin1.GetString(output, 0, (int)(zst.total_out - start_total_out));
                 err = zst.inflateEnd();
                 if(err != Z_OK)
                 {
-                    throw PythonOps.ValueError("Error finishing inflate.");
+                    throw ZlibModule.zlib_error(this.zst, err, "from inflateEnd()");
                 }
             }
 

src/ZlibModule.cs

             zst.avail_out = output.Length;
 
             int err = zst.deflateInit(level);
-            if(err != Z_OK)
+            switch(err)
             {
-                throw MakeError();
+                case (Z_OK):
+                    break;
+                
+                case (Z_STREAM_ERROR):
+                    throw PythonOps.CreateThrowable(error,
+                                    "Bad compression level");
+                
+                default:
+                    zst.deflateEnd();
+                    zlib_error(zst, err, "while compressing data");
+                    return null;
             }
 
             err = zst.deflate(FlushStrategy.Z_FINISH);
             if(err != Z_STREAM_END)
             {
                 zst.deflateEnd();
-                throw MakeError();
+                throw zlib_error(zst, err, "while compressing data");
             }
 
             err = zst.deflateEnd();
             if(err == Z_OK)
                 return Latin1.GetString(output, 0, (int)zst.total_out);
             else
-                throw MakeError();
+                throw zlib_error(zst, err, "while finishing compression");
         }
 
         #endregion
             int err = zst.inflateInit(wbits);
             if(err != Z_OK)
             {
-                throw MakeError();
+                zst.inflateEnd();
+                throw zlib_error(zst, err, "while preparing to decompress data");
             }
 
             do
             {
                 err = zst.inflate(FlushStrategy.Z_FINISH);
-
                 if(err != Z_STREAM_END)
                 {
                     if(err == Z_BUF_ERROR && zst.avail_out > 0)
                     {
                         zst.inflateEnd();
-                        throw MakeError();
+                        throw zlib_error(zst, err, "while decompressing data");
                     }
                     else if(err == Z_OK || (err == Z_BUF_ERROR && zst.avail_out == 0))
                     {
                     else
                     {
                         zst.inflateEnd();
-                        throw MakeError();
+                        throw zlib_error(zst, err, "while decompressing data");
                     }
                 }
 
             err = zst.inflateEnd();
             if(err != Z_OK)
             {
-                throw MakeError();
+                throw zlib_error(zst, err, "while finishing data decompression");
             }
 
             if(outputOffset + outputBuffer.Length - zst.avail_out > output.Length)
             return PythonOps.CreateThrowable(error, args);
         }
 
+        internal static Exception zlib_error(ZStream zst, int err, string msg)
+        {
+            string zmsg = zst.msg;
+            if(zmsg == null)
+            {
+                switch(err)
+                {
+                    case Z_BUF_ERROR:
+                        zmsg = "incomplete or truncated stream";
+                        break;
+                    case Z_STREAM_ERROR:
+                        zmsg = "inconsistent stream state";
+                        break;
+                    case Z_DATA_ERROR:
+                        zmsg = "invalid input data";
+                        break;
+                }
+            }
+
+            if(zmsg == null)
+                return MakeError(string.Format("Error {0} {1}", err, msg));
+            else
+                return MakeError(string.Format("Error {0} {1}: {2}", err, msg, zmsg));
+        }
+
         internal static System.Text.Encoding Latin1 = System.Text.Encoding.GetEncoding("iso-8859-1");
     }
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.