All jobs

Fix "AttributeError: 'array.array' object has no attribute 'tostring'" on Python 3.9 and Later

Yes. An agent renames tostring() to tobytes(), then runs the function end to end, because code that still calls it usually has older Python 2 bugs in front.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

array.array.tostring() and fromstring() were deprecated when Python 3.2 added tobytes() and frombytes(), and Python 3.9 removed them. The new methods return and accept exactly the same bytes, so the rename is safe on its own. It is rarely the whole fix. A file that still says tostring has not been run on Python 3 for a long time, if ever, and the rename often exposes the next failure on the line above. The agent does not stop when the AttributeError is gone. It calls the function with real input and follows it to a return value.

iOSForensics/pymobiledevice shows how deep that can go. pymobiledevice/util/lzss.py holds decompress_lzss, a port of Apple's kernelcache decompressor that ends with return dst.tostring(). Reading from the top on Python 3, three things go wrong before that line is reached. The first statement compares str[:8] != "complzss". The argument is compressed data, so it is bytes, and bytes never equal a text string: the function prints decompress_lzss: complzss magic missing and returns None for every input, valid or not. Past that, array("B", " "*(N + F - 1)) raises TypeError: cannot use a str to initialize an array with typecode 'B', and the same pattern builds the output buffer a few lines later. Only then comes tostring.

So the error in the title is the last of four defects in one function, and fixing only it changes nothing a caller can observe. The agent fixes them in order: a bytes literal for the magic, bytes or bytearray initialisers for the two buffers, and tobytes() at the end. The loop in between already works, since indexing a B array gives integers on both Python lines. It checks who calls the function, and in this repository a search for lzss outside the file finds no caller, so the module may be dead code. The agent reports that as a question for the maintainers and does not delete anything.

The repository's test directory has device-facing tests and nothing for this module, so the agent needs a test input. It builds one by hand from the format the code reads: the eight-byte magic, a big-endian length at offset 12, padding up to offset 0x180, then a stream in which a flag byte of 0xFF marks the next eight bytes as literals. Decompressing that must return the literal bytes. A second case uses a back-reference, so the window logic runs too. The setup.py classifiers list Python 2.7, 3.6, 3.7 and 3.11, and the agent runs the new test on the newest of those and on a current release, and says plainly that 2.7 was not run if it could not install one.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "This project fails with \"AttributeError: 'array.array' object has no attribute 'tostring'\" (or 'fromstring') on Python 3.9 or later. Find every call to .tostring() and .fromstring() in the repository and decide for each whether the receiver is an array.array, a NumPy array or something else, because only array.array lost the methods in 3.9. Replace the array.array ones with tobytes() and frombytes(). Then do not stop at the rename: for each function you touched, read it from the first line and run it on Python 3 with realistic input, and list every other Python 2 assumption that fails before or after the renamed line, such as comparing bytes with a str literal, initialising an array from a str, the removed 'c' typecode, integer division, or indexing bytes and expecting a one-character string. Fix those in the same function and say which of them made the function return early or wrongly without raising. If no test covers the function, write one: construct the smallest valid input by hand from the format the code parses, explain its layout in a comment, and assert the exact output. Check who calls each function and report any that has no caller instead of deleting it. If the package declares Python 2 support, keep the code working there or say that you could not verify it. Run the tests on the oldest and newest Python 3 versions the project lists and include the commands and output. Open a pull request with the defects listed in the order a caller would hit them.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
  1. Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
  2. Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
  3. Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

What comes back

A pull request whose description lists the defects in each touched function in the order execution reaches them, with the line, the Python 3 behaviour and whether it raised or failed quietly. For decompress_lzss that list has four entries and the title's error is the fourth. The diff for that file touches the magic comparison, the two buffers and the return. The new test file contains the hand-built input with a comment that maps each byte range to the field the code reads from it.

A reviewer starts with the test input, since everything else rests on it. If its layout matches the format, a passing test means the function works on Python 3 for the first time. They then check the quiet failures the agent flagged: a function that printed a message and returned None may have callers that treat None as a valid result. Finally they answer the open questions the description ends with, such as whether a module with no caller should stay.

Run it on every Linear issue labelled python3

Old binary-format code is found one traceback at a time, usually by a user on a newer interpreter. When your team files those as issues, let the label start the work: an automation on the Project with a linear trigger on linear.issue.labeled, your label and the prompt above. The issue text is added after the saved prompt and fills in no placeholder. The trigger needs a Linear connection for the automation's runner, set up in the console under Settings → Connections, and fires only for a person in your Rush org.

To sweep a codebase instead of waiting for reports, a person can press Run now on the automation in each repository's Project. Run now counts toward the automation's max runs per hour like any trigger.

The use case this job follows, with its automation and trigger: Turn a Linear label into a PR.

Why run it on Rush?

This job is mostly running code that has not run in years and seeing what it does. Each fix reveals the next failure, so the loop is edit, run, read the output, repeat. On a laptop that loop competes with whatever interpreter the developer happens to have. The agent's sandbox has the versions the project lists installed side by side, and every run of the function, with its output, stays in the session record, so the pull request can show the four states the function went through.

The result is also easy to overclaim. A diff that renames one method and says the error is fixed is true and useless if the function still returns nothing. A run that had to produce a passing test with a constructed input cannot make that claim without the test, and when it cannot build a valid input, its answer says so instead of opening a pull request that only renames.

Frequently asked questions

Is tobytes() exactly the same as tostring()?

Yes. From Python 3.2 tostring() was an alias for tobytes() and returned the same bytes object, and fromstring() on bytes matched frombytes(). Renaming them changes no data. On Python 2.7 the array type has tostring only, so code that must run on both needs a small compatibility wrapper.

Does this affect NumPy's ndarray.tostring()?

It is a different method on a different type. NumPy deprecated its own tostring() in favour of tobytes() on a separate schedule, so the error message would name numpy.ndarray and not array.array. The agent sorts each call by its receiver's type before changing it.

Why did the function return None instead of raising?

In Python 3, comparing bytes with str is always unequal and raises nothing unless the interpreter runs with the -b flag. A header check written for Python 2 text strings therefore rejects every input. The agent runs the code with -bb to turn such comparisons into errors and find them all.

What if the code uses array('c')?

The 'c' typecode was removed in Python 3. array('B', data) or a plain bytearray replaces it, with bytes where a string was expected. That is one of the Python 2 patterns the prompt asks the agent to list and fix in the same function.

More jobs like this: Turn a Linear label into a PR

Keep reading