Here are the examples of the python api sys.hexversion taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
136 Examples
5
Example 1
Project: PyDev.Debugger Source File: pydev_versioncheck.py
def versionok_for_gui():
''' Return True if running Python is suitable for GUI Event Integration and deeper IPython integration '''
# We require Python 2.6+ ...
if sys.hexversion < 0x02060000:
return False
# Or Python 3.2+
if sys.hexversion >= 0x03000000 and sys.hexversion < 0x03020000:
return False
# Not supported under Jython nor IronPython
if sys.platform.startswith("java") or sys.platform.startswith('cli'):
return False
return True
5
Example 2
Project: ganeti Source File: tempfile_fork_unittest.py
def testNoReset(self):
if ((sys.hexversion >= 0x020703F0 and sys.hexversion < 0x03000000) or
sys.hexversion >= 0x030203F0):
# We can't test the no_reset case on Python 2.7+
return
# evil Debian sid...
if (hasattr(tempfile._RandomNameSequence, "rng") and
type(tempfile._RandomNameSequence.rng) == property):
return
self._Test(False)
5
Example 3
Project: numba Source File: ddt.py
def is_hash_randomized():
return (((sys.hexversion >= 0x02070300 and
sys.hexversion < 0x03000000) or
(sys.hexversion >= 0x03020300)) and
sys.flags.hash_randomization and
'PYTHONHASHSEED' not in os.environ)
3
Example 4
Project: mythbox Source File: test_advice.py
def checkDoubleType(self):
if sys.hexversion >= 0x02030000:
return # you can't duplicate bases in 2.3
class aType(type,type):
ping([],1)
aType, = aType
self.assert_(aType.__class__ is type)
3
Example 5
def hook(mod):
# Forbid imports in the port_v2 directory under Python 3
# The code wouldn't import and would crash the build process.
if sys.hexversion >= 0x03000000:
mod.__path__ = []
return mod
3
Example 6
Project: PyBitmessage Source File: depends.py
def check_hashlib():
if sys.hexversion < 0x020500F0:
logger.error('The hashlib module is not included in this version of Python.')
return False
import hashlib
if '_hashlib' not in hashlib.__dict__:
logger.error('The RIPEMD-160 hash algorithm is not available. The hashlib module is not linked against OpenSSL.')
return False
try:
hashlib.new('ripemd160')
except ValueError:
logger.error('The RIPEMD-160 hash algorithm is not available. The hashlib module utilizes an OpenSSL library with RIPEMD disabled.')
return False
return True
3
Example 7
def testComplex(self):
if sys.hexversion < 0x2030000:
# no kw-args to dict in 2.2 - not worth converting!
return
clsid = pythoncom.MakeIID("{CD637886-DB8B-4b04-98B5-25731E1495BE}")
ctime, atime, wtime = self._getTestTimes()
d = dict(cFileName="foo.txt",
clsid=clsid,
sizel=(1,2),
pointl=(3,4),
dwFileAttributes = win32con.FILE_ATTRIBUTE_NORMAL,
ftCreationTime=ctime,
ftLastAccessTime=atime,
ftLastWriteTime=wtime,
nFileSize=sys_maxsize + 1)
self._testRT(d)
3
Example 8
def xprint(m, *args):
"""Print string to stdout.
Format unicode safely.
"""
if sys.hexversion < 0x3000000:
m = m.decode('utf8')
if args:
m = m % args
if sys.hexversion < 0x3000000:
m = m.encode('utf8')
sys.stdout.write(m)
sys.stdout.write('\n')
3
Example 9
def testSendBlob(self):
self.server.add_method('/blob', 'b', self.callback)
self.server.send('1234', '/blob', [4, 8, 15, 16, 23, 42])
self.assertTrue(self.server.recv())
if sys.hexversion < 0x03000000:
self.assertEqual(self.cb.args[0], [4, 8, 15, 16, 23, 42])
else:
self.assertEqual(self.cb.args[0], b'\x04\x08\x0f\x10\x17\x2a')
3
Example 10
Project: vk4xmpp Source File: transports.py
def _startSSL(self):
tcpsock = self._owner.Connection
if sys.hexversion >= 0x20600F0:
tcpsock._sslObj = ssl.wrap_socket(tcpsock._sock, None, None)
else:
tcpsock._sslObj = socket.ssl(tcpsock._sock, None, None)
tcpsock._sslIssuer = tcpsock._sslObj.issuer()
tcpsock._sslServer = tcpsock._sslObj.server()
tcpsock._recv = tcpsock._sslObj.read
tcpsock._send = tcpsock._sslObj.write
tcpsock._seen_data = 1
self._tcpsock = tcpsock
tcpsock.pending_data = self.pending_data
tcpsock._sock.setblocking(0)
self.starttls = "success"
3
Example 11
Project: numba Source File: test_auto_constants.py
def test_sys_constant(self):
def pyfunc():
return sys.hexversion
cres = compile_isolated(pyfunc, ())
cfunc = cres.entry_point
self.assertEqual(pyfunc(), cfunc())
3
Example 12
def testSendBlob(self):
self.server.add_method('/blob', 'b', self.callback)
self.server.send('1234', '/blob', [4, 8, 15, 16, 23, 42])
assert self.server.recv() == True
if sys.hexversion < 0x03000000:
assert self.cb.args[0] == [4, 8, 15, 16, 23, 42]
else:
assert self.cb.args[0] == b'\x04\x08\x0f\x10\x17\x2a'
3
Example 13
def testSendVarious(self):
self.server.add_method('/blah', 'ihfdscb', self.callback)
if sys.hexversion < 0x03000000:
self.server.send(1234, '/blah', 123, 2**42, 123.456, 666.666, "hello", ('c', 'x'), (12, 34, 56))
else:
self.server.send(1234, '/blah', 123, ('h', 2**42), 123.456, 666.666, "hello", ('c', 'x'), (12, 34, 56))
assert self.server.recv() == True
assert self.cb.types == 'ihfdscb'
assert len(self.cb.args) == len(self.cb.types)
assert self.cb.args[0] == 123
assert self.cb.args[1] == 2**42
assert approx(self.cb.args[2], 123.456)
assert approx(self.cb.args[3], 666.666)
assert self.cb.args[4] == "hello"
assert self.cb.args[5] == 'x'
if sys.hexversion < 0x03000000:
assert self.cb.args[6] == [12, 34, 56]
else:
assert self.cb.args[6] == b'\x0c\x22\x38'
3
Example 14
Project: jc-weather Source File: alfred_weather.py
def tell_about(self, name, query='', prefix=None):
import sys
import json
with open('update.json', 'rt') as uf:
data = json.load(uf)
items = [Item('Version: {0}'.format(data['version']))]
py_ver = 'Python: {:08X}'.format(sys.hexversion)
items.append(Item(py_ver))
return items
3
Example 15
Project: qutebrowser Source File: checkpyver.py
def check_python_version():
"""Check if correct python version is run."""
if sys.hexversion < 0x03040000:
# We don't use .format() and print_function here just in case someone
# still has < 2.6 installed.
# pylint: disable=bad-builtin
version_str = '.'.join(map(str, sys.version_info[:3]))
text = ("At least Python 3.4 is required to run qutebrowser, but " +
version_str + " is installed!\n")
if Tk and '--no-err-windows' not in sys.argv: # pragma: no cover
root = Tk()
root.withdraw()
messagebox.showerror("qutebrowser: Fatal error!", text)
else:
sys.stderr.write(text)
sys.stderr.flush()
sys.exit(1)
3
Example 16
Project: ruffus Source File: test_graphviz.py
def test_graphviz_dot(self):
"""Make sure annotations from graphviz appear in dot
"""
if sys.hexversion >= 0x03000000:
# everything is unicode in python3
s = BytesIO()
else:
s = StringIO()
pipeline_printout_graph (
s,
# use flowchart file name extension to decide flowchart format
# e.g. svg, jpg etc.
"dot",
[Final_target, Up_to_date_final_target], pipeline= "main")
self.assertTrue('[URL="http://cnn.com", color="#FF0000", fillcolor="#FFCCCC", fontcolor="#4B6000", height=1.5, label=<What is this?<BR/> What <FONT COLOR="red">is</FONT>this???>, pencolor="#FF0000", peripheries=5, shape=component, style=dashed]' in s.getvalue().decode())
3
Example 17
def setUp(self):
try:
os.mkdir(tempdir)
except OSError:
pass
if sys.hexversion < 0x03000000:
self.assertRaisesRegex = self.assertRaisesRegexp27
if sys.hexversion < 0x02700000:
self.assertIn = self.my_assertIn
3
Example 18
Project: picard Source File: test_bytes2human.py
def setUp(self):
# we are using temporary locales for tests
self.tmp_path = tempfile.mkdtemp().decode("utf-8")
if sys.hexversion >= 0x020700F0:
self.addCleanup(shutil.rmtree, self.tmp_path)
self.localedir = os.path.join(self.tmp_path, 'locale')
3
Example 19
Project: ExoData Source File: astroclasses.py
def _createMagConversionDict():
""" loads magnitude_conversion.dat which is table A% 1995ApJS..101..117K
"""
magnitude_conversion_filepath = resource_stream(__name__, 'data/magnitude_conversion.dat')
raw_table = np.loadtxt(magnitude_conversion_filepath, '|S5')
magDict = {}
for row in raw_table:
if sys.hexversion >= 0x03000000:
starClass = row[1].decode("utf-8") # otherwise we get byte ints or b' caused by 2to3
tableData = [x.decode("utf-8") for x in row[3:]]
else:
starClass = row[1]
tableData = row[3:]
magDict[starClass] = tableData
return magDict
3
Example 20
def _make_request(self, type, path, **kwargs):
VERBS = ['GET', 'POST', 'DELETE', 'PUT']
if type not in VERBS:
raise Exception('invalid request method')
# TODO don't assume this doesn't exist in kwargs
kwargs['headers'] = {
'User-Agent': 'nsone-python %s python 0x%s %s'
% (version, sys.hexversion, sys.platform),
'X-NSONE-Key': self._config.getAPIKey()
}
if 'body' in kwargs:
kwargs['data'] = json.dumps(kwargs['body'])
del kwargs['body']
return self._transport.send(type, self._make_url(path), **kwargs)
3
Example 21
Project: pelisalacarta Source File: downloader.py
def __get_download_headers__(self):
for x in range(3):
print ("Conectando con el servidor...")
try:
if not sys.hexversion > 0x0204FFFF:
conn = urllib2.urlopen(urllib2.Request(self.url, headers=self._headers))
conn.fp._sock.close()
else:
conn = urllib2.urlopen(urllib2.Request(self.url, headers=self._headers), timeout=5)
except:
self.response_headers = dict()
self._state = self.states.error
print ("Error al conectar con el servidor")
else:
self.response_headers = conn.headers.dict
self._state = self.states.stopped
break
3
Example 22
def version():
"""Wrapper for opj_version library routine."""
OPENJPEG.opj_version.restype = ctypes.c_char_p
library_version = OPENJPEG.opj_version()
if sys.hexversion >= 0x03000000:
return library_version.decode('utf-8')
else:
return library_version
3
Example 23
def _createMagConversionDict():
"""Loads magnitude_conversion.dat which is table A% 1995ApJS..101..117K
"""
magnitude_conversion_filepath = resource_stream(
__name__, 'data/magnitude_conversion.dat')
raw_table = np.loadtxt(magnitude_conversion_filepath, '|S5')
magDict = {}
for row in raw_table:
if sys.hexversion >= 0x03000000:
# otherwise we get byte ints or b' caused by 2to3
starClass = row[1].decode("utf-8")
tableData = [x.decode("utf-8") for x in row[3:]]
else:
starClass = row[1]
tableData = row[3:]
magDict[starClass] = tableData
return magDict
3
Example 24
def testSendVarious(self):
self.server.add_method('/blah', 'ihfdscb', self.callback)
if sys.hexversion < 0x03000000:
self.server.send(1234, '/blah', 123, 2**42, 123.456, 666.666, "hello", ('c', 'x'), (12, 34, 56))
else:
self.server.send(1234, '/blah', 123, ('h', 2**42), 123.456, 666.666, "hello", ('c', 'x'), (12, 34, 56))
self.assertTrue(self.server.recv())
self.assertEqual(self.cb.types, 'ihfdscb')
self.assertEqual(len(self.cb.args), len(self.cb.types))
self.assertEqual(self.cb.args[0], 123)
self.assertEqual(self.cb.args[1], 2**42)
self.assertAlmostEqual(self.cb.args[2], 123.456, 3)
self.assertAlmostEqual(self.cb.args[3], 666.666, 3)
self.assertEqual(self.cb.args[4], "hello")
self.assertEqual(self.cb.args[5], 'x')
if sys.hexversion < 0x03000000:
self.assertEqual(self.cb.args[6], [12, 34, 56])
else:
self.assertEqual(self.cb.args[6], b'\x0c\x22\x38')
3
Example 25
def __open_connection__(self, start, end):
headers = self._headers.copy()
if not end: end = ""
headers.update({"Range": "bytes=%s-%s" % (start, end)})
if not sys.hexversion > 0x0204FFFF:
conn = urllib2.urlopen(urllib2.Request(self.url, headers=headers))
else:
conn = urllib2.urlopen(urllib2.Request(self.url, headers=headers), timeout=5)
return conn
3
Example 26
Project: pycscope Source File: testissues.py
def testIssue0009(self):
""" Verify dumpCst works on tuples.
"""
try:
from cStringIO import StringIO
except ImportError:
from io import StringIO
out = StringIO()
import parser, sys
cst = parser.suite("import sys\na = b\n")
pycscope.dumpCst(cst.totuple(True), out)
output = out.getvalue()
if sys.hexversion < 0x03000000:
expected = "['file_input',\n ['stmt',\n ['simple_stmt',\n ['small_stmt',\n ['import_stmt',\n ['import_name',\n ['NAME', 'import', 1],\n ['dotted_as_names',\n ['dotted_as_name', ['dotted_name', ['NAME', 'sys', 1]]]]]]],\n ['NEWLINE', '', 1]]],\n ['stmt',\n ['simple_stmt',\n ['small_stmt',\n ['expr_stmt',\n ['testlist',\n ['test',\n ['or_test',\n ['and_test',\n ['not_test',\n ['comparison',\n ['expr',\n ['xor_expr',\n ['and_expr',\n ['shift_expr',\n ['arith_expr',\n ['term',\n ['factor',\n ['power', ['atom', ['NAME', 'a', 2]]]]]]]]]]]]]]]],\n ['EQUAL', '=', 2],\n ['testlist',\n ['test',\n ['or_test',\n ['and_test',\n ['not_test',\n ['comparison',\n ['expr',\n ['xor_expr',\n ['and_expr',\n ['shift_expr',\n ['arith_expr',\n ['term',\n ['factor',\n ['power', ['atom', ['NAME', 'b', 2]]]]]]]]]]]]]]]]]],\n ['NEWLINE', '', 2]]],\n ['NEWLINE', '', 2],\n ['ENDMARKER', '', 2]]\n"
else:
expected = "['file_input',\n ['stmt',\n ['simple_stmt',\n ['small_stmt',\n ['import_stmt',\n ['import_name',\n ['NAME', 'import', 1],\n ['dotted_as_names',\n ['dotted_as_name', ['dotted_name', ['NAME', 'sys', 1]]]]]]],\n ['NEWLINE', '', 1]]],\n ['stmt',\n ['simple_stmt',\n ['small_stmt',\n ['expr_stmt',\n ['testlist_star_expr',\n ['test',\n ['or_test',\n ['and_test',\n ['not_test',\n ['comparison',\n ['expr',\n ['xor_expr',\n ['and_expr',\n ['shift_expr',\n ['arith_expr',\n ['term',\n ['factor',\n ['power', ['atom', ['NAME', 'a', 2]]]]]]]]]]]]]]]],\n ['EQUAL', '=', 2],\n ['testlist_star_expr',\n ['test',\n ['or_test',\n ['and_test',\n ['not_test',\n ['comparison',\n ['expr',\n ['xor_expr',\n ['and_expr',\n ['shift_expr',\n ['arith_expr',\n ['term',\n ['factor',\n ['power', ['atom', ['NAME', 'b', 2]]]]]]]]]]]]]]]]]],\n ['NEWLINE', '', 2]]],\n ['NEWLINE', '', 2],\n ['ENDMARKER', '', 2]]\n"
print(repr(output))
self.assertEqual(output, expected)
3
Example 27
def open_url(self, link):
"""
Open the default browser at link location
@param link the link to open
"""
import webbrowser
new = 0
if sys.hexversion >= 0x2050000:
new = 2
webbrowser.open(link, new=new)
3
Example 28
def xprint(m, *args):
if sys.hexversion < 0x3000000:
m = m.decode('utf8')
if args:
m = m % args
if sys.hexversion < 0x3000000:
m = m.encode('utf8')
sys.stdout.write(m)
sys.stdout.write('\n')
3
Example 29
Project: yum Source File: pkgtag_db.py
def catchSqliteException(func):
"""This decorator converts sqlite exceptions into PkgTagsError"""
def newFunc(*args, **kwargs):
try:
return func(*args, **kwargs)
except sqlutils.sqlite.Error, e:
# 2.4.x requires this, but 2.6.x complains about even hasattr()
# of e.message ... *sigh*
if sys.hexversion < 0x02050000:
if hasattr(e,'message'):
raise PkgTagsError, str(e.message)
else:
raise PkgTagsError, str(e)
raise PkgTagsError, str(e)
newFunc.__name__ = func.__name__
newFunc.__doc__ = func.__doc__
newFunc.__dict__.update(func.__dict__)
return newFunc
3
Example 30
@classmethod
def u(cls, s):
if sys.hexversion < 0x03000000 and isinstance(s, str):
s = s.decode('utf-8')
if sys.hexversion >= 0x03000000 and isinstance(s, bytes):
s = s.decode('utf-8')
return s
3
Example 31
def get_pythonlib_name(debug = 0):
"""Return the name of python library (necessary to link on NT with
mingw."""
# Yeah, distutils burried the link option on NT deep down in
# Extension module, we cannot reuse it !
if debug == 1:
template = 'python%d%d_d'
else:
template = 'python%d%d'
return template % (sys.hexversion >> 24,
(sys.hexversion >> 16) & 0xff)
3
Example 32
def hook(mod):
# Forbid imports in the port_v3 directory under Python 2
# The code wouldn't import and would crash the build process.
if sys.hexversion < 0x03000000:
mod.__path__ = []
return mod
3
Example 33
def _generate(self):
""" Generate the Python code. """
if sys.hexversion >= 0x03000000:
if self._opts.output == '-':
from io import TextIOWrapper
pyfile = TextIOWrapper(sys.stdout.buffer, encoding='utf8')
else:
pyfile = open(self._opts.output, 'wt', encoding='utf8')
else:
if self._opts.output == '-':
pyfile = sys.stdout
else:
pyfile = open(self._opts.output, 'wt')
compileUi(self._ui_file, pyfile, self._opts.execute, self._opts.indent,
self._opts.pyqt3_wrapper, self._opts.from_imports)
3
Example 34
Project: mythbox Source File: runtime.py
def shortPythonVersion():
hv = sys.hexversion
major = (hv & 0xff000000L) >> 24
minor = (hv & 0x00ff0000L) >> 16
teeny = (hv & 0x0000ff00L) >> 8
return "%s.%s.%s" % (major,minor,teeny)
3
Example 35
Project: skytools Source File: scripting.py
def _load_log_config(fn, defs):
"""Fixed fileConfig."""
# Work around fileConfig default behaviour to disable
# not only old handlers on load (which slightly makes sense)
# but also old logger objects (which does not make sense).
if sys.hexversion >= 0x2060000:
logging.config.fileConfig(fn, defs, False)
else:
logging.config.fileConfig(fn, defs)
root = logging.getLogger()
for lg in root.manager.loggerDict.values():
lg.disabled = 0
3
Example 36
Project: compmake Source File: manager_multiprocessing.py
def process_init(self):
Shared.event_queue = Queue(self.num_processes * 1000)
# info('Starting %d processes' % self.num_processes)
kwargs = {}
if sys.hexversion >= 0x02070000:
# added in 2.7.2
kwargs['maxtasksperchild'] = 1
self.pool = Pool(processes=self.num_processes,
initializer=worker_initialization,
**kwargs)
self.max_num_processing = self.num_processes
3
Example 37
Project: brickv Source File: esp_flash.py
def unhexify(hs):
s = []
for i in range(0, len(hs) - 1, 2):
s.append(int(hs[i] + hs[i + 1], 16))
if sys.hexversion < 0x03000000:
return ''.join(map(chr, s))
else:
return bytes(s)
3
Example 38
Project: blackhole Source File: setup_helpers.py
def require_python(minimum):
if sys.hexversion < minimum:
hversion = hex(minimum)[2:]
if len(hversion) % 2 != 0:
hversion = '0' + hversion
split = list(hversion)
parts = []
while split:
parts.append(int(''.join((split.pop(0), split.pop(0))), 16))
major, minor, micro, release = parts
if release == 0xf0:
print('Python {0}.{1}.{2} or better is required'.format(
major, minor, micro))
else:
print('Python {0}.{1}.{2} ({3}) or better is required'.format(
major, minor, micro, hex(release)[2:]))
sys.exit(1)
3
Example 39
Project: picard Source File: test_releaseversions.py
def setUp(self):
# we are using temporary locales for tests
self.tmp_path = tempfile.mkdtemp().decode("utf-8")
if sys.hexversion >= 0x020700F0:
self.addCleanup(shutil.rmtree, self.tmp_path)
self.localedir = os.path.join(self.tmp_path, 'locale')
setup_gettext(self.localedir, 'C')
3
Example 40
def main():
if sys.hexversion < 0x3050000:
print('requires Python 3.5+.')
exit(-1)
if not os.path.exists('configure.json'):
print('please "configure.json" configuration file.')
exit(-1)
start_service()
3
Example 41
Project: klepto Source File: tools.py
def _b(message):
"""convert string to correct format for buffer object"""
import sys
if hex(sys.hexversion) >= '0x30000f0':
import codecs
return codecs.latin_1_encode(message)[0]
return message
2
Example 42
Project: ganga Source File: PACKAGE.py
def standardSetup(setup=setup):
# here we assume that the Ganga has been already prepended to sys.path by the caller
if checkPythonVersion(_defaultMinVersion,_defaultMinHexVersion):
for name in setup.packages:
if name == '4Suite' and (sys.hexversion > 0x2050000 and sys.hexversion < 0x2060000):
# hack the 4Suite path for 2.5
setup.packages['4Suite']['PYTHONPATH'] = [ package.replace('2.4','2.5') for package in setup.packages['4Suite']['PYTHONPATH'] ]
setup.packages['4Suite']['syspath'] = [ package.replace('2.4','2.5') for package in setup.packages['4Suite']['syspath'] ]
elif name == '4Suite' and sys.hexversion > 0x2060000:
# hack the 4Suite path for 2.6
setup.packages['4Suite']['PYTHONPATH'] = [ package.replace('2.4','2.6') for package in setup.packages['4Suite']['PYTHONPATH'] ]
setup.packages['4Suite']['syspath'] = [ package.replace('2.4','2.6') for package in setup.packages['4Suite']['syspath'] ]
else:
sys.exit()
for p in setup.packages:
setup.prependPath(p,'PYTHONPATH')
setup.prependPath(p,'LD_LIBRARY_PATH')
setup.prependPath(p,'PATH')
setup.setPath(p,'DQ2_HOME')
if 'DQ2_ENDUSER_SETUP' in setup.packages[p]:
os.environ['DQ2_ENDUSER_SETUP'] = setup.packages[p]['DQ2_ENDUSER_SETUP']
setup.setPath(p,'PANDA_SYS')
setup.setPath(p,'RUCIO_HOME')
if 'RUCIO_AUTH_TYPE' in setup.packages[p]:
os.environ['RUCIO_AUTH_TYPE'] = setup.packages[p]['RUCIO_AUTH_TYPE']
# update the syspath and PYTHONPATH for the packages
for name in setup.packages:
setup.setSysPath(name)
2
Example 43
Project: portage Source File: movefile.py
def movefile(src, dest, newmtime=None, sstat=None, mysettings=None,
hardlink_candidates=None, encoding=_encodings['fs']):
"""moves a file from src to dest, preserving all permissions and attributes; mtime will
be preserved even when moving across filesystems. Returns mtime as integer on success
and None on failure. mtime is expressed in seconds in Python <3.3 and nanoseconds in
Python >=3.3. Move is atomic."""
if mysettings is None:
mysettings = portage.settings
src_bytes = _unicode_encode(src, encoding=encoding, errors='strict')
dest_bytes = _unicode_encode(dest, encoding=encoding, errors='strict')
xattr_enabled = "xattr" in mysettings.features
selinux_enabled = mysettings.selinux_enabled()
if selinux_enabled:
selinux = _unicode_module_wrapper(_selinux, encoding=encoding)
_copyfile = selinux.copyfile
_rename = selinux.rename
else:
_copyfile = _shutil.copyfile
_rename = _os.rename
lchown = _unicode_func_wrapper(portage.data.lchown, encoding=encoding)
os = _unicode_module_wrapper(_os,
encoding=encoding, overrides=_os_overrides)
try:
if not sstat:
sstat = os.lstat(src)
except SystemExit as e:
raise
except Exception as e:
writemsg("!!! %s\n" % _("Stating source file failed... movefile()"),
noiselevel=-1)
writemsg("!!! %s\n" % (e,), noiselevel=-1)
return None
destexists = 1
try:
dstat = os.lstat(dest)
except (OSError, IOError):
dstat = os.lstat(os.path.dirname(dest))
destexists = 0
if bsd_chflags:
if destexists and dstat.st_flags != 0:
bsd_chflags.lchflags(dest, 0)
# Use normal stat/chflags for the parent since we want to
# follow any symlinks to the real parent directory.
pflags = os.stat(os.path.dirname(dest)).st_flags
if pflags != 0:
bsd_chflags.chflags(os.path.dirname(dest), 0)
if destexists:
if stat.S_ISLNK(dstat[stat.ST_MODE]):
try:
os.unlink(dest)
destexists = 0
except SystemExit as e:
raise
except Exception as e:
pass
if stat.S_ISLNK(sstat[stat.ST_MODE]):
try:
target = os.readlink(src)
if mysettings and "D" in mysettings and \
target.startswith(mysettings["D"]):
target = target[len(mysettings["D"])-1:]
if destexists and not stat.S_ISDIR(dstat[stat.ST_MODE]):
os.unlink(dest)
try:
if selinux_enabled:
selinux.symlink(target, dest, src)
else:
os.symlink(target, dest)
except OSError as e:
# Some programs will create symlinks automatically, so we have
# to tolerate these links being recreated during the merge
# process. In any case, if the link is pointing at the right
# place, we're in good shape.
if e.errno not in (errno.ENOENT, errno.EEXIST) or \
target != os.readlink(dest):
raise
lchown(dest, sstat[stat.ST_UID], sstat[stat.ST_GID])
try:
_os.unlink(src_bytes)
except OSError:
pass
if sys.hexversion >= 0x3030000:
try:
os.utime(dest, ns=(sstat.st_mtime_ns, sstat.st_mtime_ns), follow_symlinks=False)
except NotImplementedError:
# utimensat() and lutimes() missing in libc.
return os.stat(dest, follow_symlinks=False).st_mtime_ns
else:
return sstat.st_mtime_ns
else:
# utime() in Python <3.3 only works on the target of a symlink, so it's not
# possible to preserve mtime on symlinks.
return os.lstat(dest)[stat.ST_MTIME]
except SystemExit as e:
raise
except Exception as e:
writemsg("!!! %s\n" % _("failed to properly create symlink:"),
noiselevel=-1)
writemsg("!!! %s -> %s\n" % (dest, target), noiselevel=-1)
writemsg("!!! %s\n" % (e,), noiselevel=-1)
return None
hardlinked = False
# Since identical files might be merged to multiple filesystems,
# so os.link() calls might fail for some paths, so try them all.
# For atomic replacement, first create the link as a temp file
# and them use os.rename() to replace the destination.
if hardlink_candidates:
head, tail = os.path.split(dest)
hardlink_tmp = os.path.join(head, ".%s._portage_merge_.%s" % \
(tail, os.getpid()))
try:
os.unlink(hardlink_tmp)
except OSError as e:
if e.errno != errno.ENOENT:
writemsg(_("!!! Failed to remove hardlink temp file: %s\n") % \
(hardlink_tmp,), noiselevel=-1)
writemsg("!!! %s\n" % (e,), noiselevel=-1)
return None
del e
for hardlink_src in hardlink_candidates:
try:
os.link(hardlink_src, hardlink_tmp)
except OSError:
continue
else:
try:
os.rename(hardlink_tmp, dest)
except OSError as e:
writemsg(_("!!! Failed to rename %s to %s\n") % \
(hardlink_tmp, dest), noiselevel=-1)
writemsg("!!! %s\n" % (e,), noiselevel=-1)
return None
hardlinked = True
try:
_os.unlink(src_bytes)
except OSError:
pass
break
renamefailed = 1
if hardlinked:
renamefailed = False
if not hardlinked and (selinux_enabled or sstat.st_dev == dstat.st_dev):
try:
if selinux_enabled:
selinux.rename(src, dest)
else:
os.rename(src, dest)
renamefailed = 0
except OSError as e:
if e.errno != errno.EXDEV:
# Some random error.
writemsg("!!! %s\n" % _("Failed to move %(src)s to %(dest)s") %
{"src": src, "dest": dest}, noiselevel=-1)
writemsg("!!! %s\n" % (e,), noiselevel=-1)
return None
# Invalid cross-device-link 'bind' mounted or actually Cross-Device
if renamefailed:
if stat.S_ISREG(sstat[stat.ST_MODE]):
dest_tmp = dest + "#new"
dest_tmp_bytes = _unicode_encode(dest_tmp, encoding=encoding,
errors='strict')
try: # For safety copy then move it over.
_copyfile(src_bytes, dest_tmp_bytes)
if xattr_enabled:
try:
_copyxattr(src_bytes, dest_tmp_bytes,
exclude=mysettings.get("PORTAGE_XATTR_EXCLUDE", ""))
except SystemExit:
raise
except:
msg = _("Failed to copy extended attributes. "
"In order to avoid this error, set "
"FEATURES=\"-xattr\" in make.conf.")
msg = textwrap.wrap(msg, 65)
for line in msg:
writemsg("!!! %s\n" % (line,), noiselevel=-1)
raise
_apply_stat(sstat, dest_tmp_bytes)
_rename(dest_tmp_bytes, dest_bytes)
_os.unlink(src_bytes)
except SystemExit as e:
raise
except Exception as e:
writemsg("!!! %s\n" % _('copy %(src)s -> %(dest)s failed.') %
{"src": src, "dest": dest}, noiselevel=-1)
writemsg("!!! %s\n" % (e,), noiselevel=-1)
return None
else:
#we don't yet handle special, so we need to fall back to /bin/mv
a = spawn([MOVE_BINARY, '-f', src, dest], env=os.environ)
if a != os.EX_OK:
writemsg(_("!!! Failed to move special file:\n"), noiselevel=-1)
writemsg(_("!!! '%(src)s' to '%(dest)s'\n") % \
{"src": _unicode_decode(src, encoding=encoding),
"dest": _unicode_decode(dest, encoding=encoding)}, noiselevel=-1)
writemsg("!!! %s\n" % a, noiselevel=-1)
return None # failure
# In Python <3.3 always use stat_obj[stat.ST_MTIME] for the integral timestamp
# which is returned, since the stat_obj.st_mtime float attribute rounds *up*
# if the nanosecond part of the timestamp is 999999881 ns or greater.
try:
if hardlinked:
if sys.hexversion >= 0x3030000:
newmtime = os.stat(dest).st_mtime_ns
else:
newmtime = os.stat(dest)[stat.ST_MTIME]
else:
# Note: It is not possible to preserve nanosecond precision
# (supported in POSIX.1-2008 via utimensat) with the IEEE 754
# double precision float which only has a 53 bit significand.
if newmtime is not None:
if sys.hexversion >= 0x3030000:
os.utime(dest, ns=(newmtime, newmtime))
else:
os.utime(dest, (newmtime, newmtime))
else:
if sys.hexversion >= 0x3030000:
newmtime = sstat.st_mtime_ns
else:
newmtime = sstat[stat.ST_MTIME]
if renamefailed:
if sys.hexversion >= 0x3030000:
# If rename succeeded then timestamps are automatically
# preserved with complete precision because the source
# and destination inodes are the same. Otherwise, manually
# update timestamps with nanosecond precision.
os.utime(dest, ns=(newmtime, newmtime))
else:
# If rename succeeded then timestamps are automatically
# preserved with complete precision because the source
# and destination inodes are the same. Otherwise, round
# down to the nearest whole second since python's float
# st_mtime cannot be used to preserve the st_mtim.tv_nsec
# field with complete precision. Note that we have to use
# stat_obj[stat.ST_MTIME] here because the float
# stat_obj.st_mtime rounds *up* sometimes.
os.utime(dest, (newmtime, newmtime))
except OSError:
# The utime can fail here with EPERM even though the move succeeded.
# Instead of failing, use stat to return the mtime if possible.
try:
if sys.hexversion >= 0x3030000:
newmtime = os.stat(dest).st_mtime_ns
else:
newmtime = os.stat(dest)[stat.ST_MTIME]
except OSError as e:
writemsg(_("!!! Failed to stat in movefile()\n"), noiselevel=-1)
writemsg("!!! %s\n" % dest, noiselevel=-1)
writemsg("!!! %s\n" % str(e), noiselevel=-1)
return None
if bsd_chflags:
# Restore the flags we saved before moving
if pflags:
bsd_chflags.chflags(os.path.dirname(dest), pflags)
return newmtime
2
Example 44
Project: pan-python Source File: xapi.py
def __api_request(self, query):
self.__debug_request(query)
# type=keygen request will urlencode key if needed so don't
# double encode
if 'key' in query:
query2 = query.copy()
key = query2['key']
del query2['key']
data = urlencode(query2)
data += '&' + 'key=' + key
else:
data = urlencode(query)
self._log(DEBUG3, 'data: %s', type(data))
self._log(DEBUG3, 'data.encode(): %s', type(data.encode()))
url = self.uri
if self.use_get:
url += '?' + data
request = Request(url)
else:
# data must by type 'bytes' for 3.x
request = Request(url, data.encode())
self._log(DEBUG1, 'method: %s', request.get_method())
kwargs = {
'url': request,
}
if (sys.hexversion & 0xffff0000 == 0x02060000):
# XXX allow 2.6 as a one-off while still using .major
# named attribute
pass
elif (sys.version_info.major == 2 and sys.hexversion >= 0x02070900 or
sys.version_info.major == 3 and sys.hexversion >= 0x03040300):
# see PEP 476; urlopen() has context
if self.ssl_context is None:
# don't perform certificate verification
kwargs['context'] = ssl._create_unverified_context()
else:
kwargs['context'] = self.ssl_context
elif self.ssl_context is not None:
https_handler = HTTPSHandler(context=self.ssl_context)
opener = build_opener(https_handler)
install_opener(opener)
if self.timeout is not None:
kwargs['timeout'] = self.timeout
try:
response = urlopen(**kwargs)
# XXX handle httplib.BadStatusLine when http to port 443
except self._certificateerror as e:
self.status_detail = 'ssl.CertificateError: %s' % e
return False
except URLError as error:
msg = 'URLError:'
if hasattr(error, 'code'):
msg += ' code: %s' % error.code
if hasattr(error, 'reason'):
msg += ' reason: %s' % error.reason
if not (hasattr(error, 'code') or hasattr(error, 'reason')):
msg += ' unknown error (Kevin heart Python)'
self.status_detail = msg
return False
self._log(DEBUG2, 'HTTP response headers:')
self._log(DEBUG2, '%s', response.info())
return response
2
Example 45
Project: hdf5storage Source File: utilities.py
def process_path(pth):
""" Processes paths.
Processes the provided path and breaks it into it Group part
(`groupname`) and target part (`targetname`). ``bytes`` paths are
converted to ``str``. Separated paths are given as an iterable of
``str`` and ``bytes``. Each part of a separated path is escaped
using ``escape_path``. Otherwise, the path is assumed to be already
escaped. Escaping is done so that targets with a part that starts
with one or more periods, contain slashes, and/or contain nulls can
be used without causing the wrong Group to be looked in or the wrong
target to be looked at. It essentially allows one to make a Dataset
named ``'..'`` or ``'a/a'`` instead of moving around in the Dataset
hierarchy.
All paths are POSIX style.
Parameters
----------
pth : str or bytes or iterable of str or bytes
The POSIX style path as a ``str`` or ``bytes`` or the
separated path in an iterable with the elements being ``str``
and ``bytes``. For separated paths, escaping will be done
on each part.
Returns
-------
groupname : str
The path to the Group containing the target `pth` was pointing
to.
targetname : str
The name of the target pointed to by `pth` in the Group
`groupname`.
Raises
------
TypeError
If `pth` is not of the right type.
See Also
--------
escape_path
"""
# Do conversions and possibly escapes.
if isinstance(pth, bytes):
p = pth.decode('utf-8')
elif (sys.hexversion >= 0x03000000 and isinstance(pth, str)) \
or (sys.hexversion < 0x03000000 \
and isinstance(pth, unicode)):
p = pth
elif not isinstance(pth, collections.Iterable):
raise TypeError('p must be str, bytes, or an iterable '
+ 'solely of one of those two.')
else:
# Check that all elements are unicode or bytes.
if sys.hexversion >= 0x03000000:
if not all([isinstance(s, (bytes, str)) for s in pth]):
raise TypeError('Elements of p must be str or bytes.')
else:
if not all([isinstance(s, (str, unicode)) for s in pth]):
raise TypeError('Elements of p must be str or '
+ 'unicode.')
# Escape (and possibly convert to unicode) each element and then
# join them all together.
parts = [None] * len(pth)
for i, s in enumerate(pth):
if isinstance(s, bytes):
s = s.decode('utf-8')
parts[i] = escape_path(s)
parts = tuple(parts)
p = posixpath.join(*parts)
# Remove double slashes and a non-root trailing slash.
path = posixpath.normpath(p)
# Extract the group name and the target name (will be a dataset if
# data can be mapped to it, but will end up being made into a group
# otherwise. As HDF5 files use posix path, conventions, posixpath
# will do everything.
groupname = posixpath.dirname(path)
targetname = posixpath.basename(path)
# If groupname got turned into blank, then it is just root.
if len(groupname) == 0:
groupname = b'/'.decode('ascii')
# If targetname got turned blank, then it is the current directory.
if len(targetname) == 0:
targetname = b'.'.decode('ascii')
return groupname, targetname
2
Example 46
Project: hdf5storage Source File: asserts.py
def assert_equal_none_format(a, b, options=None):
# Compares a and b for equality. b is always the original. If they
# are dictionaries, a must be a structured ndarray and they must
# have the same set of keys, after which they values must all be
# compared. If they are a collection type (list, tuple, set,
# frozenset, or deque), then the compairison must be made with b
# converted to an object array. If the original is not a numpy type
# (isn't or doesn't inherit from np.generic or np.ndarray), then it
# is a matter of converting it to the appropriate numpy
# type. Otherwise, both are supposed to be numpy types. For object
# arrays, each element must be iterated over to be compared. Then,
# if it isn't a string type, then they must have the same dtype,
# shape, and all elements. If it is an empty string, then it would
# have been stored as just a null byte (recurse to do that
# comparison). If it is a bytes_ type, the dtype, shape, and
# elements must all be the same. If it is string_ type, we must
# convert to uint32 and then everything can be compared. Big longs
# and ints get written as numpy.bytes_.
if type(b) == dict or (sys.hexversion >= 0x2070000
and type(b) == collections.OrderedDict):
assert type(a) == np.ndarray
assert a.dtype.names is not None
# Determine if any of the keys could not be stored as str. If
# they all can be, then the dtype field names should be the
# keys. Otherwise, they should be 'keys' and 'values'.
all_str_keys = True
if sys.hexversion >= 0x03000000:
tp_str = str
tp_bytes = bytes
converters = {tp_str: lambda x: x,
tp_bytes: lambda x: x.decode('UTF-8'),
np.bytes_:
lambda x: bytes(x).decode('UTF-8'),
np.unicode_: lambda x: str(x)}
tp_conv = lambda x: converters[type(x)](x)
tp_conv_str = lambda x: tp_conv(x)
else:
tp_str = unicode
tp_bytes = str
converters = {tp_str: lambda x: x,
tp_bytes: lambda x: x.decode('UTF-8'),
np.bytes_:
lambda x: bytes(x).decode('UTF-8'),
np.unicode_: lambda x: unicode(x)}
tp_conv = lambda x: converters[type(x)](x)
tp_conv_str = lambda x: tp_conv(x).encode('UTF-8')
tps = tuple(converters.keys())
for k in b.keys():
if type(k) not in tps:
all_str_keys = False
break
try:
k_str = tp_conv(k)
except:
all_str_keys = False
break
if all_str_keys:
assert set(a.dtype.names) == set([tp_conv_str(k)
for k in b.keys()])
for k in b:
assert_equal_none_format(a[tp_conv_str(k)][0],
b[k], options)
else:
names = (options.dict_like_keys_name,
options.dict_like_values_name)
assert set(a.dtype.names) == set(names)
keys = a[names[0]]
values = a[names[1]]
assert_equal_none_format(keys, tuple(b.keys()), options)
assert_equal_none_format(values, tuple(b.values()), options)
elif type(b) in (list, tuple, set, frozenset, collections.deque):
assert_equal_none_format(a, np.object_(list(b)), options)
elif not isinstance(b, (np.generic, np.ndarray)):
if b is None:
# It should be np.float64([])
assert type(a) == np.ndarray
assert a.dtype == np.float64([]).dtype
assert a.shape == (0, )
elif (sys.hexversion >= 0x03000000 \
and isinstance(b, (bytes, bytearray))) \
or (sys.hexversion < 0x03000000 \
and isinstance(b, (bytes, bytearray))):
assert a == np.bytes_(b)
elif (sys.hexversion >= 0x03000000 \
and isinstance(b, str)) \
or (sys.hexversion < 0x03000000 \
and isinstance(b, unicode)):
assert_equal_none_format(a, np.unicode_(b), options)
elif (sys.hexversion >= 0x03000000 \
and type(b) == int) \
or (sys.hexversion < 0x03000000 \
and type(b) == long):
if b > 2**63 or b < -(2**63 - 1):
assert_equal_none_format(a, np.bytes_(b), options)
else:
assert_equal_none_format(a, np.int64(b), options)
else:
assert_equal_none_format(a, np.array(b)[()], options)
else:
if b.dtype.name != 'object':
if b.dtype.char in ('U', 'S'):
if b.dtype.char == 'S' and b.shape == tuple() \
and len(b) == 0:
assert_equal(a, \
np.zeros(shape=tuple(), dtype=b.dtype.char), \
options)
elif b.dtype.char == 'U':
if b.shape == tuple() and len(b) == 0:
c = np.uint32(())
else:
c = np.atleast_1d(b).view(np.uint32)
assert a.dtype == c.dtype
assert a.shape == c.shape
npt.assert_equal(a, c)
else:
assert a.dtype == b.dtype
assert a.shape == b.shape
npt.assert_equal(a, b)
else:
# Now, if b.shape is just all ones, then a.shape will
# just be (1,). Otherwise, we need to compare the shapes
# directly. Also, dimensions need to be squeezed before
# comparison in this case.
assert np.prod(a.shape) == np.prod(b.shape)
assert a.shape == b.shape \
or (np.prod(b.shape) == 1 and a.shape == (1,))
if np.prod(a.shape) == 1:
a = np.squeeze(a)
b = np.squeeze(b)
# If there was a null in the dtype, then it was written
# as a Group so the field order could have changed.
if '\\x00' in str(b.dtype):
assert set(a.dtype.descr) == set(b.dtype.descr)
# Reorder the fields of a.
c = np.empty(shape=b.shape, dtype=b.dtype)
for n in b.dtype.names:
c[n] = a[n]
else:
c = a
assert c.dtype == b.dtype
with warnings.catch_warnings():
warnings.simplefilter('ignore', RuntimeWarning)
npt.assert_equal(c, b)
else:
assert a.dtype == b.dtype
assert a.shape == b.shape
for index, x in np.ndenumerate(a):
assert_equal_none_format(a[index], b[index], options)
2
Example 47
Project: glymur Source File: test_printing.py
def test_icc_profile(self):
"""
verify icc profile printing with a jpx
2.7, 3.3, 3.4, and 3.5 all print ordered dicts differently
Original file tested was input/nonregression/text_GBR.jp2.
"""
fp = BytesIO()
fp.write(b'\x00' * 179)
# Write the colr box header.
buffer = struct.pack('>I4s', 1339, b'colr')
buffer += struct.pack('>BBB', ANY_ICC_PROFILE, 2, 1)
buffer += struct.pack('>IIBB', 1328, 1634758764, 2, 32)
buffer += b'\x00' * 2 + b'mntr' + b'RGB ' + b'XYZ '
# Need a date in bytes 24:36
buffer += struct.pack('>HHHHHH', 2009, 2, 25, 11, 26, 11)
buffer += 'acsp'.encode('utf-8')
buffer += 'APPL'.encode('utf-8')
buffer += b'\x00' * 4
buffer += 'appl'.encode('utf-8') # 48 - 52
buffer += b'\x00' * 16
buffer += struct.pack('>III', 63190, 65536, 54061) # 68 - 80
buffer += 'appl'.encode('utf-8') # 80 - 84
buffer += b'\x00' * 44
fp.write(buffer)
fp.seek(179 + 8)
# Should be able to read the colr box now
if sys.hexversion < 0x03000000:
box = glymur.jp2box.ColourSpecificationBox.parse(fp, 179, 1339)
else:
box = glymur.jp2box.ColourSpecificationBox.parse(fp, 179, 1339)
actual = str(box)
if sys.hexversion < 0x03000000:
expected = fixtures.text_gbr_27
elif sys.hexversion < 0x03040000:
expected = fixtures.text_gbr_33
elif sys.hexversion < 0x03050000:
expected = fixtures.text_gbr_34
else:
expected = fixtures.text_gbr_35
self.assertEqual(actual, expected)
2
Example 48
Project: hdf5storage Source File: asserts.py
def assert_equal_matlab_format(a, b, options=None):
# Compares a and b for equality. b is always the original. If they
# are dictionaries, a must be a structured ndarray and they must
# have the same set of keys, after which they values must all be
# compared. If they are a collection type (list, tuple, set,
# frozenset, or deque), then the compairison must be made with b
# converted to an object array. If the original is not a numpy type
# (isn't or doesn't inherit from np.generic or np.ndarray), then it
# is a matter of converting it to the appropriate numpy
# type. Otherwise, both are supposed to be numpy types. For object
# arrays, each element must be iterated over to be compared. Then,
# if it isn't a string type, then they must have the same dtype,
# shape, and all elements. All strings are converted to numpy.str_
# on read unless they were stored as a numpy.bytes_ due to having
# non-ASCII characters. If it is empty, it has shape (1, 0). A
# numpy.str_ has all of its strings per row compacted together. A
# numpy.bytes_ string has to have the same thing done, but then it
# needs to be converted up to UTF-32 and to numpy.str_ through
# uint32. Big longs and ints end up getting converted to UTF-16
# uint16's when written and read back as UTF-32 numpy.unicode_.
#
# In all cases, we expect things to be at least two dimensional
# arrays.
if type(b) == dict or (sys.hexversion >= 0x2070000
and type(b) == collections.OrderedDict):
assert type(a) == np.ndarray
assert a.dtype.names is not None
# Determine if any of the keys could not be stored as str. If
# they all can be, then the dtype field names should be the
# keys. Otherwise, they should be 'keys' and 'values'.
all_str_keys = True
if sys.hexversion >= 0x03000000:
tp_str = str
tp_bytes = bytes
converters = {tp_str: lambda x: x,
tp_bytes: lambda x: x.decode('UTF-8'),
np.bytes_:
lambda x: bytes(x).decode('UTF-8'),
np.unicode_: lambda x: str(x)}
tp_conv = lambda x: converters[type(x)](x)
tp_conv_str = lambda x: tp_conv(x)
else:
tp_str = unicode
tp_bytes = str
converters = {tp_str: lambda x: x,
tp_bytes: lambda x: x.decode('UTF-8'),
np.bytes_:
lambda x: bytes(x).decode('UTF-8'),
np.unicode_: lambda x: unicode(x)}
tp_conv = lambda x: converters[type(x)](x)
tp_conv_str = lambda x: tp_conv(x).encode('UTF-8')
tps = tuple(converters.keys())
for k in b.keys():
if type(k) not in tps:
all_str_keys = False
break
try:
k_str = tp_conv(k)
except:
all_str_keys = False
break
if all_str_keys:
assert set(a.dtype.names) == set([tp_conv_str(k)
for k in b.keys()])
for k in b:
assert_equal_matlab_format(a[tp_conv_str(k)][0],
b[k], options)
else:
names = (options.dict_like_keys_name,
options.dict_like_values_name)
assert set(a.dtype.names) == set(names)
keys = a[names[0]][0]
values = a[names[1]][0]
assert_equal_matlab_format(keys, tuple(b.keys()), options)
assert_equal_matlab_format(values, tuple(b.values()),
options)
elif type(b) in (list, tuple, set, frozenset, collections.deque):
assert_equal_matlab_format(a, np.object_(list(b)), options)
elif not isinstance(b, (np.generic, np.ndarray)):
if b is None:
# It should be np.zeros(shape=(0, 1), dtype='float64'))
assert type(a) == np.ndarray
assert a.dtype == np.dtype('float64')
assert a.shape == (1, 0)
elif (sys.hexversion >= 0x03000000 \
and isinstance(b, (bytes, str, bytearray))) \
or (sys.hexversion < 0x03000000 \
and isinstance(b, (bytes, unicode, bytearray))):
if len(b) == 0:
assert_equal(a, np.zeros(shape=(1, 0), dtype='U'),
options)
elif isinstance(b, (bytes, bytearray)):
try:
c = np.unicode_(b.decode('ASCII'))
except:
c = np.bytes_(b)
assert_equal(a, np.atleast_2d(c), options)
else:
assert_equal(a, np.atleast_2d(np.unicode_(b)), options)
elif (sys.hexversion >= 0x03000000 \
and type(b) == int) \
or (sys.hexversion < 0x03000000 \
and type(b) == long):
if b > 2**63 or b < -(2**63 - 1):
assert_equal(a, np.atleast_2d(np.unicode_(b)), options)
else:
assert_equal(a, np.atleast_2d(np.int64(b)), options)
else:
assert_equal(a, np.atleast_2d(np.array(b)), options)
else:
if b.dtype.name != 'object':
if b.dtype.char in ('U', 'S'):
if len(b) == 0 and (b.shape == tuple() \
or b.shape == (0, )):
assert_equal(a, np.zeros(shape=(1, 0),
dtype='U'), options)
elif b.dtype.char == 'U':
c = np.atleast_1d(b)
c = np.atleast_2d(c.view(np.dtype('U' \
+ str(c.shape[-1]*c.dtype.itemsize//4))))
assert a.dtype == c.dtype
assert a.shape == c.shape
npt.assert_equal(a, c)
elif b.dtype.char == 'S':
c = np.atleast_1d(b).view(np.ndarray)
if np.all(c.view(np.uint8) < 128):
c = c.view(np.dtype('S' \
+ str(c.shape[-1]*c.dtype.itemsize)))
c = c.view(np.dtype('uint8'))
c = np.uint32(c.view(np.dtype('uint8')))
c = c.view(np.dtype('U' + str(c.shape[-1])))
c = np.atleast_2d(c)
assert a.dtype == c.dtype
assert a.shape == c.shape
npt.assert_equal(a, c)
pass
else:
c = np.atleast_2d(b)
assert a.dtype == c.dtype
assert a.shape == c.shape
with warnings.catch_warnings():
warnings.simplefilter('ignore', RuntimeWarning)
npt.assert_equal(a, c)
else:
c = np.atleast_2d(b)
# An empty complex number gets turned into a real
# number when it is stored.
if np.prod(c.shape) == 0 \
and b.dtype.name.startswith('complex'):
c = np.real(c)
# If it is structured, check that the field names are
# the same, in the same order, and then go through them
# one by one. Otherwise, make sure the dtypes and shapes
# are the same before comparing all values.
if b.dtype.names is None and a.dtype.names is None:
assert a.dtype == c.dtype
assert a.shape == c.shape
with warnings.catch_warnings():
warnings.simplefilter('ignore', RuntimeWarning)
npt.assert_equal(a, c)
else:
assert a.dtype.names is not None
assert b.dtype.names is not None
assert set(a.dtype.names) == set(b.dtype.names)
# The ordering of fields must be preserved if the
# MATLAB_fields attribute could be used, which can
# only be done if there are no non-ascii characters
# in any of the field names.
if sys.hexversion >= 0x03000000:
allfields = ''.join(b.dtype.names)
else:
allfields = unicode('').join( \
[nm.decode('UTF-8') \
for nm in b.dtype.names])
if np.all(np.array([ord(ch) < 128 \
for ch in allfields])):
assert a.dtype.names == b.dtype.names
a = a.flatten()
b = b.flatten()
for k in b.dtype.names:
for index, x in np.ndenumerate(a):
assert_equal_from_matlab(a[k][index],
b[k][index],
options)
else:
c = np.atleast_2d(b)
assert a.dtype == c.dtype
assert a.shape == c.shape
for index, x in np.ndenumerate(a):
assert_equal_matlab_format(a[index], c[index], options)
2
Example 49
Project: portage-funtoo Source File: movefile.py
def movefile(src, dest, newmtime=None, sstat=None, mysettings=None,
hardlink_candidates=None, encoding=_encodings['fs']):
"""moves a file from src to dest, preserving all permissions and attributes; mtime will
be preserved even when moving across filesystems. Returns mtime as integer on success
and None on failure. mtime is expressed in seconds in Python <3.3 and nanoseconds in
Python >=3.3. Move is atomic."""
if mysettings is None:
mysettings = portage.settings
src_bytes = _unicode_encode(src, encoding=encoding, errors='strict')
dest_bytes = _unicode_encode(dest, encoding=encoding, errors='strict')
xattr_enabled = "xattr" in mysettings.features
selinux_enabled = mysettings.selinux_enabled()
if selinux_enabled:
selinux = _unicode_module_wrapper(_selinux, encoding=encoding)
_copyfile = selinux.copyfile
_rename = selinux.rename
else:
_copyfile = _shutil.copyfile
_rename = _os.rename
lchown = _unicode_func_wrapper(portage.data.lchown, encoding=encoding)
os = _unicode_module_wrapper(_os,
encoding=encoding, overrides=_os_overrides)
try:
if not sstat:
sstat=os.lstat(src)
except SystemExit as e:
raise
except Exception as e:
writemsg("!!! %s\n" % _("Stating source file failed... movefile()"),
noiselevel=-1)
writemsg("!!! %s\n" % (e,), noiselevel=-1)
return None
destexists=1
try:
dstat=os.lstat(dest)
except (OSError, IOError):
dstat=os.lstat(os.path.dirname(dest))
destexists=0
if bsd_chflags:
if destexists and dstat.st_flags != 0:
bsd_chflags.lchflags(dest, 0)
# Use normal stat/chflags for the parent since we want to
# follow any symlinks to the real parent directory.
pflags = os.stat(os.path.dirname(dest)).st_flags
if pflags != 0:
bsd_chflags.chflags(os.path.dirname(dest), 0)
if destexists:
if stat.S_ISLNK(dstat[stat.ST_MODE]):
try:
os.unlink(dest)
destexists=0
except SystemExit as e:
raise
except Exception as e:
pass
if stat.S_ISLNK(sstat[stat.ST_MODE]):
try:
target=os.readlink(src)
if mysettings and "D" in mysettings and \
target.startswith(mysettings["D"]):
target = target[len(mysettings["D"])-1:]
if destexists and not stat.S_ISDIR(dstat[stat.ST_MODE]):
os.unlink(dest)
try:
if selinux_enabled:
selinux.symlink(target, dest, src)
else:
os.symlink(target, dest)
except OSError as e:
# Some programs will create symlinks automatically, so we have
# to tolerate these links being recreated during the merge
# process. In any case, if the link is pointing at the right
# place, we're in good shape.
if e.errno not in (errno.ENOENT, errno.EEXIST) or \
target != os.readlink(dest):
raise
lchown(dest,sstat[stat.ST_UID],sstat[stat.ST_GID])
try:
_os.unlink(src_bytes)
except OSError:
pass
if sys.hexversion >= 0x3030000:
try:
os.utime(dest, ns=(sstat.st_mtime_ns, sstat.st_mtime_ns), follow_symlinks=False)
except NotImplementedError:
# utimensat() and lutimes() missing in libc.
return os.stat(dest, follow_symlinks=False).st_mtime_ns
else:
return sstat.st_mtime_ns
else:
# utime() in Python <3.3 only works on the target of a symlink, so it's not
# possible to preserve mtime on symlinks.
return os.lstat(dest)[stat.ST_MTIME]
except SystemExit as e:
raise
except Exception as e:
writemsg("!!! %s\n" % _("failed to properly create symlink:"),
noiselevel=-1)
writemsg("!!! %s -> %s\n" % (dest, target), noiselevel=-1)
writemsg("!!! %s\n" % (e,), noiselevel=-1)
return None
hardlinked = False
# Since identical files might be merged to multiple filesystems,
# so os.link() calls might fail for some paths, so try them all.
# For atomic replacement, first create the link as a temp file
# and them use os.rename() to replace the destination.
if hardlink_candidates:
head, tail = os.path.split(dest)
hardlink_tmp = os.path.join(head, ".%s._portage_merge_.%s" % \
(tail, os.getpid()))
try:
os.unlink(hardlink_tmp)
except OSError as e:
if e.errno != errno.ENOENT:
writemsg(_("!!! Failed to remove hardlink temp file: %s\n") % \
(hardlink_tmp,), noiselevel=-1)
writemsg("!!! %s\n" % (e,), noiselevel=-1)
return None
del e
for hardlink_src in hardlink_candidates:
try:
os.link(hardlink_src, hardlink_tmp)
except OSError:
continue
else:
try:
os.rename(hardlink_tmp, dest)
except OSError as e:
writemsg(_("!!! Failed to rename %s to %s\n") % \
(hardlink_tmp, dest), noiselevel=-1)
writemsg("!!! %s\n" % (e,), noiselevel=-1)
return None
hardlinked = True
try:
_os.unlink(src_bytes)
except OSError:
pass
break
renamefailed=1
if hardlinked:
renamefailed = False
if not hardlinked and (selinux_enabled or sstat.st_dev == dstat.st_dev):
try:
if selinux_enabled:
selinux.rename(src, dest)
else:
os.rename(src,dest)
renamefailed=0
except OSError as e:
if e.errno != errno.EXDEV:
# Some random error.
writemsg("!!! %s\n" % _("Failed to move %(src)s to %(dest)s") %
{"src": src, "dest": dest}, noiselevel=-1)
writemsg("!!! %s\n" % (e,), noiselevel=-1)
return None
# Invalid cross-device-link 'bind' mounted or actually Cross-Device
if renamefailed:
if stat.S_ISREG(sstat[stat.ST_MODE]):
dest_tmp = dest + "#new"
dest_tmp_bytes = _unicode_encode(dest_tmp, encoding=encoding,
errors='strict')
try: # For safety copy then move it over.
_copyfile(src_bytes, dest_tmp_bytes)
if xattr_enabled:
try:
_copyxattr(src_bytes, dest_tmp_bytes,
exclude=mysettings.get("PORTAGE_XATTR_EXCLUDE", "security.*"))
except SystemExit:
raise
except:
msg = _("Failed to copy extended attributes. "
"In order to avoid this error, set "
"FEATURES=\"-xattr\" in make.conf.")
msg = textwrap.wrap(msg, 65)
for line in msg:
writemsg("!!! %s\n" % (line,), noiselevel=-1)
raise
_apply_stat(sstat, dest_tmp_bytes)
_rename(dest_tmp_bytes, dest_bytes)
_os.unlink(src_bytes)
except SystemExit as e:
raise
except Exception as e:
writemsg("!!! %s\n" % _('copy %(src)s -> %(dest)s failed.') %
{"src": src, "dest": dest}, noiselevel=-1)
writemsg("!!! %s\n" % (e,), noiselevel=-1)
return None
else:
#we don't yet handle special, so we need to fall back to /bin/mv
a = spawn([MOVE_BINARY, '-f', src, dest], env=os.environ)
if a != os.EX_OK:
writemsg(_("!!! Failed to move special file:\n"), noiselevel=-1)
writemsg(_("!!! '%(src)s' to '%(dest)s'\n") % \
{"src": _unicode_decode(src, encoding=encoding),
"dest": _unicode_decode(dest, encoding=encoding)}, noiselevel=-1)
writemsg("!!! %s\n" % a, noiselevel=-1)
return None # failure
# In Python <3.3 always use stat_obj[stat.ST_MTIME] for the integral timestamp
# which is returned, since the stat_obj.st_mtime float attribute rounds *up*
# if the nanosecond part of the timestamp is 999999881 ns or greater.
try:
if hardlinked:
if sys.hexversion >= 0x3030000:
newmtime = os.stat(dest).st_mtime_ns
else:
newmtime = os.stat(dest)[stat.ST_MTIME]
else:
# Note: It is not possible to preserve nanosecond precision
# (supported in POSIX.1-2008 via utimensat) with the IEEE 754
# double precision float which only has a 53 bit significand.
if newmtime is not None:
if sys.hexversion >= 0x3030000:
os.utime(dest, ns=(newmtime, newmtime))
else:
os.utime(dest, (newmtime, newmtime))
else:
if sys.hexversion >= 0x3030000:
newmtime = sstat.st_mtime_ns
else:
newmtime = sstat[stat.ST_MTIME]
if renamefailed:
if sys.hexversion >= 0x3030000:
# If rename succeeded then timestamps are automatically
# preserved with complete precision because the source
# and destination inodes are the same. Otherwise, manually
# update timestamps with nanosecond precision.
os.utime(dest, ns=(newmtime, newmtime))
else:
# If rename succeeded then timestamps are automatically
# preserved with complete precision because the source
# and destination inodes are the same. Otherwise, round
# down to the nearest whole second since python's float
# st_mtime cannot be used to preserve the st_mtim.tv_nsec
# field with complete precision. Note that we have to use
# stat_obj[stat.ST_MTIME] here because the float
# stat_obj.st_mtime rounds *up* sometimes.
os.utime(dest, (newmtime, newmtime))
except OSError:
# The utime can fail here with EPERM even though the move succeeded.
# Instead of failing, use stat to return the mtime if possible.
try:
if sys.hexversion >= 0x3030000:
newmtime = os.stat(dest).st_mtime_ns
else:
newmtime = os.stat(dest)[stat.ST_MTIME]
except OSError as e:
writemsg(_("!!! Failed to stat in movefile()\n"), noiselevel=-1)
writemsg("!!! %s\n" % dest, noiselevel=-1)
writemsg("!!! %s\n" % str(e), noiselevel=-1)
return None
if bsd_chflags:
# Restore the flags we saved before moving
if pflags:
bsd_chflags.chflags(os.path.dirname(dest), pflags)
return newmtime
2
Example 50
Project: ganeti Source File: wrapper.py
def ResetTempfileModule(_time=time.time):
"""Resets the random name generator of the tempfile module.
This function should be called after C{os.fork} in the child process to
ensure it creates a newly seeded random generator. Otherwise it would
generate the same random parts as the parent process. If several processes
race for the creation of a temporary file, this could lead to one not getting
a temporary name.
"""
# pylint: disable=W0212
if ((sys.hexversion >= 0x020703F0 and sys.hexversion < 0x03000000) or
sys.hexversion >= 0x030203F0):
# Python 2.7 automatically resets the RNG on pid changes (i.e. forking)
return
try:
lock = tempfile._once_lock
lock.acquire()
try:
# Re-seed random name generator
if tempfile._name_sequence:
tempfile._name_sequence.rng.seed(hash(_time()) ^ os.getpid())
finally:
lock.release()
except AttributeError:
logging.critical("The tempfile module misses at least one of the"
" '_once_lock' and '_name_sequence' attributes")