def join(self, iterable_of_bytes):
     errmsg = 'sequence item {0}: expected bytes, {1} found'
     if isbytes(iterable_of_bytes) or istext(iterable_of_bytes):
         raise TypeError(errmsg.format(0, type(iterable_of_bytes)))
     for i, item in enumerate(iterable_of_bytes):
         if istext(item):
             raise TypeError(errmsg.format(i, type(item)))
     return newbytes(super(newbytes, self).join(iterable_of_bytes))
Beispiel #2
0
 def join(self, iterable_of_bytes):
     errmsg = 'sequence item {0}: expected bytes, {1} found'
     if isbytes(iterable_of_bytes) or istext(iterable_of_bytes):
         raise TypeError(errmsg.format(0, type(iterable_of_bytes)))
     for i, item in enumerate(iterable_of_bytes):
         if istext(item):
             raise TypeError(errmsg.format(i, type(item)))
     return newbytes(super(newbytes, self).join(iterable_of_bytes))
Beispiel #3
0
    def __call__(cls, lib, *args, **kwargs):

        # Identify the library path.
        if istext(lib) or isbytes(lib):
            if os.sep not in lib:
                lib_path = find_library(lib).path
            else:
                lib_path = os.path.realpath(lib)
                assert os.path.isfile(lib_path),\
                    'Provided path does not point to a file'
            backend_cls = cls.backends[kwargs.get('backend', 'ctypes')]

            lib_arch = LibraryPath(lib_path).arch
            py_bitness = 64 if sys.maxsize > 2**32 else 32
            if lib_arch and py_bitness not in lib_arch:
                raise OSError("Library bitness does not match Python's")
            lib = lib_path
        else:
            from .backends import identify_library, get_library_path
            backend = identify_library(lib)
            backend_cls = cls.backends[backend]
            lib_path = get_library_path(lib, backend)

        # Check whether or not this library has already been opened.
        if lib_path in cls.libs:
            return cls.libs[lib_path]

        else:
            obj = super(CLibraryMeta,
                        backend_cls).__call__(lib, *args, **kwargs)
            cls.libs[lib_path] = obj
            return obj
Beispiel #4
0
    def __call__(cls, lib, *args, **kwargs):

        # Identify the library path.
        if istext(lib) or isbytes(lib):
            if os.sep not in lib:
                lib_path = find_library(lib).path
            else:
                lib_path = os.path.realpath(lib)
                assert os.path.isfile(lib_path),\
                    'Provided path does not point to a file'
            backend_cls = cls.backends[kwargs.get('backend', 'ctypes')]

            lib_arch = LibraryPath(lib_path).arch
            py_bitness = 64 if sys.maxsize > 2**32 else 32
            if lib_arch and py_bitness not in lib_arch:
                raise OSError("Library bitness does not match Python's")
            lib = lib_path
        else:
            from .backends import identify_library, get_library_path
            backend = identify_library(lib)
            backend_cls = cls.backends[backend]
            lib_path = get_library_path(lib, backend)

        # Check whether or not this library has already been opened.
        if lib_path in cls.libs:
            return cls.libs[lib_path]

        else:
            obj = super(CLibraryMeta, backend_cls).__call__(lib, *args,
                                                            **kwargs)
            cls.libs[lib_path] = obj
            return obj
 def getheader(self, name, default=None):
     if self.headers is None:
         raise ResponseNotReady()
     headers = self.headers.get_all(name) or default
     if istext(headers) or not hasattr(headers, '__iter__'):
         return headers
     else:
         return ', '.join(headers)
Beispiel #6
0
 def getheader(self, name, default=None):
     if self.headers is None:
         raise ResponseNotReady()
     headers = self.headers.get_all(name) or default
     if istext(headers) or not hasattr(headers, '__iter__'):
         return headers
     else:
         return ', '.join(headers)
Beispiel #7
0
    def register_preferences(self):
        """Register the task preferences into the preferences system.

        """
        self.preferences.clear()
        for name in tagged_members(self, 'pref'):
            val = getattr(self, name)
            if istext(val):
                self.preferences[name] = val
            else:
                self.preferences[name] = repr(val)
Beispiel #8
0
    def register_preferences(self):
        """Register the task preferences into the preferences system.

        """
        self.preferences.clear()
        for name in tagged_members(self, 'pref'):
            val = getattr(self, name)
            if istext(val):
                self.preferences[name] = val
            else:
                self.preferences[name] = repr(val)
Beispiel #9
0
    def arg_c_type(self, arg):
        """Return the type required for the specified argument.

        Parameters
        ----------
        arg : int or unicode
            Name or index of the argument whose type should be returned.

        """
        if istext(arg) or isbytes(arg):
            arg = self.arg_inds[arg]
        return self.lib._get_type(self.sig[1][arg][1])
Beispiel #10
0
    def arg_c_type(self, arg):
        """Return the type required for the specified argument.

        Parameters
        ----------
        arg : int or unicode
            Name or index of the argument whose type should be returned.

        """
        if istext(arg) or isbytes(arg):
            arg = self.arg_inds[arg]
        return self.lib._get_type(self.sig[1][arg][1])
Beispiel #11
0
def member_to_pref(obj, member, val):
    """ Provide the value that will be stored in the preferences for a member.

    Parameters
    ----------
    obj : Atom
        Object who owns the member.

    member : Member
        Member for which the preferences should be retrieved

    val : Value
        Value of the member to be stored in the preferences

    Returns
    -------
    pref_value : Value
        The serialized value/string that will be stored in the pref.

    """
    meta_value = member.metadata[PREF_KEY]

    # If 'pref=True' then we rely on the standard save mechanism
    if meta_value is True:
        # If val is text, then we can simply cast it and rely on python/Atom
        # default methods.
        if istext(val):
            pref_value = val
        else:
            pref_value = repr(val)

    # If the user provided a custom "to_pref" function, then we check
    # that it has the correct signature and use it to obtain the value
    elif (isinstance(meta_value, (tuple, list)) and
            len(getargspec(meta_value[TO_PREF_ID])[0]) == 3):
        pref_value = meta_value[TO_PREF_ID](obj, member, val)

    elif meta_value is False:
        raise NotImplementedError(
            fill(cleandoc('''you set 'pref=False' for this member. If you did
            not want to save it you should simply not declare this tag.''')))
    else:
        raise NotImplementedError(
            fill(cleandoc('''the 'pref' tag of this member was not set to true,
            therefore the program expects you to declare two functions,
             'member_to_pref(obj,member,val)' and 'member_from_pref(obj,member,
             val)' that will handle the serialization and deserialization of
             the value. Those should be passed as a list or a tuple, where
             the first element is member_to and the second is member_from.
             It is possible that you failed to properly declare the signature
             of those two functions.''')))

    return pref_value
Beispiel #12
0
    def __new__(cls, x=0, base=10):
        """
        From the Py3 int docstring:

        |  int(x=0) -> integer
        |  int(x, base=10) -> integer
        |
        |  Convert a number or string to an integer, or return 0 if no
        |  arguments are given.  If x is a number, return x.__int__().  For
        |  floating point numbers, this truncates towards zero.
        |
        |  If x is not a number or if base is given, then x must be a string,
        |  bytes, or bytearray instance representing an integer literal in the
        |  given base.  The literal can be preceded by '+' or '-' and be
        |  surrounded by whitespace.  The base defaults to 10.  Valid bases are
        |  0 and 2-36. Base 0 means to interpret the base from the string as an
        |  integer literal.
        |  >>> int('0b100', base=0)
        |  4

        """
        try:
            val = x.__int__()
        except AttributeError:
            val = x
        else:
            if not isint(val):
                raise TypeError('__int__ returned non-int ({0})'.format(
                    type(val)))

        if base != 10:
            # Explicit base
            if not (istext(val) or isbytes(val) or isinstance(val, bytearray)):
                raise TypeError(
                    "int() can't convert non-string with explicit base")
            try:
                return super(newint, cls).__new__(cls, val, base)
            except TypeError:
                return super(newint, cls).__new__(cls, newbytes(val), base)
        # After here, base is 10
        try:
            return super(newint, cls).__new__(cls, val)
        except TypeError:
            # Py2 long doesn't handle bytearray input with an explicit base, so
            # handle this here.
            # Py3: int(bytearray(b'10'), 2) == 2
            # Py2: int(bytearray(b'10'), 2) == 2 raises TypeError
            # Py2: long(bytearray(b'10'), 2) == 2 raises TypeError
            try:
                return super(newint, cls).__new__(cls, newbytes(val))
            except:
                raise TypeError("newint argument must be a string or a number,"
                                "not '{0}'".format(type(val)))
    def __new__(cls, x=0, base=10):
        """
        From the Py3 int docstring:

        |  int(x=0) -> integer
        |  int(x, base=10) -> integer
        |  
        |  Convert a number or string to an integer, or return 0 if no arguments
        |  are given.  If x is a number, return x.__int__().  For floating point
        |  numbers, this truncates towards zero.
        |  
        |  If x is not a number or if base is given, then x must be a string,
        |  bytes, or bytearray instance representing an integer literal in the
        |  given base.  The literal can be preceded by '+' or '-' and be surrounded
        |  by whitespace.  The base defaults to 10.  Valid bases are 0 and 2-36.
        |  Base 0 means to interpret the base from the string as an integer literal.
        |  >>> int('0b100', base=0)
        |  4

        """
        try:
            val = x.__int__()
        except AttributeError:
            val = x
        else:
            if not isint(val):
                raise TypeError('__int__ returned non-int ({0})'.format(
                    type(val)))

        if base != 10:
            # Explicit base
            if not (istext(val) or isbytes(val) or isinstance(val, bytearray)):
                raise TypeError(
                    "int() can't convert non-string with explicit base")
            try:
                return super(newint, cls).__new__(cls, val, base)
            except TypeError:
                return super(newint, cls).__new__(cls, newbytes(val), base)
        # After here, base is 10
        try:
            return super(newint, cls).__new__(cls, val)
        except TypeError:
            # Py2 long doesn't handle bytearray input with an explicit base, so
            # handle this here.
            # Py3: int(bytearray(b'10'), 2) == 2
            # Py2: int(bytearray(b'10'), 2) == 2 raises TypeError
            # Py2: long(bytearray(b'10'), 2) == 2 raises TypeError
            try:
                return super(newint, cls).__new__(cls, newbytes(val))
            except:
                raise TypeError(
                    "newint argument must be a string or a number, not '{0}'".
                    format(type(val)))
Beispiel #14
0
    def __getitem__(self, n):
        if isinstance(n, int):
            arg = self.args[n]
        elif istext(n) or isbytes(n):
            n = self.find_arg(n)
            arg = self.args[n]
        else:
            raise ValueError("Index must be int or str.")

        if n in self.guessed:
            arg = arg[0]

        return self.lib._extract_val_(arg)
Beispiel #15
0
    def __getitem__(self, n):
        if isinstance(n, int):
            arg = self.args[n]
        elif istext(n) or isbytes(n):
            n = self.find_arg(n)
            arg = self.args[n]
        else:
            raise ValueError("Index must be int or str.")

        if n in self.guessed:
            arg = arg[0]

        return self.lib._extract_val_(arg)
Beispiel #16
0
    def __init__(self,
                 lib,
                 headers,
                 prefix=None,
                 lock_calls=False,
                 convention='cdll',
                 backend='ctypes',
                 **kwargs):
        # name everything using underscores to avoid name collisions with
        # library

        # Build or store the parser from the header files.
        if isinstance(headers, list):
            self._headers_ = self._build_parser(headers, kwargs)
        elif isinstance(headers, CParser):
            self._headers_ = headers
        else:
            msg = 'Expected a CParser instance or list for headers, not {}'
            raise ValueError(msg.format(type(headers)))
        self._defs_ = self._headers_.defs

        # Create or store the internal representation of the library.
        if istext(lib) or isbytes(lib):
            self._lib_ = self._link_library(lib, convention)
        else:
            self._lib_ = lib

        # Store the list of prefix.
        if prefix is None:
            self._prefix_ = []
        elif isinstance(prefix, list):
            self._prefix_ = prefix
        else:
            self._prefix_ = [prefix]

        self._lock_calls_ = lock_calls
        if lock_calls:
            self._lock_ = RLock()

        self._objs_ = {}
        for k in [
                'values', 'functions', 'types', 'structs', 'unions', 'enums'
        ]:
            self._objs_[k] = {}
        self._all_objs_ = {}
        self._structs_ = {}
        self._unions_ = {}
Beispiel #17
0
 def __init__(self, getter=None, setter=None, secure_comm=0, check=None,
              range=None):
     super(RangeValidated, self).__init__(getter, setter, secure_comm)
     if range:
         wrap = self._wrap_with_checker
         if isinstance(range, AbstractRangeValidator):
             self.range = range
             wrap(self.validate_range, 'pre_set')
         elif istext(range):
             self.range_id = range
             wrap(self.get_range_and_validate, 'pre_set')
         else:
             mess = cleandoc('''The range kwarg should either be a range
                 validator or a string used to retrieve the range through
                 get_range''')
             raise TypeError(mess)
     self.creation_kwargs['range'] = range
    def _send_request(self, method, url, body, headers):
        # Honor explicitly requested Host: and Accept-Encoding: headers.
        header_names = dict.fromkeys([k.lower() for k in headers])
        skips = {}
        if 'host' in header_names:
            skips['skip_host'] = 1
        if 'accept-encoding' in header_names:
            skips['skip_accept_encoding'] = 1

        self.putrequest(method, url, **skips)

        if body is not None and ('content-length' not in header_names):
            self._set_content_length(body)
        for hdr, value in headers.items():
            self.putheader(hdr, value)
        if istext(body):
            # RFC 2616 Section 3.7.1 says that text default has a
            # default charset of iso-8859-1.
            body = body.encode('iso-8859-1')
        self.endheaders(body)
Beispiel #19
0
    def _send_request(self, method, url, body, headers):
        # Honor explicitly requested Host: and Accept-Encoding: headers.
        header_names = dict.fromkeys([k.lower() for k in headers])
        skips = {}
        if 'host' in header_names:
            skips['skip_host'] = 1
        if 'accept-encoding' in header_names:
            skips['skip_accept_encoding'] = 1

        self.putrequest(method, url, **skips)

        if body is not None and ('content-length' not in header_names):
            self._set_content_length(body)
        for hdr, value in headers.items():
            self.putheader(hdr, value)
        if istext(body):
            # RFC 2616 Section 3.7.1 says that text default has a
            # default charset of iso-8859-1.
            body = body.encode('iso-8859-1')
        self.endheaders(body)
Beispiel #20
0
 def __init__(self,
              getter=None,
              setter=None,
              secure_comm=0,
              check=None,
              range=None):
     super(RangeValidated, self).__init__(getter, setter, secure_comm)
     if range:
         wrap = self._wrap_with_checker
         if isinstance(range, AbstractRangeValidator):
             self.range = range
             wrap(self.validate_range, 'pre_set')
         elif istext(range):
             self.range_id = range
             wrap(self.get_range_and_validate, 'pre_set')
         else:
             mess = cleandoc('''The range kwarg should either be a range
                 validator or a string used to retrieve the range through
                 get_range''')
             raise TypeError(mess)
     self.creation_kwargs['range'] = range
Beispiel #21
0
    def __init__(self, lib, headers, prefix=None, lock_calls=False,
                 convention='cdll', backend='ctypes', **kwargs):
        # name everything using underscores to avoid name collisions with
        # library

        # Build or store the parser from the header files.
        if isinstance(headers, list):
            self._headers_ = self._build_parser(headers, kwargs)
        elif isinstance(headers, CParser):
            self._headers_ = headers
        else:
            msg = 'Expected a CParser instance or list for headers, not {}'
            raise ValueError(msg.format(type(headers)))
        self._defs_ = self._headers_.defs

        # Create or store the internal representation of the library.
        if istext(lib) or isbytes(lib):
            self._lib_ = self._link_library(lib, convention)
        else:
            self._lib_ = lib

        # Store the list of prefix.
        if prefix is None:
            self._prefix_ = []
        elif isinstance(prefix, list):
            self._prefix_ = prefix
        else:
            self._prefix_ = [prefix]

        self._lock_calls_ = lock_calls
        if lock_calls:
            self._lock_ = RLock()

        self._objs_ = {}
        for k in ['values', 'functions', 'types', 'structs', 'unions',
                  'enums']:
            self._objs_[k] = {}
        self._all_objs_ = {}
        self._structs_ = {}
        self._unions_ = {}
Beispiel #22
0
    def _get_type(self, typ, pointers=True):
        """Return a ctype object representing the named type.

        If pointers is True, the class returned includes all pointer/array
        specs provided. Otherwise, the class returned is just the base type
        with no pointers.

        """
        try:
            typ = list(self._headers_.eval_type(typ))
            mods = typ[1:][:]

            # Create the initial type
            # Some types like ['char', '*'] have a specific ctype (c_char_p)
            # (but only do this if pointers == True)
            if (pointers and len(typ) > 1 and typ[1] == '*'
                    and typ[0] in self._ptr_types_):
                cls = self._ptr_types_[typ[0]]
                mods = typ[2:]

            # If the base type is in the list of existing ctypes:
            elif typ[0] in self._types_:
                cls = self._types_[typ[0]]

            # structs, unions, enums:
            elif typ[0][:7] == 'struct ':
                cls = self._get_struct('structs',
                                       self._defs_['types'][typ[0]][1])
            elif typ[0][:6] == 'union ':
                cls = self._get_struct('unions',
                                       self._defs_['types'][typ[0]][1])
            elif typ[0][:5] == 'enum ':
                cls = c_int

            # void
            elif typ[0] == 'void':
                cls = None
            else:
                raise KeyError("Can't find base type for {}".format(typ))

            if not pointers:
                return cls

            # apply pointers and arrays
            while len(mods) > 0:
                m = mods.pop(0)
                if istext(m):  # pointer or reference
                    if m[0] == '*' or m[0] == '&':
                        for i in m:
                            cls = POINTER(cls)

                elif isinstance(m, list):  # array
                    for i in m:
                        # -1 indicates an 'incomplete type' like "int
                        # variable[]"
                        if i == -1:
                            # which we should interpret like "int *variable"
                            cls = POINTER(cls)
                        else:
                            cls = cls * i

                # Probably a function pointer
                elif isinstance(m, tuple):
                    # Find pointer and calling convention
                    is_ptr = False
                    conv = '__cdecl'
                    if len(mods) == 0:
                        mess = "Function signature with no pointer:"
                        raise DefinitionError(mess, m, mods)
                    for i in [0, 1]:
                        if len(mods) < 1:
                            break
                        if mods[0] == '*':
                            mods.pop(0)
                            is_ptr = True
                        elif mods[0] in ['__stdcall', '__cdecl']:
                            conv = mods.pop(0)
                        else:
                            break
                    if not is_ptr:
                        mess = make_mess("""Not sure how to handle type
                            (function without single pointer): {}""")
                        raise DefinitionError(mess.format(typ))

                    if conv == '__stdcall':
                        mkfn = WINFUNCTYPE

                    else:
                        mkfn = CFUNCTYPE

                    args = [self._get_type(arg[1]) for arg in m]
                    cls = mkfn(cls, *args)

                else:
                    mess = "Not sure what to do with this type modifier: '{}'"
                    raise TypeError(mess.format(m))
            return cls

        except:
            logger.error("Error while processing type: {}".format(typ))
            raise
Beispiel #23
0
    def _get_type(self, typ, pointers=True):
        """Return a ctype object representing the named type.

        If pointers is True, the class returned includes all pointer/array
        specs provided. Otherwise, the class returned is just the base type
        with no pointers.

        """
        try:
            typ = list(self._headers_.eval_type(typ))
            mods = typ[1:][:]

            # Create the initial type
            # Some types like ['char', '*'] have a specific ctype (c_char_p)
            # (but only do this if pointers == True)
            if (pointers and len(typ) > 1 and typ[1] == '*' and
                    typ[0] in self._ptr_types_):
                cls = self._ptr_types_[typ[0]]
                mods = typ[2:]

            # If the base type is in the list of existing ctypes:
            elif typ[0] in self._types_:
                cls = self._types_[typ[0]]

            # structs, unions, enums:
            elif typ[0][:7] == 'struct ':
                cls = self._get_struct('structs',
                                       self._defs_['types'][typ[0]][1])
            elif typ[0][:6] == 'union ':
                cls = self._get_struct('unions',
                                       self._defs_['types'][typ[0]][1])
            elif typ[0][:5] == 'enum ':
                cls = c_int

            # void
            elif typ[0] == 'void':
                cls = None
            else:
                raise KeyError("Can't find base type for {}".format(typ))

            if not pointers:
                return cls

            # apply pointers and arrays
            while len(mods) > 0:
                m = mods.pop(0)
                if istext(m):  # pointer or reference
                    if m[0] == '*' or m[0] == '&':
                        for i in m:
                            cls = POINTER(cls)

                elif isinstance(m, list):      # array
                    for i in m:
                        # -1 indicates an 'incomplete type' like "int
                        # variable[]"
                        if i == -1:
                            # which we should interpret like "int *variable"
                            cls = POINTER(cls)
                        else:
                            cls = cls * i

                # Probably a function pointer
                elif isinstance(m, tuple):
                    # Find pointer and calling convention
                    is_ptr = False
                    conv = '__cdecl'
                    if len(mods) == 0:
                        mess = "Function signature with no pointer:"
                        raise DefinitionError(mess, m, mods)
                    for i in [0, 1]:
                        if len(mods) < 1:
                            break
                        if mods[0] == '*':
                            mods.pop(0)
                            is_ptr = True
                        elif mods[0] in ['__stdcall', '__cdecl']:
                            conv = mods.pop(0)
                        else:
                            break
                    if not is_ptr:
                        mess = make_mess("""Not sure how to handle type
                            (function without single pointer): {}""")
                        raise DefinitionError(mess.format(typ))

                    if conv == '__stdcall':
                        mkfn = WINFUNCTYPE

                    else:
                        mkfn = CFUNCTYPE

                    args = [self._get_type(arg[1]) for arg in m]
                    cls = mkfn(cls, *args)

                else:
                    mess = "Not sure what to do with this type modifier: '{}'"
                    raise TypeError(mess.format(m))
            return cls

        except:
            logger.error("Error while processing type: {}".format(typ))
            raise
Beispiel #24
0
 def test_istext(self):
     self.assertTrue(istext(self.s))
     self.assertTrue(istext(self.s2))
     self.assertFalse(istext(self.b))
     self.assertFalse(istext(self.b2))
Beispiel #25
0
 def __ge__(self, other):
     if not istext(other):
         raise TypeError(self.unorderable_err.format(type(other)))
     return super(newstr, self).__ge__(other)
 def __ge__(self, other):
     if not istext(other):
         raise TypeError(self.unorderable_err.format(type(other)))
     return super(newstr, self).__ge__(other)
Beispiel #27
0
 def test_istext(self):
     self.assertTrue(istext(self.s))
     self.assertTrue(istext(self.s2))
     self.assertFalse(istext(self.b))
     self.assertFalse(istext(self.b2))