Skip to content

Commit

Permalink
cleaning stuff up
Browse files Browse the repository at this point in the history
  • Loading branch information
Vortex2Oblivion committed Nov 9, 2023
1 parent 4251da4 commit f30bbbf
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 95 deletions.
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,17 @@ With a file:
```haxe
package;
import hxpy.PythonOpen;
import hxpy.Python;
import hxpy.Python.File;
class Main {
public static function main():Void {
//initializes the python instance
PythonOpen.pythonInitialize();
Python.initialize();
//runs code path to script file
PythonFile.pythonRunSimpleFile("script.py");
File.runSimpleFile("script.py");
//closes the python instance
PythonOpen.pythonFinalize();
Python.finalize();
}
}
```
Expand All @@ -51,16 +52,16 @@ Without a file:
```haxe
package;
import hxpy.PythonOpen;
import hxpy.Python;
class Main {
public static function main():Void {
//initializes the python instance
PythonOpen.pythonInitialize();
//runs code
PythonOpen.pythonRunSimpleString("print('Hello World From Embedded Python')");
Python.initialize();
//runs code
Python.runSimpleString("print('Hello World From Embedded Python')");
//closes the python instance
PythonOpen.pythonFinalize();
Python.finalize();
}
}
```
Expand Down
66 changes: 63 additions & 3 deletions hxpy/Python.hx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package hxpy;
@:include("Python.h")
@:keep
/**
* Contains some other misc variables like version and copyright from Python
* Class that contains most of the variables and functions in hxpy!
*/
extern class Python
{
Expand Down Expand Up @@ -43,5 +43,65 @@ extern class Python
*Function for tracing the Python copyright information.
*/
@:native("Py_GetCopyright")
public static function pythonGetCopyright():String;
}
public static function getCopyright():String;

/**
*Function for initializing the Python interpreter.
*/
@:native("Py_Initialize")
public static function initialize():Void;

/**
*Function for loading Python code from a string.
@param pycode The actual Python code that is going to be run.
*/
@:native("PyRun_SimpleString")
public static function runSimpleString(pycode:String):Void;

/**
*Function for closing a Python instance.
*/
@:native("Py_Finalize")
public static function finalize():Void;

}

@:cppFileCode('
#define PY_SSIZE_T_CLEAN
#ifdef _DEBUG
#undef _DEBUG
#include <Python.h>
#define _DEBUG
#else
#include <Python.h>
#endif
#include <string>
#include <iostream>
#include <pythonrun.h>
using std::string;
using namespace std;
')
/**
* Class that contains the function for running code from a file
* Cuz you cant use untyped cpp in extern classes. a.
* I don't know what any of this means lmao.
*/
class File {
/**
* Function for loading Python code from a file.
* @param filetoParse The path of your Python script. (eg: script.py)
*/
public static function runSimpleFile(filetoParse:String) {
untyped __cpp__('
PyObject *obj = Py_BuildValue("s", filetoParse.c_str());
FILE* PScriptFile = _Py_fopen_obj(obj, "r+");
if(PScriptFile){
PyRun_SimpleFile(PScriptFile, filetoParse);
fclose(PScriptFile);
}
else{
std::cout << "File Not Found!";
}
');
}
}
42 changes: 0 additions & 42 deletions hxpy/PythonFile.hx

This file was deleted.

30 changes: 0 additions & 30 deletions hxpy/PythonOpen.hx

This file was deleted.

14 changes: 7 additions & 7 deletions test/demos/running code from a file/src/Main.hx
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package;

import hxpy.PythonOpen;
import hxpy.PythonFile;
import hxpy.Python;
import hxpy.Python.File;

class Main {
public static function main():Void {
//tracing some version information.
trace("Python Ver: " + Python.VERSION);
PythonOpen.pythonInitialize(); // Initializes python
PythonFile.pythonRunSimpleFile(/*path to script file*/"script.py"); // runs code
PythonOpen.pythonFinalize(); // closes python
//initializes the python instance
Python.initialize();
//runs code path to script file
File.runSimpleFile("script.py");
//closes the python instance
Python.finalize();
}
}
7 changes: 3 additions & 4 deletions test/demos/running code without a file/src/Main.hx
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package;

import hxpy.PythonOpen;
import hxpy.Python;

class Main {
public static function main():Void {
trace("Python Ver: " + Python.VERSION);
PythonOpen.pythonInitialize(); //initializes the python instance
PythonOpen.pythonRunSimpleString("print('Hello World From Embedded Python')"); //runs code
PythonOpen.pythonFinalize();//closes the python instance
Python.initialize(); //initializes the python instance
Python.runSimpleString("print('Hello World From Embedded Python')"); //runs code
Python.finalize();//closes the python instance
}
}

0 comments on commit f30bbbf

Please sign in to comment.