Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pfPython Cleanups #601

Merged
merged 7 commits into from
Oct 25, 2019
Merged

pfPython Cleanups #601

merged 7 commits into from
Oct 25, 2019

Conversation

Hoikas
Copy link
Member

@Hoikas Hoikas commented Sep 19, 2019

This was originally intended to be a set of changes to rewrite the glue.py file in C++. However, things got a little out of hand in the associated cleanups, so here they are instead. They include:

  • A huge sweep of code style cleanups in plPythonFileMod and plPythonSDLModifier
  • Rewriting the instance method calling code in plPythonFileMod to use variadic templates for ease of use
  • Cleaning up some of the PyObject* reference management
  • A handful of string cleanups

This depends on H-uru/moul-scripts#110

Copy link
Member

@zrax zrax left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's going to take time to fully review the PFM changes... But I wanted to post my observations so far.

Sources/Plasma/FeatureLib/pfPython/pyObjectRef.h Outdated Show resolved Hide resolved
@Hoikas
Copy link
Member Author

Hoikas commented Sep 19, 2019

This was really way too much. I'll wait for everyone to finish looking before pushing any fixes...

@Hoikas
Copy link
Member Author

Hoikas commented Oct 18, 2019

I rebased this onto master and fixed the issues previously pointed out to facilitate review.

Copy link
Member

@zrax zrax left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall this is a HUGE improvement... It's just also very difficult to review, so I hope I didn't miss anything important.

Sources/Plasma/FeatureLib/pfPython/plPythonFileMod.cpp Outdated Show resolved Hide resolved
Sources/Plasma/FeatureLib/pfPython/plPythonFileMod.cpp Outdated Show resolved Hide resolved
Sources/Plasma/FeatureLib/pfPython/plPythonFileMod.cpp Outdated Show resolved Hide resolved
IBuildTupleArgs<sizeof...(args)>(tuple.Get(), std::forward<Args>(args)...);

plProfile_BeginTiming(PythonUpdate);
pyObjectRef retVal = PyObject_CallObject(callable, tuple.Get());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The old code called PyObject_CallMethod(callable, fPyFunctionNames[methodId], ...), which actually invokes callable.name(*args) whereas this runs callable(*args)... Am I missing something?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reworked the way the callables are stored internally. Previously, it stored an array of pointers mapping method ID to class instances that was resolved at AddTarget() time. There should really only be one class instance in plPythonFileMod, so this was redundant. I changed the array to map method IDs to the actual method callables themselves.

This removes the ability to do fancy metaprogramming hacks like changing the OnNotify method, but that was flakey at best before considering if the callable was not a member of the class instance at AddTarget() time, said method would never be called, ever.

// target's key to the pySceneObject.
if (fInstance) {
pyObjectRef pSceneObject = PyObject_GetAttrString(fInstance, "sceneobject");
pySceneObject::ConvertFrom(pSceneObject.Get())->addObjKey(sobj->GetKey());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This removes the opportunity for Python to overload the addKey method...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original intent of this PR was to rewrite glue.py in C++. This is happening deep inside the glue, I think, so I'm wondering how much we care?

Sources/Plasma/FeatureLib/pfPython/plPythonFileMod.h Outdated Show resolved Hide resolved
Sources/Plasma/FeatureLib/pfPython/plPythonFileMod.h Outdated Show resolved Hide resolved
@Hoikas
Copy link
Member Author

Hoikas commented Oct 25, 2019

Updated to address issues from review. Please let me know if there any other concerns!

This does a number of things that make plPythonFileMod much easier to
play with. Namely:
- An RAII helper class `pyObjectRef` has been added that acts much like
  `std::unique_ptr` in that it steals a `PyObject*` and decrefs it on
  destruct. This helps eliminate lots of tedious code.
- Removed a lot of boilerplate commentary that was copied/pasted around
  from the beginning of time.
- Removed the specialization on internal clients killing erroring python
  methods. This did not make sense as we actually would rather error
  *more* in an internal client since that's who is actually testing the
  code.
This adds a helper for determining if a script has a method and the
message matches.
This changes the way script methods are called. Instead of the verbose
calling we did previously, we now have a variadic template powered
calling function for PFM methods. I had to change fPyFunctionInstances
to actually hold a pointer to the methods instead of a pointer to the
PFM instance... Who thought it was a good idea to have a 30+ member
array that points to either fInstance or null???
This should clean up the remaining style, const, and temporary
copying turds in plPythonFileMod.
Among the stylistic fixes there are also fixes for PyTuple
initialization, a PyObject* leak, and some various modernization tweaks.
Copy link
Member

@zrax zrax left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me

@Hoikas Hoikas merged commit a54fc9f into H-uru:master Oct 25, 2019
@Hoikas Hoikas deleted the pyglue branch October 25, 2019 21:11
Hoikas added a commit to Hoikas/Plasma that referenced this pull request May 17, 2020
This issue was exposed by H-uru#601. The Relto bahro pole smokers would
intentionally use a negative value as the timer callback ID. While this
is strictly speaking not kosher according to the `PtAtTimeCallback`
docstring, the implementation gladly accepted signed integers.
Additionally, throughout the code, -1 was used as a magic value.
Therefore, we fix everyone to use signed integers for timer callback
IDs. plPythonFileMod is now correctly passing signed values to the PFM's
`OnTimer` method.

Fixes the observed Relto traceback:
(05/17 16:02:38) cPythBahroPoles - Traceback (most recent call last):
(05/17 16:02:38)   File ".\python\psnlBahroPoles.py", line 679, in OnTimer
(05/17 16:02:38)     self.RunState(self.PoleIDMap[ageID], state, 0)
(05/17 16:02:38) KeyError: 429496729
Hoikas added a commit to Hoikas/Plasma that referenced this pull request May 17, 2020
This issue was exposed by H-uru#601. The Relto bahro pole smokers would
intentionally use a negative value as the timer callback ID. While this
is strictly speaking not kosher according to the `PtAtTimeCallback`
docstring, the implementation gladly accepted signed integers.
Additionally, throughout the code, -1 was used as a magic value.
Therefore, we fix everyone to use signed integers for timer callback
IDs. plPythonFileMod is now correctly passing signed values to the PFM's
`OnTimer` method.

Fixes the observed Relto traceback:
(05/17 16:02:38) cPythBahroPoles - Traceback (most recent call last):
(05/17 16:02:38)   File ".\python\psnlBahroPoles.py", line 679, in OnTimer
(05/17 16:02:38)     self.RunState(self.PoleIDMap[ageID], state, 0)
(05/17 16:02:38) KeyError: 429496729
Hoikas added a commit to Hoikas/Plasma that referenced this pull request May 17, 2020
This issue was exposed by H-uru#601. The Relto bahro pole smokers would
intentionally use a negative value as the timer callback ID. While this
is strictly speaking not kosher according to the `PtAtTimeCallback`
docstring, the implementation gladly accepted signed integers.
Additionally, throughout the code, -1 was used as a magic value.
Therefore, we fix everyone to use signed integers for timer callback
IDs. plPythonFileMod is now correctly passing signed values to the PFM's
`OnTimer` method.

Fixes the observed Relto traceback:
(05/17 16:02:38) cPythBahroPoles - Traceback (most recent call last):
(05/17 16:02:38)   File ".\python\psnlBahroPoles.py", line 679, in OnTimer
(05/17 16:02:38)     self.RunState(self.PoleIDMap[ageID], state, 0)
(05/17 16:02:38) KeyError: 429496729
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.

None yet

2 participants