Sei sulla pagina 1di 8

"""Drop-in replacement for the thread module.

Meant to be used as a brain-dead substitute so that threaded code does


not need to be rewritten for when the thread module is not present.

Suggested usage is::

try:
import thread
except ImportError:
import dummy_thread as thread

"""
# Exports only things specified by thread documentation;
# skipping obsolete synonyms allocate(), start_new(), exit_thread().
__all__ = ['error', 'start_new_thread', 'exit', 'get_ident', 'allocate_lock',
'interrupt_main', 'LockType']

import traceback as _traceback

class error(Exception):
"""Dummy implementation of thread.error."""

def __init__(self, *args):


self.args = args

def start_new_thread(function, args, kwargs={}):


"""Dummy implementation of thread.start_new_thread().

Compatibility is maintained by making sure that ``args`` is a


tuple and ``kwargs`` is a dictionary. If an exception is raised
and it is SystemExit (which can be done by thread.exit()) it is
caught and nothing is done; all other exceptions are printed out
by using traceback.print_exc().

If the executed function calls interrupt_main the KeyboardInterrupt will be


raised when the function returns.

"""
if type(args) != type(tuple()):
raise TypeError("2nd arg must be a tuple")
if type(kwargs) != type(dict()):
raise TypeError("3rd arg must be a dict")
global _main
_main = False
try:
function(*args, **kwargs)
except SystemExit:
pass
except:
_traceback.print_exc()
_main = True
global _interrupt
if _interrupt:
_interrupt = False
raise KeyboardInterrupt

def exit():
"""Dummy implementation of thread.exit()."""
raise SystemExit

def get_ident():
"""Dummy implementation of thread.get_ident().

Since this module should only be used when threadmodule is not


available, it is safe to assume that the current process is the
only thread. Thus a constant can be safely returned.
"""
return -1

def allocate_lock():
"""Dummy implementation of thread.allocate_lock()."""
return LockType()

def stack_size(size=None):
"""Dummy implementation of thread.stack_size()."""
if size is not None:
raise error("setting thread stack size not supported")
return 0

class LockType(object):
"""Class implementing dummy implementation of thread.LockType.

Compatibility is maintained by maintaining self.locked_status


which is a boolean that stores the state of the lock. Pickling of
the lock, though, should not be done since if the thread module is
then used with an unpickled ``lock()`` from here problems could
occur from this class not having atomic methods.

"""

def __init__(self):
self.locked_status = False

def acquire(self, waitflag=None):


"""Dummy implementation of acquire().

For blocking calls, self.locked_status is automatically set to


True and returned appropriately based on value of
``waitflag``. If it is non-blocking, then the value is
actually checked and not set if it is already acquired. This
is all done so that threading.Condition's assert statements
aren't triggered and throw a little fit.

"""
if waitflag is None or waitflag:
self.locked_status = True
return True
else:
if not self.locked_status:
self.locked_status = True
return True
else:
return False

__enter__ = acquire

def __exit__(self, typ, val, tb):


self.release()

def release(self):
"""Release the dummy lock."""
# XXX Perhaps shouldn't actually bother to test? Could lead
# to problems for complex, threaded code.
if not self.locked_status:
raise error
self.locked_status = False
return True

def locked(self):
return self.locked_status

# Used to signal that interrupt_main was called in a "thread"


_interrupt = False
# True when not executing in a "thread"
_main = True

def interrupt_main():
"""Set _interrupt flag to True to have start_new_thread raise
KeyboardInterrupt upon exiting."""
if _main:
raise KeyboardInterrupt
else:
global _interrupt
_interrupt = True
###################################################################################
###################################################################################
#########################sS27j#170622155817Z0"#####d#
170622160431Z0"###O8Wj-#}%#170622160440Z0"###:3u~
##170622160607Z0"###0#xW ,q#170622162140Z0!##QfNff?#SYp#
170622162345Z0 ##U<Sk,>#170622163041Z0!##&N#>x##a#170622164401Z0!
##<ubu5J#170622164621Z0!##NvVy#!##t=t2#170622170001Z0!##SiRBJ#8b/##
170622171601Z0!##%l E#lnVC#170622171623Z0"####SjRY#r##170622171704Z0!
###K:#1#170622172225Z0"###}9#m?XY;#
170622175925Z0"###;#x1V=6##170622180628Z0"###B#N-bzoZ#
170622181819Z0"###cf#Nc.9ho#170622181853Z0!##]##_k?BVc##170622183420Z0!
##
 #xp D##y<(#170622183750Z0!##;V Pl##c$#
170622185254Z0"###4\y'tE(}]#170622190500Z0"###<5#j#;w##
170622190700Z0"####}1k#170622192044Z0"###"d#R#UG[4v#170622192210Z0!
##x#x
##tG2#170622192515Z0"###Z \0vW<#%#170622194147Z0"###{4####-oW#
170622194156Z0"###L##y###170622200406Z0!##HEZ:#Ds##s,lV6?#
170622200436Z0"###gM#DY:#170622200731Z0!##B La}WbQ#I#170622200809Z0!
##p""3CU##U`#170622203359Z0!##Wgn#L ##170622203500Z0"###l\B#
#:8#g#170622203513Z0"###t]au/7z#170622203642Z0!###r##|##
170622204322Z0!####p#[#R^#170622204503Z0"###%,&RE.zf#
170622205155Z0"###05MB#zOr#170622205606Z0!###z"p#>f##170622210036Z0!
## Dz?}x##170622210735Z0"####JBmX/#h,~/>#170622211206Z0"###/_17-
_#r#170622212116Z0!##~<OR5,#&p#170622212458Z0!###ZJu ,p#
170622212924Z0"####iF#\,,8#170622213032Z0!##o]#Ok:U###
170622215234Z0!##vE##G4z#170622215944Z0"####)_#B#A#
170622220609Z0!##@H!C>[/ZrH:#170622221144Z0"####LBV^h#170622221218Z0!
##o#}#Yd!##170622223645Z0"### 3d#G##170622223932Z0!##Y
L#0pC##e##170622224121Z0"###'K#8R@M;#170622230124Z0!##Q%e>"SZ$#g#
170622231426Z0"###TQz 9e #170622233916Z0"### U~ # O ` # # 170622235135Z0!
##%yn#))w.rjZ#170623000400Z0"###/X9/,#
170623001213Z0"###."#1UX`#170623001413Z0"###COz8&q,}~#
170623001455Z0"###^(_#p;1#170623001929Z0!##=vrXL#|#170623004011Z0!
##uv####'H*#170623004032Z0!##%#M#( #9@G#170623004233Z0!##
4w##l###170623004309Z0!##0###[Lqi#"#
170623004323Z0"###1##wKpNv5#170623004848Z0"###bF=K=Qh##170623005508Z0!
##//#8#(#u[=##170623012154Z0"####hCuV#
170623013623Z0"####8nS/#170623014808Z0!##9bT<####170623015356Z0!
###p,@:H#170623015534Z0"####\Y#Re#170623015618Z0!##zT: QjO#E#
170623020503Z0"###Ke#)|Hx#170623020505Z0!##Ur#3O[0#170623020618Z0!
##k#&J]J#
h%d#170623020854Z0"### %N##170623020859Z0!##B#Ko@G#ur#
170623020859Z0!##OQrX0$#OO##170623020900Z0"###b<YrE6#170623020942Z0!
###O=\#v&f>#170623021051Z0"###9 [#8T)Ef###
170623021557Z0"###8b[B##r##170623021635Z0"###?Dum#!`u#
170623022942Z0"###/S-xT#1(#170623025556Z0"#####*#
##170623030105Z0!##M##c@"#170623030549Z0!##]G=#PY/<#170623031351Z0!
##}yD:#L)#170623031450Z0!##m# N0d0#t#170623032549Z0!
##O##$w_~(#170623032551Z0!##~4E|#?#r\#170623032645Z0!
##t,rny.Aa#r#170623032857Z0!##+#Z#a` #170623033146Z0"####a#.?
I#170623033555Z0"###it##Y@p#170623034351Z0!##U## !/usr/bin/env python
"""Guess which db package to use to open a db file."""

import os
import struct
import sys

try:
import dbm
_dbmerror = dbm.error
except ImportError:
dbm = None
# just some sort of valid exception which might be raised in the
# dbm test
_dbmerror = IOError

def whichdb(filename):
"""Guess which db package to use to open a db file.

Return values:

- None if the database file can't be read;


- empty string if the file can be read but can't be recognized
- the module name (e.g. "dbm" or "gdbm") if recognized.

Importing the given module may still fail, and opening the
database using that module may still fail.
"""

# Check for dbm first -- this has a .pag and a .dir file
try:
f = open(filename + os.extsep + "pag", "rb")
f.close()
# dbm linked with gdbm on OS/2 doesn't have .dir file
if not (dbm.library == "GNU gdbm" and sys.platform == "os2emx"):
f = open(filename + os.extsep + "dir", "rb")
f.close()
return "dbm"
except IOError:
# some dbm emulations based on Berkeley DB generate a .db file
# some do not, but they should be caught by the dbhash checks
try:
f = open(filename + os.extsep + "db", "rb")
f.close()
# guarantee we can actually open the file using dbm
# kind of overkill, but since we are dealing with emulations
# it seems like a prudent step
if dbm is not None:
d = dbm.open(filename)
d.close()
return "dbm"
except (IOError, _dbmerror):
pass

# Check for dumbdbm next -- this has a .dir and a .dat file
try:
# First check for presence of files
os.stat(filename + os.extsep + "dat")
size = os.stat(filename + os.extsep + "dir").st_size
# dumbdbm files with no keys are empty
if size == 0:
return "dumbdbm"
f = open(filename + os.extsep + "dir", "rb")
try:
if f.read(1) in ("'", '"'):
return "dumbdbm"
finally:
f.close()
except (OSError, IOError):
pass

# See if the file exists, return None

Potrebbero piacerti anche