Skip to content

Commit

Permalink
Publish standard and Python registered structs
Browse files Browse the repository at this point in the history
Standard struct definition where not usable in dynamic settings
(e.g. to_type()), as the definition was not registered to the corresponding
struct name and type.
  • Loading branch information
amotzkau committed Nov 5, 2024
1 parent 986c882 commit 2c0412a
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/pkg-python.c
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,7 @@ python_register_struct (PyObject *module, PyObject *args, PyObject *kwds)
return NULL;

assert(stype != NULL);
struct_publish_global_type(stype);

ident = make_python_identifier(name);
if (!ident)
Expand Down
20 changes: 20 additions & 0 deletions src/structs.c
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,25 @@ struct_publish_type ( struct_type_t * pSType )
pSType->name->current = pSType;
} /* struct_publish_type() */

/*-------------------------------------------------------------------------*/
void
struct_publish_global_type ( struct_type_t * pSType )

/* Make the struct type <pSType> the current definition for its name,
* replacing an existing entry if necessary.
* It is safe to publish the same type multiple times.
*/

{
#ifdef DEBUG
if (pSType == NULL)
fatal("NULL typeobject pointer passed to struct_publish_type().\n");
#endif

pSType->prog_id = -1;
pSType->name->current = pSType;
} /* struct_publish_type() */

/*-------------------------------------------------------------------------*/
Bool
struct_type_equivalent (struct_type_t * pSType1, struct_type_t *pSType2)
Expand Down Expand Up @@ -1265,6 +1284,7 @@ create_std_struct_type (int std_struct_idx, lpctype_t *lpctype, const char* name
init_global_identifier(ident, /* bProgram: */ false);
ident->u.global.std_struct_id = std_struct_idx;

struct_publish_global_type(result);
return result;
} /* create_std_struct_type() */

Expand Down
1 change: 1 addition & 0 deletions src/structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ extern void struct_free_type (struct_type_t *pSType);
extern void struct_free_name (struct_name_t *pSName);
extern struct_type_t * struct_lookup_type ( struct_type_t * pSType );
extern void struct_publish_type ( struct_type_t * pSType );
extern void struct_publish_global_type ( struct_type_t * pSType );
extern Bool struct_type_equivalent (struct_type_t * pSType1, struct_type_t *pSType2);
extern void struct_type_update ( struct_type_t * pSType, struct_type_t * pOld, struct_type_t * pNew);
extern struct_type_t * struct_find (string_t *name, program_t * prog);
Expand Down
1 change: 1 addition & 0 deletions test/t-efuns.c
Original file line number Diff line number Diff line change
Expand Up @@ -1592,6 +1592,7 @@ mixed *tests = (this_object() == blueprint()) &&
({ "to_type mixed* to string* with keep_zero", 0, (: deep_eq(to_type(({"abc", 0, 1, 2.3}), [string*], (<tt_opts> keep_zero: 1)), ({"abc", 0, "1", "2.3"})) :) }),
({ "to_type mixed* to struct mixed", 0, (: deep_eq(to_type(({"abc", 3, #'abs}), [struct mixed]), to_struct(({"abc", 3, #'abs}))) :) }),
({ "to_type mixed* to derived_struct", 0, (: deep_eq(to_type(({ ({1, "2", 3.3}), ({4, "5", 6.6}) }), [struct derived_struct]), (<derived_struct> arg: ({1, 2, 3}), values: ({"4", "5", "6.6"})) ) :) }),
({ "to_type mixed* to standard struct", 0, (: deep_eq(to_type(({ "ascii" }), [struct to_type_options]), (<to_type_options> "ascii")) :) }),
({ "to_type mixed* to string|mapping", 0, (: deep_eq(to_type(({"abc", 5, #'to_type}), [string|mapping]), (["abc", 5, #'to_type])) :) }),
({ "to_type mixed* to quoted array", 0, (: deep_eq(to_type(({"abc", 5, #'to_type}), decltype('({}))), '({"abc", 5, #'to_type})) :) }),
({ "to_type bytes to bytes|int*", 0, (: deep_eq(to_type(b"\x01\x02\x03", [bytes|int*]) , b"\x01\x02\x03") :) }),
Expand Down
22 changes: 17 additions & 5 deletions test/t-python/startup.py
Original file line number Diff line number Diff line change
Expand Up @@ -1049,32 +1049,44 @@ def setUp(self):
ldmud.register_struct("testregisteredstructs_struct1", None, (("value", int),))
ldmud.register_struct("testregisteredstructs_struct2", None, (("value", ldmud.Object),))
ldmud.unregister_struct("testregisteredstructs_struct2")
ldmud.register_struct("testregisteredstructs_struct3", None, (("value", int),))
ldmud.register_struct("testregisteredstructs_struct3", None, (("value", str),))

def tearDown(self):
ldmud.unregister_struct("testregisteredstructs_struct1")
ldmud.unregister_struct("testregisteredstructs_struct3")

def testDir(self):
self.assertIn("testregisteredstructs_struct1", dir(ldmud.registered_structs))
self.assertNotIn("testregisteredstructs_struct2", dir(ldmud.registered_structs))
self.assertNotIn("testregisteredstructs_struct3", dir(ldmud.registered_structs))
self.assertIn("testregisteredstructs_struct3", dir(ldmud.registered_structs))
self.assertNotIn("testregisteredstructs_struct4", dir(ldmud.registered_structs))
self.assertGreater(len(dir(ldmud.registered_structs)), 0)

def testDict(self):
self.assertIn("testregisteredstructs_struct1", ldmud.registered_structs.__dict__)
self.assertNotIn("testregisteredstructs_struct2", ldmud.registered_structs.__dict__)
self.assertNotIn("testregisteredstructs_struct3", ldmud.registered_structs.__dict__)
self.assertIn("testregisteredstructs_struct3", ldmud.registered_structs.__dict__)
self.assertNotIn("testregisteredstructs_struct4", ldmud.registered_structs.__dict__)
self.assertEqual(ldmud.registered_structs.__dict__["testregisteredstructs_struct1"], ldmud.Struct["python","testregisteredstructs_struct1"])
self.assertEqual(len(ldmud.registered_structs.__dict__), 2)
self.assertEqual(ldmud.registered_structs.__dict__["testregisteredstructs_struct3"], ldmud.Struct["python","testregisteredstructs_struct3"])
self.assertEqual(len(ldmud.registered_structs.__dict__), 3)

def testAttribute(self):
self.assertTrue(hasattr(ldmud.registered_structs, 'testregisteredstructs_struct1'))
self.assertFalse(hasattr(ldmud.registered_structs, 'testregisteredstructs_struct2'))
self.assertFalse(hasattr(ldmud.registered_structs, 'testregisteredstructs_struct3'))
self.assertTrue(hasattr(ldmud.registered_structs, 'testregisteredstructs_struct3'))
self.assertFalse(hasattr(ldmud.registered_structs, 'testregisteredstructs_struct4'))
self.assertEqual(ldmud.registered_structs.testregisteredstructs_struct1, ldmud.Struct["python","testregisteredstructs_struct1"])
self.assertEqual(ldmud.registered_structs.testregisteredstructs_struct3, ldmud.Struct["python","testregisteredstructs_struct3"])
with self.assertRaises(AttributeError):
ldmud.registered_structs.testregisteredstructs_struct2
with self.assertRaises(AttributeError):
ldmud.registered_structs.testregisteredstructs_struct3
ldmud.registered_structs.testregisteredstructs_struct4

def testUsage(self):
instance1 = ldmud.registered_structs.testregisteredstructs_struct1({ "value": 10 })
instance3 = ldmud.registered_structs.testregisteredstructs_struct3({ "value": "10" })

class TestRegisteredTypes(unittest.TestCase):
def testDir(self):
Expand Down

0 comments on commit 2c0412a

Please sign in to comment.