Skip to content

file.append fails when encoding doesn't match system encoding #50903

@squidpickles

Description

@squidpickles

Description of Issue/Question

Using file.append state on a file with an encoding that's incompatible with the system encoding fails.
For example, I have a file containing ISO-8859-1 encoded data. The system encoding is ASCII. If I try to use file.append, it fails with the following error:

An exception occurred in this state: Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/salt/state.py", line 1944, in call
    **cdata['kwargs'])
  File "/usr/lib/python3/dist-packages/salt/loader.py", line 1959, in wrapper
    return f(*args, **kwargs)
  File "/usr/lib/python3/dist-packages/salt/states/file.py", line 5669, in append
     slines = slines.decode(__salt_system_encoding__)
  UnicodeDecodeError: 'ascii' codec can't decode byte 0xed in position 4: ordinal not in range(128)

If the system encoding were UTF-8, it would similarly fail because 0xed is not a valid UTF-8 byte.

In the simplest case, it would be helpful if the state.file functions that work with existing files could take an encoding, much as managed files do.

Setup

See attached zip file with Docker config that reproduces the bug

Steps to Reproduce Issue

  1. Build attached config: docker build salt-bug -t salt-bug:latest
  2. Run configuration: docker run -it --rm salt-bug:latest
    Output:
[ERROR   ] An exception occurred in this state: Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/salt/state.py", line 1944, in call
    **cdata['kwargs'])
  File "/usr/lib/python3/dist-packages/salt/loader.py", line 1959, in wrapper
    return f(*args, **kwargs)
  File "/usr/lib/python3/dist-packages/salt/states/file.py", line 5669, in append
    slines = slines.decode(__salt_system_encoding__)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xed in position 4: ordinal not in range(128)

local:
----------
          ID: /bugfile
    Function: file.append
      Result: False
     Comment: An exception occurred in this state: Traceback (most recent call last):
                File "/usr/lib/python3/dist-packages/salt/state.py", line 1944, in call
                  **cdata['kwargs'])
                File "/usr/lib/python3/dist-packages/salt/loader.py", line 1959, in wrapper
                  return f(*args, **kwargs)
                File "/usr/lib/python3/dist-packages/salt/states/file.py", line 5669, in append
                  slines = slines.decode(__salt_system_encoding__)
              UnicodeDecodeError: 'ascii' codec can't decode byte 0xed in position 4: ordinal not in range(128)
     Started: 21:35:25.452802
    Duration: 39.99999999996362 ms
     Changes:

Summary for local
------------
Succeeded: 0
Failed:    1
------------
Total states run:     1
Total run time:  40.000 ms

Versions Report

Salt Version:
           Salt: 2018.3.0-n/a-501caa9

Dependency Versions:
           cffi: Not Installed
       cherrypy: Not Installed
       dateutil: Not Installed
      docker-py: Not Installed
          gitdb: Not Installed
      gitpython: Not Installed
         Jinja2: 2.9.4
        libgit2: Not Installed
       M2Crypto: Not Installed
           Mako: Not Installed
   msgpack-pure: Not Installed
 msgpack-python: 0.4.8
   mysql-python: Not Installed
      pycparser: Not Installed
       pycrypto: 2.6.1
   pycryptodome: Not Installed
         pygit2: Not Installed
         Python: 3.5.3 (default, Sep 27 2018, 17:25:39)
   python-gnupg: Not Installed
         PyYAML: 3.12
          PyZMQ: 16.0.2
          smmap: Not Installed
        timelib: Not Installed
        Tornado: 4.4.3
            ZMQ: 4.2.1

System Versions:
           dist: debian 9.6
         locale: ANSI_X3.4-1968
        machine: x86_64
        release: 4.9.125-linuxkit
         system: Linux
        version: debian 9.6

salt-bug.zip

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugbroken, incorrect, or confusing behaviorseverity-low4th level, cosemtic problems, work around exists

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions