[Python-modules-commits] [portalocker] 01/04: Import portalocker_0.6.1.orig.tar.gz

Josué Ortega josue at moszumanska.debian.org
Wed Sep 7 03:38:49 UTC 2016


This is an automated email from the git hooks/post-receive script.

josue pushed a commit to branch master
in repository portalocker.

commit c4a45fbcefb8d3937fcd0bf072794f36275044b8
Author: Josue Ortega <josue at debian.org>
Date:   Tue Sep 6 21:25:17 2016 -0600

    Import portalocker_0.6.1.orig.tar.gz
---
 MANIFEST.in                      |  3 +-
 PKG-INFO                         | 16 +++++---
 README.rst                       |  6 ++-
 portalocker.egg-info/PKG-INFO    | 16 +++++---
 portalocker.egg-info/SOURCES.txt |  3 +-
 portalocker/portalocker.py       | 71 ++++++++++++++++++---------------
 portalocker/utils.py             | 12 ++++--
 setup.cfg                        |  3 ++
 setup.py                         | 10 +++--
 tests/tests.py                   | 86 ++++++++++++++++++++++++++++++++++++++++
 10 files changed, 173 insertions(+), 53 deletions(-)

diff --git a/MANIFEST.in b/MANIFEST.in
index bf378c5..076f508 100644
--- a/MANIFEST.in
+++ b/MANIFEST.in
@@ -1,3 +1,4 @@
 include CHANGELOG
-include README.rest
+include README.rst
 include LICENSE
+recursive-include tests *.py
diff --git a/PKG-INFO b/PKG-INFO
index f851b96..6988304 100644
--- a/PKG-INFO
+++ b/PKG-INFO
@@ -1,6 +1,6 @@
 Metadata-Version: 1.1
 Name: portalocker
-Version: 0.5.7
+Version: 0.6.1
 Summary: Wraps the portalocker recipe for easy usage
 Home-page: https://github.com/WoLpH/portalocker
 Author: Rick van Hattem
@@ -11,8 +11,12 @@ Description: ############################################
         ############################################
         
         .. image:: https://travis-ci.org/WoLpH/portalocker.svg?branch=master
-            :alt: Test Status
+            :alt: Linux Test Status
             :target: https://travis-ci.org/WoLpH/portalocker
+            
+        .. image:: https://img.shields.io/appveyor/ci/WoLpH/portalocker.svg
+            :alt: Windows Tests Status
+            :target: https://ci.appveyor.com/project/WoLpH/portalocker
         
         .. image:: https://coveralls.io/repos/WoLpH/portalocker/badge.svg?branch=master
             :alt: Coverage Status
@@ -90,8 +94,10 @@ Keywords: locking,locks,with statement,windows,linux,unix
 Platform: any
 Classifier: Intended Audience :: Developers
 Classifier: Programming Language :: Python
-Classifier: Programming Language :: Python :: 2.5
 Classifier: Programming Language :: Python :: 2.6
 Classifier: Programming Language :: Python :: 2.7
-Classifier: Programming Language :: Python :: 3
-Classifier: Programming Language :: Python :: 3.1
+Classifier: Programming Language :: Python :: 3.3
+Classifier: Programming Language :: Python :: 3.4
+Classifier: Programming Language :: Python :: 3.5
+Classifier: Programming Language :: Python :: Implementation :: CPython
+Classifier: Programming Language :: Python :: Implementation :: PyPy
diff --git a/README.rst b/README.rst
index 5212491..844a5b5 100644
--- a/README.rst
+++ b/README.rst
@@ -3,8 +3,12 @@ portalocker - Cross-platform locking library
 ############################################
 
 .. image:: https://travis-ci.org/WoLpH/portalocker.svg?branch=master
-    :alt: Test Status
+    :alt: Linux Test Status
     :target: https://travis-ci.org/WoLpH/portalocker
+    
+.. image:: https://img.shields.io/appveyor/ci/WoLpH/portalocker.svg
+    :alt: Windows Tests Status
+    :target: https://ci.appveyor.com/project/WoLpH/portalocker
 
 .. image:: https://coveralls.io/repos/WoLpH/portalocker/badge.svg?branch=master
     :alt: Coverage Status
diff --git a/portalocker.egg-info/PKG-INFO b/portalocker.egg-info/PKG-INFO
index f851b96..6988304 100644
--- a/portalocker.egg-info/PKG-INFO
+++ b/portalocker.egg-info/PKG-INFO
@@ -1,6 +1,6 @@
 Metadata-Version: 1.1
 Name: portalocker
-Version: 0.5.7
+Version: 0.6.1
 Summary: Wraps the portalocker recipe for easy usage
 Home-page: https://github.com/WoLpH/portalocker
 Author: Rick van Hattem
@@ -11,8 +11,12 @@ Description: ############################################
         ############################################
         
         .. image:: https://travis-ci.org/WoLpH/portalocker.svg?branch=master
-            :alt: Test Status
+            :alt: Linux Test Status
             :target: https://travis-ci.org/WoLpH/portalocker
+            
+        .. image:: https://img.shields.io/appveyor/ci/WoLpH/portalocker.svg
+            :alt: Windows Tests Status
+            :target: https://ci.appveyor.com/project/WoLpH/portalocker
         
         .. image:: https://coveralls.io/repos/WoLpH/portalocker/badge.svg?branch=master
             :alt: Coverage Status
@@ -90,8 +94,10 @@ Keywords: locking,locks,with statement,windows,linux,unix
 Platform: any
 Classifier: Intended Audience :: Developers
 Classifier: Programming Language :: Python
-Classifier: Programming Language :: Python :: 2.5
 Classifier: Programming Language :: Python :: 2.6
 Classifier: Programming Language :: Python :: 2.7
-Classifier: Programming Language :: Python :: 3
-Classifier: Programming Language :: Python :: 3.1
+Classifier: Programming Language :: Python :: 3.3
+Classifier: Programming Language :: Python :: 3.4
+Classifier: Programming Language :: Python :: 3.5
+Classifier: Programming Language :: Python :: Implementation :: CPython
+Classifier: Programming Language :: Python :: Implementation :: PyPy
diff --git a/portalocker.egg-info/SOURCES.txt b/portalocker.egg-info/SOURCES.txt
index 52a2440..6526c00 100644
--- a/portalocker.egg-info/SOURCES.txt
+++ b/portalocker.egg-info/SOURCES.txt
@@ -11,4 +11,5 @@ portalocker.egg-info/PKG-INFO
 portalocker.egg-info/SOURCES.txt
 portalocker.egg-info/dependency_links.txt
 portalocker.egg-info/not-zip-safe
-portalocker.egg-info/top_level.txt
\ No newline at end of file
+portalocker.egg-info/top_level.txt
+tests/tests.py
\ No newline at end of file
diff --git a/portalocker/portalocker.py b/portalocker/portalocker.py
index eff991b..3c68b96 100644
--- a/portalocker/portalocker.py
+++ b/portalocker/portalocker.py
@@ -67,15 +67,10 @@ class LockException(Exception):
     LOCK_FAILED = 1
 
 if os.name == 'nt':  # pragma: no cover
-    import win32con
-    import win32file
-    import pywintypes
-    import winerror
-    LOCK_EX = win32con.LOCKFILE_EXCLUSIVE_LOCK
-    LOCK_SH = 0  # the default
-    LOCK_NB = win32con.LOCKFILE_FAIL_IMMEDIATELY
-    # is there any reason not to reuse the following structure?
-    __overlapped = pywintypes.OVERLAPPED()
+    import msvcrt
+    LOCK_EX = 0x1      # exclusive - msvcrt.LK_LOCK or msvcrt.LK_NBLCK
+    LOCK_SH = 0x2      # shared    - msvcrt.LK_RLOCK or msvcrt.LK_NBRLCK
+    LOCK_NB = 0x4      # 
 elif os.name == 'posix':
     import fcntl
     LOCK_EX = fcntl.LOCK_EX
@@ -86,35 +81,47 @@ else:  # pragma: no cover
 
 
 def nt_lock(file_, flags):  # pragma: no cover
-    hfile = win32file._get_osfhandle(file_.fileno())
+    if flags & LOCK_SH:
+        mode = msvcrt.LK_NBRLCK if (flags & LOCK_NB) else msvcrt.LK_RLOCK
+    else:
+        mode = msvcrt.LK_NBLCK if (flags & LOCK_NB) else msvcrt.LK_LOCK
+
+    # windows locks byte ranges, so make sure to lock from file start
     try:
-        win32file.LockFileEx(hfile, flags, 0, -0x10000, __overlapped)
-    except pywintypes.error as exc_value:
-        # error: (33, 'LockFileEx', 'The process cannot access the file
-        # because another process has locked a portion of the file.')
-        if exc_value.winerror == winerror.ERROR_LOCK_VIOLATION:
+        savepos = file_.tell()
+        if savepos:
+            # [ ] test exclusive lock fails on seek here
+            # [ ] test if shared lock passes this point
+            file_.seek(0)
+        # [x] check if 0 param locks entire file (not documented in Python)
+	#   [x] just fails with "IOError: [Errno 13] Permission denied",
+        #        but -1 seems to do the trick
+        try:
+            msvcrt.locking(file_.fileno(), mode, -1)
+        except IOError as exc_value:
+            # [ ] be more specific here
             raise LockException(LockException.LOCK_FAILED, exc_value.strerror)
-        else:
-            # Q:  Are there exceptions/codes we should be dealing with
-            # here?
-            raise
+        finally:
+            if savepos:
+                file_.seek(savepos)
+    except IOError as exc_value:
+        raise LockException(LockException.LOCK_FAILED, exc_value.strerror)
 
 
 def nt_unlock(file_):  # pragma: no cover
-    hfile = win32file._get_osfhandle(file_.fileno())
     try:
-        win32file.UnlockFileEx(hfile, 0, -0x10000, __overlapped)
-    except pywintypes.error as exc_value:
-        if exc_value.winerror == winerror.ERROR_NOT_LOCKED:
-            # error: (158, 'UnlockFileEx', 'The segment is already '
-            #         'unlocked.')
-            # To match the 'posix' implementation, silently ignore this
-            # error
-            pass
-        else:
-            # Q:  Are there exceptions/codes we should be dealing with
-            # here?
-            raise
+        savepos = file_.tell()
+        if savepos:
+            file_.seek(0)
+        try:
+            msvcrt.locking(file_.fileno(), msvcrt.LK_UNLCK, -1)
+        except IOError as exc_value:
+            raise LockException(LockException.LOCK_FAILED, exc_value.strerror)
+        finally:
+            if savepos:
+                file_.seek(savepos)
+    except IOError as exc_value:
+        raise LockException(LockException.LOCK_FAILED, exc_value.strerror)
 
 
 def posix_lock(file_, flags):
diff --git a/portalocker/utils.py b/portalocker/utils.py
index 825804b..e55826f 100644
--- a/portalocker/utils.py
+++ b/portalocker/utils.py
@@ -156,6 +156,12 @@ class Lock(object):
         self.fh = fh
         return fh
 
+    def release(self):
+        '''Releases the currently locked file handle'''
+        if self.fh:
+            self.fh.close()
+            self.fh = None
+
     def _get_fh(self):
         '''Get a new filehandle'''
         return open(self.filename, self.mode)
@@ -185,9 +191,7 @@ class Lock(object):
         return fh
 
     def __enter__(self):
-        self.fh = self.acquire()
-        return self.fh
+        return self.acquire()
 
     def __exit__(self, type_, value, tb):
-        if self.fh:
-            self.fh.close()
+        self.release()
diff --git a/setup.cfg b/setup.cfg
index 934a773..01c7b78 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -9,6 +9,9 @@ all_files = 1
 [upload_sphinx]
 upload-dir = docs/_build/html
 
+[bdist_wheel]
+universal = 1
+
 [egg_info]
 tag_build = 
 tag_date = 0
diff --git a/setup.py b/setup.py
index 63b377a..bf899a5 100644
--- a/setup.py
+++ b/setup.py
@@ -5,7 +5,7 @@ from setuptools.command.test import test as TestCommand
 __package_name__ = 'portalocker'
 __author__ = 'Rick van Hattem'
 __email__ = 'wolph at wol.ph'
-__version__ = '0.5.7'
+__version__ = '0.6.1'
 __description__ = '''Wraps the portalocker recipe for easy usage'''
 __url__ = 'https://github.com/WoLpH/portalocker'
 
@@ -37,11 +37,13 @@ if __name__ == '__main__':
         classifiers=[
             'Intended Audience :: Developers',
             'Programming Language :: Python',
-            'Programming Language :: Python :: 2.5',
             'Programming Language :: Python :: 2.6',
             'Programming Language :: Python :: 2.7',
-            'Programming Language :: Python :: 3',
-            'Programming Language :: Python :: 3.1',
+            'Programming Language :: Python :: 3.3',
+            'Programming Language :: Python :: 3.4',
+            'Programming Language :: Python :: 3.5',
+            'Programming Language :: Python :: Implementation :: CPython',
+            'Programming Language :: Python :: Implementation :: PyPy',
         ],
         keywords='locking, locks, with statement, windows, linux, unix',
         author=__author__,
diff --git a/tests/tests.py b/tests/tests.py
new file mode 100644
index 0000000..540c515
--- /dev/null
+++ b/tests/tests.py
@@ -0,0 +1,86 @@
+from __future__ import print_function
+from __future__ import with_statement
+import pytest
+import portalocker
+
+
+def test_exceptions():
+    # Open the file 2 times
+    a = open('locked_file', 'a')
+    b = open('locked_file', 'a')
+
+    # Lock exclusive non-blocking
+    lock_flags = portalocker.LOCK_EX | portalocker.LOCK_NB
+
+    # First lock file a
+    portalocker.lock(a, lock_flags)
+
+    # Now see if we can lock file b
+    with pytest.raises(portalocker.LockException):
+        portalocker.lock(b, lock_flags)
+
+    # Cleanup
+    a.close()
+    b.close()
+
+
+def test_with_timeout():
+    # Open the file 2 times
+    with pytest.raises(portalocker.AlreadyLocked):
+        with portalocker.Lock('locked_file', timeout=0.1) as fh:
+            print('writing some stuff to my cache...', file=fh)
+            with portalocker.Lock('locked_file', timeout=0.1):
+                pass
+            print('writing more stuff to my cache...', file=fh)
+
+
+def test_without_timeout():
+    # Open the file 2 times
+    with pytest.raises(portalocker.LockException):
+        with portalocker.Lock('locked_file', timeout=None) as fh:
+            print('writing some stuff to my cache...', file=fh)
+            with portalocker.Lock('locked_file', timeout=None):
+                pass
+            print('writing more stuff to my cache...', file=fh)
+
+
+def test_simple():
+    fh = open('tests/test_file.txt', 'r+')
+    portalocker.lock(fh, portalocker.LOCK_EX)
+    fh.seek(12)
+    fh.write('foo')
+    portalocker.unlock(fh)
+    fh.close()
+
+
+def test_class():
+    lock = portalocker.Lock('tests/test_file.txt')
+    lock2 = portalocker.Lock('tests/test_file.txt', fail_when_locked=False,
+                             timeout=0.01)
+
+    with lock:
+        lock.acquire()
+
+        with pytest.raises(portalocker.LockException):
+            with lock2:
+                pass
+
+    with lock2:
+        pass
+
+
+def test_acquire_release():
+    lock = portalocker.Lock('tests/test_file.txt')
+    lock2 = portalocker.Lock('tests/test_file.txt', fail_when_locked=False)
+
+    lock.acquire()  # acquire lock when nobody is using it
+    with pytest.raises(portalocker.LockException):
+        # another party should not be able to acquire the lock
+        lock2.acquire(timeout=0.01)
+
+        # re-acquire a held lock is a no-op
+        lock.acquire()
+
+    lock.release()  # release the lock
+    lock.release()  # second release does nothing
+

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/portalocker.git



More information about the Python-modules-commits mailing list