def _handle_binary_in_string(self, arg): try: if not is_bytes(arg): arg = arg.encode('ASCII') except UnicodeError: raise ValueError('Cannot represent %r as binary.' % arg) return xmlrpclib.Binary(arg)
def _handle_binary_in_string(self, arg): try: # FIXME: This doesn't currently handle bytearray correctly if not is_bytes(arg): arg = arg.encode('ASCII') except UnicodeError: raise ValueError('Cannot represent %r as binary.' % arg) return xmlrpclib.Binary(arg)
def _handle_binary_in_string(self, arg): try: if not is_bytes(arg): # Map Unicode code points to bytes directly arg = arg.encode('latin-1') except UnicodeError: raise ValueError('Cannot represent %r as binary.' % arg) return xmlrpc.client.Binary(arg)
def should_be_byte_string(self, item, msg=None): """Fails if the given ``item`` is not a byte string. Use `Should Be String` if you want to verify the ``item`` is a string. The default error message can be overridden with the optional ``msg`` argument. """ if not is_bytes(item): self._fail(msg, "'%s' is not a byte string.", item)
def _to_string(self, value, allow_tuple=False, allow_none=False): if is_unicode(value): return value if is_bytes(value): return value.decode('UTF-8') if allow_tuple and is_list_like(value) and len(value) > 0: return tuple(value) if allow_none and value is None: return value or_tuple = ' or a non-empty tuple' if allow_tuple else '' raise DataError('Return value must be a string%s, got %s.' % (or_tuple, type_name(value)))
def _encode_value(self, value, message, little_endian=False): if isinstance(value, Field): value = value._value else: if not is_bytes(value): value = str(value or '') if PY3: value = BuiltIn().convert_to_bytes(value) value += self._terminator length, aligned_length = self.length.find_length_and_set_if_necessary( message, len(value)) return value.ljust(length, b'\x00'), aligned_length
def should_be_byte_string(self, item, msg=None): """Fails if the given ``item`` is not a byte string. Use `Should Be Unicode String` if you want to verify the ``item`` is a Unicode string, or `Should Be String` if both Unicode and byte strings are fine. See `Should Be String` for more details about Unicode strings and byte strings. The default error message can be overridden with the optional ``msg`` argument. """ if not is_bytes(item): self._fail(msg, "'%s' is not a byte string.", item)
def validate(self, parent, paramdict, name=None): name = name or self.name field = parent[name] value = field.bytes forced_value = self._get_element_value_and_remove_from_params( paramdict, name) forced_value_unicode = forced_value if PY3 and is_bytes(forced_value): forced_value_unicode = BuiltIn().convert_to_string(forced_value) try: if not forced_value_unicode or forced_value_unicode == 'None': return [] elif forced_value_unicode.startswith('('): return self._validate_pattern(forced_value_unicode, value, field) except AttributeError as e: e.args = ( 'Validating {}:{} failed. {}.\n Did you set default value as numeric object instead of string?' .format(name, forced_value_unicode, e.args[0]), ) raise e if forced_value_unicode.startswith('REGEXP'): return self._validate_regexp(forced_value_unicode, value, field) return self._validate_exact_match(forced_value, value, field)
def should_be_title_case(self, string, msg=None, exclude=None): """Fails if given ``string`` is not title. ``string`` is a title cased string if there is at least one uppercase letter in each word. For example, ``'This Is Title'`` and ``'OK, Give Me My iPhone'`` would pass. ``'all words lower'`` and ``'Word In lower'`` would fail. This logic changed in Robot Framework 4.0 to be compatible with `Convert to Title Case`. See `Convert to Title Case` for title case algorithm and reasoning. The default error message can be overridden with the optional ``msg`` argument. Words can be explicitly excluded with the optional ``exclude`` argument. Explicitly excluded words can be given as a list or as a string with words separated by a comma and an optional space. Excluded words are actually considered to be regular expression patterns, so it is possible to use something like "example[.!?]?" to match the word "example" on it own and also if followed by ".", "!" or "?". See `BuiltIn.Should Match Regexp` for more information about Python regular expression syntax in general and how to use it in Robot Framework test data in particular. See also `Should Be Uppercase` and `Should Be Lowercase`. """ if PY2 and is_bytes(string): try: string = string.decode('ASCII') except UnicodeError: raise TypeError('This keyword works only with Unicode strings ' 'and non-ASCII bytes.') if string != self.convert_to_title_case(string, exclude): self._fail(msg, "'%s' is not title case.", string)
def _to_string(self, value): if is_unicode(value): return value if is_bytes(value): return value.decode('UTF-8') raise DataError('Return value must be string.')
def _string_contains_binary(self, arg): return (self.binary.search(arg) or is_bytes(arg) and self.non_ascii.search(arg))
def _contains_binary(self, arg): return (self.binary.search(arg) or is_bytes(arg) and not IRONPYTHON and self.non_ascii.search(arg))
def test_bytes(self): for thing in [b'bytes', bytearray(b'ba'), b'', bytearray()]: assert_equal(is_bytes(thing), True, thing) assert_equal(is_string(thing), False, thing)
def test_strings(self): for thing in ['string', 'hyvä', '']: assert_equal(is_string(thing), True, thing) assert_equal(is_bytes(thing), False, thing)
def test_strings(self): for thing in ['string', u'unicode', '', u'']: assert_equal(is_string(thing), True, thing) assert_equal(is_bytes(thing), isinstance(thing, bytes), thing)
def _to_string(self, value): if not (is_string(value) or is_bytes(value)): raise DataError("Return value must be string.") return value if is_unicode(value) else unic(value, "UTF-8")
def test_bytes(self): for thing in [b'bytes', bytearray(b'ba'), b'', bytearray()]: assert_equal(is_bytes(thing), True, thing) assert_equal(is_string(thing), isinstance(thing, str), thing)
def _to_string(self, value): if not (is_string(value) or is_bytes(value)): raise DataError('Return value must be string.') return value if is_unicode(value) else unic(value, 'UTF-8')