Commit c4a45fbc authored by Josué Ortega's avatar Josué Ortega

Import portalocker_0.6.1.orig.tar.gz

parent 2c284cbc
include CHANGELOG
include README.rest
include README.rst
include LICENSE
recursive-include tests *.py
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
......@@ -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
......
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
......@@ -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
......@@ -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):
......
......@@ -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()
......@@ -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
......
......@@ -5,7 +5,7 @@ from setuptools.command.test import test as TestCommand
__package_name__ = 'portalocker'
__author__ = 'Rick van Hattem'
__email__ = 'wolph@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__,
......
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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment