Skip to content

Serialization#99

Merged
generalmimon merged 152 commits intomasterfrom
serialization
Aug 31, 2025
Merged

Serialization#99
generalmimon merged 152 commits intomasterfrom
serialization

Conversation

@GreyCat
Copy link
Member

@GreyCat GreyCat commented Jul 30, 2023

No description provided.

GreyCat added 30 commits March 24, 2017 10:42
This is needed to fix the `aggregate/convert_to_json` crash due to error
`Unable to parse classname: "unittest.loader.ModuleImportFailure"` that
currently occurs when running ./ci-python on Python 2.

When `unittest` automatically imports modules during test discovery and
some file cannot be imported (e.g. due to a syntax error), the error is
reported under the classname `unittest.loader.ModuleImportFailure`,
which obviously doesn't match the `/\.Test([^.]*)$/` pattern expected
for normal tests. But in that case we just want to skip prepending the
classname and continue, not crash - this is not a fatal error.
Avoid unnecessarily compiling both `spec` and `specwrite` suites twice,
each time only to run tests from one suite.

This means that the compilation should be faster, the compile errors
(mostly in generated Java sources) won't be duplicated in the console
output, and our CI infrastructure will no longer report them (also) in
the wrong suite.
The `assertRaisesRegexp` method of `unittest.TestCase` was removed in
Python 3.12, see kaitai-io/kaitai_struct#1104

Since 8420002...b1452a3
we have a better way of checking the error message that doesn't use
regular expressions (and is well compatible across Python versions), so
let's use it.
This is way more flexible than the `-w, --read-write` option, and
arguably makes more sense. For example, you can now pass `-t python -t
python-write` at once, which wasn't possible before.

It also fixes `-t all` to really mean *all* target languages (including
`java-write` and `python-write`), not just the read-only "languages".
Previously, the `_check_{inst}()` methods were invoked before the
corresponding parse instances, leading to attempts to access undefined
`_m_*` attributes. As a result, some tests that should have passed were
failing.

The fix is to call all `_check*()` methods only after dumping the entire
object, when all instances have been already invoked.
See d0da8e5

Note: it was tricky to reproduce the problem that this commit fixes.
According to the Java documentation [for the
`java.lang.Class.getDeclaredMethods()`
method](https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getDeclaredMethods--):

> The elements in the returned array are not sorted and are not in any
> particular order.

And indeed, when I compared the values of the `methodName` variable from
two consecutive runs of the entire test suite, they were in a slightly
different order. But not different enough to cause any test failures.

In order to reliably reproduce the problem, it proved effective to
explicitly shuffle the array of methods, for example as follows:

```diff
diff --git i/spec/java/src/io/kaitai/struct/specwrite/CommonSpec.java w/spec/java/src/io/kaitai/struct/specwrite/CommonSpec.java
index 0169d82..16916052 100644
--- i/spec/java/src/io/kaitai/struct/specwrite/CommonSpec.java
+++ w/spec/java/src/io/kaitai/struct/specwrite/CommonSpec.java
@@ -7,4 +7,6 @@ import java.nio.file.Files;
 import java.nio.file.Paths;
 import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.IdentityHashMap;
@@ -108,5 +110,8 @@ public abstract class CommonSpec extends io.kaitai.struct.spec.CommonSpec {
             }
             parentStructs.put(struct, currentPath);
-            for (final Method m : struct.getClass().getDeclaredMethods()) {
+            Method[] methods = struct.getClass().getDeclaredMethods();
+            List<Method> methodsAsList = Arrays.asList(methods);
+            Collections.shuffle(methodsAsList);
+            for (final Method m : methods) {
                 if (!Modifier.isPublic(m.getModifiers())) continue;
                 if (Modifier.isStatic(m.getModifiers())) continue; // ignore static methods, i.e. "fromFile"
```
See b4b2ccc

Of course, `java-write` and `python-write` are not real languages, so
their generated test specs should go into the `spec/{java,python}`
folders (not `spec/{java,python}-write`).
@generalmimon generalmimon merged commit b844544 into master Aug 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants