summaryrefslogtreecommitdiffstats
path: root/src/libs/xpcom18a4/python/src/module/_xpcom.cpp
blob: d40d2b52384cf2c953809d4533006df76d684666 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is the Python XPCOM language bindings.
 *
 * The Initial Developer of the Original Code is
 * ActiveState Tool Corp.
 * Portions created by the Initial Developer are Copyright (C) 2000
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *   Mark Hammond <mhammond@skippinet.com.au> (original author)
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

//
// This code is part of the XPCOM extensions for Python.
//
// Written May 2000 by Mark Hammond.
//
// Based heavily on the Python COM support, which is
// (c) Mark Hammond and Greg Stein.
//
// (c) 2000, ActiveState corp.

#include "PyXPCOM.h"
#include "nsXPCOM.h"
#include "nsISupportsPrimitives.h"
#include "nsIFile.h"
#include "nsIComponentRegistrar.h"
#include "nsIComponentManagerObsolete.h"
#include "nsIConsoleService.h"
#include "nspr.h" // PR_fprintf
#ifdef VBOX
# include "nsEventQueueUtils.h"
#endif

#ifdef XP_WIN
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include "windows.h"
#endif

#include "nsIEventQueue.h"
#include "nsIProxyObjectManager.h"

#define LOADER_LINKS_WITH_PYTHON

#ifndef PYXPCOM_USE_PYGILSTATE
extern PYXPCOM_EXPORT void PyXPCOM_InterpreterState_Ensure();
#endif

#ifdef VBOX_PYXPCOM
# include <iprt/cdefs.h>
# include <VBox/com/com.h>
# ifndef MODULE_NAME_SUFFIX
#  define MANGLE_MODULE_NAME(a_szName)  a_szName
#  define MANGLE_MODULE_INIT(a_Name)    a_Name
# else
#  define MANGLE_MODULE_NAME(a_szName)  a_szName RT_XSTR(MODULE_NAME_SUFFIX)
#  define MANGLE_MODULE_INIT(a_Name)    RT_CONCAT(a_Name, MODULE_NAME_SUFFIX)
# endif
# if defined(VBOX_PYXPCOM_VERSIONED) && !defined(VBOX_PYXPCOM_MAJOR_VERSIONED)
#  if   PY_VERSION_HEX >= 0x030a0000 && PY_VERSION_HEX < 0x030b0000
#   define MODULE_NAME    MANGLE_MODULE_NAME("VBoxPython3_10")
#   define initVBoxPython MANGLE_MODULE_INIT(PyInit_VBoxPython3_10)

#  elif   PY_VERSION_HEX >= 0x03090000 && PY_VERSION_HEX < 0x030a0000
#   define MODULE_NAME    MANGLE_MODULE_NAME("VBoxPython3_9")
#   define initVBoxPython MANGLE_MODULE_INIT(PyInit_VBoxPython3_9)

#  elif PY_VERSION_HEX >= 0x03080000 && PY_VERSION_HEX < 0x03090000
#   define MODULE_NAME    MANGLE_MODULE_NAME("VBoxPython3_8")
#   define initVBoxPython MANGLE_MODULE_INIT(PyInit_VBoxPython3_8)

#  elif PY_VERSION_HEX >= 0x03070000 && PY_VERSION_HEX < 0x03080000
#   define MODULE_NAME    MANGLE_MODULE_NAME("VBoxPython3_7")
#   define initVBoxPython MANGLE_MODULE_INIT(PyInit_VBoxPython3_7)

#  elif PY_VERSION_HEX >= 0x03060000 && PY_VERSION_HEX < 0x03070000
#   define MODULE_NAME    MANGLE_MODULE_NAME("VBoxPython3_6")
#   define initVBoxPython MANGLE_MODULE_INIT(PyInit_VBoxPython3_6)

#  elif PY_VERSION_HEX >= 0x03050000 && PY_VERSION_HEX < 0x03060000
#   define MODULE_NAME    MANGLE_MODULE_NAME("VBoxPython3_5")
#   define initVBoxPython MANGLE_MODULE_INIT(PyInit_VBoxPython3_5)

#  elif PY_VERSION_HEX >= 0x03040000 && PY_VERSION_HEX < 0x03050000
#   define MODULE_NAME    MANGLE_MODULE_NAME("VBoxPython3_4")
#   define initVBoxPython MANGLE_MODULE_INIT(PyInit_VBoxPython3_4)

#  elif PY_VERSION_HEX >= 0x03030000 && PY_VERSION_HEX < 0x03040000
#   define MODULE_NAME    MANGLE_MODULE_NAME("VBoxPython3_3")
#   define initVBoxPython MANGLE_MODULE_INIT(PyInit_VBoxPython3_3)

#  elif PY_VERSION_HEX >= 0x03020000 && PY_VERSION_HEX < 0x03030000
#   define MODULE_NAME    MANGLE_MODULE_NAME("VBoxPython3_2")
#   define initVBoxPython MANGLE_MODULE_INIT(PyInit_VBoxPython3_2)

#  elif PY_VERSION_HEX >= 0x03010000 && PY_VERSION_HEX < 0x03020000
#   define MODULE_NAME    MANGLE_MODULE_NAME("VBoxPython3_1")
#   define initVBoxPython MANGLE_MODULE_INIT(PyInit_VBoxPython3_1)

#  elif PY_VERSION_HEX >= 0x02080000 && PY_VERSION_HEX < 0x02090000
#   define MODULE_NAME    MANGLE_MODULE_NAME("VBoxPython2_8")
#   define initVBoxPython MANGLE_MODULE_INIT(initVBoxPython2_8)

#  elif PY_VERSION_HEX >= 0x02070000 && PY_VERSION_HEX < 0x02080000
#   define MODULE_NAME    MANGLE_MODULE_NAME("VBoxPython2_7")
#   define initVBoxPython MANGLE_MODULE_INIT(initVBoxPython2_7)

#  elif PY_VERSION_HEX >= 0x02060000 && PY_VERSION_HEX < 0x02070000
#   define MODULE_NAME    MANGLE_MODULE_NAME("VBoxPython2_6")
#   define initVBoxPython MANGLE_MODULE_INIT(initVBoxPython2_6)
#  else
#   error "Fix module versioning. This Python version is not recognized."
#  endif
# else
#  if PY_MAJOR_VERSION <= 2 && defined(VBOX_PYXPCOM_MAJOR_VERSIONED)
#   define MODULE_NAME 	  MANGLE_MODULE_NAME("VBoxPython2")
#   define initVBoxPython MANGLE_MODULE_INIT(initVBoxPython2)
#  elif PY_MAJOR_VERSION <= 2
#   define MODULE_NAME 	  MANGLE_MODULE_NAME("VBoxPython")
#   define initVBoxPython MANGLE_MODULE_INIT(initVBoxPython)
#  elif defined(Py_LIMITED_API) || defined(VBOX_PYXPCOM_MAJOR_VERSIONED)
#   define MODULE_NAME 	  MANGLE_MODULE_NAME("VBoxPython3")
#   define initVBoxPython MANGLE_MODULE_INIT(PyInit_VBoxPython3)
#  else
#   define MODULE_NAME 	  MANGLE_MODULE_NAME("VBoxPython")
#   define initVBoxPython MANGLE_MODULE_INIT(PyInit_VBoxPython)
#  endif
# endif
#else
#define MODULE_NAME "_xpcom"
#endif

// "boot-strap" methods - interfaces we need to get the base
// interface support!

#ifndef VBOX
/* deprecated, included for backward compatibility */
static PyObject *
PyXPCOMMethod_NS_GetGlobalComponentManager(PyObject *self, PyObject *args)
{
	if (PyErr_Warn(PyExc_DeprecationWarning, "Use GetComponentManager instead") < 0)
	       return NULL;
	if (!PyArg_ParseTuple(args, ""))
		return NULL;
	nsCOMPtr<nsIComponentManager> cm;
	nsresult rv;
	Py_BEGIN_ALLOW_THREADS;
	rv = NS_GetComponentManager(getter_AddRefs(cm));
	Py_END_ALLOW_THREADS;
	if ( NS_FAILED(rv) )
		return PyXPCOM_BuildPyException(rv);

	nsCOMPtr<nsIComponentManagerObsolete> ocm(do_QueryInterface(cm, &rv));
	if ( NS_FAILED(rv) )
		return PyXPCOM_BuildPyException(rv);

	return Py_nsISupports::PyObjectFromInterface(ocm, NS_GET_IID(nsIComponentManagerObsolete), PR_FALSE);
}
#endif

static PyObject *
PyXPCOMMethod_GetComponentManager(PyObject *self, PyObject *args)
{
	if (!PyArg_ParseTuple(args, ""))
		return NULL;
	nsCOMPtr<nsIComponentManager> cm;
	nsresult rv;
	Py_BEGIN_ALLOW_THREADS;
	rv = NS_GetComponentManager(getter_AddRefs(cm));
	Py_END_ALLOW_THREADS;
	if ( NS_FAILED(rv) )
		return PyXPCOM_BuildPyException(rv);

	return Py_nsISupports::PyObjectFromInterface(cm, NS_GET_IID(nsIComponentManager), PR_FALSE);
}

// No xpcom callable way to get at the registrar, even though the interface
// is scriptable.
static PyObject *
PyXPCOMMethod_GetComponentRegistrar(PyObject *self, PyObject *args)
{
	if (!PyArg_ParseTuple(args, ""))
		return NULL;
	nsCOMPtr<nsIComponentRegistrar> cm;
	nsresult rv;
	Py_BEGIN_ALLOW_THREADS;
	rv = NS_GetComponentRegistrar(getter_AddRefs(cm));
	Py_END_ALLOW_THREADS;
	if ( NS_FAILED(rv) )
		return PyXPCOM_BuildPyException(rv);

	return Py_nsISupports::PyObjectFromInterface(cm, NS_GET_IID(nsISupports), PR_FALSE);
}

static PyObject *
PyXPCOMMethod_GetServiceManager(PyObject *self, PyObject *args)
{
	if (!PyArg_ParseTuple(args, ""))
		return NULL;
	nsCOMPtr<nsIServiceManager> sm;
	nsresult rv;
	Py_BEGIN_ALLOW_THREADS;
	rv = NS_GetServiceManager(getter_AddRefs(sm));
	Py_END_ALLOW_THREADS;
	if ( NS_FAILED(rv) )
		return PyXPCOM_BuildPyException(rv);

	// Return a type based on the IID.
	return Py_nsISupports::PyObjectFromInterface(sm, NS_GET_IID(nsIServiceManager));
}

#ifndef VBOX
/* deprecated, included for backward compatibility */
static PyObject *
PyXPCOMMethod_GetGlobalServiceManager(PyObject *self, PyObject *args)
{
	if (PyErr_Warn(PyExc_DeprecationWarning, "Use GetServiceManager instead") < 0)
		return NULL;

	return PyXPCOMMethod_GetComponentManager(self, args);
}
#endif

static PyObject *
PyXPCOMMethod_XPTI_GetInterfaceInfoManager(PyObject *self, PyObject *args)
{
	if (!PyArg_ParseTuple(args, ""))
		return NULL;
	nsIInterfaceInfoManager* im;
	Py_BEGIN_ALLOW_THREADS;
	im = XPTI_GetInterfaceInfoManager();
	Py_END_ALLOW_THREADS;
	if ( im == nsnull )
		return PyXPCOM_BuildPyException(NS_ERROR_FAILURE);

	/* Return a type based on the IID (with no extra ref) */
	// Can not auto-wrap the interface info manager as it is critical to
	// building the support we need for autowrap.
	PyObject *ret = Py_nsISupports::PyObjectFromInterface(im, NS_GET_IID(nsIInterfaceInfoManager), PR_FALSE);
	NS_IF_RELEASE(im);
	return ret;
}

static PyObject *
PyXPCOMMethod_XPTC_InvokeByIndex(PyObject *self, PyObject *args)
{
	PyObject *obIS, *obParams;
	nsCOMPtr<nsISupports> pis;
	int index;

	// We no longer rely on PyErr_Occurred() for our error state,
	// but keeping this assertion can't hurt - it should still always be true!
	NS_WARN_IF_FALSE(!PyErr_Occurred(), "Should be no pending Python error!");

	if (!PyArg_ParseTuple(args, "OiO", &obIS, &index, &obParams))
		return NULL;

	if (!Py_nsISupports::Check(obIS)) {
		return PyErr_Format(PyExc_TypeError,
		                    "First param must be a native nsISupports wrapper (got %s)",
				    PyXPCOM_ObTypeName(obIS));
	}
	// Ack!  We must ask for the "native" interface supported by
	// the object, not specifically nsISupports, else we may not
	// back the same pointer (eg, Python, following identity rules,
	// will return the "original" gateway when QI'd for nsISupports)
	if (!Py_nsISupports::InterfaceFromPyObject(
			obIS,
			Py_nsIID_NULL,
			getter_AddRefs(pis),
			PR_FALSE))
		return NULL;

	PyXPCOM_InterfaceVariantHelper arg_helper((Py_nsISupports *)obIS, index);
	if (!arg_helper.Init(obParams))
		return NULL;

	if (!arg_helper.FillArray())
		return NULL;

	nsresult r;
	Py_BEGIN_ALLOW_THREADS;
	r = XPTC_InvokeByIndex(pis, index, arg_helper.m_num_array, arg_helper.m_var_array);
/** @todo bird: Maybe we could processing pending XPCOM events here to make
 *        life a bit simpler inside python? */
	Py_END_ALLOW_THREADS;
	if ( NS_FAILED(r) )
		return PyXPCOM_BuildPyException(r);

	return arg_helper.MakePythonResult();
}

static PyObject *
PyXPCOMMethod_WrapObject(PyObject *self, PyObject *args)
{
	PyObject *ob, *obIID;
	int bWrapClient = 1;
	if (!PyArg_ParseTuple(args, "OO|i", &ob, &obIID, &bWrapClient))
		return NULL;

	nsIID	iid;
	if (!Py_nsIID::IIDFromPyObject(obIID, &iid))
		return NULL;

	nsCOMPtr<nsISupports> ret;
	nsresult r = PyXPCOM_XPTStub::CreateNew(ob, iid, getter_AddRefs(ret));
	if ( NS_FAILED(r) )
		return PyXPCOM_BuildPyException(r);

	// _ALL_ wrapped objects are associated with a weak-ref
	// to their "main" instance.
	AddDefaultGateway(ob, ret); // inject a weak reference to myself into the instance.

	// Now wrap it in an interface.
	return Py_nsISupports::PyObjectFromInterface(ret, iid, bWrapClient);
}

static PyObject *
PyXPCOMMethod_UnwrapObject(PyObject *self, PyObject *args)
{
	PyObject *ob;
	if (!PyArg_ParseTuple(args, "O", &ob))
		return NULL;

	nsISupports *uob = NULL;
	nsIInternalPython *iob = NULL;
	PyObject *ret = NULL;
	if (!Py_nsISupports::InterfaceFromPyObject(ob,
				NS_GET_IID(nsISupports),
				&uob,
				PR_FALSE))
		goto done;
	if (NS_FAILED(uob->QueryInterface(NS_GET_IID(nsIInternalPython), reinterpret_cast<void **>(&iob)))) {
		PyErr_SetString(PyExc_ValueError, "This XPCOM object is not implemented by Python");
		goto done;
	}
	ret = iob->UnwrapPythonObject();
done:
	Py_BEGIN_ALLOW_THREADS;
	NS_IF_RELEASE(uob);
	NS_IF_RELEASE(iob);
	Py_END_ALLOW_THREADS;
	return ret;
}

// @pymethod int|pythoncom|_GetInterfaceCount|Retrieves the number of interface objects currently in existance
static PyObject *
PyXPCOMMethod_GetInterfaceCount(PyObject *self, PyObject *args)
{
	if (!PyArg_ParseTuple(args, ":_GetInterfaceCount"))
		return NULL;
	return PyInt_FromLong(_PyXPCOM_GetInterfaceCount());
	// @comm If is occasionally a good idea to call this function before your Python program
	// terminates.  If this function returns non-zero, then you still have PythonCOM objects
	// alive in your program (possibly in global variables).
}

#ifdef VBOX_DEBUG_LIFETIMES
// @pymethod int|pythoncom|_DumpInterfaces|Dumps the interfaces still in existance to standard output
static PyObject *
PyXPCOMMethod_DumpInterfaces(PyObject *self, PyObject *args)
{
	if (!PyArg_ParseTuple(args, ":_DumpInterfaces"))
		return NULL;
	return PyInt_FromLong(_PyXPCOM_DumpInterfaces());
}
#endif

// @pymethod int|pythoncom|_GetGatewayCount|Retrieves the number of gateway objects currently in existance
static PyObject *
PyXPCOMMethod_GetGatewayCount(PyObject *self, PyObject *args)
{
	// @comm This is the number of Python object that implement COM servers which
	// are still alive (ie, serving a client).  The only way to reduce this count
	// is to have the process which uses these PythonCOM servers release its references.
	if (!PyArg_ParseTuple(args, ":_GetGatewayCount"))
		return NULL;
	return PyInt_FromLong(_PyXPCOM_GetGatewayCount());
}

static PyObject *
PyXPCOMMethod_NS_ShutdownXPCOM(PyObject *self, PyObject *args)
{
	// @comm This is the number of Python object that implement COM servers which
	// are still alive (ie, serving a client).  The only way to reduce this count
	// is to have the process which uses these PythonCOM servers release its references.
	if (!PyArg_ParseTuple(args, ":NS_ShutdownXPCOM"))
		return NULL;
	nsresult nr;
	Py_BEGIN_ALLOW_THREADS;
	nr = NS_ShutdownXPCOM(nsnull);
	Py_END_ALLOW_THREADS;

#ifdef VBOX_DEBUG_LIFETIME
	Py_nsISupports::dumpList();
#endif

	// Dont raise an exception - as we are probably shutting down
	// and dont really case - just return the status
	return PyInt_FromLong(nr);
}

static NS_DEFINE_CID(kProxyObjectManagerCID, NS_PROXYEVENT_MANAGER_CID);

// A hack to work around their magic constants!
static PyObject *
PyXPCOMMethod_GetProxyForObject(PyObject *self, PyObject *args)
{
	PyObject *obQueue, *obIID, *obOb;
	int flags;
	if (!PyArg_ParseTuple(args, "OOOi", &obQueue, &obIID, &obOb, &flags))
		return NULL;
	nsIID iid;
	if (!Py_nsIID::IIDFromPyObject(obIID, &iid))
		return NULL;
	nsCOMPtr<nsISupports> pob;
	if (!Py_nsISupports::InterfaceFromPyObject(obOb, iid, getter_AddRefs(pob), PR_FALSE))
		return NULL;
	nsIEventQueue *pQueue = NULL;
	nsIEventQueue *pQueueRelease = NULL;

	if (PyInt_Check(obQueue)) {
		pQueue = (nsIEventQueue *)PyInt_AsLong(obQueue);
	} else {
		if (!Py_nsISupports::InterfaceFromPyObject(obQueue, NS_GET_IID(nsIEventQueue), (nsISupports **)&pQueue, PR_TRUE))
			return NULL;
		pQueueRelease = pQueue;
	}

	nsresult rv_proxy;
	nsCOMPtr<nsISupports> presult;
	Py_BEGIN_ALLOW_THREADS;
	nsCOMPtr<nsIProxyObjectManager> proxyMgr =
	         do_GetService(kProxyObjectManagerCID, &rv_proxy);

	if ( NS_SUCCEEDED(rv_proxy) ) {
		rv_proxy = proxyMgr->GetProxyForObject(pQueue,
				iid,
				pob,
				flags,
				getter_AddRefs(presult));
	}
	if (pQueueRelease)
		pQueueRelease->Release();
	Py_END_ALLOW_THREADS;

	PyObject *result;
	if (NS_SUCCEEDED(rv_proxy) ) {
		result = Py_nsISupports::PyObjectFromInterface(presult, iid);
	} else {
		result = PyXPCOM_BuildPyException(rv_proxy);
	}
	return result;
}

static PyObject *
PyXPCOMMethod_MakeVariant(PyObject *self, PyObject *args)
{
	PyObject *ob;
	if (!PyArg_ParseTuple(args, "O:MakeVariant", &ob))
		return NULL;
	nsCOMPtr<nsIVariant> pVar;
	nsresult nr = PyObject_AsVariant(ob, getter_AddRefs(pVar));
	if (NS_FAILED(nr))
		return PyXPCOM_BuildPyException(nr);
	if (pVar == nsnull) {
		NS_ERROR("PyObject_AsVariant worked but returned a NULL ptr!");
		return PyXPCOM_BuildPyException(NS_ERROR_UNEXPECTED);
	}
	return Py_nsISupports::PyObjectFromInterface(pVar, NS_GET_IID(nsIVariant));
}

static PyObject *
PyXPCOMMethod_GetVariantValue(PyObject *self, PyObject *args)
{
	PyObject *ob, *obParent = NULL;
	if (!PyArg_ParseTuple(args, "O|O:GetVariantValue", &ob, &obParent))
		return NULL;

	nsCOMPtr<nsIVariant> var;
	if (!Py_nsISupports::InterfaceFromPyObject(ob,
				NS_GET_IID(nsISupports),
				getter_AddRefs(var),
				PR_FALSE))
		return PyErr_Format(PyExc_ValueError,
				    "Object is not an nsIVariant (got %s)",
				    PyXPCOM_ObTypeName(ob));

	Py_nsISupports *parent = nsnull;
	if (obParent && obParent != Py_None) {
		if (!Py_nsISupports::Check(obParent)) {
			PyErr_SetString(PyExc_ValueError,
					"Object not an nsISupports wrapper");
			return NULL;
		}
		parent = (Py_nsISupports *)obParent;
	}
	return PyObject_FromVariant(parent, var);
}

PyObject *PyGetSpecialDirectory(PyObject *self, PyObject *args)
{
	char *dirname;
	if (!PyArg_ParseTuple(args, "s:GetSpecialDirectory", &dirname))
		return NULL;
	nsCOMPtr<nsIFile> file;
	nsresult r = NS_GetSpecialDirectory(dirname, getter_AddRefs(file));
	if ( NS_FAILED(r) )
		return PyXPCOM_BuildPyException(r);
	// returned object swallows our reference.
	return Py_nsISupports::PyObjectFromInterface(file, NS_GET_IID(nsIFile));
}

PyObject *AllocateBuffer(PyObject *self, PyObject *args)
{
	int bufSize;
	if (!PyArg_ParseTuple(args, "i", &bufSize))
		return NULL;
#if PY_MAJOR_VERSION <= 2
	return PyBuffer_New(bufSize);
#else
    return PyBytes_FromStringAndSize(NULL, bufSize);
#endif
}

// Writes a message to the console service.  This could be done via pure
// Python code, but is useful when the logging code is actually the
// xpcom .py framework itself (ie, we don't want our logging framework to
// call back into the very code generating the log messages!
PyObject *LogConsoleMessage(PyObject *self, PyObject *args)
{
	char *msg;
	if (!PyArg_ParseTuple(args, "s", &msg))
		return NULL;

	nsCOMPtr<nsIConsoleService> consoleService = do_GetService(NS_CONSOLESERVICE_CONTRACTID);
	if (consoleService)
		consoleService->LogStringMessage(NS_ConvertASCIItoUCS2(msg).get());
	else {
	// This either means no such service, or in shutdown - hardly worth
	// the warning, and not worth reporting an error to Python about - its
	// log handler would just need to catch and ignore it.
	// And as this is only called by this logging setup, any messages should
	// still go to stderr or a logfile.
		NS_WARNING("pyxpcom can't log console message.");
	}

	Py_INCREF(Py_None);
	return Py_None;
}

#ifdef VBOX

#  include <VBox/com/NativeEventQueue.h>
#  include <iprt/err.h>

static PyObject *
PyXPCOMMethod_WaitForEvents(PyObject *self, PyObject *args)
{
    long lTimeout;
    if (!PyArg_ParseTuple(args, "l", &lTimeout))
        return NULL;

    int rc;
    com::NativeEventQueue* aEventQ = com::NativeEventQueue::getMainEventQueue();
    NS_WARN_IF_FALSE(aEventQ != nsnull, "Null main event queue");
    if (!aEventQ)
    {
        PyErr_SetString(PyExc_TypeError, "the main event queue is NULL");
        return NULL;
    }

    Py_BEGIN_ALLOW_THREADS

    RTMSINTERVAL cMsTimeout = (RTMSINTERVAL)lTimeout;
    if (lTimeout < 0 || (long)cMsTimeout != lTimeout)
        cMsTimeout = RT_INDEFINITE_WAIT;
    rc = aEventQ->processEventQueue(cMsTimeout);

    Py_END_ALLOW_THREADS
    if (RT_SUCCESS(rc))
        return PyInt_FromLong(0);

    if (   rc == VERR_TIMEOUT
        || rc == VERR_INTERRUPTED)
        return PyInt_FromLong(1);

    if (rc == VERR_INVALID_CONTEXT)
    {
        PyErr_SetString(PyExc_Exception, "wrong thread, use the main thread");
        return NULL;
    }

    return PyInt_FromLong(2);
}

static PyObject*
PyXPCOMMethod_InterruptWait(PyObject *self, PyObject *args)
{
  com::NativeEventQueue* aEventQ = com::NativeEventQueue::getMainEventQueue();
  NS_WARN_IF_FALSE(aEventQ != nsnull, "Null main event queue");
  if (!aEventQ)
      return NULL;

  int rc = aEventQ->interruptEventQueueProcessing();
  return PyBool_FromLong(RT_SUCCESS(rc));
}

static nsresult deinitVBoxPython();

static PyObject*
PyXPCOMMethod_DeinitCOM(PyObject *self, PyObject *args)
{
    nsresult nr;
    Py_BEGIN_ALLOW_THREADS;
    nr = deinitVBoxPython();
    Py_END_ALLOW_THREADS;
    return PyInt_FromLong(nr);
}

static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);

static PyObject*
PyXPCOMMethod_AttachThread(PyObject *self, PyObject *args)
{
    nsresult rv;
    PRInt32  result = 0;
    nsCOMPtr<nsIEventQueueService> eqs;

    // Create the Event Queue for this thread...
    Py_BEGIN_ALLOW_THREADS;
    eqs =
      do_GetService(kEventQueueServiceCID, &rv);
    Py_END_ALLOW_THREADS;
    if (NS_FAILED(rv))
    {
      result = 1;
      goto done;
    }

    Py_BEGIN_ALLOW_THREADS;
    rv = eqs->CreateThreadEventQueue();
    Py_END_ALLOW_THREADS;
    if (NS_FAILED(rv))
    {
      result = 2;
      goto done;
    }

 done:
    /** @todo: better throw an exception on error */
    return PyInt_FromLong(result);
}

static PyObject*
PyXPCOMMethod_DetachThread(PyObject *self, PyObject *args)
{
    nsresult rv;
    PRInt32  result = 0;
    nsCOMPtr<nsIEventQueueService> eqs;

    // Destroy the Event Queue for this thread...
    Py_BEGIN_ALLOW_THREADS;
    eqs =
      do_GetService(kEventQueueServiceCID, &rv);
    Py_END_ALLOW_THREADS;
    if (NS_FAILED(rv))
    {
      result = 1;
      goto done;
    }

    Py_BEGIN_ALLOW_THREADS;
    rv = eqs->DestroyThreadEventQueue();
    Py_END_ALLOW_THREADS;
    if (NS_FAILED(rv))
    {
      result = 2;
      goto done;
    }

 done:
    /** @todo: better throw an exception on error */
    return PyInt_FromLong(result);
}

#endif /* VBOX */

extern PYXPCOM_EXPORT PyObject *PyXPCOMMethod_IID(PyObject *self, PyObject *args);

static struct PyMethodDef xpcom_methods[]=
{
	{"GetComponentManager", PyXPCOMMethod_GetComponentManager, 1},
	{"GetComponentRegistrar", PyXPCOMMethod_GetComponentRegistrar, 1},
#ifndef VBOX
	{"NS_GetGlobalComponentManager", PyXPCOMMethod_NS_GetGlobalComponentManager, 1}, // deprecated
#endif
	{"XPTI_GetInterfaceInfoManager", PyXPCOMMethod_XPTI_GetInterfaceInfoManager, 1},
	{"XPTC_InvokeByIndex", PyXPCOMMethod_XPTC_InvokeByIndex, 1},
	{"GetServiceManager", PyXPCOMMethod_GetServiceManager, 1},
#ifndef VBOX
	{"GetGlobalServiceManager", PyXPCOMMethod_GetGlobalServiceManager, 1}, // deprecated
	{"IID", PyXPCOMMethod_IID, 1}, // IID is wrong - deprecated - not just IID, but CID, etc.
#endif
	{"ID", PyXPCOMMethod_IID, 1}, // This is the official name.
	{"NS_ShutdownXPCOM", PyXPCOMMethod_NS_ShutdownXPCOM, 1},
	{"WrapObject", PyXPCOMMethod_WrapObject, 1},
	{"UnwrapObject", PyXPCOMMethod_UnwrapObject, 1},
	{"_GetInterfaceCount", PyXPCOMMethod_GetInterfaceCount, 1},
	{"_GetGatewayCount", PyXPCOMMethod_GetGatewayCount, 1},
	{"getProxyForObject", PyXPCOMMethod_GetProxyForObject, 1},
	{"GetProxyForObject", PyXPCOMMethod_GetProxyForObject, 1},
	{"GetSpecialDirectory", PyGetSpecialDirectory, 1},
	{"AllocateBuffer", AllocateBuffer, 1},
	{"LogConsoleMessage", LogConsoleMessage, 1, "Write a message to the xpcom console service"},
	{"MakeVariant", PyXPCOMMethod_MakeVariant, 1},
	{"GetVariantValue", PyXPCOMMethod_GetVariantValue, 1},
#ifdef VBOX
	{"WaitForEvents", PyXPCOMMethod_WaitForEvents, 1},
	{"InterruptWait", PyXPCOMMethod_InterruptWait, 1},
	{"DeinitCOM",     PyXPCOMMethod_DeinitCOM, 1},
	{"AttachThread",  PyXPCOMMethod_AttachThread, 1},
	{"DetachThread",  PyXPCOMMethod_DetachThread, 1},
#endif
#ifdef VBOX_DEBUG_LIFETIMES
	{"_DumpInterfaces", PyXPCOMMethod_DumpInterfaces, 1},
#endif
	// These should no longer be used - just use the logging.getLogger('pyxpcom')...
	/* bird: The above comment refers to LogWarning and LogError. Both now removed. */
	{ NULL }
};

#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef xpcom_module =
{
    PyModuleDef_HEAD_INIT,
    MODULE_NAME,    /* name of module */
    NULL,           /* module documentation */
    -1,             /* size of per-interpreter state or -1 if using globals */
    xpcom_methods
};
#endif


#define REGISTER_IID(t) { \
	PyObject *iid_ob = Py_nsIID::PyObjectFromIID(NS_GET_IID(t)); \
	PyDict_SetItemString(dict, "IID_"#t, iid_ob); \
	Py_DECREF(iid_ob); \
	}

#define REGISTER_INT(val) { \
	PyObject *ob = PyInt_FromLong(val); \
	PyDict_SetItemString(dict, #val, ob); \
	Py_DECREF(ob); \
	}


////////////////////////////////////////////////////////////
// The module init code.
//
#if PY_MAJOR_VERSION <= 2
extern "C" NS_EXPORT
void
#else
PyObject *
#endif
init_xpcom() {
	PyObject *oModule;

	// ensure the framework has valid state to work with.
	if (!PyXPCOM_Globals_Ensure())
#if PY_MAJOR_VERSION <= 2
		return;
#else
		return NULL;
#endif

	// Must force Python to start using thread locks
	PyEval_InitThreads();

	// Create the module and add the functions
#if PY_MAJOR_VERSION <= 2
	oModule = Py_InitModule(MODULE_NAME, xpcom_methods);
#else
	oModule = PyModule_Create(&xpcom_module);
#endif

	PyObject *dict = PyModule_GetDict(oModule);
	PyObject *pycom_Error = PyXPCOM_Error;
	if (pycom_Error == NULL || PyDict_SetItemString(dict, "error", pycom_Error) != 0)
	{
		PyErr_SetString(PyExc_MemoryError, "can't define error");
#if PY_MAJOR_VERSION <= 2
		return;
#else
		return NULL;
#endif
	}
#ifndef Py_LIMITED_API
	PyDict_SetItemString(dict, "IIDType", (PyObject *)&Py_nsIID::type);
#else
	PyDict_SetItemString(dict, "IIDType", (PyObject *)Py_nsIID::GetTypeObject());
#endif

	REGISTER_IID(nsISupports);
	REGISTER_IID(nsISupportsCString);
	REGISTER_IID(nsISupportsString);
	REGISTER_IID(nsIModule);
	REGISTER_IID(nsIFactory);
	REGISTER_IID(nsIWeakReference);
	REGISTER_IID(nsISupportsWeakReference);
	REGISTER_IID(nsIClassInfo);
	REGISTER_IID(nsIServiceManager);
	REGISTER_IID(nsIComponentRegistrar);

	// Register our custom interfaces.
	REGISTER_IID(nsIComponentManager);
	REGISTER_IID(nsIInterfaceInfoManager);
	REGISTER_IID(nsIEnumerator);
	REGISTER_IID(nsISimpleEnumerator);
	REGISTER_IID(nsIInterfaceInfo);
	REGISTER_IID(nsIInputStream);
	REGISTER_IID(nsIClassInfo);
	REGISTER_IID(nsIVariant);
	// for backward compatibility:
	REGISTER_IID(nsIComponentManagerObsolete);

	// No good reason not to expose this impl detail, and tests can use it
	REGISTER_IID(nsIInternalPython);
    // We have special support for proxies - may as well add their constants!
    REGISTER_INT(PROXY_SYNC);
    REGISTER_INT(PROXY_ASYNC);
    REGISTER_INT(PROXY_ALWAYS);
    // Build flags that may be useful.
    PyObject *ob = PyBool_FromLong(
#ifdef NS_DEBUG
                                   1
#else
                                   0
#endif
                                   );
    PyDict_SetItemString(dict, "NS_DEBUG", ob);
    Py_DECREF(ob);
#if PY_MAJOR_VERSION >= 3
    return oModule;
#endif
}

#ifdef VBOX_PYXPCOM
# include <VBox/com/com.h>
using namespace com;

# include <iprt/initterm.h>
# include <iprt/string.h>
# include <iprt/alloca.h>
# include <iprt/stream.h>

/** Set if NS_ShutdownXPCOM has been called successfully already and we don't
 * need to do it again during module termination.  This avoids assertion in the
 * VBoxCOM glue code. */
static bool g_fComShutdownAlready = true;

# if PY_MAJOR_VERSION <= 2
extern "C" NS_EXPORT
void
# else
/** @todo r=klaus this is hacky, but as Python3 doesn't deal with ELF
 * visibility, assuming that all globals are visible (which is ugly and not
 * true in our case). */
#  undef PyMODINIT_FUNC
#  define PyMODINIT_FUNC extern "C" NS_EXPORT PyObject*
PyMODINIT_FUNC
# endif
initVBoxPython() { /* NOTE! This name is redefined at the top of the file! */
  static bool s_vboxInited = false;
  if (!s_vboxInited) {
    int rc = 0; /* Error handling in this code is NON-EXISTING. Sigh. */

# if defined(VBOX_PATH_APP_PRIVATE_ARCH) && defined(VBOX_PATH_SHARED_LIBS)
    rc = RTR3InitDll(RTR3INIT_FLAGS_UNOBTRUSIVE);
# else
    const char *home = getenv("VBOX_PROGRAM_PATH");
    if (home) {
      size_t len = strlen(home);
      char *exepath = (char *)alloca(len + 32);
      memcpy(exepath, home, len);
      memcpy(exepath + len, "/pythonfake", sizeof("/pythonfake"));
      rc = RTR3InitEx(RTR3INIT_VER_CUR, RTR3INIT_FLAGS_DLL | RTR3INIT_FLAGS_UNOBTRUSIVE, 0, NULL, exepath);
    } else {
      rc = RTR3InitDll(RTR3INIT_FLAGS_UNOBTRUSIVE);
    }
# endif

    rc = com::Initialize();
    g_fComShutdownAlready = false;

# if PY_MAJOR_VERSION <= 2
    init_xpcom();
# else
    return init_xpcom();
# endif
  }
# if PY_MAJOR_VERSION >= 3
  return NULL;
# endif
}

static
nsresult deinitVBoxPython()
{
  nsresult nr;
  if (!g_fComShutdownAlready)
  {
    nr = com::Shutdown();
    if (!NS_FAILED(nr))
      g_fComShutdownAlready = true;
  }
  else
    nr = NS_ERROR_NOT_INITIALIZED;
  return nr;
}

#endif /* VBOX_PYXPCOM */