Steffen Daode Nurpmeso <sdao...@googlemail.com> added the comment:

My last idea for today was to split the writes.
This also works for the C version, but it does not for test_zlib.py.
I attach the updated files.  And for completeness:

    Adler-32 <7a54018b> CRC-32 <7f1be672> -- @test_13713_tmp
    Adler-32 <7a54018b> CRC-32 <7f1be672> -- c-mmap-testfile

----------
Added file: http://bugs.python.org/file21675/11277.2.diff
Added file: http://bugs.python.org/file21676/11277.mmap-1.c

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue11277>
_______________________________________
diff --git a/Lib/test/test_zlib.py b/Lib/test/test_zlib.py
--- a/Lib/test/test_zlib.py
+++ b/Lib/test/test_zlib.py
@@ -3,6 +3,7 @@
 import binascii
 import random
 import sys
+import os
 from test.support import precisionbigmemtest, _1G, _4G
 
 zlib = support.import_module('zlib')
@@ -68,9 +69,11 @@
 
     def setUp(self):
         with open(support.TESTFN, "wb+") as f:
-            f.seek(_4G)
-            f.write(b"asdf")
-        with open(support.TESTFN, "rb") as f:
+            f.write(b"a")
+            f.seek(_4G + mmap.PAGESIZE + 1)
+            f.write(b"bcd")
+            f.flush()
+            os.fsync(f.fileno())
             self.mapping = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
 
     def tearDown(self):
@@ -82,9 +85,8 @@
     @unittest.skipUnless(support.is_resource_enabled("largefile"),
                          "May use lots of disk space.")
     def test_big_buffer(self):
-        self.assertEqual(zlib.crc32(self.mapping), 3058686908)
-        self.assertEqual(zlib.adler32(self.mapping), 82837919)
-
+        self.assertEqual(zlib.crc32(self.mapping), 0x7f1be672)
+        self.assertEqual(zlib.adler32(self.mapping), 0x7a54018b)
 
 class ExceptionTestCase(unittest.TestCase):
     # make sure we generate some expected errors
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>

#include <fcntl.h>
#include <unistd.h>

#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/uio.h>

#define PATH        "c-mmap-testfile"
#define PAGESIZE    4096

static void sighdl(int);

static void
sighdl(int signo)
{
    const char errmsg[] = "\nSignal occurred, cleaning up\n";
    (void)signo;
    (void)signal(SIGSEGV, SIG_DFL);
    (void)signal(SIGBUS, SIG_DFL);
    write(2, errmsg, sizeof(errmsg)-1);
    (void)unlink(PATH);
    return;
}

int
main(void) {
    int fd, estat = 0;
    void *addr;
    auto struct stat s;
    /* *Final* sizes (string written after lseek(2): "abcd") */
    const size_t *ct, tests[] = {
        /* Tested good */
        //0x100000000 - PAGESIZE - 5,
        //0x100000000 - 4,
        //0x100000000 - 3,
        //0x100000000 - 1,
        0x100000000 + PAGESIZE + 4,
        //0x100000000 + PAGESIZE + 5,
        /* Tested bad */
        //0x100000000,
        //0x100000000 + PAGESIZE,
        //0x100000000 + PAGESIZE + 1,
        //0x100000000 + PAGESIZE + 3,
        0
    };

    if (signal(SIGSEGV, &sighdl) == SIG_ERR)
        goto jerror;
    if (signal(SIGBUS, &sighdl) == SIG_ERR)
        goto jerror;

    for (ct = tests; *ct != 0; ++ct) {
        fprintf(stderr, "Size %lu/0x%lX: open", *ct, *ct);
        fd = open(PATH, O_RDWR|O_TRUNC|O_CREAT, 0666);
        if (fd < 0)
            goto jerror;
        fprintf(stderr, ". ");

        fprintf(stderr, "write-I");
        if (write(fd, "a", 1) != 1)
            goto jerror;
        fprintf(stderr, ". ");

        fprintf(stderr, "lseek");
        if (lseek(fd, *ct-4, SEEK_END) < 0)
            goto jerror;
        fprintf(stderr, ". ");

        fprintf(stderr, "write-II");
        if (write(fd, "bcd", 3) != 3)
            goto jerror;
        fprintf(stderr, ". ");

        fprintf(stderr, "fsync");
        if (fsync(fd) != 0)
            goto jerror;
        fprintf(stderr, ". ");

        fprintf(stderr, "fstat");
        if (fstat(fd, &s) != 0)
            goto jerror;
        fprintf(stderr, ". ");

        if (*ct != (size_t)s.st_size) {
            fprintf(stderr, "fstat size mismatch: %lu is not %lu\n",
                    (size_t)s.st_size, *ct);
            continue;
        }

        fprintf(stderr, "mmap");
        addr = mmap(NULL, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
        if (addr == NULL)
            goto jerror;
        fprintf(stderr, ". ");

        (void)close(fd);

        fprintf(stderr, "[0]");
        if (((char*)addr)[0] != 'a')
            goto jerror;
        fprintf(stderr, ". ");

        fprintf(stderr, "[s.st_size-3]");
        if (((char*)addr)[s.st_size-3] != 'b')
            goto jerror;
        fprintf(stderr, ". ");

        fprintf(stderr, "munmap");
        if (munmap(addr, s.st_size) != 0)
            goto jerror;
        fprintf(stderr, ".");

        fprintf(stderr, "\n");
    }

jleave:
    (void)unlink(PATH);
    return estat;

jerror:
    fprintf(stderr, "\n%s\n", strerror(errno));
    estat = 1;
    goto jleave;
}
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to